LLVM API Documentation
00001 //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Bitcode writer implementation. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Bitcode/ReaderWriter.h" 00015 #include "ValueEnumerator.h" 00016 #include "llvm/ADT/Triple.h" 00017 #include "llvm/Bitcode/BitstreamWriter.h" 00018 #include "llvm/Bitcode/LLVMBitCodes.h" 00019 #include "llvm/IR/Constants.h" 00020 #include "llvm/IR/DerivedTypes.h" 00021 #include "llvm/IR/InlineAsm.h" 00022 #include "llvm/IR/Instructions.h" 00023 #include "llvm/IR/Module.h" 00024 #include "llvm/IR/Operator.h" 00025 #include "llvm/IR/UseListOrder.h" 00026 #include "llvm/IR/ValueSymbolTable.h" 00027 #include "llvm/Support/CommandLine.h" 00028 #include "llvm/Support/ErrorHandling.h" 00029 #include "llvm/Support/MathExtras.h" 00030 #include "llvm/Support/Program.h" 00031 #include "llvm/Support/raw_ostream.h" 00032 #include <cctype> 00033 #include <map> 00034 using namespace llvm; 00035 00036 /// These are manifest constants used by the bitcode writer. They do not need to 00037 /// be kept in sync with the reader, but need to be consistent within this file. 00038 enum { 00039 // VALUE_SYMTAB_BLOCK abbrev id's. 00040 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 00041 VST_ENTRY_7_ABBREV, 00042 VST_ENTRY_6_ABBREV, 00043 VST_BBENTRY_6_ABBREV, 00044 00045 // CONSTANTS_BLOCK abbrev id's. 00046 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 00047 CONSTANTS_INTEGER_ABBREV, 00048 CONSTANTS_CE_CAST_Abbrev, 00049 CONSTANTS_NULL_Abbrev, 00050 00051 // FUNCTION_BLOCK abbrev id's. 00052 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 00053 FUNCTION_INST_BINOP_ABBREV, 00054 FUNCTION_INST_BINOP_FLAGS_ABBREV, 00055 FUNCTION_INST_CAST_ABBREV, 00056 FUNCTION_INST_RET_VOID_ABBREV, 00057 FUNCTION_INST_RET_VAL_ABBREV, 00058 FUNCTION_INST_UNREACHABLE_ABBREV 00059 }; 00060 00061 static unsigned GetEncodedCastOpcode(unsigned Opcode) { 00062 switch (Opcode) { 00063 default: llvm_unreachable("Unknown cast instruction!"); 00064 case Instruction::Trunc : return bitc::CAST_TRUNC; 00065 case Instruction::ZExt : return bitc::CAST_ZEXT; 00066 case Instruction::SExt : return bitc::CAST_SEXT; 00067 case Instruction::FPToUI : return bitc::CAST_FPTOUI; 00068 case Instruction::FPToSI : return bitc::CAST_FPTOSI; 00069 case Instruction::UIToFP : return bitc::CAST_UITOFP; 00070 case Instruction::SIToFP : return bitc::CAST_SITOFP; 00071 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; 00072 case Instruction::FPExt : return bitc::CAST_FPEXT; 00073 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; 00074 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; 00075 case Instruction::BitCast : return bitc::CAST_BITCAST; 00076 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST; 00077 } 00078 } 00079 00080 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) { 00081 switch (Opcode) { 00082 default: llvm_unreachable("Unknown binary instruction!"); 00083 case Instruction::Add: 00084 case Instruction::FAdd: return bitc::BINOP_ADD; 00085 case Instruction::Sub: 00086 case Instruction::FSub: return bitc::BINOP_SUB; 00087 case Instruction::Mul: 00088 case Instruction::FMul: return bitc::BINOP_MUL; 00089 case Instruction::UDiv: return bitc::BINOP_UDIV; 00090 case Instruction::FDiv: 00091 case Instruction::SDiv: return bitc::BINOP_SDIV; 00092 case Instruction::URem: return bitc::BINOP_UREM; 00093 case Instruction::FRem: 00094 case Instruction::SRem: return bitc::BINOP_SREM; 00095 case Instruction::Shl: return bitc::BINOP_SHL; 00096 case Instruction::LShr: return bitc::BINOP_LSHR; 00097 case Instruction::AShr: return bitc::BINOP_ASHR; 00098 case Instruction::And: return bitc::BINOP_AND; 00099 case Instruction::Or: return bitc::BINOP_OR; 00100 case Instruction::Xor: return bitc::BINOP_XOR; 00101 } 00102 } 00103 00104 static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) { 00105 switch (Op) { 00106 default: llvm_unreachable("Unknown RMW operation!"); 00107 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG; 00108 case AtomicRMWInst::Add: return bitc::RMW_ADD; 00109 case AtomicRMWInst::Sub: return bitc::RMW_SUB; 00110 case AtomicRMWInst::And: return bitc::RMW_AND; 00111 case AtomicRMWInst::Nand: return bitc::RMW_NAND; 00112 case AtomicRMWInst::Or: return bitc::RMW_OR; 00113 case AtomicRMWInst::Xor: return bitc::RMW_XOR; 00114 case AtomicRMWInst::Max: return bitc::RMW_MAX; 00115 case AtomicRMWInst::Min: return bitc::RMW_MIN; 00116 case AtomicRMWInst::UMax: return bitc::RMW_UMAX; 00117 case AtomicRMWInst::UMin: return bitc::RMW_UMIN; 00118 } 00119 } 00120 00121 static unsigned GetEncodedOrdering(AtomicOrdering Ordering) { 00122 switch (Ordering) { 00123 case NotAtomic: return bitc::ORDERING_NOTATOMIC; 00124 case Unordered: return bitc::ORDERING_UNORDERED; 00125 case Monotonic: return bitc::ORDERING_MONOTONIC; 00126 case Acquire: return bitc::ORDERING_ACQUIRE; 00127 case Release: return bitc::ORDERING_RELEASE; 00128 case AcquireRelease: return bitc::ORDERING_ACQREL; 00129 case SequentiallyConsistent: return bitc::ORDERING_SEQCST; 00130 } 00131 llvm_unreachable("Invalid ordering"); 00132 } 00133 00134 static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) { 00135 switch (SynchScope) { 00136 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD; 00137 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD; 00138 } 00139 llvm_unreachable("Invalid synch scope"); 00140 } 00141 00142 static void WriteStringRecord(unsigned Code, StringRef Str, 00143 unsigned AbbrevToUse, BitstreamWriter &Stream) { 00144 SmallVector<unsigned, 64> Vals; 00145 00146 // Code: [strchar x N] 00147 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 00148 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i])) 00149 AbbrevToUse = 0; 00150 Vals.push_back(Str[i]); 00151 } 00152 00153 // Emit the finished record. 00154 Stream.EmitRecord(Code, Vals, AbbrevToUse); 00155 } 00156 00157 static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) { 00158 switch (Kind) { 00159 case Attribute::Alignment: 00160 return bitc::ATTR_KIND_ALIGNMENT; 00161 case Attribute::AlwaysInline: 00162 return bitc::ATTR_KIND_ALWAYS_INLINE; 00163 case Attribute::Builtin: 00164 return bitc::ATTR_KIND_BUILTIN; 00165 case Attribute::ByVal: 00166 return bitc::ATTR_KIND_BY_VAL; 00167 case Attribute::InAlloca: 00168 return bitc::ATTR_KIND_IN_ALLOCA; 00169 case Attribute::Cold: 00170 return bitc::ATTR_KIND_COLD; 00171 case Attribute::InlineHint: 00172 return bitc::ATTR_KIND_INLINE_HINT; 00173 case Attribute::InReg: 00174 return bitc::ATTR_KIND_IN_REG; 00175 case Attribute::JumpTable: 00176 return bitc::ATTR_KIND_JUMP_TABLE; 00177 case Attribute::MinSize: 00178 return bitc::ATTR_KIND_MIN_SIZE; 00179 case Attribute::Naked: 00180 return bitc::ATTR_KIND_NAKED; 00181 case Attribute::Nest: 00182 return bitc::ATTR_KIND_NEST; 00183 case Attribute::NoAlias: 00184 return bitc::ATTR_KIND_NO_ALIAS; 00185 case Attribute::NoBuiltin: 00186 return bitc::ATTR_KIND_NO_BUILTIN; 00187 case Attribute::NoCapture: 00188 return bitc::ATTR_KIND_NO_CAPTURE; 00189 case Attribute::NoDuplicate: 00190 return bitc::ATTR_KIND_NO_DUPLICATE; 00191 case Attribute::NoImplicitFloat: 00192 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT; 00193 case Attribute::NoInline: 00194 return bitc::ATTR_KIND_NO_INLINE; 00195 case Attribute::NonLazyBind: 00196 return bitc::ATTR_KIND_NON_LAZY_BIND; 00197 case Attribute::NonNull: 00198 return bitc::ATTR_KIND_NON_NULL; 00199 case Attribute::Dereferenceable: 00200 return bitc::ATTR_KIND_DEREFERENCEABLE; 00201 case Attribute::NoRedZone: 00202 return bitc::ATTR_KIND_NO_RED_ZONE; 00203 case Attribute::NoReturn: 00204 return bitc::ATTR_KIND_NO_RETURN; 00205 case Attribute::NoUnwind: 00206 return bitc::ATTR_KIND_NO_UNWIND; 00207 case Attribute::OptimizeForSize: 00208 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE; 00209 case Attribute::OptimizeNone: 00210 return bitc::ATTR_KIND_OPTIMIZE_NONE; 00211 case Attribute::ReadNone: 00212 return bitc::ATTR_KIND_READ_NONE; 00213 case Attribute::ReadOnly: 00214 return bitc::ATTR_KIND_READ_ONLY; 00215 case Attribute::Returned: 00216 return bitc::ATTR_KIND_RETURNED; 00217 case Attribute::ReturnsTwice: 00218 return bitc::ATTR_KIND_RETURNS_TWICE; 00219 case Attribute::SExt: 00220 return bitc::ATTR_KIND_S_EXT; 00221 case Attribute::StackAlignment: 00222 return bitc::ATTR_KIND_STACK_ALIGNMENT; 00223 case Attribute::StackProtect: 00224 return bitc::ATTR_KIND_STACK_PROTECT; 00225 case Attribute::StackProtectReq: 00226 return bitc::ATTR_KIND_STACK_PROTECT_REQ; 00227 case Attribute::StackProtectStrong: 00228 return bitc::ATTR_KIND_STACK_PROTECT_STRONG; 00229 case Attribute::StructRet: 00230 return bitc::ATTR_KIND_STRUCT_RET; 00231 case Attribute::SanitizeAddress: 00232 return bitc::ATTR_KIND_SANITIZE_ADDRESS; 00233 case Attribute::SanitizeThread: 00234 return bitc::ATTR_KIND_SANITIZE_THREAD; 00235 case Attribute::SanitizeMemory: 00236 return bitc::ATTR_KIND_SANITIZE_MEMORY; 00237 case Attribute::UWTable: 00238 return bitc::ATTR_KIND_UW_TABLE; 00239 case Attribute::ZExt: 00240 return bitc::ATTR_KIND_Z_EXT; 00241 case Attribute::EndAttrKinds: 00242 llvm_unreachable("Can not encode end-attribute kinds marker."); 00243 case Attribute::None: 00244 llvm_unreachable("Can not encode none-attribute."); 00245 } 00246 00247 llvm_unreachable("Trying to encode unknown attribute"); 00248 } 00249 00250 static void WriteAttributeGroupTable(const ValueEnumerator &VE, 00251 BitstreamWriter &Stream) { 00252 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups(); 00253 if (AttrGrps.empty()) return; 00254 00255 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3); 00256 00257 SmallVector<uint64_t, 64> Record; 00258 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) { 00259 AttributeSet AS = AttrGrps[i]; 00260 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) { 00261 AttributeSet A = AS.getSlotAttributes(i); 00262 00263 Record.push_back(VE.getAttributeGroupID(A)); 00264 Record.push_back(AS.getSlotIndex(i)); 00265 00266 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0); 00267 I != E; ++I) { 00268 Attribute Attr = *I; 00269 if (Attr.isEnumAttribute()) { 00270 Record.push_back(0); 00271 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); 00272 } else if (Attr.isIntAttribute()) { 00273 Record.push_back(1); 00274 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); 00275 Record.push_back(Attr.getValueAsInt()); 00276 } else { 00277 StringRef Kind = Attr.getKindAsString(); 00278 StringRef Val = Attr.getValueAsString(); 00279 00280 Record.push_back(Val.empty() ? 3 : 4); 00281 Record.append(Kind.begin(), Kind.end()); 00282 Record.push_back(0); 00283 if (!Val.empty()) { 00284 Record.append(Val.begin(), Val.end()); 00285 Record.push_back(0); 00286 } 00287 } 00288 } 00289 00290 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record); 00291 Record.clear(); 00292 } 00293 } 00294 00295 Stream.ExitBlock(); 00296 } 00297 00298 static void WriteAttributeTable(const ValueEnumerator &VE, 00299 BitstreamWriter &Stream) { 00300 const std::vector<AttributeSet> &Attrs = VE.getAttributes(); 00301 if (Attrs.empty()) return; 00302 00303 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3); 00304 00305 SmallVector<uint64_t, 64> Record; 00306 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { 00307 const AttributeSet &A = Attrs[i]; 00308 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) 00309 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i))); 00310 00311 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); 00312 Record.clear(); 00313 } 00314 00315 Stream.ExitBlock(); 00316 } 00317 00318 /// WriteTypeTable - Write out the type table for a module. 00319 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { 00320 const ValueEnumerator::TypeList &TypeList = VE.getTypes(); 00321 00322 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */); 00323 SmallVector<uint64_t, 64> TypeVals; 00324 00325 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); 00326 00327 // Abbrev for TYPE_CODE_POINTER. 00328 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 00329 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER)); 00330 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 00331 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0 00332 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); 00333 00334 // Abbrev for TYPE_CODE_FUNCTION. 00335 Abbv = new BitCodeAbbrev(); 00336 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); 00337 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg 00338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 00339 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 00340 00341 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); 00342 00343 // Abbrev for TYPE_CODE_STRUCT_ANON. 00344 Abbv = new BitCodeAbbrev(); 00345 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON)); 00346 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 00347 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 00348 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 00349 00350 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); 00351 00352 // Abbrev for TYPE_CODE_STRUCT_NAME. 00353 Abbv = new BitCodeAbbrev(); 00354 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME)); 00355 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 00356 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 00357 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); 00358 00359 // Abbrev for TYPE_CODE_STRUCT_NAMED. 00360 Abbv = new BitCodeAbbrev(); 00361 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED)); 00362 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 00363 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 00364 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 00365 00366 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); 00367 00368 // Abbrev for TYPE_CODE_ARRAY. 00369 Abbv = new BitCodeAbbrev(); 00370 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY)); 00371 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size 00372 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 00373 00374 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); 00375 00376 // Emit an entry count so the reader can reserve space. 00377 TypeVals.push_back(TypeList.size()); 00378 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); 00379 TypeVals.clear(); 00380 00381 // Loop over all of the types, emitting each in turn. 00382 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { 00383 Type *T = TypeList[i]; 00384 int AbbrevToUse = 0; 00385 unsigned Code = 0; 00386 00387 switch (T->getTypeID()) { 00388 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; 00389 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break; 00390 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; 00391 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; 00392 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break; 00393 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break; 00394 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break; 00395 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; 00396 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break; 00397 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break; 00398 case Type::IntegerTyID: 00399 // INTEGER: [width] 00400 Code = bitc::TYPE_CODE_INTEGER; 00401 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); 00402 break; 00403 case Type::PointerTyID: { 00404 PointerType *PTy = cast<PointerType>(T); 00405 // POINTER: [pointee type, address space] 00406 Code = bitc::TYPE_CODE_POINTER; 00407 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); 00408 unsigned AddressSpace = PTy->getAddressSpace(); 00409 TypeVals.push_back(AddressSpace); 00410 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; 00411 break; 00412 } 00413 case Type::FunctionTyID: { 00414 FunctionType *FT = cast<FunctionType>(T); 00415 // FUNCTION: [isvararg, retty, paramty x N] 00416 Code = bitc::TYPE_CODE_FUNCTION; 00417 TypeVals.push_back(FT->isVarArg()); 00418 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); 00419 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) 00420 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); 00421 AbbrevToUse = FunctionAbbrev; 00422 break; 00423 } 00424 case Type::StructTyID: { 00425 StructType *ST = cast<StructType>(T); 00426 // STRUCT: [ispacked, eltty x N] 00427 TypeVals.push_back(ST->isPacked()); 00428 // Output all of the element types. 00429 for (StructType::element_iterator I = ST->element_begin(), 00430 E = ST->element_end(); I != E; ++I) 00431 TypeVals.push_back(VE.getTypeID(*I)); 00432 00433 if (ST->isLiteral()) { 00434 Code = bitc::TYPE_CODE_STRUCT_ANON; 00435 AbbrevToUse = StructAnonAbbrev; 00436 } else { 00437 if (ST->isOpaque()) { 00438 Code = bitc::TYPE_CODE_OPAQUE; 00439 } else { 00440 Code = bitc::TYPE_CODE_STRUCT_NAMED; 00441 AbbrevToUse = StructNamedAbbrev; 00442 } 00443 00444 // Emit the name if it is present. 00445 if (!ST->getName().empty()) 00446 WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(), 00447 StructNameAbbrev, Stream); 00448 } 00449 break; 00450 } 00451 case Type::ArrayTyID: { 00452 ArrayType *AT = cast<ArrayType>(T); 00453 // ARRAY: [numelts, eltty] 00454 Code = bitc::TYPE_CODE_ARRAY; 00455 TypeVals.push_back(AT->getNumElements()); 00456 TypeVals.push_back(VE.getTypeID(AT->getElementType())); 00457 AbbrevToUse = ArrayAbbrev; 00458 break; 00459 } 00460 case Type::VectorTyID: { 00461 VectorType *VT = cast<VectorType>(T); 00462 // VECTOR [numelts, eltty] 00463 Code = bitc::TYPE_CODE_VECTOR; 00464 TypeVals.push_back(VT->getNumElements()); 00465 TypeVals.push_back(VE.getTypeID(VT->getElementType())); 00466 break; 00467 } 00468 } 00469 00470 // Emit the finished record. 00471 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); 00472 TypeVals.clear(); 00473 } 00474 00475 Stream.ExitBlock(); 00476 } 00477 00478 static unsigned getEncodedLinkage(const GlobalValue &GV) { 00479 switch (GV.getLinkage()) { 00480 case GlobalValue::ExternalLinkage: return 0; 00481 case GlobalValue::WeakAnyLinkage: return 1; 00482 case GlobalValue::AppendingLinkage: return 2; 00483 case GlobalValue::InternalLinkage: return 3; 00484 case GlobalValue::LinkOnceAnyLinkage: return 4; 00485 case GlobalValue::ExternalWeakLinkage: return 7; 00486 case GlobalValue::CommonLinkage: return 8; 00487 case GlobalValue::PrivateLinkage: return 9; 00488 case GlobalValue::WeakODRLinkage: return 10; 00489 case GlobalValue::LinkOnceODRLinkage: return 11; 00490 case GlobalValue::AvailableExternallyLinkage: return 12; 00491 } 00492 llvm_unreachable("Invalid linkage"); 00493 } 00494 00495 static unsigned getEncodedVisibility(const GlobalValue &GV) { 00496 switch (GV.getVisibility()) { 00497 case GlobalValue::DefaultVisibility: return 0; 00498 case GlobalValue::HiddenVisibility: return 1; 00499 case GlobalValue::ProtectedVisibility: return 2; 00500 } 00501 llvm_unreachable("Invalid visibility"); 00502 } 00503 00504 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) { 00505 switch (GV.getDLLStorageClass()) { 00506 case GlobalValue::DefaultStorageClass: return 0; 00507 case GlobalValue::DLLImportStorageClass: return 1; 00508 case GlobalValue::DLLExportStorageClass: return 2; 00509 } 00510 llvm_unreachable("Invalid DLL storage class"); 00511 } 00512 00513 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) { 00514 switch (GV.getThreadLocalMode()) { 00515 case GlobalVariable::NotThreadLocal: return 0; 00516 case GlobalVariable::GeneralDynamicTLSModel: return 1; 00517 case GlobalVariable::LocalDynamicTLSModel: return 2; 00518 case GlobalVariable::InitialExecTLSModel: return 3; 00519 case GlobalVariable::LocalExecTLSModel: return 4; 00520 } 00521 llvm_unreachable("Invalid TLS model"); 00522 } 00523 00524 static unsigned getEncodedComdatSelectionKind(const Comdat &C) { 00525 switch (C.getSelectionKind()) { 00526 case Comdat::Any: 00527 return bitc::COMDAT_SELECTION_KIND_ANY; 00528 case Comdat::ExactMatch: 00529 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH; 00530 case Comdat::Largest: 00531 return bitc::COMDAT_SELECTION_KIND_LARGEST; 00532 case Comdat::NoDuplicates: 00533 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES; 00534 case Comdat::SameSize: 00535 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE; 00536 } 00537 llvm_unreachable("Invalid selection kind"); 00538 } 00539 00540 static void writeComdats(const ValueEnumerator &VE, BitstreamWriter &Stream) { 00541 SmallVector<uint8_t, 64> Vals; 00542 for (const Comdat *C : VE.getComdats()) { 00543 // COMDAT: [selection_kind, name] 00544 Vals.push_back(getEncodedComdatSelectionKind(*C)); 00545 Vals.push_back(C->getName().size()); 00546 for (char Chr : C->getName()) 00547 Vals.push_back((unsigned char)Chr); 00548 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0); 00549 Vals.clear(); 00550 } 00551 } 00552 00553 // Emit top-level description of module, including target triple, inline asm, 00554 // descriptors for global variables, and function prototype info. 00555 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, 00556 BitstreamWriter &Stream) { 00557 // Emit various pieces of data attached to a module. 00558 if (!M->getTargetTriple().empty()) 00559 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 00560 0/*TODO*/, Stream); 00561 const std::string &DL = M->getDataLayoutStr(); 00562 if (!DL.empty()) 00563 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream); 00564 if (!M->getModuleInlineAsm().empty()) 00565 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 00566 0/*TODO*/, Stream); 00567 00568 // Emit information about sections and GC, computing how many there are. Also 00569 // compute the maximum alignment value. 00570 std::map<std::string, unsigned> SectionMap; 00571 std::map<std::string, unsigned> GCMap; 00572 unsigned MaxAlignment = 0; 00573 unsigned MaxGlobalType = 0; 00574 for (const GlobalValue &GV : M->globals()) { 00575 MaxAlignment = std::max(MaxAlignment, GV.getAlignment()); 00576 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getType())); 00577 if (GV.hasSection()) { 00578 // Give section names unique ID's. 00579 unsigned &Entry = SectionMap[GV.getSection()]; 00580 if (!Entry) { 00581 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(), 00582 0/*TODO*/, Stream); 00583 Entry = SectionMap.size(); 00584 } 00585 } 00586 } 00587 for (const Function &F : *M) { 00588 MaxAlignment = std::max(MaxAlignment, F.getAlignment()); 00589 if (F.hasSection()) { 00590 // Give section names unique ID's. 00591 unsigned &Entry = SectionMap[F.getSection()]; 00592 if (!Entry) { 00593 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(), 00594 0/*TODO*/, Stream); 00595 Entry = SectionMap.size(); 00596 } 00597 } 00598 if (F.hasGC()) { 00599 // Same for GC names. 00600 unsigned &Entry = GCMap[F.getGC()]; 00601 if (!Entry) { 00602 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(), 00603 0/*TODO*/, Stream); 00604 Entry = GCMap.size(); 00605 } 00606 } 00607 } 00608 00609 // Emit abbrev for globals, now that we know # sections and max alignment. 00610 unsigned SimpleGVarAbbrev = 0; 00611 if (!M->global_empty()) { 00612 // Add an abbrev for common globals with no visibility or thread localness. 00613 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 00614 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); 00615 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 00616 Log2_32_Ceil(MaxGlobalType+1))); 00617 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. 00618 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. 00619 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage. 00620 if (MaxAlignment == 0) // Alignment. 00621 Abbv->Add(BitCodeAbbrevOp(0)); 00622 else { 00623 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; 00624 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 00625 Log2_32_Ceil(MaxEncAlignment+1))); 00626 } 00627 if (SectionMap.empty()) // Section. 00628 Abbv->Add(BitCodeAbbrevOp(0)); 00629 else 00630 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 00631 Log2_32_Ceil(SectionMap.size()+1))); 00632 // Don't bother emitting vis + thread local. 00633 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); 00634 } 00635 00636 // Emit the global variable information. 00637 SmallVector<unsigned, 64> Vals; 00638 for (const GlobalVariable &GV : M->globals()) { 00639 unsigned AbbrevToUse = 0; 00640 00641 // GLOBALVAR: [type, isconst, initid, 00642 // linkage, alignment, section, visibility, threadlocal, 00643 // unnamed_addr, externally_initialized, dllstorageclass] 00644 Vals.push_back(VE.getTypeID(GV.getType())); 00645 Vals.push_back(GV.isConstant()); 00646 Vals.push_back(GV.isDeclaration() ? 0 : 00647 (VE.getValueID(GV.getInitializer()) + 1)); 00648 Vals.push_back(getEncodedLinkage(GV)); 00649 Vals.push_back(Log2_32(GV.getAlignment())+1); 00650 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0); 00651 if (GV.isThreadLocal() || 00652 GV.getVisibility() != GlobalValue::DefaultVisibility || 00653 GV.hasUnnamedAddr() || GV.isExternallyInitialized() || 00654 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass || 00655 GV.hasComdat()) { 00656 Vals.push_back(getEncodedVisibility(GV)); 00657 Vals.push_back(getEncodedThreadLocalMode(GV)); 00658 Vals.push_back(GV.hasUnnamedAddr()); 00659 Vals.push_back(GV.isExternallyInitialized()); 00660 Vals.push_back(getEncodedDLLStorageClass(GV)); 00661 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0); 00662 } else { 00663 AbbrevToUse = SimpleGVarAbbrev; 00664 } 00665 00666 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 00667 Vals.clear(); 00668 } 00669 00670 // Emit the function proto information. 00671 for (const Function &F : *M) { 00672 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, 00673 // section, visibility, gc, unnamed_addr, prefix] 00674 Vals.push_back(VE.getTypeID(F.getType())); 00675 Vals.push_back(F.getCallingConv()); 00676 Vals.push_back(F.isDeclaration()); 00677 Vals.push_back(getEncodedLinkage(F)); 00678 Vals.push_back(VE.getAttributeID(F.getAttributes())); 00679 Vals.push_back(Log2_32(F.getAlignment())+1); 00680 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0); 00681 Vals.push_back(getEncodedVisibility(F)); 00682 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0); 00683 Vals.push_back(F.hasUnnamedAddr()); 00684 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1) 00685 : 0); 00686 Vals.push_back(getEncodedDLLStorageClass(F)); 00687 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0); 00688 00689 unsigned AbbrevToUse = 0; 00690 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); 00691 Vals.clear(); 00692 } 00693 00694 // Emit the alias information. 00695 for (const GlobalAlias &A : M->aliases()) { 00696 // ALIAS: [alias type, aliasee val#, linkage, visibility] 00697 Vals.push_back(VE.getTypeID(A.getType())); 00698 Vals.push_back(VE.getValueID(A.getAliasee())); 00699 Vals.push_back(getEncodedLinkage(A)); 00700 Vals.push_back(getEncodedVisibility(A)); 00701 Vals.push_back(getEncodedDLLStorageClass(A)); 00702 Vals.push_back(getEncodedThreadLocalMode(A)); 00703 Vals.push_back(A.hasUnnamedAddr()); 00704 unsigned AbbrevToUse = 0; 00705 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 00706 Vals.clear(); 00707 } 00708 } 00709 00710 static uint64_t GetOptimizationFlags(const Value *V) { 00711 uint64_t Flags = 0; 00712 00713 if (const OverflowingBinaryOperator *OBO = 00714 dyn_cast<OverflowingBinaryOperator>(V)) { 00715 if (OBO->hasNoSignedWrap()) 00716 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP; 00717 if (OBO->hasNoUnsignedWrap()) 00718 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP; 00719 } else if (const PossiblyExactOperator *PEO = 00720 dyn_cast<PossiblyExactOperator>(V)) { 00721 if (PEO->isExact()) 00722 Flags |= 1 << bitc::PEO_EXACT; 00723 } else if (const FPMathOperator *FPMO = 00724 dyn_cast<const FPMathOperator>(V)) { 00725 if (FPMO->hasUnsafeAlgebra()) 00726 Flags |= FastMathFlags::UnsafeAlgebra; 00727 if (FPMO->hasNoNaNs()) 00728 Flags |= FastMathFlags::NoNaNs; 00729 if (FPMO->hasNoInfs()) 00730 Flags |= FastMathFlags::NoInfs; 00731 if (FPMO->hasNoSignedZeros()) 00732 Flags |= FastMathFlags::NoSignedZeros; 00733 if (FPMO->hasAllowReciprocal()) 00734 Flags |= FastMathFlags::AllowReciprocal; 00735 } 00736 00737 return Flags; 00738 } 00739 00740 static void WriteMDNode(const MDNode *N, 00741 const ValueEnumerator &VE, 00742 BitstreamWriter &Stream, 00743 SmallVectorImpl<uint64_t> &Record) { 00744 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 00745 if (N->getOperand(i)) { 00746 Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); 00747 Record.push_back(VE.getValueID(N->getOperand(i))); 00748 } else { 00749 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext()))); 00750 Record.push_back(0); 00751 } 00752 } 00753 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE : 00754 bitc::METADATA_NODE; 00755 Stream.EmitRecord(MDCode, Record, 0); 00756 Record.clear(); 00757 } 00758 00759 static void WriteModuleMetadata(const Module *M, 00760 const ValueEnumerator &VE, 00761 BitstreamWriter &Stream) { 00762 const ValueEnumerator::ValueList &Vals = VE.getMDValues(); 00763 bool StartedMetadataBlock = false; 00764 unsigned MDSAbbrev = 0; 00765 SmallVector<uint64_t, 64> Record; 00766 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 00767 00768 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { 00769 if (!N->isFunctionLocal() || !N->getFunction()) { 00770 if (!StartedMetadataBlock) { 00771 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 00772 StartedMetadataBlock = true; 00773 } 00774 WriteMDNode(N, VE, Stream, Record); 00775 } 00776 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { 00777 if (!StartedMetadataBlock) { 00778 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 00779 00780 // Abbrev for METADATA_STRING. 00781 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 00782 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); 00783 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 00784 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 00785 MDSAbbrev = Stream.EmitAbbrev(Abbv); 00786 StartedMetadataBlock = true; 00787 } 00788 00789 // Code: [strchar x N] 00790 Record.append(MDS->begin(), MDS->end()); 00791 00792 // Emit the finished record. 00793 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); 00794 Record.clear(); 00795 } 00796 } 00797 00798 // Write named metadata. 00799 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 00800 E = M->named_metadata_end(); I != E; ++I) { 00801 const NamedMDNode *NMD = I; 00802 if (!StartedMetadataBlock) { 00803 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 00804 StartedMetadataBlock = true; 00805 } 00806 00807 // Write name. 00808 StringRef Str = NMD->getName(); 00809 for (unsigned i = 0, e = Str.size(); i != e; ++i) 00810 Record.push_back(Str[i]); 00811 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/); 00812 Record.clear(); 00813 00814 // Write named metadata operands. 00815 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 00816 Record.push_back(VE.getValueID(NMD->getOperand(i))); 00817 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); 00818 Record.clear(); 00819 } 00820 00821 if (StartedMetadataBlock) 00822 Stream.ExitBlock(); 00823 } 00824 00825 static void WriteFunctionLocalMetadata(const Function &F, 00826 const ValueEnumerator &VE, 00827 BitstreamWriter &Stream) { 00828 bool StartedMetadataBlock = false; 00829 SmallVector<uint64_t, 64> Record; 00830 const SmallVectorImpl<const MDNode *> &Vals = VE.getFunctionLocalMDValues(); 00831 for (unsigned i = 0, e = Vals.size(); i != e; ++i) 00832 if (const MDNode *N = Vals[i]) 00833 if (N->isFunctionLocal() && N->getFunction() == &F) { 00834 if (!StartedMetadataBlock) { 00835 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 00836 StartedMetadataBlock = true; 00837 } 00838 WriteMDNode(N, VE, Stream, Record); 00839 } 00840 00841 if (StartedMetadataBlock) 00842 Stream.ExitBlock(); 00843 } 00844 00845 static void WriteMetadataAttachment(const Function &F, 00846 const ValueEnumerator &VE, 00847 BitstreamWriter &Stream) { 00848 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3); 00849 00850 SmallVector<uint64_t, 64> Record; 00851 00852 // Write metadata attachments 00853 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] 00854 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; 00855 00856 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 00857 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 00858 I != E; ++I) { 00859 MDs.clear(); 00860 I->getAllMetadataOtherThanDebugLoc(MDs); 00861 00862 // If no metadata, ignore instruction. 00863 if (MDs.empty()) continue; 00864 00865 Record.push_back(VE.getInstructionID(I)); 00866 00867 for (unsigned i = 0, e = MDs.size(); i != e; ++i) { 00868 Record.push_back(MDs[i].first); 00869 Record.push_back(VE.getValueID(MDs[i].second)); 00870 } 00871 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); 00872 Record.clear(); 00873 } 00874 00875 Stream.ExitBlock(); 00876 } 00877 00878 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) { 00879 SmallVector<uint64_t, 64> Record; 00880 00881 // Write metadata kinds 00882 // METADATA_KIND - [n x [id, name]] 00883 SmallVector<StringRef, 8> Names; 00884 M->getMDKindNames(Names); 00885 00886 if (Names.empty()) return; 00887 00888 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 00889 00890 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { 00891 Record.push_back(MDKindID); 00892 StringRef KName = Names[MDKindID]; 00893 Record.append(KName.begin(), KName.end()); 00894 00895 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0); 00896 Record.clear(); 00897 } 00898 00899 Stream.ExitBlock(); 00900 } 00901 00902 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { 00903 if ((int64_t)V >= 0) 00904 Vals.push_back(V << 1); 00905 else 00906 Vals.push_back((-V << 1) | 1); 00907 } 00908 00909 static void WriteConstants(unsigned FirstVal, unsigned LastVal, 00910 const ValueEnumerator &VE, 00911 BitstreamWriter &Stream, bool isGlobal) { 00912 if (FirstVal == LastVal) return; 00913 00914 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); 00915 00916 unsigned AggregateAbbrev = 0; 00917 unsigned String8Abbrev = 0; 00918 unsigned CString7Abbrev = 0; 00919 unsigned CString6Abbrev = 0; 00920 // If this is a constant pool for the module, emit module-specific abbrevs. 00921 if (isGlobal) { 00922 // Abbrev for CST_CODE_AGGREGATE. 00923 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 00924 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); 00925 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 00926 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); 00927 AggregateAbbrev = Stream.EmitAbbrev(Abbv); 00928 00929 // Abbrev for CST_CODE_STRING. 00930 Abbv = new BitCodeAbbrev(); 00931 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); 00932 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 00933 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 00934 String8Abbrev = Stream.EmitAbbrev(Abbv); 00935 // Abbrev for CST_CODE_CSTRING. 00936 Abbv = new BitCodeAbbrev(); 00937 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 00938 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 00939 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 00940 CString7Abbrev = Stream.EmitAbbrev(Abbv); 00941 // Abbrev for CST_CODE_CSTRING. 00942 Abbv = new BitCodeAbbrev(); 00943 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 00944 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 00945 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 00946 CString6Abbrev = Stream.EmitAbbrev(Abbv); 00947 } 00948 00949 SmallVector<uint64_t, 64> Record; 00950 00951 const ValueEnumerator::ValueList &Vals = VE.getValues(); 00952 Type *LastTy = nullptr; 00953 for (unsigned i = FirstVal; i != LastVal; ++i) { 00954 const Value *V = Vals[i].first; 00955 // If we need to switch types, do so now. 00956 if (V->getType() != LastTy) { 00957 LastTy = V->getType(); 00958 Record.push_back(VE.getTypeID(LastTy)); 00959 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, 00960 CONSTANTS_SETTYPE_ABBREV); 00961 Record.clear(); 00962 } 00963 00964 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 00965 Record.push_back(unsigned(IA->hasSideEffects()) | 00966 unsigned(IA->isAlignStack()) << 1 | 00967 unsigned(IA->getDialect()&1) << 2); 00968 00969 // Add the asm string. 00970 const std::string &AsmStr = IA->getAsmString(); 00971 Record.push_back(AsmStr.size()); 00972 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i) 00973 Record.push_back(AsmStr[i]); 00974 00975 // Add the constraint string. 00976 const std::string &ConstraintStr = IA->getConstraintString(); 00977 Record.push_back(ConstraintStr.size()); 00978 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i) 00979 Record.push_back(ConstraintStr[i]); 00980 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); 00981 Record.clear(); 00982 continue; 00983 } 00984 const Constant *C = cast<Constant>(V); 00985 unsigned Code = -1U; 00986 unsigned AbbrevToUse = 0; 00987 if (C->isNullValue()) { 00988 Code = bitc::CST_CODE_NULL; 00989 } else if (isa<UndefValue>(C)) { 00990 Code = bitc::CST_CODE_UNDEF; 00991 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { 00992 if (IV->getBitWidth() <= 64) { 00993 uint64_t V = IV->getSExtValue(); 00994 emitSignedInt64(Record, V); 00995 Code = bitc::CST_CODE_INTEGER; 00996 AbbrevToUse = CONSTANTS_INTEGER_ABBREV; 00997 } else { // Wide integers, > 64 bits in size. 00998 // We have an arbitrary precision integer value to write whose 00999 // bit width is > 64. However, in canonical unsigned integer 01000 // format it is likely that the high bits are going to be zero. 01001 // So, we only write the number of active words. 01002 unsigned NWords = IV->getValue().getActiveWords(); 01003 const uint64_t *RawWords = IV->getValue().getRawData(); 01004 for (unsigned i = 0; i != NWords; ++i) { 01005 emitSignedInt64(Record, RawWords[i]); 01006 } 01007 Code = bitc::CST_CODE_WIDE_INTEGER; 01008 } 01009 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 01010 Code = bitc::CST_CODE_FLOAT; 01011 Type *Ty = CFP->getType(); 01012 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) { 01013 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 01014 } else if (Ty->isX86_FP80Ty()) { 01015 // api needed to prevent premature destruction 01016 // bits are not in the same order as a normal i80 APInt, compensate. 01017 APInt api = CFP->getValueAPF().bitcastToAPInt(); 01018 const uint64_t *p = api.getRawData(); 01019 Record.push_back((p[1] << 48) | (p[0] >> 16)); 01020 Record.push_back(p[0] & 0xffffLL); 01021 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) { 01022 APInt api = CFP->getValueAPF().bitcastToAPInt(); 01023 const uint64_t *p = api.getRawData(); 01024 Record.push_back(p[0]); 01025 Record.push_back(p[1]); 01026 } else { 01027 assert (0 && "Unknown FP type!"); 01028 } 01029 } else if (isa<ConstantDataSequential>(C) && 01030 cast<ConstantDataSequential>(C)->isString()) { 01031 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C); 01032 // Emit constant strings specially. 01033 unsigned NumElts = Str->getNumElements(); 01034 // If this is a null-terminated string, use the denser CSTRING encoding. 01035 if (Str->isCString()) { 01036 Code = bitc::CST_CODE_CSTRING; 01037 --NumElts; // Don't encode the null, which isn't allowed by char6. 01038 } else { 01039 Code = bitc::CST_CODE_STRING; 01040 AbbrevToUse = String8Abbrev; 01041 } 01042 bool isCStr7 = Code == bitc::CST_CODE_CSTRING; 01043 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; 01044 for (unsigned i = 0; i != NumElts; ++i) { 01045 unsigned char V = Str->getElementAsInteger(i); 01046 Record.push_back(V); 01047 isCStr7 &= (V & 128) == 0; 01048 if (isCStrChar6) 01049 isCStrChar6 = BitCodeAbbrevOp::isChar6(V); 01050 } 01051 01052 if (isCStrChar6) 01053 AbbrevToUse = CString6Abbrev; 01054 else if (isCStr7) 01055 AbbrevToUse = CString7Abbrev; 01056 } else if (const ConstantDataSequential *CDS = 01057 dyn_cast<ConstantDataSequential>(C)) { 01058 Code = bitc::CST_CODE_DATA; 01059 Type *EltTy = CDS->getType()->getElementType(); 01060 if (isa<IntegerType>(EltTy)) { 01061 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) 01062 Record.push_back(CDS->getElementAsInteger(i)); 01063 } else if (EltTy->isFloatTy()) { 01064 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { 01065 union { float F; uint32_t I; }; 01066 F = CDS->getElementAsFloat(i); 01067 Record.push_back(I); 01068 } 01069 } else { 01070 assert(EltTy->isDoubleTy() && "Unknown ConstantData element type"); 01071 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { 01072 union { double F; uint64_t I; }; 01073 F = CDS->getElementAsDouble(i); 01074 Record.push_back(I); 01075 } 01076 } 01077 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) || 01078 isa<ConstantVector>(C)) { 01079 Code = bitc::CST_CODE_AGGREGATE; 01080 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 01081 Record.push_back(VE.getValueID(C->getOperand(i))); 01082 AbbrevToUse = AggregateAbbrev; 01083 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 01084 switch (CE->getOpcode()) { 01085 default: 01086 if (Instruction::isCast(CE->getOpcode())) { 01087 Code = bitc::CST_CODE_CE_CAST; 01088 Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); 01089 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 01090 Record.push_back(VE.getValueID(C->getOperand(0))); 01091 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; 01092 } else { 01093 assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); 01094 Code = bitc::CST_CODE_CE_BINOP; 01095 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); 01096 Record.push_back(VE.getValueID(C->getOperand(0))); 01097 Record.push_back(VE.getValueID(C->getOperand(1))); 01098 uint64_t Flags = GetOptimizationFlags(CE); 01099 if (Flags != 0) 01100 Record.push_back(Flags); 01101 } 01102 break; 01103 case Instruction::GetElementPtr: 01104 Code = bitc::CST_CODE_CE_GEP; 01105 if (cast<GEPOperator>(C)->isInBounds()) 01106 Code = bitc::CST_CODE_CE_INBOUNDS_GEP; 01107 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 01108 Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); 01109 Record.push_back(VE.getValueID(C->getOperand(i))); 01110 } 01111 break; 01112 case Instruction::Select: 01113 Code = bitc::CST_CODE_CE_SELECT; 01114 Record.push_back(VE.getValueID(C->getOperand(0))); 01115 Record.push_back(VE.getValueID(C->getOperand(1))); 01116 Record.push_back(VE.getValueID(C->getOperand(2))); 01117 break; 01118 case Instruction::ExtractElement: 01119 Code = bitc::CST_CODE_CE_EXTRACTELT; 01120 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 01121 Record.push_back(VE.getValueID(C->getOperand(0))); 01122 Record.push_back(VE.getTypeID(C->getOperand(1)->getType())); 01123 Record.push_back(VE.getValueID(C->getOperand(1))); 01124 break; 01125 case Instruction::InsertElement: 01126 Code = bitc::CST_CODE_CE_INSERTELT; 01127 Record.push_back(VE.getValueID(C->getOperand(0))); 01128 Record.push_back(VE.getValueID(C->getOperand(1))); 01129 Record.push_back(VE.getTypeID(C->getOperand(2)->getType())); 01130 Record.push_back(VE.getValueID(C->getOperand(2))); 01131 break; 01132 case Instruction::ShuffleVector: 01133 // If the return type and argument types are the same, this is a 01134 // standard shufflevector instruction. If the types are different, 01135 // then the shuffle is widening or truncating the input vectors, and 01136 // the argument type must also be encoded. 01137 if (C->getType() == C->getOperand(0)->getType()) { 01138 Code = bitc::CST_CODE_CE_SHUFFLEVEC; 01139 } else { 01140 Code = bitc::CST_CODE_CE_SHUFVEC_EX; 01141 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 01142 } 01143 Record.push_back(VE.getValueID(C->getOperand(0))); 01144 Record.push_back(VE.getValueID(C->getOperand(1))); 01145 Record.push_back(VE.getValueID(C->getOperand(2))); 01146 break; 01147 case Instruction::ICmp: 01148 case Instruction::FCmp: 01149 Code = bitc::CST_CODE_CE_CMP; 01150 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 01151 Record.push_back(VE.getValueID(C->getOperand(0))); 01152 Record.push_back(VE.getValueID(C->getOperand(1))); 01153 Record.push_back(CE->getPredicate()); 01154 break; 01155 } 01156 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) { 01157 Code = bitc::CST_CODE_BLOCKADDRESS; 01158 Record.push_back(VE.getTypeID(BA->getFunction()->getType())); 01159 Record.push_back(VE.getValueID(BA->getFunction())); 01160 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock())); 01161 } else { 01162 #ifndef NDEBUG 01163 C->dump(); 01164 #endif 01165 llvm_unreachable("Unknown constant!"); 01166 } 01167 Stream.EmitRecord(Code, Record, AbbrevToUse); 01168 Record.clear(); 01169 } 01170 01171 Stream.ExitBlock(); 01172 } 01173 01174 static void WriteModuleConstants(const ValueEnumerator &VE, 01175 BitstreamWriter &Stream) { 01176 const ValueEnumerator::ValueList &Vals = VE.getValues(); 01177 01178 // Find the first constant to emit, which is the first non-globalvalue value. 01179 // We know globalvalues have been emitted by WriteModuleInfo. 01180 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 01181 if (!isa<GlobalValue>(Vals[i].first)) { 01182 WriteConstants(i, Vals.size(), VE, Stream, true); 01183 return; 01184 } 01185 } 01186 } 01187 01188 /// PushValueAndType - The file has to encode both the value and type id for 01189 /// many values, because we need to know what type to create for forward 01190 /// references. However, most operands are not forward references, so this type 01191 /// field is not needed. 01192 /// 01193 /// This function adds V's value ID to Vals. If the value ID is higher than the 01194 /// instruction ID, then it is a forward reference, and it also includes the 01195 /// type ID. The value ID that is written is encoded relative to the InstID. 01196 static bool PushValueAndType(const Value *V, unsigned InstID, 01197 SmallVectorImpl<unsigned> &Vals, 01198 ValueEnumerator &VE) { 01199 unsigned ValID = VE.getValueID(V); 01200 // Make encoding relative to the InstID. 01201 Vals.push_back(InstID - ValID); 01202 if (ValID >= InstID) { 01203 Vals.push_back(VE.getTypeID(V->getType())); 01204 return true; 01205 } 01206 return false; 01207 } 01208 01209 /// pushValue - Like PushValueAndType, but where the type of the value is 01210 /// omitted (perhaps it was already encoded in an earlier operand). 01211 static void pushValue(const Value *V, unsigned InstID, 01212 SmallVectorImpl<unsigned> &Vals, 01213 ValueEnumerator &VE) { 01214 unsigned ValID = VE.getValueID(V); 01215 Vals.push_back(InstID - ValID); 01216 } 01217 01218 static void pushValueSigned(const Value *V, unsigned InstID, 01219 SmallVectorImpl<uint64_t> &Vals, 01220 ValueEnumerator &VE) { 01221 unsigned ValID = VE.getValueID(V); 01222 int64_t diff = ((int32_t)InstID - (int32_t)ValID); 01223 emitSignedInt64(Vals, diff); 01224 } 01225 01226 /// WriteInstruction - Emit an instruction to the specified stream. 01227 static void WriteInstruction(const Instruction &I, unsigned InstID, 01228 ValueEnumerator &VE, BitstreamWriter &Stream, 01229 SmallVectorImpl<unsigned> &Vals) { 01230 unsigned Code = 0; 01231 unsigned AbbrevToUse = 0; 01232 VE.setInstructionID(&I); 01233 switch (I.getOpcode()) { 01234 default: 01235 if (Instruction::isCast(I.getOpcode())) { 01236 Code = bitc::FUNC_CODE_INST_CAST; 01237 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 01238 AbbrevToUse = FUNCTION_INST_CAST_ABBREV; 01239 Vals.push_back(VE.getTypeID(I.getType())); 01240 Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); 01241 } else { 01242 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 01243 Code = bitc::FUNC_CODE_INST_BINOP; 01244 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 01245 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; 01246 pushValue(I.getOperand(1), InstID, Vals, VE); 01247 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); 01248 uint64_t Flags = GetOptimizationFlags(&I); 01249 if (Flags != 0) { 01250 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV) 01251 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV; 01252 Vals.push_back(Flags); 01253 } 01254 } 01255 break; 01256 01257 case Instruction::GetElementPtr: 01258 Code = bitc::FUNC_CODE_INST_GEP; 01259 if (cast<GEPOperator>(&I)->isInBounds()) 01260 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP; 01261 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 01262 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 01263 break; 01264 case Instruction::ExtractValue: { 01265 Code = bitc::FUNC_CODE_INST_EXTRACTVAL; 01266 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 01267 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); 01268 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) 01269 Vals.push_back(*i); 01270 break; 01271 } 01272 case Instruction::InsertValue: { 01273 Code = bitc::FUNC_CODE_INST_INSERTVAL; 01274 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 01275 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 01276 const InsertValueInst *IVI = cast<InsertValueInst>(&I); 01277 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) 01278 Vals.push_back(*i); 01279 break; 01280 } 01281 case Instruction::Select: 01282 Code = bitc::FUNC_CODE_INST_VSELECT; 01283 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 01284 pushValue(I.getOperand(2), InstID, Vals, VE); 01285 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 01286 break; 01287 case Instruction::ExtractElement: 01288 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 01289 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 01290 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 01291 break; 01292 case Instruction::InsertElement: 01293 Code = bitc::FUNC_CODE_INST_INSERTELT; 01294 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 01295 pushValue(I.getOperand(1), InstID, Vals, VE); 01296 PushValueAndType(I.getOperand(2), InstID, Vals, VE); 01297 break; 01298 case Instruction::ShuffleVector: 01299 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 01300 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 01301 pushValue(I.getOperand(1), InstID, Vals, VE); 01302 pushValue(I.getOperand(2), InstID, Vals, VE); 01303 break; 01304 case Instruction::ICmp: 01305 case Instruction::FCmp: 01306 // compare returning Int1Ty or vector of Int1Ty 01307 Code = bitc::FUNC_CODE_INST_CMP2; 01308 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 01309 pushValue(I.getOperand(1), InstID, Vals, VE); 01310 Vals.push_back(cast<CmpInst>(I).getPredicate()); 01311 break; 01312 01313 case Instruction::Ret: 01314 { 01315 Code = bitc::FUNC_CODE_INST_RET; 01316 unsigned NumOperands = I.getNumOperands(); 01317 if (NumOperands == 0) 01318 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; 01319 else if (NumOperands == 1) { 01320 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 01321 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; 01322 } else { 01323 for (unsigned i = 0, e = NumOperands; i != e; ++i) 01324 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 01325 } 01326 } 01327 break; 01328 case Instruction::Br: 01329 { 01330 Code = bitc::FUNC_CODE_INST_BR; 01331 const BranchInst &II = cast<BranchInst>(I); 01332 Vals.push_back(VE.getValueID(II.getSuccessor(0))); 01333 if (II.isConditional()) { 01334 Vals.push_back(VE.getValueID(II.getSuccessor(1))); 01335 pushValue(II.getCondition(), InstID, Vals, VE); 01336 } 01337 } 01338 break; 01339 case Instruction::Switch: 01340 { 01341 Code = bitc::FUNC_CODE_INST_SWITCH; 01342 const SwitchInst &SI = cast<SwitchInst>(I); 01343 Vals.push_back(VE.getTypeID(SI.getCondition()->getType())); 01344 pushValue(SI.getCondition(), InstID, Vals, VE); 01345 Vals.push_back(VE.getValueID(SI.getDefaultDest())); 01346 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end(); 01347 i != e; ++i) { 01348 Vals.push_back(VE.getValueID(i.getCaseValue())); 01349 Vals.push_back(VE.getValueID(i.getCaseSuccessor())); 01350 } 01351 } 01352 break; 01353 case Instruction::IndirectBr: 01354 Code = bitc::FUNC_CODE_INST_INDIRECTBR; 01355 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 01356 // Encode the address operand as relative, but not the basic blocks. 01357 pushValue(I.getOperand(0), InstID, Vals, VE); 01358 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) 01359 Vals.push_back(VE.getValueID(I.getOperand(i))); 01360 break; 01361 01362 case Instruction::Invoke: { 01363 const InvokeInst *II = cast<InvokeInst>(&I); 01364 const Value *Callee(II->getCalledValue()); 01365 PointerType *PTy = cast<PointerType>(Callee->getType()); 01366 FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 01367 Code = bitc::FUNC_CODE_INST_INVOKE; 01368 01369 Vals.push_back(VE.getAttributeID(II->getAttributes())); 01370 Vals.push_back(II->getCallingConv()); 01371 Vals.push_back(VE.getValueID(II->getNormalDest())); 01372 Vals.push_back(VE.getValueID(II->getUnwindDest())); 01373 PushValueAndType(Callee, InstID, Vals, VE); 01374 01375 // Emit value #'s for the fixed parameters. 01376 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 01377 pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param. 01378 01379 // Emit type/value pairs for varargs params. 01380 if (FTy->isVarArg()) { 01381 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3; 01382 i != e; ++i) 01383 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg 01384 } 01385 break; 01386 } 01387 case Instruction::Resume: 01388 Code = bitc::FUNC_CODE_INST_RESUME; 01389 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 01390 break; 01391 case Instruction::Unreachable: 01392 Code = bitc::FUNC_CODE_INST_UNREACHABLE; 01393 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; 01394 break; 01395 01396 case Instruction::PHI: { 01397 const PHINode &PN = cast<PHINode>(I); 01398 Code = bitc::FUNC_CODE_INST_PHI; 01399 // With the newer instruction encoding, forward references could give 01400 // negative valued IDs. This is most common for PHIs, so we use 01401 // signed VBRs. 01402 SmallVector<uint64_t, 128> Vals64; 01403 Vals64.push_back(VE.getTypeID(PN.getType())); 01404 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 01405 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE); 01406 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i))); 01407 } 01408 // Emit a Vals64 vector and exit. 01409 Stream.EmitRecord(Code, Vals64, AbbrevToUse); 01410 Vals64.clear(); 01411 return; 01412 } 01413 01414 case Instruction::LandingPad: { 01415 const LandingPadInst &LP = cast<LandingPadInst>(I); 01416 Code = bitc::FUNC_CODE_INST_LANDINGPAD; 01417 Vals.push_back(VE.getTypeID(LP.getType())); 01418 PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE); 01419 Vals.push_back(LP.isCleanup()); 01420 Vals.push_back(LP.getNumClauses()); 01421 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) { 01422 if (LP.isCatch(I)) 01423 Vals.push_back(LandingPadInst::Catch); 01424 else 01425 Vals.push_back(LandingPadInst::Filter); 01426 PushValueAndType(LP.getClause(I), InstID, Vals, VE); 01427 } 01428 break; 01429 } 01430 01431 case Instruction::Alloca: { 01432 Code = bitc::FUNC_CODE_INST_ALLOCA; 01433 Vals.push_back(VE.getTypeID(I.getType())); 01434 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 01435 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 01436 const AllocaInst &AI = cast<AllocaInst>(I); 01437 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1; 01438 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 && 01439 "not enough bits for maximum alignment"); 01440 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64"); 01441 AlignRecord |= AI.isUsedWithInAlloca() << 5; 01442 Vals.push_back(AlignRecord); 01443 break; 01444 } 01445 01446 case Instruction::Load: 01447 if (cast<LoadInst>(I).isAtomic()) { 01448 Code = bitc::FUNC_CODE_INST_LOADATOMIC; 01449 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 01450 } else { 01451 Code = bitc::FUNC_CODE_INST_LOAD; 01452 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr 01453 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 01454 } 01455 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); 01456 Vals.push_back(cast<LoadInst>(I).isVolatile()); 01457 if (cast<LoadInst>(I).isAtomic()) { 01458 Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering())); 01459 Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope())); 01460 } 01461 break; 01462 case Instruction::Store: 01463 if (cast<StoreInst>(I).isAtomic()) 01464 Code = bitc::FUNC_CODE_INST_STOREATOMIC; 01465 else 01466 Code = bitc::FUNC_CODE_INST_STORE; 01467 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr 01468 pushValue(I.getOperand(0), InstID, Vals, VE); // val. 01469 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); 01470 Vals.push_back(cast<StoreInst>(I).isVolatile()); 01471 if (cast<StoreInst>(I).isAtomic()) { 01472 Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering())); 01473 Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope())); 01474 } 01475 break; 01476 case Instruction::AtomicCmpXchg: 01477 Code = bitc::FUNC_CODE_INST_CMPXCHG; 01478 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr 01479 pushValue(I.getOperand(1), InstID, Vals, VE); // cmp. 01480 pushValue(I.getOperand(2), InstID, Vals, VE); // newval. 01481 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile()); 01482 Vals.push_back(GetEncodedOrdering( 01483 cast<AtomicCmpXchgInst>(I).getSuccessOrdering())); 01484 Vals.push_back(GetEncodedSynchScope( 01485 cast<AtomicCmpXchgInst>(I).getSynchScope())); 01486 Vals.push_back(GetEncodedOrdering( 01487 cast<AtomicCmpXchgInst>(I).getFailureOrdering())); 01488 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak()); 01489 break; 01490 case Instruction::AtomicRMW: 01491 Code = bitc::FUNC_CODE_INST_ATOMICRMW; 01492 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr 01493 pushValue(I.getOperand(1), InstID, Vals, VE); // val. 01494 Vals.push_back(GetEncodedRMWOperation( 01495 cast<AtomicRMWInst>(I).getOperation())); 01496 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile()); 01497 Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering())); 01498 Vals.push_back(GetEncodedSynchScope( 01499 cast<AtomicRMWInst>(I).getSynchScope())); 01500 break; 01501 case Instruction::Fence: 01502 Code = bitc::FUNC_CODE_INST_FENCE; 01503 Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering())); 01504 Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope())); 01505 break; 01506 case Instruction::Call: { 01507 const CallInst &CI = cast<CallInst>(I); 01508 PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType()); 01509 FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 01510 01511 Code = bitc::FUNC_CODE_INST_CALL; 01512 01513 Vals.push_back(VE.getAttributeID(CI.getAttributes())); 01514 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()) | 01515 unsigned(CI.isMustTailCall()) << 14); 01516 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee 01517 01518 // Emit value #'s for the fixed parameters. 01519 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) { 01520 // Check for labels (can happen with asm labels). 01521 if (FTy->getParamType(i)->isLabelTy()) 01522 Vals.push_back(VE.getValueID(CI.getArgOperand(i))); 01523 else 01524 pushValue(CI.getArgOperand(i), InstID, Vals, VE); // fixed param. 01525 } 01526 01527 // Emit type/value pairs for varargs params. 01528 if (FTy->isVarArg()) { 01529 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands(); 01530 i != e; ++i) 01531 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs 01532 } 01533 break; 01534 } 01535 case Instruction::VAArg: 01536 Code = bitc::FUNC_CODE_INST_VAARG; 01537 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 01538 pushValue(I.getOperand(0), InstID, Vals, VE); // valist. 01539 Vals.push_back(VE.getTypeID(I.getType())); // restype. 01540 break; 01541 } 01542 01543 Stream.EmitRecord(Code, Vals, AbbrevToUse); 01544 Vals.clear(); 01545 } 01546 01547 // Emit names for globals/functions etc. 01548 static void WriteValueSymbolTable(const ValueSymbolTable &VST, 01549 const ValueEnumerator &VE, 01550 BitstreamWriter &Stream) { 01551 if (VST.empty()) return; 01552 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 01553 01554 // FIXME: Set up the abbrev, we know how many values there are! 01555 // FIXME: We know if the type names can use 7-bit ascii. 01556 SmallVector<unsigned, 64> NameVals; 01557 01558 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 01559 SI != SE; ++SI) { 01560 01561 const ValueName &Name = *SI; 01562 01563 // Figure out the encoding to use for the name. 01564 bool is7Bit = true; 01565 bool isChar6 = true; 01566 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); 01567 C != E; ++C) { 01568 if (isChar6) 01569 isChar6 = BitCodeAbbrevOp::isChar6(*C); 01570 if ((unsigned char)*C & 128) { 01571 is7Bit = false; 01572 break; // don't bother scanning the rest. 01573 } 01574 } 01575 01576 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 01577 01578 // VST_ENTRY: [valueid, namechar x N] 01579 // VST_BBENTRY: [bbid, namechar x N] 01580 unsigned Code; 01581 if (isa<BasicBlock>(SI->getValue())) { 01582 Code = bitc::VST_CODE_BBENTRY; 01583 if (isChar6) 01584 AbbrevToUse = VST_BBENTRY_6_ABBREV; 01585 } else { 01586 Code = bitc::VST_CODE_ENTRY; 01587 if (isChar6) 01588 AbbrevToUse = VST_ENTRY_6_ABBREV; 01589 else if (is7Bit) 01590 AbbrevToUse = VST_ENTRY_7_ABBREV; 01591 } 01592 01593 NameVals.push_back(VE.getValueID(SI->getValue())); 01594 for (const char *P = Name.getKeyData(), 01595 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) 01596 NameVals.push_back((unsigned char)*P); 01597 01598 // Emit the finished record. 01599 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 01600 NameVals.clear(); 01601 } 01602 Stream.ExitBlock(); 01603 } 01604 01605 static void WriteUseList(ValueEnumerator &VE, UseListOrder &&Order, 01606 BitstreamWriter &Stream) { 01607 assert(Order.Shuffle.size() >= 2 && "Shuffle too small"); 01608 unsigned Code; 01609 if (isa<BasicBlock>(Order.V)) 01610 Code = bitc::USELIST_CODE_BB; 01611 else 01612 Code = bitc::USELIST_CODE_DEFAULT; 01613 01614 SmallVector<uint64_t, 64> Record; 01615 for (unsigned I : Order.Shuffle) 01616 Record.push_back(I); 01617 Record.push_back(VE.getValueID(Order.V)); 01618 Stream.EmitRecord(Code, Record); 01619 } 01620 01621 static void WriteUseListBlock(const Function *F, ValueEnumerator &VE, 01622 BitstreamWriter &Stream) { 01623 auto hasMore = [&]() { 01624 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F; 01625 }; 01626 if (!hasMore()) 01627 // Nothing to do. 01628 return; 01629 01630 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3); 01631 while (hasMore()) { 01632 WriteUseList(VE, std::move(VE.UseListOrders.back()), Stream); 01633 VE.UseListOrders.pop_back(); 01634 } 01635 Stream.ExitBlock(); 01636 } 01637 01638 /// WriteFunction - Emit a function body to the module stream. 01639 static void WriteFunction(const Function &F, ValueEnumerator &VE, 01640 BitstreamWriter &Stream) { 01641 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4); 01642 VE.incorporateFunction(F); 01643 01644 SmallVector<unsigned, 64> Vals; 01645 01646 // Emit the number of basic blocks, so the reader can create them ahead of 01647 // time. 01648 Vals.push_back(VE.getBasicBlocks().size()); 01649 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 01650 Vals.clear(); 01651 01652 // If there are function-local constants, emit them now. 01653 unsigned CstStart, CstEnd; 01654 VE.getFunctionConstantRange(CstStart, CstEnd); 01655 WriteConstants(CstStart, CstEnd, VE, Stream, false); 01656 01657 // If there is function-local metadata, emit it now. 01658 WriteFunctionLocalMetadata(F, VE, Stream); 01659 01660 // Keep a running idea of what the instruction ID is. 01661 unsigned InstID = CstEnd; 01662 01663 bool NeedsMetadataAttachment = false; 01664 01665 DebugLoc LastDL; 01666 01667 // Finally, emit all the instructions, in order. 01668 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 01669 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 01670 I != E; ++I) { 01671 WriteInstruction(*I, InstID, VE, Stream, Vals); 01672 01673 if (!I->getType()->isVoidTy()) 01674 ++InstID; 01675 01676 // If the instruction has metadata, write a metadata attachment later. 01677 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc(); 01678 01679 // If the instruction has a debug location, emit it. 01680 DebugLoc DL = I->getDebugLoc(); 01681 if (DL.isUnknown()) { 01682 // nothing todo. 01683 } else if (DL == LastDL) { 01684 // Just repeat the same debug loc as last time. 01685 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals); 01686 } else { 01687 MDNode *Scope, *IA; 01688 DL.getScopeAndInlinedAt(Scope, IA, I->getContext()); 01689 01690 Vals.push_back(DL.getLine()); 01691 Vals.push_back(DL.getCol()); 01692 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0); 01693 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0); 01694 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals); 01695 Vals.clear(); 01696 01697 LastDL = DL; 01698 } 01699 } 01700 01701 // Emit names for all the instructions etc. 01702 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); 01703 01704 if (NeedsMetadataAttachment) 01705 WriteMetadataAttachment(F, VE, Stream); 01706 if (shouldPreserveBitcodeUseListOrder()) 01707 WriteUseListBlock(&F, VE, Stream); 01708 VE.purgeFunction(); 01709 Stream.ExitBlock(); 01710 } 01711 01712 // Emit blockinfo, which defines the standard abbreviations etc. 01713 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { 01714 // We only want to emit block info records for blocks that have multiple 01715 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. 01716 // Other blocks can define their abbrevs inline. 01717 Stream.EnterBlockInfoBlock(2); 01718 01719 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 01720 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01721 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 01722 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 01723 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 01724 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 01725 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 01726 Abbv) != VST_ENTRY_8_ABBREV) 01727 llvm_unreachable("Unexpected abbrev ordering!"); 01728 } 01729 01730 { // 7-bit fixed width VST_ENTRY strings. 01731 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01732 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 01733 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 01734 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 01735 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 01736 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 01737 Abbv) != VST_ENTRY_7_ABBREV) 01738 llvm_unreachable("Unexpected abbrev ordering!"); 01739 } 01740 { // 6-bit char6 VST_ENTRY strings. 01741 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01742 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 01743 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 01744 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 01745 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 01746 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 01747 Abbv) != VST_ENTRY_6_ABBREV) 01748 llvm_unreachable("Unexpected abbrev ordering!"); 01749 } 01750 { // 6-bit char6 VST_BBENTRY strings. 01751 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01752 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 01753 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 01754 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 01755 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 01756 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 01757 Abbv) != VST_BBENTRY_6_ABBREV) 01758 llvm_unreachable("Unexpected abbrev ordering!"); 01759 } 01760 01761 01762 01763 { // SETTYPE abbrev for CONSTANTS_BLOCK. 01764 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01765 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 01766 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 01767 Log2_32_Ceil(VE.getTypes().size()+1))); 01768 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 01769 Abbv) != CONSTANTS_SETTYPE_ABBREV) 01770 llvm_unreachable("Unexpected abbrev ordering!"); 01771 } 01772 01773 { // INTEGER abbrev for CONSTANTS_BLOCK. 01774 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01775 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 01776 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 01777 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 01778 Abbv) != CONSTANTS_INTEGER_ABBREV) 01779 llvm_unreachable("Unexpected abbrev ordering!"); 01780 } 01781 01782 { // CE_CAST abbrev for CONSTANTS_BLOCK. 01783 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01784 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 01785 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 01786 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 01787 Log2_32_Ceil(VE.getTypes().size()+1))); 01788 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 01789 01790 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 01791 Abbv) != CONSTANTS_CE_CAST_Abbrev) 01792 llvm_unreachable("Unexpected abbrev ordering!"); 01793 } 01794 { // NULL abbrev for CONSTANTS_BLOCK. 01795 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01796 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 01797 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 01798 Abbv) != CONSTANTS_NULL_Abbrev) 01799 llvm_unreachable("Unexpected abbrev ordering!"); 01800 } 01801 01802 // FIXME: This should only use space for first class types! 01803 01804 { // INST_LOAD abbrev for FUNCTION_BLOCK. 01805 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01806 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 01807 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr 01808 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 01809 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 01810 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 01811 Abbv) != FUNCTION_INST_LOAD_ABBREV) 01812 llvm_unreachable("Unexpected abbrev ordering!"); 01813 } 01814 { // INST_BINOP abbrev for FUNCTION_BLOCK. 01815 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01816 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 01817 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 01818 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 01819 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 01820 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 01821 Abbv) != FUNCTION_INST_BINOP_ABBREV) 01822 llvm_unreachable("Unexpected abbrev ordering!"); 01823 } 01824 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK. 01825 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01826 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 01827 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 01828 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 01829 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 01830 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags 01831 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 01832 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) 01833 llvm_unreachable("Unexpected abbrev ordering!"); 01834 } 01835 { // INST_CAST abbrev for FUNCTION_BLOCK. 01836 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01837 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); 01838 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal 01839 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty 01840 Log2_32_Ceil(VE.getTypes().size()+1))); 01841 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 01842 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 01843 Abbv) != FUNCTION_INST_CAST_ABBREV) 01844 llvm_unreachable("Unexpected abbrev ordering!"); 01845 } 01846 01847 { // INST_RET abbrev for FUNCTION_BLOCK. 01848 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01849 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 01850 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 01851 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) 01852 llvm_unreachable("Unexpected abbrev ordering!"); 01853 } 01854 { // INST_RET abbrev for FUNCTION_BLOCK. 01855 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01856 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 01857 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID 01858 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 01859 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) 01860 llvm_unreachable("Unexpected abbrev ordering!"); 01861 } 01862 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 01863 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 01864 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); 01865 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 01866 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) 01867 llvm_unreachable("Unexpected abbrev ordering!"); 01868 } 01869 01870 Stream.ExitBlock(); 01871 } 01872 01873 /// WriteModule - Emit the specified module to the bitstream. 01874 static void WriteModule(const Module *M, BitstreamWriter &Stream) { 01875 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 01876 01877 SmallVector<unsigned, 1> Vals; 01878 unsigned CurVersion = 1; 01879 Vals.push_back(CurVersion); 01880 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 01881 01882 // Analyze the module, enumerating globals, functions, etc. 01883 ValueEnumerator VE(M); 01884 01885 // Emit blockinfo, which defines the standard abbreviations etc. 01886 WriteBlockInfo(VE, Stream); 01887 01888 // Emit information about attribute groups. 01889 WriteAttributeGroupTable(VE, Stream); 01890 01891 // Emit information about parameter attributes. 01892 WriteAttributeTable(VE, Stream); 01893 01894 // Emit information describing all of the types in the module. 01895 WriteTypeTable(VE, Stream); 01896 01897 writeComdats(VE, Stream); 01898 01899 // Emit top-level description of module, including target triple, inline asm, 01900 // descriptors for global variables, and function prototype info. 01901 WriteModuleInfo(M, VE, Stream); 01902 01903 // Emit constants. 01904 WriteModuleConstants(VE, Stream); 01905 01906 // Emit metadata. 01907 WriteModuleMetadata(M, VE, Stream); 01908 01909 // Emit metadata. 01910 WriteModuleMetadataStore(M, Stream); 01911 01912 // Emit names for globals/functions etc. 01913 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); 01914 01915 // Emit module-level use-lists. 01916 if (shouldPreserveBitcodeUseListOrder()) 01917 WriteUseListBlock(nullptr, VE, Stream); 01918 01919 // Emit function bodies. 01920 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) 01921 if (!F->isDeclaration()) 01922 WriteFunction(*F, VE, Stream); 01923 01924 Stream.ExitBlock(); 01925 } 01926 01927 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a 01928 /// header and trailer to make it compatible with the system archiver. To do 01929 /// this we emit the following header, and then emit a trailer that pads the 01930 /// file out to be a multiple of 16 bytes. 01931 /// 01932 /// struct bc_header { 01933 /// uint32_t Magic; // 0x0B17C0DE 01934 /// uint32_t Version; // Version, currently always 0. 01935 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 01936 /// uint32_t BitcodeSize; // Size of traditional bitcode file. 01937 /// uint32_t CPUType; // CPU specifier. 01938 /// ... potentially more later ... 01939 /// }; 01940 enum { 01941 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size. 01942 DarwinBCHeaderSize = 5*4 01943 }; 01944 01945 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer, 01946 uint32_t &Position) { 01947 Buffer[Position + 0] = (unsigned char) (Value >> 0); 01948 Buffer[Position + 1] = (unsigned char) (Value >> 8); 01949 Buffer[Position + 2] = (unsigned char) (Value >> 16); 01950 Buffer[Position + 3] = (unsigned char) (Value >> 24); 01951 Position += 4; 01952 } 01953 01954 static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer, 01955 const Triple &TT) { 01956 unsigned CPUType = ~0U; 01957 01958 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*, 01959 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic 01960 // number from /usr/include/mach/machine.h. It is ok to reproduce the 01961 // specific constants here because they are implicitly part of the Darwin ABI. 01962 enum { 01963 DARWIN_CPU_ARCH_ABI64 = 0x01000000, 01964 DARWIN_CPU_TYPE_X86 = 7, 01965 DARWIN_CPU_TYPE_ARM = 12, 01966 DARWIN_CPU_TYPE_POWERPC = 18 01967 }; 01968 01969 Triple::ArchType Arch = TT.getArch(); 01970 if (Arch == Triple::x86_64) 01971 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64; 01972 else if (Arch == Triple::x86) 01973 CPUType = DARWIN_CPU_TYPE_X86; 01974 else if (Arch == Triple::ppc) 01975 CPUType = DARWIN_CPU_TYPE_POWERPC; 01976 else if (Arch == Triple::ppc64) 01977 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64; 01978 else if (Arch == Triple::arm || Arch == Triple::thumb) 01979 CPUType = DARWIN_CPU_TYPE_ARM; 01980 01981 // Traditional Bitcode starts after header. 01982 assert(Buffer.size() >= DarwinBCHeaderSize && 01983 "Expected header size to be reserved"); 01984 unsigned BCOffset = DarwinBCHeaderSize; 01985 unsigned BCSize = Buffer.size()-DarwinBCHeaderSize; 01986 01987 // Write the magic and version. 01988 unsigned Position = 0; 01989 WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position); 01990 WriteInt32ToBuffer(0 , Buffer, Position); // Version. 01991 WriteInt32ToBuffer(BCOffset , Buffer, Position); 01992 WriteInt32ToBuffer(BCSize , Buffer, Position); 01993 WriteInt32ToBuffer(CPUType , Buffer, Position); 01994 01995 // If the file is not a multiple of 16 bytes, insert dummy padding. 01996 while (Buffer.size() & 15) 01997 Buffer.push_back(0); 01998 } 01999 02000 /// WriteBitcodeToFile - Write the specified module to the specified output 02001 /// stream. 02002 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) { 02003 SmallVector<char, 0> Buffer; 02004 Buffer.reserve(256*1024); 02005 02006 // If this is darwin or another generic macho target, reserve space for the 02007 // header. 02008 Triple TT(M->getTargetTriple()); 02009 if (TT.isOSDarwin()) 02010 Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0); 02011 02012 // Emit the module into the buffer. 02013 { 02014 BitstreamWriter Stream(Buffer); 02015 02016 // Emit the file header. 02017 Stream.Emit((unsigned)'B', 8); 02018 Stream.Emit((unsigned)'C', 8); 02019 Stream.Emit(0x0, 4); 02020 Stream.Emit(0xC, 4); 02021 Stream.Emit(0xE, 4); 02022 Stream.Emit(0xD, 4); 02023 02024 // Emit the module. 02025 WriteModule(M, Stream); 02026 } 02027 02028 if (TT.isOSDarwin()) 02029 EmitDarwinBCHeaderAndTrailer(Buffer, TT); 02030 02031 // Write the generated bitstream to "Out". 02032 Out.write((char*)&Buffer.front(), Buffer.size()); 02033 }