1f22ef01cSRoman Divacky //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // This header defines the BitcodeReader class. 11f22ef01cSRoman Divacky // 12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 13f22ef01cSRoman Divacky 14f22ef01cSRoman Divacky #include "llvm/Bitcode/ReaderWriter.h" 15f22ef01cSRoman Divacky #include "BitcodeReader.h" 16f22ef01cSRoman Divacky #include "llvm/Constants.h" 17f22ef01cSRoman Divacky #include "llvm/DerivedTypes.h" 18f22ef01cSRoman Divacky #include "llvm/InlineAsm.h" 19f22ef01cSRoman Divacky #include "llvm/IntrinsicInst.h" 20f22ef01cSRoman Divacky #include "llvm/Module.h" 21f22ef01cSRoman Divacky #include "llvm/Operator.h" 22f22ef01cSRoman Divacky #include "llvm/AutoUpgrade.h" 23f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h" 24f22ef01cSRoman Divacky #include "llvm/ADT/SmallVector.h" 25f22ef01cSRoman Divacky #include "llvm/Support/MathExtras.h" 26f22ef01cSRoman Divacky #include "llvm/Support/MemoryBuffer.h" 27f22ef01cSRoman Divacky #include "llvm/OperandTraits.h" 28f22ef01cSRoman Divacky using namespace llvm; 29f22ef01cSRoman Divacky 30f22ef01cSRoman Divacky void BitcodeReader::FreeState() { 31f22ef01cSRoman Divacky if (BufferOwned) 32f22ef01cSRoman Divacky delete Buffer; 33f22ef01cSRoman Divacky Buffer = 0; 34f22ef01cSRoman Divacky std::vector<PATypeHolder>().swap(TypeList); 35f22ef01cSRoman Divacky ValueList.clear(); 36f22ef01cSRoman Divacky MDValueList.clear(); 37f22ef01cSRoman Divacky 38f22ef01cSRoman Divacky std::vector<AttrListPtr>().swap(MAttributes); 39f22ef01cSRoman Divacky std::vector<BasicBlock*>().swap(FunctionBBs); 40f22ef01cSRoman Divacky std::vector<Function*>().swap(FunctionsWithBodies); 41f22ef01cSRoman Divacky DeferredFunctionInfo.clear(); 42e580952dSDimitry Andric MDKindMap.clear(); 43f22ef01cSRoman Divacky } 44f22ef01cSRoman Divacky 45f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 46f22ef01cSRoman Divacky // Helper functions to implement forward reference resolution, etc. 47f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 48f22ef01cSRoman Divacky 49f22ef01cSRoman Divacky /// ConvertToString - Convert a string from a record into an std::string, return 50f22ef01cSRoman Divacky /// true on failure. 51f22ef01cSRoman Divacky template<typename StrTy> 52f22ef01cSRoman Divacky static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx, 53f22ef01cSRoman Divacky StrTy &Result) { 54f22ef01cSRoman Divacky if (Idx > Record.size()) 55f22ef01cSRoman Divacky return true; 56f22ef01cSRoman Divacky 57f22ef01cSRoman Divacky for (unsigned i = Idx, e = Record.size(); i != e; ++i) 58f22ef01cSRoman Divacky Result += (char)Record[i]; 59f22ef01cSRoman Divacky return false; 60f22ef01cSRoman Divacky } 61f22ef01cSRoman Divacky 62f22ef01cSRoman Divacky static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { 63f22ef01cSRoman Divacky switch (Val) { 64f22ef01cSRoman Divacky default: // Map unknown/new linkages to external 65f22ef01cSRoman Divacky case 0: return GlobalValue::ExternalLinkage; 66f22ef01cSRoman Divacky case 1: return GlobalValue::WeakAnyLinkage; 67f22ef01cSRoman Divacky case 2: return GlobalValue::AppendingLinkage; 68f22ef01cSRoman Divacky case 3: return GlobalValue::InternalLinkage; 69f22ef01cSRoman Divacky case 4: return GlobalValue::LinkOnceAnyLinkage; 70f22ef01cSRoman Divacky case 5: return GlobalValue::DLLImportLinkage; 71f22ef01cSRoman Divacky case 6: return GlobalValue::DLLExportLinkage; 72f22ef01cSRoman Divacky case 7: return GlobalValue::ExternalWeakLinkage; 73f22ef01cSRoman Divacky case 8: return GlobalValue::CommonLinkage; 74f22ef01cSRoman Divacky case 9: return GlobalValue::PrivateLinkage; 75f22ef01cSRoman Divacky case 10: return GlobalValue::WeakODRLinkage; 76f22ef01cSRoman Divacky case 11: return GlobalValue::LinkOnceODRLinkage; 77f22ef01cSRoman Divacky case 12: return GlobalValue::AvailableExternallyLinkage; 78f22ef01cSRoman Divacky case 13: return GlobalValue::LinkerPrivateLinkage; 79ffd1746dSEd Schouten case 14: return GlobalValue::LinkerPrivateWeakLinkage; 80e580952dSDimitry Andric case 15: return GlobalValue::LinkerPrivateWeakDefAutoLinkage; 81f22ef01cSRoman Divacky } 82f22ef01cSRoman Divacky } 83f22ef01cSRoman Divacky 84f22ef01cSRoman Divacky static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { 85f22ef01cSRoman Divacky switch (Val) { 86f22ef01cSRoman Divacky default: // Map unknown visibilities to default. 87f22ef01cSRoman Divacky case 0: return GlobalValue::DefaultVisibility; 88f22ef01cSRoman Divacky case 1: return GlobalValue::HiddenVisibility; 89f22ef01cSRoman Divacky case 2: return GlobalValue::ProtectedVisibility; 90f22ef01cSRoman Divacky } 91f22ef01cSRoman Divacky } 92f22ef01cSRoman Divacky 93f22ef01cSRoman Divacky static int GetDecodedCastOpcode(unsigned Val) { 94f22ef01cSRoman Divacky switch (Val) { 95f22ef01cSRoman Divacky default: return -1; 96f22ef01cSRoman Divacky case bitc::CAST_TRUNC : return Instruction::Trunc; 97f22ef01cSRoman Divacky case bitc::CAST_ZEXT : return Instruction::ZExt; 98f22ef01cSRoman Divacky case bitc::CAST_SEXT : return Instruction::SExt; 99f22ef01cSRoman Divacky case bitc::CAST_FPTOUI : return Instruction::FPToUI; 100f22ef01cSRoman Divacky case bitc::CAST_FPTOSI : return Instruction::FPToSI; 101f22ef01cSRoman Divacky case bitc::CAST_UITOFP : return Instruction::UIToFP; 102f22ef01cSRoman Divacky case bitc::CAST_SITOFP : return Instruction::SIToFP; 103f22ef01cSRoman Divacky case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; 104f22ef01cSRoman Divacky case bitc::CAST_FPEXT : return Instruction::FPExt; 105f22ef01cSRoman Divacky case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; 106f22ef01cSRoman Divacky case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; 107f22ef01cSRoman Divacky case bitc::CAST_BITCAST : return Instruction::BitCast; 108f22ef01cSRoman Divacky } 109f22ef01cSRoman Divacky } 110f22ef01cSRoman Divacky static int GetDecodedBinaryOpcode(unsigned Val, const Type *Ty) { 111f22ef01cSRoman Divacky switch (Val) { 112f22ef01cSRoman Divacky default: return -1; 113f22ef01cSRoman Divacky case bitc::BINOP_ADD: 114f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add; 115f22ef01cSRoman Divacky case bitc::BINOP_SUB: 116f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub; 117f22ef01cSRoman Divacky case bitc::BINOP_MUL: 118f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul; 119f22ef01cSRoman Divacky case bitc::BINOP_UDIV: return Instruction::UDiv; 120f22ef01cSRoman Divacky case bitc::BINOP_SDIV: 121f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv; 122f22ef01cSRoman Divacky case bitc::BINOP_UREM: return Instruction::URem; 123f22ef01cSRoman Divacky case bitc::BINOP_SREM: 124f22ef01cSRoman Divacky return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem; 125f22ef01cSRoman Divacky case bitc::BINOP_SHL: return Instruction::Shl; 126f22ef01cSRoman Divacky case bitc::BINOP_LSHR: return Instruction::LShr; 127f22ef01cSRoman Divacky case bitc::BINOP_ASHR: return Instruction::AShr; 128f22ef01cSRoman Divacky case bitc::BINOP_AND: return Instruction::And; 129f22ef01cSRoman Divacky case bitc::BINOP_OR: return Instruction::Or; 130f22ef01cSRoman Divacky case bitc::BINOP_XOR: return Instruction::Xor; 131f22ef01cSRoman Divacky } 132f22ef01cSRoman Divacky } 133f22ef01cSRoman Divacky 134f22ef01cSRoman Divacky namespace llvm { 135f22ef01cSRoman Divacky namespace { 136f22ef01cSRoman Divacky /// @brief A class for maintaining the slot number definition 137f22ef01cSRoman Divacky /// as a placeholder for the actual definition for forward constants defs. 138f22ef01cSRoman Divacky class ConstantPlaceHolder : public ConstantExpr { 139f22ef01cSRoman Divacky void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT 140f22ef01cSRoman Divacky public: 141f22ef01cSRoman Divacky // allocate space for exactly one operand 142f22ef01cSRoman Divacky void *operator new(size_t s) { 143f22ef01cSRoman Divacky return User::operator new(s, 1); 144f22ef01cSRoman Divacky } 145f22ef01cSRoman Divacky explicit ConstantPlaceHolder(const Type *Ty, LLVMContext& Context) 146f22ef01cSRoman Divacky : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) { 147f22ef01cSRoman Divacky Op<0>() = UndefValue::get(Type::getInt32Ty(Context)); 148f22ef01cSRoman Divacky } 149f22ef01cSRoman Divacky 150f22ef01cSRoman Divacky /// @brief Methods to support type inquiry through isa, cast, and dyn_cast. 1512754fe60SDimitry Andric //static inline bool classof(const ConstantPlaceHolder *) { return true; } 152f22ef01cSRoman Divacky static bool classof(const Value *V) { 153f22ef01cSRoman Divacky return isa<ConstantExpr>(V) && 154f22ef01cSRoman Divacky cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1; 155f22ef01cSRoman Divacky } 156f22ef01cSRoman Divacky 157f22ef01cSRoman Divacky 158f22ef01cSRoman Divacky /// Provide fast operand accessors 159f22ef01cSRoman Divacky //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 160f22ef01cSRoman Divacky }; 161f22ef01cSRoman Divacky } 162f22ef01cSRoman Divacky 163f22ef01cSRoman Divacky // FIXME: can we inherit this from ConstantExpr? 164f22ef01cSRoman Divacky template <> 1652754fe60SDimitry Andric struct OperandTraits<ConstantPlaceHolder> : 1662754fe60SDimitry Andric public FixedNumOperandTraits<ConstantPlaceHolder, 1> { 167f22ef01cSRoman Divacky }; 168f22ef01cSRoman Divacky } 169f22ef01cSRoman Divacky 170f22ef01cSRoman Divacky 171f22ef01cSRoman Divacky void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) { 172f22ef01cSRoman Divacky if (Idx == size()) { 173f22ef01cSRoman Divacky push_back(V); 174f22ef01cSRoman Divacky return; 175f22ef01cSRoman Divacky } 176f22ef01cSRoman Divacky 177f22ef01cSRoman Divacky if (Idx >= size()) 178f22ef01cSRoman Divacky resize(Idx+1); 179f22ef01cSRoman Divacky 180f22ef01cSRoman Divacky WeakVH &OldV = ValuePtrs[Idx]; 181f22ef01cSRoman Divacky if (OldV == 0) { 182f22ef01cSRoman Divacky OldV = V; 183f22ef01cSRoman Divacky return; 184f22ef01cSRoman Divacky } 185f22ef01cSRoman Divacky 186f22ef01cSRoman Divacky // Handle constants and non-constants (e.g. instrs) differently for 187f22ef01cSRoman Divacky // efficiency. 188f22ef01cSRoman Divacky if (Constant *PHC = dyn_cast<Constant>(&*OldV)) { 189f22ef01cSRoman Divacky ResolveConstants.push_back(std::make_pair(PHC, Idx)); 190f22ef01cSRoman Divacky OldV = V; 191f22ef01cSRoman Divacky } else { 192f22ef01cSRoman Divacky // If there was a forward reference to this value, replace it. 193f22ef01cSRoman Divacky Value *PrevVal = OldV; 194f22ef01cSRoman Divacky OldV->replaceAllUsesWith(V); 195f22ef01cSRoman Divacky delete PrevVal; 196f22ef01cSRoman Divacky } 197f22ef01cSRoman Divacky } 198f22ef01cSRoman Divacky 199f22ef01cSRoman Divacky 200f22ef01cSRoman Divacky Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, 201f22ef01cSRoman Divacky const Type *Ty) { 202f22ef01cSRoman Divacky if (Idx >= size()) 203f22ef01cSRoman Divacky resize(Idx + 1); 204f22ef01cSRoman Divacky 205f22ef01cSRoman Divacky if (Value *V = ValuePtrs[Idx]) { 206f22ef01cSRoman Divacky assert(Ty == V->getType() && "Type mismatch in constant table!"); 207f22ef01cSRoman Divacky return cast<Constant>(V); 208f22ef01cSRoman Divacky } 209f22ef01cSRoman Divacky 210f22ef01cSRoman Divacky // Create and return a placeholder, which will later be RAUW'd. 211f22ef01cSRoman Divacky Constant *C = new ConstantPlaceHolder(Ty, Context); 212f22ef01cSRoman Divacky ValuePtrs[Idx] = C; 213f22ef01cSRoman Divacky return C; 214f22ef01cSRoman Divacky } 215f22ef01cSRoman Divacky 216f22ef01cSRoman Divacky Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) { 217f22ef01cSRoman Divacky if (Idx >= size()) 218f22ef01cSRoman Divacky resize(Idx + 1); 219f22ef01cSRoman Divacky 220f22ef01cSRoman Divacky if (Value *V = ValuePtrs[Idx]) { 221f22ef01cSRoman Divacky assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!"); 222f22ef01cSRoman Divacky return V; 223f22ef01cSRoman Divacky } 224f22ef01cSRoman Divacky 225f22ef01cSRoman Divacky // No type specified, must be invalid reference. 226f22ef01cSRoman Divacky if (Ty == 0) return 0; 227f22ef01cSRoman Divacky 228f22ef01cSRoman Divacky // Create and return a placeholder, which will later be RAUW'd. 229f22ef01cSRoman Divacky Value *V = new Argument(Ty); 230f22ef01cSRoman Divacky ValuePtrs[Idx] = V; 231f22ef01cSRoman Divacky return V; 232f22ef01cSRoman Divacky } 233f22ef01cSRoman Divacky 234f22ef01cSRoman Divacky /// ResolveConstantForwardRefs - Once all constants are read, this method bulk 235f22ef01cSRoman Divacky /// resolves any forward references. The idea behind this is that we sometimes 236f22ef01cSRoman Divacky /// get constants (such as large arrays) which reference *many* forward ref 237f22ef01cSRoman Divacky /// constants. Replacing each of these causes a lot of thrashing when 238f22ef01cSRoman Divacky /// building/reuniquing the constant. Instead of doing this, we look at all the 239f22ef01cSRoman Divacky /// uses and rewrite all the place holders at once for any constant that uses 240f22ef01cSRoman Divacky /// a placeholder. 241f22ef01cSRoman Divacky void BitcodeReaderValueList::ResolveConstantForwardRefs() { 242f22ef01cSRoman Divacky // Sort the values by-pointer so that they are efficient to look up with a 243f22ef01cSRoman Divacky // binary search. 244f22ef01cSRoman Divacky std::sort(ResolveConstants.begin(), ResolveConstants.end()); 245f22ef01cSRoman Divacky 246f22ef01cSRoman Divacky SmallVector<Constant*, 64> NewOps; 247f22ef01cSRoman Divacky 248f22ef01cSRoman Divacky while (!ResolveConstants.empty()) { 249f22ef01cSRoman Divacky Value *RealVal = operator[](ResolveConstants.back().second); 250f22ef01cSRoman Divacky Constant *Placeholder = ResolveConstants.back().first; 251f22ef01cSRoman Divacky ResolveConstants.pop_back(); 252f22ef01cSRoman Divacky 253f22ef01cSRoman Divacky // Loop over all users of the placeholder, updating them to reference the 254f22ef01cSRoman Divacky // new value. If they reference more than one placeholder, update them all 255f22ef01cSRoman Divacky // at once. 256f22ef01cSRoman Divacky while (!Placeholder->use_empty()) { 257f22ef01cSRoman Divacky Value::use_iterator UI = Placeholder->use_begin(); 258ffd1746dSEd Schouten User *U = *UI; 259f22ef01cSRoman Divacky 260f22ef01cSRoman Divacky // If the using object isn't uniqued, just update the operands. This 261f22ef01cSRoman Divacky // handles instructions and initializers for global variables. 262ffd1746dSEd Schouten if (!isa<Constant>(U) || isa<GlobalValue>(U)) { 263f22ef01cSRoman Divacky UI.getUse().set(RealVal); 264f22ef01cSRoman Divacky continue; 265f22ef01cSRoman Divacky } 266f22ef01cSRoman Divacky 267f22ef01cSRoman Divacky // Otherwise, we have a constant that uses the placeholder. Replace that 268f22ef01cSRoman Divacky // constant with a new constant that has *all* placeholder uses updated. 269ffd1746dSEd Schouten Constant *UserC = cast<Constant>(U); 270f22ef01cSRoman Divacky for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end(); 271f22ef01cSRoman Divacky I != E; ++I) { 272f22ef01cSRoman Divacky Value *NewOp; 273f22ef01cSRoman Divacky if (!isa<ConstantPlaceHolder>(*I)) { 274f22ef01cSRoman Divacky // Not a placeholder reference. 275f22ef01cSRoman Divacky NewOp = *I; 276f22ef01cSRoman Divacky } else if (*I == Placeholder) { 277f22ef01cSRoman Divacky // Common case is that it just references this one placeholder. 278f22ef01cSRoman Divacky NewOp = RealVal; 279f22ef01cSRoman Divacky } else { 280f22ef01cSRoman Divacky // Otherwise, look up the placeholder in ResolveConstants. 281f22ef01cSRoman Divacky ResolveConstantsTy::iterator It = 282f22ef01cSRoman Divacky std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(), 283f22ef01cSRoman Divacky std::pair<Constant*, unsigned>(cast<Constant>(*I), 284f22ef01cSRoman Divacky 0)); 285f22ef01cSRoman Divacky assert(It != ResolveConstants.end() && It->first == *I); 286f22ef01cSRoman Divacky NewOp = operator[](It->second); 287f22ef01cSRoman Divacky } 288f22ef01cSRoman Divacky 289f22ef01cSRoman Divacky NewOps.push_back(cast<Constant>(NewOp)); 290f22ef01cSRoman Divacky } 291f22ef01cSRoman Divacky 292f22ef01cSRoman Divacky // Make the new constant. 293f22ef01cSRoman Divacky Constant *NewC; 294f22ef01cSRoman Divacky if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) { 295f22ef01cSRoman Divacky NewC = ConstantArray::get(UserCA->getType(), &NewOps[0], 296f22ef01cSRoman Divacky NewOps.size()); 297f22ef01cSRoman Divacky } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) { 298f22ef01cSRoman Divacky NewC = ConstantStruct::get(Context, &NewOps[0], NewOps.size(), 299f22ef01cSRoman Divacky UserCS->getType()->isPacked()); 300f22ef01cSRoman Divacky } else if (isa<ConstantVector>(UserC)) { 3012754fe60SDimitry Andric NewC = ConstantVector::get(NewOps); 302f22ef01cSRoman Divacky } else { 303f22ef01cSRoman Divacky assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr."); 3043b0f4066SDimitry Andric NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps); 305f22ef01cSRoman Divacky } 306f22ef01cSRoman Divacky 307f22ef01cSRoman Divacky UserC->replaceAllUsesWith(NewC); 308f22ef01cSRoman Divacky UserC->destroyConstant(); 309f22ef01cSRoman Divacky NewOps.clear(); 310f22ef01cSRoman Divacky } 311f22ef01cSRoman Divacky 312f22ef01cSRoman Divacky // Update all ValueHandles, they should be the only users at this point. 313f22ef01cSRoman Divacky Placeholder->replaceAllUsesWith(RealVal); 314f22ef01cSRoman Divacky delete Placeholder; 315f22ef01cSRoman Divacky } 316f22ef01cSRoman Divacky } 317f22ef01cSRoman Divacky 318f22ef01cSRoman Divacky void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) { 319f22ef01cSRoman Divacky if (Idx == size()) { 320f22ef01cSRoman Divacky push_back(V); 321f22ef01cSRoman Divacky return; 322f22ef01cSRoman Divacky } 323f22ef01cSRoman Divacky 324f22ef01cSRoman Divacky if (Idx >= size()) 325f22ef01cSRoman Divacky resize(Idx+1); 326f22ef01cSRoman Divacky 327f22ef01cSRoman Divacky WeakVH &OldV = MDValuePtrs[Idx]; 328f22ef01cSRoman Divacky if (OldV == 0) { 329f22ef01cSRoman Divacky OldV = V; 330f22ef01cSRoman Divacky return; 331f22ef01cSRoman Divacky } 332f22ef01cSRoman Divacky 333f22ef01cSRoman Divacky // If there was a forward reference to this value, replace it. 334e580952dSDimitry Andric MDNode *PrevVal = cast<MDNode>(OldV); 335f22ef01cSRoman Divacky OldV->replaceAllUsesWith(V); 336e580952dSDimitry Andric MDNode::deleteTemporary(PrevVal); 337f22ef01cSRoman Divacky // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new 338f22ef01cSRoman Divacky // value for Idx. 339f22ef01cSRoman Divacky MDValuePtrs[Idx] = V; 340f22ef01cSRoman Divacky } 341f22ef01cSRoman Divacky 342f22ef01cSRoman Divacky Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { 343f22ef01cSRoman Divacky if (Idx >= size()) 344f22ef01cSRoman Divacky resize(Idx + 1); 345f22ef01cSRoman Divacky 346f22ef01cSRoman Divacky if (Value *V = MDValuePtrs[Idx]) { 347f22ef01cSRoman Divacky assert(V->getType()->isMetadataTy() && "Type mismatch in value table!"); 348f22ef01cSRoman Divacky return V; 349f22ef01cSRoman Divacky } 350f22ef01cSRoman Divacky 351f22ef01cSRoman Divacky // Create and return a placeholder, which will later be RAUW'd. 3523b0f4066SDimitry Andric Value *V = MDNode::getTemporary(Context, ArrayRef<Value*>()); 353f22ef01cSRoman Divacky MDValuePtrs[Idx] = V; 354f22ef01cSRoman Divacky return V; 355f22ef01cSRoman Divacky } 356f22ef01cSRoman Divacky 357f22ef01cSRoman Divacky const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) { 358f22ef01cSRoman Divacky // If the TypeID is in range, return it. 359f22ef01cSRoman Divacky if (ID < TypeList.size()) 360f22ef01cSRoman Divacky return TypeList[ID].get(); 361f22ef01cSRoman Divacky if (!isTypeTable) return 0; 362f22ef01cSRoman Divacky 363f22ef01cSRoman Divacky // The type table allows forward references. Push as many Opaque types as 364f22ef01cSRoman Divacky // needed to get up to ID. 365f22ef01cSRoman Divacky while (TypeList.size() <= ID) 366f22ef01cSRoman Divacky TypeList.push_back(OpaqueType::get(Context)); 367f22ef01cSRoman Divacky return TypeList.back().get(); 368f22ef01cSRoman Divacky } 369f22ef01cSRoman Divacky 370f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 371f22ef01cSRoman Divacky // Functions for parsing blocks from the bitcode file 372f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 373f22ef01cSRoman Divacky 374f22ef01cSRoman Divacky bool BitcodeReader::ParseAttributeBlock() { 375f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) 376f22ef01cSRoman Divacky return Error("Malformed block record"); 377f22ef01cSRoman Divacky 378f22ef01cSRoman Divacky if (!MAttributes.empty()) 379f22ef01cSRoman Divacky return Error("Multiple PARAMATTR blocks found!"); 380f22ef01cSRoman Divacky 381f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 382f22ef01cSRoman Divacky 383f22ef01cSRoman Divacky SmallVector<AttributeWithIndex, 8> Attrs; 384f22ef01cSRoman Divacky 385f22ef01cSRoman Divacky // Read all the records. 386f22ef01cSRoman Divacky while (1) { 387f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 388f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 389f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 390f22ef01cSRoman Divacky return Error("Error at end of PARAMATTR block"); 391f22ef01cSRoman Divacky return false; 392f22ef01cSRoman Divacky } 393f22ef01cSRoman Divacky 394f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 395f22ef01cSRoman Divacky // No known subblocks, always skip them. 396f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 397f22ef01cSRoman Divacky if (Stream.SkipBlock()) 398f22ef01cSRoman Divacky return Error("Malformed block record"); 399f22ef01cSRoman Divacky continue; 400f22ef01cSRoman Divacky } 401f22ef01cSRoman Divacky 402f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 403f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 404f22ef01cSRoman Divacky continue; 405f22ef01cSRoman Divacky } 406f22ef01cSRoman Divacky 407f22ef01cSRoman Divacky // Read a record. 408f22ef01cSRoman Divacky Record.clear(); 409f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 410f22ef01cSRoman Divacky default: // Default behavior: ignore. 411f22ef01cSRoman Divacky break; 412f22ef01cSRoman Divacky case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [paramidx0, attr0, ...] 413f22ef01cSRoman Divacky if (Record.size() & 1) 414f22ef01cSRoman Divacky return Error("Invalid ENTRY record"); 415f22ef01cSRoman Divacky 416f22ef01cSRoman Divacky // FIXME : Remove this autoupgrade code in LLVM 3.0. 417f22ef01cSRoman Divacky // If Function attributes are using index 0 then transfer them 418f22ef01cSRoman Divacky // to index ~0. Index 0 is used for return value attributes but used to be 419f22ef01cSRoman Divacky // used for function attributes. 420f22ef01cSRoman Divacky Attributes RetAttribute = Attribute::None; 421f22ef01cSRoman Divacky Attributes FnAttribute = Attribute::None; 422f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 423f22ef01cSRoman Divacky // FIXME: remove in LLVM 3.0 424f22ef01cSRoman Divacky // The alignment is stored as a 16-bit raw value from bits 31--16. 425f22ef01cSRoman Divacky // We shift the bits above 31 down by 11 bits. 426f22ef01cSRoman Divacky 427f22ef01cSRoman Divacky unsigned Alignment = (Record[i+1] & (0xffffull << 16)) >> 16; 428f22ef01cSRoman Divacky if (Alignment && !isPowerOf2_32(Alignment)) 429f22ef01cSRoman Divacky return Error("Alignment is not a power of two."); 430f22ef01cSRoman Divacky 431f22ef01cSRoman Divacky Attributes ReconstitutedAttr = Record[i+1] & 0xffff; 432f22ef01cSRoman Divacky if (Alignment) 433f22ef01cSRoman Divacky ReconstitutedAttr |= Attribute::constructAlignmentFromInt(Alignment); 434f22ef01cSRoman Divacky ReconstitutedAttr |= (Record[i+1] & (0xffffull << 32)) >> 11; 435f22ef01cSRoman Divacky Record[i+1] = ReconstitutedAttr; 436f22ef01cSRoman Divacky 437f22ef01cSRoman Divacky if (Record[i] == 0) 438f22ef01cSRoman Divacky RetAttribute = Record[i+1]; 439f22ef01cSRoman Divacky else if (Record[i] == ~0U) 440f22ef01cSRoman Divacky FnAttribute = Record[i+1]; 441f22ef01cSRoman Divacky } 442f22ef01cSRoman Divacky 443f22ef01cSRoman Divacky unsigned OldRetAttrs = (Attribute::NoUnwind|Attribute::NoReturn| 444f22ef01cSRoman Divacky Attribute::ReadOnly|Attribute::ReadNone); 445f22ef01cSRoman Divacky 446f22ef01cSRoman Divacky if (FnAttribute == Attribute::None && RetAttribute != Attribute::None && 447f22ef01cSRoman Divacky (RetAttribute & OldRetAttrs) != 0) { 448f22ef01cSRoman Divacky if (FnAttribute == Attribute::None) { // add a slot so they get added. 449f22ef01cSRoman Divacky Record.push_back(~0U); 450f22ef01cSRoman Divacky Record.push_back(0); 451f22ef01cSRoman Divacky } 452f22ef01cSRoman Divacky 453f22ef01cSRoman Divacky FnAttribute |= RetAttribute & OldRetAttrs; 454f22ef01cSRoman Divacky RetAttribute &= ~OldRetAttrs; 455f22ef01cSRoman Divacky } 456f22ef01cSRoman Divacky 457f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 458f22ef01cSRoman Divacky if (Record[i] == 0) { 459f22ef01cSRoman Divacky if (RetAttribute != Attribute::None) 460f22ef01cSRoman Divacky Attrs.push_back(AttributeWithIndex::get(0, RetAttribute)); 461f22ef01cSRoman Divacky } else if (Record[i] == ~0U) { 462f22ef01cSRoman Divacky if (FnAttribute != Attribute::None) 463f22ef01cSRoman Divacky Attrs.push_back(AttributeWithIndex::get(~0U, FnAttribute)); 464f22ef01cSRoman Divacky } else if (Record[i+1] != Attribute::None) 465f22ef01cSRoman Divacky Attrs.push_back(AttributeWithIndex::get(Record[i], Record[i+1])); 466f22ef01cSRoman Divacky } 467f22ef01cSRoman Divacky 468f22ef01cSRoman Divacky MAttributes.push_back(AttrListPtr::get(Attrs.begin(), Attrs.end())); 469f22ef01cSRoman Divacky Attrs.clear(); 470f22ef01cSRoman Divacky break; 471f22ef01cSRoman Divacky } 472f22ef01cSRoman Divacky } 473f22ef01cSRoman Divacky } 474f22ef01cSRoman Divacky } 475f22ef01cSRoman Divacky 476f22ef01cSRoman Divacky 477f22ef01cSRoman Divacky bool BitcodeReader::ParseTypeTable() { 478f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID)) 479f22ef01cSRoman Divacky return Error("Malformed block record"); 480f22ef01cSRoman Divacky 481f22ef01cSRoman Divacky if (!TypeList.empty()) 482f22ef01cSRoman Divacky return Error("Multiple TYPE_BLOCKs found!"); 483f22ef01cSRoman Divacky 484f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 485f22ef01cSRoman Divacky unsigned NumRecords = 0; 486f22ef01cSRoman Divacky 487f22ef01cSRoman Divacky // Read all the records for this type table. 488f22ef01cSRoman Divacky while (1) { 489f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 490f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 491f22ef01cSRoman Divacky if (NumRecords != TypeList.size()) 492f22ef01cSRoman Divacky return Error("Invalid type forward reference in TYPE_BLOCK"); 493f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 494f22ef01cSRoman Divacky return Error("Error at end of type table block"); 495f22ef01cSRoman Divacky return false; 496f22ef01cSRoman Divacky } 497f22ef01cSRoman Divacky 498f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 499f22ef01cSRoman Divacky // No known subblocks, always skip them. 500f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 501f22ef01cSRoman Divacky if (Stream.SkipBlock()) 502f22ef01cSRoman Divacky return Error("Malformed block record"); 503f22ef01cSRoman Divacky continue; 504f22ef01cSRoman Divacky } 505f22ef01cSRoman Divacky 506f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 507f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 508f22ef01cSRoman Divacky continue; 509f22ef01cSRoman Divacky } 510f22ef01cSRoman Divacky 511f22ef01cSRoman Divacky // Read a record. 512f22ef01cSRoman Divacky Record.clear(); 513f22ef01cSRoman Divacky const Type *ResultTy = 0; 514f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 515f22ef01cSRoman Divacky default: // Default behavior: unknown type. 516f22ef01cSRoman Divacky ResultTy = 0; 517f22ef01cSRoman Divacky break; 518f22ef01cSRoman Divacky case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 519f22ef01cSRoman Divacky // TYPE_CODE_NUMENTRY contains a count of the number of types in the 520f22ef01cSRoman Divacky // type list. This allows us to reserve space. 521f22ef01cSRoman Divacky if (Record.size() < 1) 522f22ef01cSRoman Divacky return Error("Invalid TYPE_CODE_NUMENTRY record"); 523f22ef01cSRoman Divacky TypeList.reserve(Record[0]); 524f22ef01cSRoman Divacky continue; 525f22ef01cSRoman Divacky case bitc::TYPE_CODE_VOID: // VOID 526f22ef01cSRoman Divacky ResultTy = Type::getVoidTy(Context); 527f22ef01cSRoman Divacky break; 528f22ef01cSRoman Divacky case bitc::TYPE_CODE_FLOAT: // FLOAT 529f22ef01cSRoman Divacky ResultTy = Type::getFloatTy(Context); 530f22ef01cSRoman Divacky break; 531f22ef01cSRoman Divacky case bitc::TYPE_CODE_DOUBLE: // DOUBLE 532f22ef01cSRoman Divacky ResultTy = Type::getDoubleTy(Context); 533f22ef01cSRoman Divacky break; 534f22ef01cSRoman Divacky case bitc::TYPE_CODE_X86_FP80: // X86_FP80 535f22ef01cSRoman Divacky ResultTy = Type::getX86_FP80Ty(Context); 536f22ef01cSRoman Divacky break; 537f22ef01cSRoman Divacky case bitc::TYPE_CODE_FP128: // FP128 538f22ef01cSRoman Divacky ResultTy = Type::getFP128Ty(Context); 539f22ef01cSRoman Divacky break; 540f22ef01cSRoman Divacky case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 541f22ef01cSRoman Divacky ResultTy = Type::getPPC_FP128Ty(Context); 542f22ef01cSRoman Divacky break; 543f22ef01cSRoman Divacky case bitc::TYPE_CODE_LABEL: // LABEL 544f22ef01cSRoman Divacky ResultTy = Type::getLabelTy(Context); 545f22ef01cSRoman Divacky break; 546f22ef01cSRoman Divacky case bitc::TYPE_CODE_OPAQUE: // OPAQUE 547f22ef01cSRoman Divacky ResultTy = 0; 548f22ef01cSRoman Divacky break; 549f22ef01cSRoman Divacky case bitc::TYPE_CODE_METADATA: // METADATA 550f22ef01cSRoman Divacky ResultTy = Type::getMetadataTy(Context); 551f22ef01cSRoman Divacky break; 5522754fe60SDimitry Andric case bitc::TYPE_CODE_X86_MMX: // X86_MMX 5532754fe60SDimitry Andric ResultTy = Type::getX86_MMXTy(Context); 5542754fe60SDimitry Andric break; 555f22ef01cSRoman Divacky case bitc::TYPE_CODE_INTEGER: // INTEGER: [width] 556f22ef01cSRoman Divacky if (Record.size() < 1) 557f22ef01cSRoman Divacky return Error("Invalid Integer type record"); 558f22ef01cSRoman Divacky 559f22ef01cSRoman Divacky ResultTy = IntegerType::get(Context, Record[0]); 560f22ef01cSRoman Divacky break; 561f22ef01cSRoman Divacky case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 562f22ef01cSRoman Divacky // [pointee type, address space] 563f22ef01cSRoman Divacky if (Record.size() < 1) 564f22ef01cSRoman Divacky return Error("Invalid POINTER type record"); 565f22ef01cSRoman Divacky unsigned AddressSpace = 0; 566f22ef01cSRoman Divacky if (Record.size() == 2) 567f22ef01cSRoman Divacky AddressSpace = Record[1]; 568f22ef01cSRoman Divacky ResultTy = PointerType::get(getTypeByID(Record[0], true), 569f22ef01cSRoman Divacky AddressSpace); 570f22ef01cSRoman Divacky break; 571f22ef01cSRoman Divacky } 572f22ef01cSRoman Divacky case bitc::TYPE_CODE_FUNCTION: { 573f22ef01cSRoman Divacky // FIXME: attrid is dead, remove it in LLVM 3.0 574f22ef01cSRoman Divacky // FUNCTION: [vararg, attrid, retty, paramty x N] 575f22ef01cSRoman Divacky if (Record.size() < 3) 576f22ef01cSRoman Divacky return Error("Invalid FUNCTION type record"); 577f22ef01cSRoman Divacky std::vector<const Type*> ArgTys; 578f22ef01cSRoman Divacky for (unsigned i = 3, e = Record.size(); i != e; ++i) 579f22ef01cSRoman Divacky ArgTys.push_back(getTypeByID(Record[i], true)); 580f22ef01cSRoman Divacky 581f22ef01cSRoman Divacky ResultTy = FunctionType::get(getTypeByID(Record[2], true), ArgTys, 582f22ef01cSRoman Divacky Record[0]); 583f22ef01cSRoman Divacky break; 584f22ef01cSRoman Divacky } 585f22ef01cSRoman Divacky case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, eltty x N] 586f22ef01cSRoman Divacky if (Record.size() < 1) 587f22ef01cSRoman Divacky return Error("Invalid STRUCT type record"); 588f22ef01cSRoman Divacky std::vector<const Type*> EltTys; 589f22ef01cSRoman Divacky for (unsigned i = 1, e = Record.size(); i != e; ++i) 590f22ef01cSRoman Divacky EltTys.push_back(getTypeByID(Record[i], true)); 591f22ef01cSRoman Divacky ResultTy = StructType::get(Context, EltTys, Record[0]); 592f22ef01cSRoman Divacky break; 593f22ef01cSRoman Divacky } 594f22ef01cSRoman Divacky case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] 595f22ef01cSRoman Divacky if (Record.size() < 2) 596f22ef01cSRoman Divacky return Error("Invalid ARRAY type record"); 597f22ef01cSRoman Divacky ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]); 598f22ef01cSRoman Divacky break; 599f22ef01cSRoman Divacky case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] 600f22ef01cSRoman Divacky if (Record.size() < 2) 601f22ef01cSRoman Divacky return Error("Invalid VECTOR type record"); 602f22ef01cSRoman Divacky ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]); 603f22ef01cSRoman Divacky break; 604f22ef01cSRoman Divacky } 605f22ef01cSRoman Divacky 606f22ef01cSRoman Divacky if (NumRecords == TypeList.size()) { 607f22ef01cSRoman Divacky // If this is a new type slot, just append it. 608f22ef01cSRoman Divacky TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get(Context)); 609f22ef01cSRoman Divacky ++NumRecords; 610f22ef01cSRoman Divacky } else if (ResultTy == 0) { 611f22ef01cSRoman Divacky // Otherwise, this was forward referenced, so an opaque type was created, 612f22ef01cSRoman Divacky // but the result type is actually just an opaque. Leave the one we 613f22ef01cSRoman Divacky // created previously. 614f22ef01cSRoman Divacky ++NumRecords; 615f22ef01cSRoman Divacky } else { 616f22ef01cSRoman Divacky // Otherwise, this was forward referenced, so an opaque type was created. 617f22ef01cSRoman Divacky // Resolve the opaque type to the real type now. 618f22ef01cSRoman Divacky assert(NumRecords < TypeList.size() && "Typelist imbalance"); 619f22ef01cSRoman Divacky const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get()); 620f22ef01cSRoman Divacky 621f22ef01cSRoman Divacky // Don't directly push the new type on the Tab. Instead we want to replace 622f22ef01cSRoman Divacky // the opaque type we previously inserted with the new concrete value. The 623f22ef01cSRoman Divacky // refinement from the abstract (opaque) type to the new type causes all 624f22ef01cSRoman Divacky // uses of the abstract type to use the concrete type (NewTy). This will 625f22ef01cSRoman Divacky // also cause the opaque type to be deleted. 626f22ef01cSRoman Divacky const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy); 627f22ef01cSRoman Divacky 628f22ef01cSRoman Divacky // This should have replaced the old opaque type with the new type in the 629f22ef01cSRoman Divacky // value table... or with a preexisting type that was already in the 630f22ef01cSRoman Divacky // system. Let's just make sure it did. 631f22ef01cSRoman Divacky assert(TypeList[NumRecords-1].get() != OldTy && 632f22ef01cSRoman Divacky "refineAbstractType didn't work!"); 633f22ef01cSRoman Divacky } 634f22ef01cSRoman Divacky } 635f22ef01cSRoman Divacky } 636f22ef01cSRoman Divacky 637f22ef01cSRoman Divacky 638f22ef01cSRoman Divacky bool BitcodeReader::ParseTypeSymbolTable() { 639f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::TYPE_SYMTAB_BLOCK_ID)) 640f22ef01cSRoman Divacky return Error("Malformed block record"); 641f22ef01cSRoman Divacky 642f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 643f22ef01cSRoman Divacky 644f22ef01cSRoman Divacky // Read all the records for this type table. 645f22ef01cSRoman Divacky std::string TypeName; 646f22ef01cSRoman Divacky while (1) { 647f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 648f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 649f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 650f22ef01cSRoman Divacky return Error("Error at end of type symbol table block"); 651f22ef01cSRoman Divacky return false; 652f22ef01cSRoman Divacky } 653f22ef01cSRoman Divacky 654f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 655f22ef01cSRoman Divacky // No known subblocks, always skip them. 656f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 657f22ef01cSRoman Divacky if (Stream.SkipBlock()) 658f22ef01cSRoman Divacky return Error("Malformed block record"); 659f22ef01cSRoman Divacky continue; 660f22ef01cSRoman Divacky } 661f22ef01cSRoman Divacky 662f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 663f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 664f22ef01cSRoman Divacky continue; 665f22ef01cSRoman Divacky } 666f22ef01cSRoman Divacky 667f22ef01cSRoman Divacky // Read a record. 668f22ef01cSRoman Divacky Record.clear(); 669f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 670f22ef01cSRoman Divacky default: // Default behavior: unknown type. 671f22ef01cSRoman Divacky break; 672f22ef01cSRoman Divacky case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namechar x N] 673f22ef01cSRoman Divacky if (ConvertToString(Record, 1, TypeName)) 674f22ef01cSRoman Divacky return Error("Invalid TST_ENTRY record"); 675f22ef01cSRoman Divacky unsigned TypeID = Record[0]; 676f22ef01cSRoman Divacky if (TypeID >= TypeList.size()) 677f22ef01cSRoman Divacky return Error("Invalid Type ID in TST_ENTRY record"); 678f22ef01cSRoman Divacky 679f22ef01cSRoman Divacky TheModule->addTypeName(TypeName, TypeList[TypeID].get()); 680f22ef01cSRoman Divacky TypeName.clear(); 681f22ef01cSRoman Divacky break; 682f22ef01cSRoman Divacky } 683f22ef01cSRoman Divacky } 684f22ef01cSRoman Divacky } 685f22ef01cSRoman Divacky 686f22ef01cSRoman Divacky bool BitcodeReader::ParseValueSymbolTable() { 687f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 688f22ef01cSRoman Divacky return Error("Malformed block record"); 689f22ef01cSRoman Divacky 690f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 691f22ef01cSRoman Divacky 692f22ef01cSRoman Divacky // Read all the records for this value table. 693f22ef01cSRoman Divacky SmallString<128> ValueName; 694f22ef01cSRoman Divacky while (1) { 695f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 696f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 697f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 698f22ef01cSRoman Divacky return Error("Error at end of value symbol table block"); 699f22ef01cSRoman Divacky return false; 700f22ef01cSRoman Divacky } 701f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 702f22ef01cSRoman Divacky // No known subblocks, always skip them. 703f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 704f22ef01cSRoman Divacky if (Stream.SkipBlock()) 705f22ef01cSRoman Divacky return Error("Malformed block record"); 706f22ef01cSRoman Divacky continue; 707f22ef01cSRoman Divacky } 708f22ef01cSRoman Divacky 709f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 710f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 711f22ef01cSRoman Divacky continue; 712f22ef01cSRoman Divacky } 713f22ef01cSRoman Divacky 714f22ef01cSRoman Divacky // Read a record. 715f22ef01cSRoman Divacky Record.clear(); 716f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 717f22ef01cSRoman Divacky default: // Default behavior: unknown type. 718f22ef01cSRoman Divacky break; 719f22ef01cSRoman Divacky case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N] 720f22ef01cSRoman Divacky if (ConvertToString(Record, 1, ValueName)) 721f22ef01cSRoman Divacky return Error("Invalid VST_ENTRY record"); 722f22ef01cSRoman Divacky unsigned ValueID = Record[0]; 723f22ef01cSRoman Divacky if (ValueID >= ValueList.size()) 724f22ef01cSRoman Divacky return Error("Invalid Value ID in VST_ENTRY record"); 725f22ef01cSRoman Divacky Value *V = ValueList[ValueID]; 726f22ef01cSRoman Divacky 727f22ef01cSRoman Divacky V->setName(StringRef(ValueName.data(), ValueName.size())); 728f22ef01cSRoman Divacky ValueName.clear(); 729f22ef01cSRoman Divacky break; 730f22ef01cSRoman Divacky } 731f22ef01cSRoman Divacky case bitc::VST_CODE_BBENTRY: { 732f22ef01cSRoman Divacky if (ConvertToString(Record, 1, ValueName)) 733f22ef01cSRoman Divacky return Error("Invalid VST_BBENTRY record"); 734f22ef01cSRoman Divacky BasicBlock *BB = getBasicBlock(Record[0]); 735f22ef01cSRoman Divacky if (BB == 0) 736f22ef01cSRoman Divacky return Error("Invalid BB ID in VST_BBENTRY record"); 737f22ef01cSRoman Divacky 738f22ef01cSRoman Divacky BB->setName(StringRef(ValueName.data(), ValueName.size())); 739f22ef01cSRoman Divacky ValueName.clear(); 740f22ef01cSRoman Divacky break; 741f22ef01cSRoman Divacky } 742f22ef01cSRoman Divacky } 743f22ef01cSRoman Divacky } 744f22ef01cSRoman Divacky } 745f22ef01cSRoman Divacky 746f22ef01cSRoman Divacky bool BitcodeReader::ParseMetadata() { 747f22ef01cSRoman Divacky unsigned NextMDValueNo = MDValueList.size(); 748f22ef01cSRoman Divacky 749f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) 750f22ef01cSRoman Divacky return Error("Malformed block record"); 751f22ef01cSRoman Divacky 752f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 753f22ef01cSRoman Divacky 754f22ef01cSRoman Divacky // Read all the records. 755f22ef01cSRoman Divacky while (1) { 756f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 757f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 758f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 759f22ef01cSRoman Divacky return Error("Error at end of PARAMATTR block"); 760f22ef01cSRoman Divacky return false; 761f22ef01cSRoman Divacky } 762f22ef01cSRoman Divacky 763f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 764f22ef01cSRoman Divacky // No known subblocks, always skip them. 765f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 766f22ef01cSRoman Divacky if (Stream.SkipBlock()) 767f22ef01cSRoman Divacky return Error("Malformed block record"); 768f22ef01cSRoman Divacky continue; 769f22ef01cSRoman Divacky } 770f22ef01cSRoman Divacky 771f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 772f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 773f22ef01cSRoman Divacky continue; 774f22ef01cSRoman Divacky } 775f22ef01cSRoman Divacky 776f22ef01cSRoman Divacky bool IsFunctionLocal = false; 777f22ef01cSRoman Divacky // Read a record. 778f22ef01cSRoman Divacky Record.clear(); 779e580952dSDimitry Andric Code = Stream.ReadRecord(Code, Record); 780e580952dSDimitry Andric switch (Code) { 781f22ef01cSRoman Divacky default: // Default behavior: ignore. 782f22ef01cSRoman Divacky break; 783f22ef01cSRoman Divacky case bitc::METADATA_NAME: { 784f22ef01cSRoman Divacky // Read named of the named metadata. 785f22ef01cSRoman Divacky unsigned NameLength = Record.size(); 786f22ef01cSRoman Divacky SmallString<8> Name; 787f22ef01cSRoman Divacky Name.resize(NameLength); 788f22ef01cSRoman Divacky for (unsigned i = 0; i != NameLength; ++i) 789f22ef01cSRoman Divacky Name[i] = Record[i]; 790f22ef01cSRoman Divacky Record.clear(); 791f22ef01cSRoman Divacky Code = Stream.ReadCode(); 792f22ef01cSRoman Divacky 793e580952dSDimitry Andric // METADATA_NAME is always followed by METADATA_NAMED_NODE2. 794e580952dSDimitry Andric // Or METADATA_NAMED_NODE in LLVM 2.7. FIXME: Remove this in LLVM 3.0. 795e580952dSDimitry Andric unsigned NextBitCode = Stream.ReadRecord(Code, Record); 796e580952dSDimitry Andric if (NextBitCode == bitc::METADATA_NAMED_NODE) { 797e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 798e580952dSDimitry Andric } else if (NextBitCode != bitc::METADATA_NAMED_NODE2) 7992754fe60SDimitry Andric assert ( 0 && "Invalid Named Metadata record"); 800f22ef01cSRoman Divacky 801f22ef01cSRoman Divacky // Read named metadata elements. 802f22ef01cSRoman Divacky unsigned Size = Record.size(); 803e580952dSDimitry Andric NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name); 804f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) { 805f22ef01cSRoman Divacky MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i])); 806f22ef01cSRoman Divacky if (MD == 0) 807f22ef01cSRoman Divacky return Error("Malformed metadata record"); 808e580952dSDimitry Andric NMD->addOperand(MD); 809f22ef01cSRoman Divacky } 810e580952dSDimitry Andric // Backwards compatibility hack: NamedMDValues used to be Values, 811e580952dSDimitry Andric // and they got their own slots in the value numbering. They are no 812e580952dSDimitry Andric // longer Values, however we still need to account for them in the 813e580952dSDimitry Andric // numbering in order to be able to read old bitcode files. 814e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 815e580952dSDimitry Andric if (LLVM2_7MetadataDetected) 816e580952dSDimitry Andric MDValueList.AssignValue(0, NextMDValueNo++); 817f22ef01cSRoman Divacky break; 818f22ef01cSRoman Divacky } 819e580952dSDimitry Andric case bitc::METADATA_FN_NODE: // FIXME: Remove in LLVM 3.0. 820e580952dSDimitry Andric case bitc::METADATA_FN_NODE2: 821f22ef01cSRoman Divacky IsFunctionLocal = true; 822f22ef01cSRoman Divacky // fall-through 823e580952dSDimitry Andric case bitc::METADATA_NODE: // FIXME: Remove in LLVM 3.0. 824e580952dSDimitry Andric case bitc::METADATA_NODE2: { 825e580952dSDimitry Andric 826e580952dSDimitry Andric // Detect 2.7-era metadata. 827e580952dSDimitry Andric // FIXME: Remove in LLVM 3.0. 828e580952dSDimitry Andric if (Code == bitc::METADATA_FN_NODE || Code == bitc::METADATA_NODE) 829e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 830e580952dSDimitry Andric 831ffd1746dSEd Schouten if (Record.size() % 2 == 1) 832e580952dSDimitry Andric return Error("Invalid METADATA_NODE2 record"); 833f22ef01cSRoman Divacky 834f22ef01cSRoman Divacky unsigned Size = Record.size(); 835f22ef01cSRoman Divacky SmallVector<Value*, 8> Elts; 836f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; i += 2) { 8372754fe60SDimitry Andric const Type *Ty = getTypeByID(Record[i]); 8382754fe60SDimitry Andric if (!Ty) return Error("Invalid METADATA_NODE2 record"); 839f22ef01cSRoman Divacky if (Ty->isMetadataTy()) 840f22ef01cSRoman Divacky Elts.push_back(MDValueList.getValueFwdRef(Record[i+1])); 841f22ef01cSRoman Divacky else if (!Ty->isVoidTy()) 842f22ef01cSRoman Divacky Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty)); 843f22ef01cSRoman Divacky else 844f22ef01cSRoman Divacky Elts.push_back(NULL); 845f22ef01cSRoman Divacky } 8463b0f4066SDimitry Andric Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal); 847f22ef01cSRoman Divacky IsFunctionLocal = false; 848f22ef01cSRoman Divacky MDValueList.AssignValue(V, NextMDValueNo++); 849f22ef01cSRoman Divacky break; 850f22ef01cSRoman Divacky } 851f22ef01cSRoman Divacky case bitc::METADATA_STRING: { 852f22ef01cSRoman Divacky unsigned MDStringLength = Record.size(); 853f22ef01cSRoman Divacky SmallString<8> String; 854f22ef01cSRoman Divacky String.resize(MDStringLength); 855f22ef01cSRoman Divacky for (unsigned i = 0; i != MDStringLength; ++i) 856f22ef01cSRoman Divacky String[i] = Record[i]; 857f22ef01cSRoman Divacky Value *V = MDString::get(Context, 858f22ef01cSRoman Divacky StringRef(String.data(), String.size())); 859f22ef01cSRoman Divacky MDValueList.AssignValue(V, NextMDValueNo++); 860f22ef01cSRoman Divacky break; 861f22ef01cSRoman Divacky } 862f22ef01cSRoman Divacky case bitc::METADATA_KIND: { 863f22ef01cSRoman Divacky unsigned RecordLength = Record.size(); 864f22ef01cSRoman Divacky if (Record.empty() || RecordLength < 2) 865f22ef01cSRoman Divacky return Error("Invalid METADATA_KIND record"); 866f22ef01cSRoman Divacky SmallString<8> Name; 867f22ef01cSRoman Divacky Name.resize(RecordLength-1); 868f22ef01cSRoman Divacky unsigned Kind = Record[0]; 869f22ef01cSRoman Divacky for (unsigned i = 1; i != RecordLength; ++i) 870f22ef01cSRoman Divacky Name[i-1] = Record[i]; 871f22ef01cSRoman Divacky 872f22ef01cSRoman Divacky unsigned NewKind = TheModule->getMDKindID(Name.str()); 873e580952dSDimitry Andric if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) 874e580952dSDimitry Andric return Error("Conflicting METADATA_KIND records"); 875f22ef01cSRoman Divacky break; 876f22ef01cSRoman Divacky } 877f22ef01cSRoman Divacky } 878f22ef01cSRoman Divacky } 879f22ef01cSRoman Divacky } 880f22ef01cSRoman Divacky 881f22ef01cSRoman Divacky /// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in 882f22ef01cSRoman Divacky /// the LSB for dense VBR encoding. 883f22ef01cSRoman Divacky static uint64_t DecodeSignRotatedValue(uint64_t V) { 884f22ef01cSRoman Divacky if ((V & 1) == 0) 885f22ef01cSRoman Divacky return V >> 1; 886f22ef01cSRoman Divacky if (V != 1) 887f22ef01cSRoman Divacky return -(V >> 1); 888f22ef01cSRoman Divacky // There is no such thing as -0 with integers. "-0" really means MININT. 889f22ef01cSRoman Divacky return 1ULL << 63; 890f22ef01cSRoman Divacky } 891f22ef01cSRoman Divacky 892f22ef01cSRoman Divacky /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global 893f22ef01cSRoman Divacky /// values and aliases that we can. 894f22ef01cSRoman Divacky bool BitcodeReader::ResolveGlobalAndAliasInits() { 895f22ef01cSRoman Divacky std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; 896f22ef01cSRoman Divacky std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; 897f22ef01cSRoman Divacky 898f22ef01cSRoman Divacky GlobalInitWorklist.swap(GlobalInits); 899f22ef01cSRoman Divacky AliasInitWorklist.swap(AliasInits); 900f22ef01cSRoman Divacky 901f22ef01cSRoman Divacky while (!GlobalInitWorklist.empty()) { 902f22ef01cSRoman Divacky unsigned ValID = GlobalInitWorklist.back().second; 903f22ef01cSRoman Divacky if (ValID >= ValueList.size()) { 904f22ef01cSRoman Divacky // Not ready to resolve this yet, it requires something later in the file. 905f22ef01cSRoman Divacky GlobalInits.push_back(GlobalInitWorklist.back()); 906f22ef01cSRoman Divacky } else { 907f22ef01cSRoman Divacky if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) 908f22ef01cSRoman Divacky GlobalInitWorklist.back().first->setInitializer(C); 909f22ef01cSRoman Divacky else 910f22ef01cSRoman Divacky return Error("Global variable initializer is not a constant!"); 911f22ef01cSRoman Divacky } 912f22ef01cSRoman Divacky GlobalInitWorklist.pop_back(); 913f22ef01cSRoman Divacky } 914f22ef01cSRoman Divacky 915f22ef01cSRoman Divacky while (!AliasInitWorklist.empty()) { 916f22ef01cSRoman Divacky unsigned ValID = AliasInitWorklist.back().second; 917f22ef01cSRoman Divacky if (ValID >= ValueList.size()) { 918f22ef01cSRoman Divacky AliasInits.push_back(AliasInitWorklist.back()); 919f22ef01cSRoman Divacky } else { 920f22ef01cSRoman Divacky if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) 921f22ef01cSRoman Divacky AliasInitWorklist.back().first->setAliasee(C); 922f22ef01cSRoman Divacky else 923f22ef01cSRoman Divacky return Error("Alias initializer is not a constant!"); 924f22ef01cSRoman Divacky } 925f22ef01cSRoman Divacky AliasInitWorklist.pop_back(); 926f22ef01cSRoman Divacky } 927f22ef01cSRoman Divacky return false; 928f22ef01cSRoman Divacky } 929f22ef01cSRoman Divacky 930f22ef01cSRoman Divacky bool BitcodeReader::ParseConstants() { 931f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) 932f22ef01cSRoman Divacky return Error("Malformed block record"); 933f22ef01cSRoman Divacky 934f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 935f22ef01cSRoman Divacky 936f22ef01cSRoman Divacky // Read all the records for this value table. 937f22ef01cSRoman Divacky const Type *CurTy = Type::getInt32Ty(Context); 938f22ef01cSRoman Divacky unsigned NextCstNo = ValueList.size(); 939f22ef01cSRoman Divacky while (1) { 940f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 941f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) 942f22ef01cSRoman Divacky break; 943f22ef01cSRoman Divacky 944f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 945f22ef01cSRoman Divacky // No known subblocks, always skip them. 946f22ef01cSRoman Divacky Stream.ReadSubBlockID(); 947f22ef01cSRoman Divacky if (Stream.SkipBlock()) 948f22ef01cSRoman Divacky return Error("Malformed block record"); 949f22ef01cSRoman Divacky continue; 950f22ef01cSRoman Divacky } 951f22ef01cSRoman Divacky 952f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 953f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 954f22ef01cSRoman Divacky continue; 955f22ef01cSRoman Divacky } 956f22ef01cSRoman Divacky 957f22ef01cSRoman Divacky // Read a record. 958f22ef01cSRoman Divacky Record.clear(); 959f22ef01cSRoman Divacky Value *V = 0; 960f22ef01cSRoman Divacky unsigned BitCode = Stream.ReadRecord(Code, Record); 961f22ef01cSRoman Divacky switch (BitCode) { 962f22ef01cSRoman Divacky default: // Default behavior: unknown constant 963f22ef01cSRoman Divacky case bitc::CST_CODE_UNDEF: // UNDEF 964f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 965f22ef01cSRoman Divacky break; 966f22ef01cSRoman Divacky case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] 967f22ef01cSRoman Divacky if (Record.empty()) 968f22ef01cSRoman Divacky return Error("Malformed CST_SETTYPE record"); 969f22ef01cSRoman Divacky if (Record[0] >= TypeList.size()) 970f22ef01cSRoman Divacky return Error("Invalid Type ID in CST_SETTYPE record"); 971f22ef01cSRoman Divacky CurTy = TypeList[Record[0]]; 972f22ef01cSRoman Divacky continue; // Skip the ValueList manipulation. 973f22ef01cSRoman Divacky case bitc::CST_CODE_NULL: // NULL 974f22ef01cSRoman Divacky V = Constant::getNullValue(CurTy); 975f22ef01cSRoman Divacky break; 976f22ef01cSRoman Divacky case bitc::CST_CODE_INTEGER: // INTEGER: [intval] 977f22ef01cSRoman Divacky if (!CurTy->isIntegerTy() || Record.empty()) 978f22ef01cSRoman Divacky return Error("Invalid CST_INTEGER record"); 979f22ef01cSRoman Divacky V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0])); 980f22ef01cSRoman Divacky break; 981f22ef01cSRoman Divacky case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] 982f22ef01cSRoman Divacky if (!CurTy->isIntegerTy() || Record.empty()) 983f22ef01cSRoman Divacky return Error("Invalid WIDE_INTEGER record"); 984f22ef01cSRoman Divacky 985f22ef01cSRoman Divacky unsigned NumWords = Record.size(); 986f22ef01cSRoman Divacky SmallVector<uint64_t, 8> Words; 987f22ef01cSRoman Divacky Words.resize(NumWords); 988f22ef01cSRoman Divacky for (unsigned i = 0; i != NumWords; ++i) 989f22ef01cSRoman Divacky Words[i] = DecodeSignRotatedValue(Record[i]); 990f22ef01cSRoman Divacky V = ConstantInt::get(Context, 991f22ef01cSRoman Divacky APInt(cast<IntegerType>(CurTy)->getBitWidth(), 992f22ef01cSRoman Divacky NumWords, &Words[0])); 993f22ef01cSRoman Divacky break; 994f22ef01cSRoman Divacky } 995f22ef01cSRoman Divacky case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] 996f22ef01cSRoman Divacky if (Record.empty()) 997f22ef01cSRoman Divacky return Error("Invalid FLOAT record"); 998f22ef01cSRoman Divacky if (CurTy->isFloatTy()) 999f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(32, (uint32_t)Record[0]))); 1000f22ef01cSRoman Divacky else if (CurTy->isDoubleTy()) 1001f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(64, Record[0]))); 1002f22ef01cSRoman Divacky else if (CurTy->isX86_FP80Ty()) { 1003f22ef01cSRoman Divacky // Bits are not stored the same way as a normal i80 APInt, compensate. 1004f22ef01cSRoman Divacky uint64_t Rearrange[2]; 1005f22ef01cSRoman Divacky Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); 1006f22ef01cSRoman Divacky Rearrange[1] = Record[0] >> 48; 1007f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(80, 2, Rearrange))); 1008f22ef01cSRoman Divacky } else if (CurTy->isFP128Ty()) 1009f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(128, 2, &Record[0]), true)); 1010f22ef01cSRoman Divacky else if (CurTy->isPPC_FP128Ty()) 1011f22ef01cSRoman Divacky V = ConstantFP::get(Context, APFloat(APInt(128, 2, &Record[0]))); 1012f22ef01cSRoman Divacky else 1013f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 1014f22ef01cSRoman Divacky break; 1015f22ef01cSRoman Divacky } 1016f22ef01cSRoman Divacky 1017f22ef01cSRoman Divacky case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] 1018f22ef01cSRoman Divacky if (Record.empty()) 1019f22ef01cSRoman Divacky return Error("Invalid CST_AGGREGATE record"); 1020f22ef01cSRoman Divacky 1021f22ef01cSRoman Divacky unsigned Size = Record.size(); 1022f22ef01cSRoman Divacky std::vector<Constant*> Elts; 1023f22ef01cSRoman Divacky 1024f22ef01cSRoman Divacky if (const StructType *STy = dyn_cast<StructType>(CurTy)) { 1025f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1026f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], 1027f22ef01cSRoman Divacky STy->getElementType(i))); 1028f22ef01cSRoman Divacky V = ConstantStruct::get(STy, Elts); 1029f22ef01cSRoman Divacky } else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { 1030f22ef01cSRoman Divacky const Type *EltTy = ATy->getElementType(); 1031f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1032f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 1033f22ef01cSRoman Divacky V = ConstantArray::get(ATy, Elts); 1034f22ef01cSRoman Divacky } else if (const VectorType *VTy = dyn_cast<VectorType>(CurTy)) { 1035f22ef01cSRoman Divacky const Type *EltTy = VTy->getElementType(); 1036f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1037f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 1038f22ef01cSRoman Divacky V = ConstantVector::get(Elts); 1039f22ef01cSRoman Divacky } else { 1040f22ef01cSRoman Divacky V = UndefValue::get(CurTy); 1041f22ef01cSRoman Divacky } 1042f22ef01cSRoman Divacky break; 1043f22ef01cSRoman Divacky } 1044f22ef01cSRoman Divacky case bitc::CST_CODE_STRING: { // STRING: [values] 1045f22ef01cSRoman Divacky if (Record.empty()) 1046f22ef01cSRoman Divacky return Error("Invalid CST_AGGREGATE record"); 1047f22ef01cSRoman Divacky 1048f22ef01cSRoman Divacky const ArrayType *ATy = cast<ArrayType>(CurTy); 1049f22ef01cSRoman Divacky const Type *EltTy = ATy->getElementType(); 1050f22ef01cSRoman Divacky 1051f22ef01cSRoman Divacky unsigned Size = Record.size(); 1052f22ef01cSRoman Divacky std::vector<Constant*> Elts; 1053f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1054f22ef01cSRoman Divacky Elts.push_back(ConstantInt::get(EltTy, Record[i])); 1055f22ef01cSRoman Divacky V = ConstantArray::get(ATy, Elts); 1056f22ef01cSRoman Divacky break; 1057f22ef01cSRoman Divacky } 1058f22ef01cSRoman Divacky case bitc::CST_CODE_CSTRING: { // CSTRING: [values] 1059f22ef01cSRoman Divacky if (Record.empty()) 1060f22ef01cSRoman Divacky return Error("Invalid CST_AGGREGATE record"); 1061f22ef01cSRoman Divacky 1062f22ef01cSRoman Divacky const ArrayType *ATy = cast<ArrayType>(CurTy); 1063f22ef01cSRoman Divacky const Type *EltTy = ATy->getElementType(); 1064f22ef01cSRoman Divacky 1065f22ef01cSRoman Divacky unsigned Size = Record.size(); 1066f22ef01cSRoman Divacky std::vector<Constant*> Elts; 1067f22ef01cSRoman Divacky for (unsigned i = 0; i != Size; ++i) 1068f22ef01cSRoman Divacky Elts.push_back(ConstantInt::get(EltTy, Record[i])); 1069f22ef01cSRoman Divacky Elts.push_back(Constant::getNullValue(EltTy)); 1070f22ef01cSRoman Divacky V = ConstantArray::get(ATy, Elts); 1071f22ef01cSRoman Divacky break; 1072f22ef01cSRoman Divacky } 1073f22ef01cSRoman Divacky case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] 1074f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_BINOP record"); 1075f22ef01cSRoman Divacky int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); 1076f22ef01cSRoman Divacky if (Opc < 0) { 1077f22ef01cSRoman Divacky V = UndefValue::get(CurTy); // Unknown binop. 1078f22ef01cSRoman Divacky } else { 1079f22ef01cSRoman Divacky Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); 1080f22ef01cSRoman Divacky Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); 1081f22ef01cSRoman Divacky unsigned Flags = 0; 1082f22ef01cSRoman Divacky if (Record.size() >= 4) { 1083f22ef01cSRoman Divacky if (Opc == Instruction::Add || 1084f22ef01cSRoman Divacky Opc == Instruction::Sub || 10852754fe60SDimitry Andric Opc == Instruction::Mul || 10862754fe60SDimitry Andric Opc == Instruction::Shl) { 1087f22ef01cSRoman Divacky if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 1088f22ef01cSRoman Divacky Flags |= OverflowingBinaryOperator::NoSignedWrap; 1089f22ef01cSRoman Divacky if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 1090f22ef01cSRoman Divacky Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 10912754fe60SDimitry Andric } else if (Opc == Instruction::SDiv || 10922754fe60SDimitry Andric Opc == Instruction::UDiv || 10932754fe60SDimitry Andric Opc == Instruction::LShr || 10942754fe60SDimitry Andric Opc == Instruction::AShr) { 10952754fe60SDimitry Andric if (Record[3] & (1 << bitc::PEO_EXACT)) 1096f22ef01cSRoman Divacky Flags |= SDivOperator::IsExact; 1097f22ef01cSRoman Divacky } 1098f22ef01cSRoman Divacky } 1099f22ef01cSRoman Divacky V = ConstantExpr::get(Opc, LHS, RHS, Flags); 1100f22ef01cSRoman Divacky } 1101f22ef01cSRoman Divacky break; 1102f22ef01cSRoman Divacky } 1103f22ef01cSRoman Divacky case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] 1104f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_CAST record"); 1105f22ef01cSRoman Divacky int Opc = GetDecodedCastOpcode(Record[0]); 1106f22ef01cSRoman Divacky if (Opc < 0) { 1107f22ef01cSRoman Divacky V = UndefValue::get(CurTy); // Unknown cast. 1108f22ef01cSRoman Divacky } else { 1109f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[1]); 1110f22ef01cSRoman Divacky if (!OpTy) return Error("Invalid CE_CAST record"); 1111f22ef01cSRoman Divacky Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); 1112f22ef01cSRoman Divacky V = ConstantExpr::getCast(Opc, Op, CurTy); 1113f22ef01cSRoman Divacky } 1114f22ef01cSRoman Divacky break; 1115f22ef01cSRoman Divacky } 1116f22ef01cSRoman Divacky case bitc::CST_CODE_CE_INBOUNDS_GEP: 1117f22ef01cSRoman Divacky case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] 1118f22ef01cSRoman Divacky if (Record.size() & 1) return Error("Invalid CE_GEP record"); 1119f22ef01cSRoman Divacky SmallVector<Constant*, 16> Elts; 1120f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 1121f22ef01cSRoman Divacky const Type *ElTy = getTypeByID(Record[i]); 1122f22ef01cSRoman Divacky if (!ElTy) return Error("Invalid CE_GEP record"); 1123f22ef01cSRoman Divacky Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); 1124f22ef01cSRoman Divacky } 1125f22ef01cSRoman Divacky if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) 1126f22ef01cSRoman Divacky V = ConstantExpr::getInBoundsGetElementPtr(Elts[0], &Elts[1], 1127f22ef01cSRoman Divacky Elts.size()-1); 1128f22ef01cSRoman Divacky else 1129f22ef01cSRoman Divacky V = ConstantExpr::getGetElementPtr(Elts[0], &Elts[1], 1130f22ef01cSRoman Divacky Elts.size()-1); 1131f22ef01cSRoman Divacky break; 1132f22ef01cSRoman Divacky } 1133f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#] 1134f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_SELECT record"); 1135f22ef01cSRoman Divacky V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], 1136f22ef01cSRoman Divacky Type::getInt1Ty(Context)), 1137f22ef01cSRoman Divacky ValueList.getConstantFwdRef(Record[1],CurTy), 1138f22ef01cSRoman Divacky ValueList.getConstantFwdRef(Record[2],CurTy)); 1139f22ef01cSRoman Divacky break; 1140f22ef01cSRoman Divacky case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval] 1141f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_EXTRACTELT record"); 1142f22ef01cSRoman Divacky const VectorType *OpTy = 1143f22ef01cSRoman Divacky dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 1144f22ef01cSRoman Divacky if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record"); 1145f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 1146f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 1147f22ef01cSRoman Divacky V = ConstantExpr::getExtractElement(Op0, Op1); 1148f22ef01cSRoman Divacky break; 1149f22ef01cSRoman Divacky } 1150f22ef01cSRoman Divacky case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval] 1151f22ef01cSRoman Divacky const VectorType *OpTy = dyn_cast<VectorType>(CurTy); 1152f22ef01cSRoman Divacky if (Record.size() < 3 || OpTy == 0) 1153f22ef01cSRoman Divacky return Error("Invalid CE_INSERTELT record"); 1154f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 1155f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[1], 1156f22ef01cSRoman Divacky OpTy->getElementType()); 1157f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 1158f22ef01cSRoman Divacky V = ConstantExpr::getInsertElement(Op0, Op1, Op2); 1159f22ef01cSRoman Divacky break; 1160f22ef01cSRoman Divacky } 1161f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] 1162f22ef01cSRoman Divacky const VectorType *OpTy = dyn_cast<VectorType>(CurTy); 1163f22ef01cSRoman Divacky if (Record.size() < 3 || OpTy == 0) 1164f22ef01cSRoman Divacky return Error("Invalid CE_SHUFFLEVEC record"); 1165f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 1166f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); 1167f22ef01cSRoman Divacky const Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 1168f22ef01cSRoman Divacky OpTy->getNumElements()); 1169f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); 1170f22ef01cSRoman Divacky V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 1171f22ef01cSRoman Divacky break; 1172f22ef01cSRoman Divacky } 1173f22ef01cSRoman Divacky case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] 1174f22ef01cSRoman Divacky const VectorType *RTy = dyn_cast<VectorType>(CurTy); 11752754fe60SDimitry Andric const VectorType *OpTy = 11762754fe60SDimitry Andric dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 1177f22ef01cSRoman Divacky if (Record.size() < 4 || RTy == 0 || OpTy == 0) 1178f22ef01cSRoman Divacky return Error("Invalid CE_SHUFVEC_EX record"); 1179f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 1180f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 1181f22ef01cSRoman Divacky const Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 1182f22ef01cSRoman Divacky RTy->getNumElements()); 1183f22ef01cSRoman Divacky Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); 1184f22ef01cSRoman Divacky V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 1185f22ef01cSRoman Divacky break; 1186f22ef01cSRoman Divacky } 1187f22ef01cSRoman Divacky case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] 1188f22ef01cSRoman Divacky if (Record.size() < 4) return Error("Invalid CE_CMP record"); 1189f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[0]); 1190f22ef01cSRoman Divacky if (OpTy == 0) return Error("Invalid CE_CMP record"); 1191f22ef01cSRoman Divacky Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 1192f22ef01cSRoman Divacky Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 1193f22ef01cSRoman Divacky 1194f22ef01cSRoman Divacky if (OpTy->isFPOrFPVectorTy()) 1195f22ef01cSRoman Divacky V = ConstantExpr::getFCmp(Record[3], Op0, Op1); 1196f22ef01cSRoman Divacky else 1197f22ef01cSRoman Divacky V = ConstantExpr::getICmp(Record[3], Op0, Op1); 1198f22ef01cSRoman Divacky break; 1199f22ef01cSRoman Divacky } 1200f22ef01cSRoman Divacky case bitc::CST_CODE_INLINEASM: { 1201f22ef01cSRoman Divacky if (Record.size() < 2) return Error("Invalid INLINEASM record"); 1202f22ef01cSRoman Divacky std::string AsmStr, ConstrStr; 1203f22ef01cSRoman Divacky bool HasSideEffects = Record[0] & 1; 1204f22ef01cSRoman Divacky bool IsAlignStack = Record[0] >> 1; 1205f22ef01cSRoman Divacky unsigned AsmStrSize = Record[1]; 1206f22ef01cSRoman Divacky if (2+AsmStrSize >= Record.size()) 1207f22ef01cSRoman Divacky return Error("Invalid INLINEASM record"); 1208f22ef01cSRoman Divacky unsigned ConstStrSize = Record[2+AsmStrSize]; 1209f22ef01cSRoman Divacky if (3+AsmStrSize+ConstStrSize > Record.size()) 1210f22ef01cSRoman Divacky return Error("Invalid INLINEASM record"); 1211f22ef01cSRoman Divacky 1212f22ef01cSRoman Divacky for (unsigned i = 0; i != AsmStrSize; ++i) 1213f22ef01cSRoman Divacky AsmStr += (char)Record[2+i]; 1214f22ef01cSRoman Divacky for (unsigned i = 0; i != ConstStrSize; ++i) 1215f22ef01cSRoman Divacky ConstrStr += (char)Record[3+AsmStrSize+i]; 1216f22ef01cSRoman Divacky const PointerType *PTy = cast<PointerType>(CurTy); 1217f22ef01cSRoman Divacky V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), 1218f22ef01cSRoman Divacky AsmStr, ConstrStr, HasSideEffects, IsAlignStack); 1219f22ef01cSRoman Divacky break; 1220f22ef01cSRoman Divacky } 1221f22ef01cSRoman Divacky case bitc::CST_CODE_BLOCKADDRESS:{ 1222f22ef01cSRoman Divacky if (Record.size() < 3) return Error("Invalid CE_BLOCKADDRESS record"); 1223f22ef01cSRoman Divacky const Type *FnTy = getTypeByID(Record[0]); 1224f22ef01cSRoman Divacky if (FnTy == 0) return Error("Invalid CE_BLOCKADDRESS record"); 1225f22ef01cSRoman Divacky Function *Fn = 1226f22ef01cSRoman Divacky dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); 1227f22ef01cSRoman Divacky if (Fn == 0) return Error("Invalid CE_BLOCKADDRESS record"); 1228f22ef01cSRoman Divacky 1229f22ef01cSRoman Divacky GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(), 1230f22ef01cSRoman Divacky Type::getInt8Ty(Context), 1231f22ef01cSRoman Divacky false, GlobalValue::InternalLinkage, 1232f22ef01cSRoman Divacky 0, ""); 1233f22ef01cSRoman Divacky BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef)); 1234f22ef01cSRoman Divacky V = FwdRef; 1235f22ef01cSRoman Divacky break; 1236f22ef01cSRoman Divacky } 1237f22ef01cSRoman Divacky } 1238f22ef01cSRoman Divacky 1239f22ef01cSRoman Divacky ValueList.AssignValue(V, NextCstNo); 1240f22ef01cSRoman Divacky ++NextCstNo; 1241f22ef01cSRoman Divacky } 1242f22ef01cSRoman Divacky 1243f22ef01cSRoman Divacky if (NextCstNo != ValueList.size()) 1244f22ef01cSRoman Divacky return Error("Invalid constant reference!"); 1245f22ef01cSRoman Divacky 1246f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 1247f22ef01cSRoman Divacky return Error("Error at end of constants block"); 1248f22ef01cSRoman Divacky 1249f22ef01cSRoman Divacky // Once all the constants have been read, go through and resolve forward 1250f22ef01cSRoman Divacky // references. 1251f22ef01cSRoman Divacky ValueList.ResolveConstantForwardRefs(); 1252f22ef01cSRoman Divacky return false; 1253f22ef01cSRoman Divacky } 1254f22ef01cSRoman Divacky 1255f22ef01cSRoman Divacky /// RememberAndSkipFunctionBody - When we see the block for a function body, 1256f22ef01cSRoman Divacky /// remember where it is and then skip it. This lets us lazily deserialize the 1257f22ef01cSRoman Divacky /// functions. 1258f22ef01cSRoman Divacky bool BitcodeReader::RememberAndSkipFunctionBody() { 1259f22ef01cSRoman Divacky // Get the function we are talking about. 1260f22ef01cSRoman Divacky if (FunctionsWithBodies.empty()) 1261f22ef01cSRoman Divacky return Error("Insufficient function protos"); 1262f22ef01cSRoman Divacky 1263f22ef01cSRoman Divacky Function *Fn = FunctionsWithBodies.back(); 1264f22ef01cSRoman Divacky FunctionsWithBodies.pop_back(); 1265f22ef01cSRoman Divacky 1266f22ef01cSRoman Divacky // Save the current stream state. 1267f22ef01cSRoman Divacky uint64_t CurBit = Stream.GetCurrentBitNo(); 1268f22ef01cSRoman Divacky DeferredFunctionInfo[Fn] = CurBit; 1269f22ef01cSRoman Divacky 1270f22ef01cSRoman Divacky // Skip over the function block for now. 1271f22ef01cSRoman Divacky if (Stream.SkipBlock()) 1272f22ef01cSRoman Divacky return Error("Malformed block record"); 1273f22ef01cSRoman Divacky return false; 1274f22ef01cSRoman Divacky } 1275f22ef01cSRoman Divacky 1276f22ef01cSRoman Divacky bool BitcodeReader::ParseModule() { 1277f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 1278f22ef01cSRoman Divacky return Error("Malformed block record"); 1279f22ef01cSRoman Divacky 1280f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1281f22ef01cSRoman Divacky std::vector<std::string> SectionTable; 1282f22ef01cSRoman Divacky std::vector<std::string> GCTable; 1283f22ef01cSRoman Divacky 1284f22ef01cSRoman Divacky // Read all the records for this module. 1285f22ef01cSRoman Divacky while (!Stream.AtEndOfStream()) { 1286f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 1287f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 1288f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 1289f22ef01cSRoman Divacky return Error("Error at end of module block"); 1290f22ef01cSRoman Divacky 1291f22ef01cSRoman Divacky // Patch the initializers for globals and aliases up. 1292f22ef01cSRoman Divacky ResolveGlobalAndAliasInits(); 1293f22ef01cSRoman Divacky if (!GlobalInits.empty() || !AliasInits.empty()) 1294f22ef01cSRoman Divacky return Error("Malformed global initializer set"); 1295f22ef01cSRoman Divacky if (!FunctionsWithBodies.empty()) 1296f22ef01cSRoman Divacky return Error("Too few function bodies found"); 1297f22ef01cSRoman Divacky 1298f22ef01cSRoman Divacky // Look for intrinsic functions which need to be upgraded at some point 1299f22ef01cSRoman Divacky for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); 1300f22ef01cSRoman Divacky FI != FE; ++FI) { 1301f22ef01cSRoman Divacky Function* NewFn; 1302f22ef01cSRoman Divacky if (UpgradeIntrinsicFunction(FI, NewFn)) 1303f22ef01cSRoman Divacky UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn)); 1304f22ef01cSRoman Divacky } 1305f22ef01cSRoman Divacky 1306e580952dSDimitry Andric // Look for global variables which need to be renamed. 1307e580952dSDimitry Andric for (Module::global_iterator 1308e580952dSDimitry Andric GI = TheModule->global_begin(), GE = TheModule->global_end(); 1309e580952dSDimitry Andric GI != GE; ++GI) 1310e580952dSDimitry Andric UpgradeGlobalVariable(GI); 1311e580952dSDimitry Andric 1312f22ef01cSRoman Divacky // Force deallocation of memory for these vectors to favor the client that 1313f22ef01cSRoman Divacky // want lazy deserialization. 1314f22ef01cSRoman Divacky std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); 1315f22ef01cSRoman Divacky std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); 1316f22ef01cSRoman Divacky std::vector<Function*>().swap(FunctionsWithBodies); 1317f22ef01cSRoman Divacky return false; 1318f22ef01cSRoman Divacky } 1319f22ef01cSRoman Divacky 1320f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 1321f22ef01cSRoman Divacky switch (Stream.ReadSubBlockID()) { 1322f22ef01cSRoman Divacky default: // Skip unknown content. 1323f22ef01cSRoman Divacky if (Stream.SkipBlock()) 1324f22ef01cSRoman Divacky return Error("Malformed block record"); 1325f22ef01cSRoman Divacky break; 1326f22ef01cSRoman Divacky case bitc::BLOCKINFO_BLOCK_ID: 1327f22ef01cSRoman Divacky if (Stream.ReadBlockInfoBlock()) 1328f22ef01cSRoman Divacky return Error("Malformed BlockInfoBlock"); 1329f22ef01cSRoman Divacky break; 1330f22ef01cSRoman Divacky case bitc::PARAMATTR_BLOCK_ID: 1331f22ef01cSRoman Divacky if (ParseAttributeBlock()) 1332f22ef01cSRoman Divacky return true; 1333f22ef01cSRoman Divacky break; 1334f22ef01cSRoman Divacky case bitc::TYPE_BLOCK_ID: 1335f22ef01cSRoman Divacky if (ParseTypeTable()) 1336f22ef01cSRoman Divacky return true; 1337f22ef01cSRoman Divacky break; 1338f22ef01cSRoman Divacky case bitc::TYPE_SYMTAB_BLOCK_ID: 1339f22ef01cSRoman Divacky if (ParseTypeSymbolTable()) 1340f22ef01cSRoman Divacky return true; 1341f22ef01cSRoman Divacky break; 1342f22ef01cSRoman Divacky case bitc::VALUE_SYMTAB_BLOCK_ID: 1343f22ef01cSRoman Divacky if (ParseValueSymbolTable()) 1344f22ef01cSRoman Divacky return true; 1345f22ef01cSRoman Divacky break; 1346f22ef01cSRoman Divacky case bitc::CONSTANTS_BLOCK_ID: 1347f22ef01cSRoman Divacky if (ParseConstants() || ResolveGlobalAndAliasInits()) 1348f22ef01cSRoman Divacky return true; 1349f22ef01cSRoman Divacky break; 1350f22ef01cSRoman Divacky case bitc::METADATA_BLOCK_ID: 1351f22ef01cSRoman Divacky if (ParseMetadata()) 1352f22ef01cSRoman Divacky return true; 1353f22ef01cSRoman Divacky break; 1354f22ef01cSRoman Divacky case bitc::FUNCTION_BLOCK_ID: 1355f22ef01cSRoman Divacky // If this is the first function body we've seen, reverse the 1356f22ef01cSRoman Divacky // FunctionsWithBodies list. 1357f22ef01cSRoman Divacky if (!HasReversedFunctionsWithBodies) { 1358f22ef01cSRoman Divacky std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 1359f22ef01cSRoman Divacky HasReversedFunctionsWithBodies = true; 1360f22ef01cSRoman Divacky } 1361f22ef01cSRoman Divacky 1362f22ef01cSRoman Divacky if (RememberAndSkipFunctionBody()) 1363f22ef01cSRoman Divacky return true; 1364f22ef01cSRoman Divacky break; 1365f22ef01cSRoman Divacky } 1366f22ef01cSRoman Divacky continue; 1367f22ef01cSRoman Divacky } 1368f22ef01cSRoman Divacky 1369f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 1370f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 1371f22ef01cSRoman Divacky continue; 1372f22ef01cSRoman Divacky } 1373f22ef01cSRoman Divacky 1374f22ef01cSRoman Divacky // Read a record. 1375f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 1376f22ef01cSRoman Divacky default: break; // Default behavior, ignore unknown content. 1377f22ef01cSRoman Divacky case bitc::MODULE_CODE_VERSION: // VERSION: [version#] 1378f22ef01cSRoman Divacky if (Record.size() < 1) 1379f22ef01cSRoman Divacky return Error("Malformed MODULE_CODE_VERSION"); 1380f22ef01cSRoman Divacky // Only version #0 is supported so far. 1381f22ef01cSRoman Divacky if (Record[0] != 0) 1382f22ef01cSRoman Divacky return Error("Unknown bitstream version!"); 1383f22ef01cSRoman Divacky break; 1384f22ef01cSRoman Divacky case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 1385f22ef01cSRoman Divacky std::string S; 1386f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1387f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_TRIPLE record"); 1388f22ef01cSRoman Divacky TheModule->setTargetTriple(S); 1389f22ef01cSRoman Divacky break; 1390f22ef01cSRoman Divacky } 1391f22ef01cSRoman Divacky case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] 1392f22ef01cSRoman Divacky std::string S; 1393f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1394f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_DATALAYOUT record"); 1395f22ef01cSRoman Divacky TheModule->setDataLayout(S); 1396f22ef01cSRoman Divacky break; 1397f22ef01cSRoman Divacky } 1398f22ef01cSRoman Divacky case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] 1399f22ef01cSRoman Divacky std::string S; 1400f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1401f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_ASM record"); 1402f22ef01cSRoman Divacky TheModule->setModuleInlineAsm(S); 1403f22ef01cSRoman Divacky break; 1404f22ef01cSRoman Divacky } 1405f22ef01cSRoman Divacky case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] 1406f22ef01cSRoman Divacky std::string S; 1407f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1408f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_DEPLIB record"); 1409f22ef01cSRoman Divacky TheModule->addLibrary(S); 1410f22ef01cSRoman Divacky break; 1411f22ef01cSRoman Divacky } 1412f22ef01cSRoman Divacky case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 1413f22ef01cSRoman Divacky std::string S; 1414f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1415f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_SECTIONNAME record"); 1416f22ef01cSRoman Divacky SectionTable.push_back(S); 1417f22ef01cSRoman Divacky break; 1418f22ef01cSRoman Divacky } 1419f22ef01cSRoman Divacky case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] 1420f22ef01cSRoman Divacky std::string S; 1421f22ef01cSRoman Divacky if (ConvertToString(Record, 0, S)) 1422f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_GCNAME record"); 1423f22ef01cSRoman Divacky GCTable.push_back(S); 1424f22ef01cSRoman Divacky break; 1425f22ef01cSRoman Divacky } 1426f22ef01cSRoman Divacky // GLOBALVAR: [pointer type, isconst, initid, 14272754fe60SDimitry Andric // linkage, alignment, section, visibility, threadlocal, 14282754fe60SDimitry Andric // unnamed_addr] 1429f22ef01cSRoman Divacky case bitc::MODULE_CODE_GLOBALVAR: { 1430f22ef01cSRoman Divacky if (Record.size() < 6) 1431f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_GLOBALVAR record"); 1432f22ef01cSRoman Divacky const Type *Ty = getTypeByID(Record[0]); 14332754fe60SDimitry Andric if (!Ty) return Error("Invalid MODULE_CODE_GLOBALVAR record"); 1434f22ef01cSRoman Divacky if (!Ty->isPointerTy()) 1435f22ef01cSRoman Divacky return Error("Global not a pointer type!"); 1436f22ef01cSRoman Divacky unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); 1437f22ef01cSRoman Divacky Ty = cast<PointerType>(Ty)->getElementType(); 1438f22ef01cSRoman Divacky 1439f22ef01cSRoman Divacky bool isConstant = Record[1]; 1440f22ef01cSRoman Divacky GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]); 1441f22ef01cSRoman Divacky unsigned Alignment = (1 << Record[4]) >> 1; 1442f22ef01cSRoman Divacky std::string Section; 1443f22ef01cSRoman Divacky if (Record[5]) { 1444f22ef01cSRoman Divacky if (Record[5]-1 >= SectionTable.size()) 1445f22ef01cSRoman Divacky return Error("Invalid section ID"); 1446f22ef01cSRoman Divacky Section = SectionTable[Record[5]-1]; 1447f22ef01cSRoman Divacky } 1448f22ef01cSRoman Divacky GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; 1449f22ef01cSRoman Divacky if (Record.size() > 6) 1450f22ef01cSRoman Divacky Visibility = GetDecodedVisibility(Record[6]); 1451f22ef01cSRoman Divacky bool isThreadLocal = false; 1452f22ef01cSRoman Divacky if (Record.size() > 7) 1453f22ef01cSRoman Divacky isThreadLocal = Record[7]; 1454f22ef01cSRoman Divacky 14552754fe60SDimitry Andric bool UnnamedAddr = false; 14562754fe60SDimitry Andric if (Record.size() > 8) 14572754fe60SDimitry Andric UnnamedAddr = Record[8]; 14582754fe60SDimitry Andric 1459f22ef01cSRoman Divacky GlobalVariable *NewGV = 1460f22ef01cSRoman Divacky new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0, 1461f22ef01cSRoman Divacky isThreadLocal, AddressSpace); 1462f22ef01cSRoman Divacky NewGV->setAlignment(Alignment); 1463f22ef01cSRoman Divacky if (!Section.empty()) 1464f22ef01cSRoman Divacky NewGV->setSection(Section); 1465f22ef01cSRoman Divacky NewGV->setVisibility(Visibility); 1466f22ef01cSRoman Divacky NewGV->setThreadLocal(isThreadLocal); 14672754fe60SDimitry Andric NewGV->setUnnamedAddr(UnnamedAddr); 1468f22ef01cSRoman Divacky 1469f22ef01cSRoman Divacky ValueList.push_back(NewGV); 1470f22ef01cSRoman Divacky 1471f22ef01cSRoman Divacky // Remember which value to use for the global initializer. 1472f22ef01cSRoman Divacky if (unsigned InitID = Record[2]) 1473f22ef01cSRoman Divacky GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); 1474f22ef01cSRoman Divacky break; 1475f22ef01cSRoman Divacky } 1476f22ef01cSRoman Divacky // FUNCTION: [type, callingconv, isproto, linkage, paramattr, 14772754fe60SDimitry Andric // alignment, section, visibility, gc, unnamed_addr] 1478f22ef01cSRoman Divacky case bitc::MODULE_CODE_FUNCTION: { 1479f22ef01cSRoman Divacky if (Record.size() < 8) 1480f22ef01cSRoman Divacky return Error("Invalid MODULE_CODE_FUNCTION record"); 1481f22ef01cSRoman Divacky const Type *Ty = getTypeByID(Record[0]); 14822754fe60SDimitry Andric if (!Ty) return Error("Invalid MODULE_CODE_FUNCTION record"); 1483f22ef01cSRoman Divacky if (!Ty->isPointerTy()) 1484f22ef01cSRoman Divacky return Error("Function not a pointer type!"); 1485f22ef01cSRoman Divacky const FunctionType *FTy = 1486f22ef01cSRoman Divacky dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); 1487f22ef01cSRoman Divacky if (!FTy) 1488f22ef01cSRoman Divacky return Error("Function not a pointer to function type!"); 1489f22ef01cSRoman Divacky 1490f22ef01cSRoman Divacky Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, 1491f22ef01cSRoman Divacky "", TheModule); 1492f22ef01cSRoman Divacky 1493f22ef01cSRoman Divacky Func->setCallingConv(static_cast<CallingConv::ID>(Record[1])); 1494f22ef01cSRoman Divacky bool isProto = Record[2]; 1495f22ef01cSRoman Divacky Func->setLinkage(GetDecodedLinkage(Record[3])); 1496f22ef01cSRoman Divacky Func->setAttributes(getAttributes(Record[4])); 1497f22ef01cSRoman Divacky 1498f22ef01cSRoman Divacky Func->setAlignment((1 << Record[5]) >> 1); 1499f22ef01cSRoman Divacky if (Record[6]) { 1500f22ef01cSRoman Divacky if (Record[6]-1 >= SectionTable.size()) 1501f22ef01cSRoman Divacky return Error("Invalid section ID"); 1502f22ef01cSRoman Divacky Func->setSection(SectionTable[Record[6]-1]); 1503f22ef01cSRoman Divacky } 1504f22ef01cSRoman Divacky Func->setVisibility(GetDecodedVisibility(Record[7])); 1505f22ef01cSRoman Divacky if (Record.size() > 8 && Record[8]) { 1506f22ef01cSRoman Divacky if (Record[8]-1 > GCTable.size()) 1507f22ef01cSRoman Divacky return Error("Invalid GC ID"); 1508f22ef01cSRoman Divacky Func->setGC(GCTable[Record[8]-1].c_str()); 1509f22ef01cSRoman Divacky } 15102754fe60SDimitry Andric bool UnnamedAddr = false; 15112754fe60SDimitry Andric if (Record.size() > 9) 15122754fe60SDimitry Andric UnnamedAddr = Record[9]; 15132754fe60SDimitry Andric Func->setUnnamedAddr(UnnamedAddr); 1514f22ef01cSRoman Divacky ValueList.push_back(Func); 1515f22ef01cSRoman Divacky 1516f22ef01cSRoman Divacky // If this is a function with a body, remember the prototype we are 1517f22ef01cSRoman Divacky // creating now, so that we can match up the body with them later. 1518f22ef01cSRoman Divacky if (!isProto) 1519f22ef01cSRoman Divacky FunctionsWithBodies.push_back(Func); 1520f22ef01cSRoman Divacky break; 1521f22ef01cSRoman Divacky } 1522f22ef01cSRoman Divacky // ALIAS: [alias type, aliasee val#, linkage] 1523f22ef01cSRoman Divacky // ALIAS: [alias type, aliasee val#, linkage, visibility] 1524f22ef01cSRoman Divacky case bitc::MODULE_CODE_ALIAS: { 1525f22ef01cSRoman Divacky if (Record.size() < 3) 1526f22ef01cSRoman Divacky return Error("Invalid MODULE_ALIAS record"); 1527f22ef01cSRoman Divacky const Type *Ty = getTypeByID(Record[0]); 15282754fe60SDimitry Andric if (!Ty) return Error("Invalid MODULE_ALIAS record"); 1529f22ef01cSRoman Divacky if (!Ty->isPointerTy()) 1530f22ef01cSRoman Divacky return Error("Function not a pointer type!"); 1531f22ef01cSRoman Divacky 1532f22ef01cSRoman Divacky GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]), 1533f22ef01cSRoman Divacky "", 0, TheModule); 1534f22ef01cSRoman Divacky // Old bitcode files didn't have visibility field. 1535f22ef01cSRoman Divacky if (Record.size() > 3) 1536f22ef01cSRoman Divacky NewGA->setVisibility(GetDecodedVisibility(Record[3])); 1537f22ef01cSRoman Divacky ValueList.push_back(NewGA); 1538f22ef01cSRoman Divacky AliasInits.push_back(std::make_pair(NewGA, Record[1])); 1539f22ef01cSRoman Divacky break; 1540f22ef01cSRoman Divacky } 1541f22ef01cSRoman Divacky /// MODULE_CODE_PURGEVALS: [numvals] 1542f22ef01cSRoman Divacky case bitc::MODULE_CODE_PURGEVALS: 1543f22ef01cSRoman Divacky // Trim down the value list to the specified size. 1544f22ef01cSRoman Divacky if (Record.size() < 1 || Record[0] > ValueList.size()) 1545f22ef01cSRoman Divacky return Error("Invalid MODULE_PURGEVALS record"); 1546f22ef01cSRoman Divacky ValueList.shrinkTo(Record[0]); 1547f22ef01cSRoman Divacky break; 1548f22ef01cSRoman Divacky } 1549f22ef01cSRoman Divacky Record.clear(); 1550f22ef01cSRoman Divacky } 1551f22ef01cSRoman Divacky 1552f22ef01cSRoman Divacky return Error("Premature end of bitstream"); 1553f22ef01cSRoman Divacky } 1554f22ef01cSRoman Divacky 1555f22ef01cSRoman Divacky bool BitcodeReader::ParseBitcodeInto(Module *M) { 1556f22ef01cSRoman Divacky TheModule = 0; 1557f22ef01cSRoman Divacky 1558f22ef01cSRoman Divacky unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); 1559f22ef01cSRoman Divacky unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); 1560f22ef01cSRoman Divacky 1561f22ef01cSRoman Divacky if (Buffer->getBufferSize() & 3) { 1562f22ef01cSRoman Divacky if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd)) 1563f22ef01cSRoman Divacky return Error("Invalid bitcode signature"); 1564f22ef01cSRoman Divacky else 1565f22ef01cSRoman Divacky return Error("Bitcode stream should be a multiple of 4 bytes in length"); 1566f22ef01cSRoman Divacky } 1567f22ef01cSRoman Divacky 1568f22ef01cSRoman Divacky // If we have a wrapper header, parse it and ignore the non-bc file contents. 1569f22ef01cSRoman Divacky // The magic number is 0x0B17C0DE stored in little endian. 1570f22ef01cSRoman Divacky if (isBitcodeWrapper(BufPtr, BufEnd)) 1571f22ef01cSRoman Divacky if (SkipBitcodeWrapperHeader(BufPtr, BufEnd)) 1572f22ef01cSRoman Divacky return Error("Invalid bitcode wrapper header"); 1573f22ef01cSRoman Divacky 1574f22ef01cSRoman Divacky StreamFile.init(BufPtr, BufEnd); 1575f22ef01cSRoman Divacky Stream.init(StreamFile); 1576f22ef01cSRoman Divacky 1577f22ef01cSRoman Divacky // Sniff for the signature. 1578f22ef01cSRoman Divacky if (Stream.Read(8) != 'B' || 1579f22ef01cSRoman Divacky Stream.Read(8) != 'C' || 1580f22ef01cSRoman Divacky Stream.Read(4) != 0x0 || 1581f22ef01cSRoman Divacky Stream.Read(4) != 0xC || 1582f22ef01cSRoman Divacky Stream.Read(4) != 0xE || 1583f22ef01cSRoman Divacky Stream.Read(4) != 0xD) 1584f22ef01cSRoman Divacky return Error("Invalid bitcode signature"); 1585f22ef01cSRoman Divacky 1586f22ef01cSRoman Divacky // We expect a number of well-defined blocks, though we don't necessarily 1587f22ef01cSRoman Divacky // need to understand them all. 1588f22ef01cSRoman Divacky while (!Stream.AtEndOfStream()) { 1589f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 1590f22ef01cSRoman Divacky 1591bd5abe19SDimitry Andric if (Code != bitc::ENTER_SUBBLOCK) { 1592bd5abe19SDimitry Andric 1593bd5abe19SDimitry Andric // The ranlib in xcode 4 will align archive members by appending newlines to the 1594bd5abe19SDimitry Andric // end of them. If this file size is a multiple of 4 but not 8, we have to read and 1595bd5abe19SDimitry Andric // ignore these final 4 bytes :-( 1596bd5abe19SDimitry Andric if (Stream.GetAbbrevIDWidth() == 2 && Code == 2 && 1597bd5abe19SDimitry Andric Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a && 1598bd5abe19SDimitry Andric Stream.AtEndOfStream()) 1599bd5abe19SDimitry Andric return false; 1600bd5abe19SDimitry Andric 1601f22ef01cSRoman Divacky return Error("Invalid record at top-level"); 1602bd5abe19SDimitry Andric } 1603f22ef01cSRoman Divacky 1604f22ef01cSRoman Divacky unsigned BlockID = Stream.ReadSubBlockID(); 1605f22ef01cSRoman Divacky 1606f22ef01cSRoman Divacky // We only know the MODULE subblock ID. 1607f22ef01cSRoman Divacky switch (BlockID) { 1608f22ef01cSRoman Divacky case bitc::BLOCKINFO_BLOCK_ID: 1609f22ef01cSRoman Divacky if (Stream.ReadBlockInfoBlock()) 1610f22ef01cSRoman Divacky return Error("Malformed BlockInfoBlock"); 1611f22ef01cSRoman Divacky break; 1612f22ef01cSRoman Divacky case bitc::MODULE_BLOCK_ID: 1613f22ef01cSRoman Divacky // Reject multiple MODULE_BLOCK's in a single bitstream. 1614f22ef01cSRoman Divacky if (TheModule) 1615f22ef01cSRoman Divacky return Error("Multiple MODULE_BLOCKs in same stream"); 1616f22ef01cSRoman Divacky TheModule = M; 1617f22ef01cSRoman Divacky if (ParseModule()) 1618f22ef01cSRoman Divacky return true; 1619f22ef01cSRoman Divacky break; 1620f22ef01cSRoman Divacky default: 1621f22ef01cSRoman Divacky if (Stream.SkipBlock()) 1622f22ef01cSRoman Divacky return Error("Malformed block record"); 1623f22ef01cSRoman Divacky break; 1624f22ef01cSRoman Divacky } 1625f22ef01cSRoman Divacky } 1626f22ef01cSRoman Divacky 1627f22ef01cSRoman Divacky return false; 1628f22ef01cSRoman Divacky } 1629f22ef01cSRoman Divacky 16302754fe60SDimitry Andric bool BitcodeReader::ParseModuleTriple(std::string &Triple) { 16312754fe60SDimitry Andric if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 16322754fe60SDimitry Andric return Error("Malformed block record"); 16332754fe60SDimitry Andric 16342754fe60SDimitry Andric SmallVector<uint64_t, 64> Record; 16352754fe60SDimitry Andric 16362754fe60SDimitry Andric // Read all the records for this module. 16372754fe60SDimitry Andric while (!Stream.AtEndOfStream()) { 16382754fe60SDimitry Andric unsigned Code = Stream.ReadCode(); 16392754fe60SDimitry Andric if (Code == bitc::END_BLOCK) { 16402754fe60SDimitry Andric if (Stream.ReadBlockEnd()) 16412754fe60SDimitry Andric return Error("Error at end of module block"); 16422754fe60SDimitry Andric 16432754fe60SDimitry Andric return false; 16442754fe60SDimitry Andric } 16452754fe60SDimitry Andric 16462754fe60SDimitry Andric if (Code == bitc::ENTER_SUBBLOCK) { 16472754fe60SDimitry Andric switch (Stream.ReadSubBlockID()) { 16482754fe60SDimitry Andric default: // Skip unknown content. 16492754fe60SDimitry Andric if (Stream.SkipBlock()) 16502754fe60SDimitry Andric return Error("Malformed block record"); 16512754fe60SDimitry Andric break; 16522754fe60SDimitry Andric } 16532754fe60SDimitry Andric continue; 16542754fe60SDimitry Andric } 16552754fe60SDimitry Andric 16562754fe60SDimitry Andric if (Code == bitc::DEFINE_ABBREV) { 16572754fe60SDimitry Andric Stream.ReadAbbrevRecord(); 16582754fe60SDimitry Andric continue; 16592754fe60SDimitry Andric } 16602754fe60SDimitry Andric 16612754fe60SDimitry Andric // Read a record. 16622754fe60SDimitry Andric switch (Stream.ReadRecord(Code, Record)) { 16632754fe60SDimitry Andric default: break; // Default behavior, ignore unknown content. 16642754fe60SDimitry Andric case bitc::MODULE_CODE_VERSION: // VERSION: [version#] 16652754fe60SDimitry Andric if (Record.size() < 1) 16662754fe60SDimitry Andric return Error("Malformed MODULE_CODE_VERSION"); 16672754fe60SDimitry Andric // Only version #0 is supported so far. 16682754fe60SDimitry Andric if (Record[0] != 0) 16692754fe60SDimitry Andric return Error("Unknown bitstream version!"); 16702754fe60SDimitry Andric break; 16712754fe60SDimitry Andric case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 16722754fe60SDimitry Andric std::string S; 16732754fe60SDimitry Andric if (ConvertToString(Record, 0, S)) 16742754fe60SDimitry Andric return Error("Invalid MODULE_CODE_TRIPLE record"); 16752754fe60SDimitry Andric Triple = S; 16762754fe60SDimitry Andric break; 16772754fe60SDimitry Andric } 16782754fe60SDimitry Andric } 16792754fe60SDimitry Andric Record.clear(); 16802754fe60SDimitry Andric } 16812754fe60SDimitry Andric 16822754fe60SDimitry Andric return Error("Premature end of bitstream"); 16832754fe60SDimitry Andric } 16842754fe60SDimitry Andric 16852754fe60SDimitry Andric bool BitcodeReader::ParseTriple(std::string &Triple) { 16862754fe60SDimitry Andric if (Buffer->getBufferSize() & 3) 16872754fe60SDimitry Andric return Error("Bitcode stream should be a multiple of 4 bytes in length"); 16882754fe60SDimitry Andric 16892754fe60SDimitry Andric unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); 16902754fe60SDimitry Andric unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); 16912754fe60SDimitry Andric 16922754fe60SDimitry Andric // If we have a wrapper header, parse it and ignore the non-bc file contents. 16932754fe60SDimitry Andric // The magic number is 0x0B17C0DE stored in little endian. 16942754fe60SDimitry Andric if (isBitcodeWrapper(BufPtr, BufEnd)) 16952754fe60SDimitry Andric if (SkipBitcodeWrapperHeader(BufPtr, BufEnd)) 16962754fe60SDimitry Andric return Error("Invalid bitcode wrapper header"); 16972754fe60SDimitry Andric 16982754fe60SDimitry Andric StreamFile.init(BufPtr, BufEnd); 16992754fe60SDimitry Andric Stream.init(StreamFile); 17002754fe60SDimitry Andric 17012754fe60SDimitry Andric // Sniff for the signature. 17022754fe60SDimitry Andric if (Stream.Read(8) != 'B' || 17032754fe60SDimitry Andric Stream.Read(8) != 'C' || 17042754fe60SDimitry Andric Stream.Read(4) != 0x0 || 17052754fe60SDimitry Andric Stream.Read(4) != 0xC || 17062754fe60SDimitry Andric Stream.Read(4) != 0xE || 17072754fe60SDimitry Andric Stream.Read(4) != 0xD) 17082754fe60SDimitry Andric return Error("Invalid bitcode signature"); 17092754fe60SDimitry Andric 17102754fe60SDimitry Andric // We expect a number of well-defined blocks, though we don't necessarily 17112754fe60SDimitry Andric // need to understand them all. 17122754fe60SDimitry Andric while (!Stream.AtEndOfStream()) { 17132754fe60SDimitry Andric unsigned Code = Stream.ReadCode(); 17142754fe60SDimitry Andric 17152754fe60SDimitry Andric if (Code != bitc::ENTER_SUBBLOCK) 17162754fe60SDimitry Andric return Error("Invalid record at top-level"); 17172754fe60SDimitry Andric 17182754fe60SDimitry Andric unsigned BlockID = Stream.ReadSubBlockID(); 17192754fe60SDimitry Andric 17202754fe60SDimitry Andric // We only know the MODULE subblock ID. 17212754fe60SDimitry Andric switch (BlockID) { 17222754fe60SDimitry Andric case bitc::MODULE_BLOCK_ID: 17232754fe60SDimitry Andric if (ParseModuleTriple(Triple)) 17242754fe60SDimitry Andric return true; 17252754fe60SDimitry Andric break; 17262754fe60SDimitry Andric default: 17272754fe60SDimitry Andric if (Stream.SkipBlock()) 17282754fe60SDimitry Andric return Error("Malformed block record"); 17292754fe60SDimitry Andric break; 17302754fe60SDimitry Andric } 17312754fe60SDimitry Andric } 17322754fe60SDimitry Andric 17332754fe60SDimitry Andric return false; 17342754fe60SDimitry Andric } 17352754fe60SDimitry Andric 1736f22ef01cSRoman Divacky /// ParseMetadataAttachment - Parse metadata attachments. 1737f22ef01cSRoman Divacky bool BitcodeReader::ParseMetadataAttachment() { 1738f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) 1739f22ef01cSRoman Divacky return Error("Malformed block record"); 1740f22ef01cSRoman Divacky 1741f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1742f22ef01cSRoman Divacky while(1) { 1743f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 1744f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 1745f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 1746f22ef01cSRoman Divacky return Error("Error at end of PARAMATTR block"); 1747f22ef01cSRoman Divacky break; 1748f22ef01cSRoman Divacky } 1749f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 1750f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 1751f22ef01cSRoman Divacky continue; 1752f22ef01cSRoman Divacky } 1753f22ef01cSRoman Divacky // Read a metadata attachment record. 1754f22ef01cSRoman Divacky Record.clear(); 1755f22ef01cSRoman Divacky switch (Stream.ReadRecord(Code, Record)) { 1756f22ef01cSRoman Divacky default: // Default behavior: ignore. 1757f22ef01cSRoman Divacky break; 1758e580952dSDimitry Andric // FIXME: Remove in LLVM 3.0. 1759e580952dSDimitry Andric case bitc::METADATA_ATTACHMENT: 1760e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 1761e580952dSDimitry Andric case bitc::METADATA_ATTACHMENT2: { 1762f22ef01cSRoman Divacky unsigned RecordLength = Record.size(); 1763f22ef01cSRoman Divacky if (Record.empty() || (RecordLength - 1) % 2 == 1) 1764f22ef01cSRoman Divacky return Error ("Invalid METADATA_ATTACHMENT reader!"); 1765f22ef01cSRoman Divacky Instruction *Inst = InstructionList[Record[0]]; 1766f22ef01cSRoman Divacky for (unsigned i = 1; i != RecordLength; i = i+2) { 1767f22ef01cSRoman Divacky unsigned Kind = Record[i]; 1768e580952dSDimitry Andric DenseMap<unsigned, unsigned>::iterator I = 1769e580952dSDimitry Andric MDKindMap.find(Kind); 1770e580952dSDimitry Andric if (I == MDKindMap.end()) 1771e580952dSDimitry Andric return Error("Invalid metadata kind ID"); 1772f22ef01cSRoman Divacky Value *Node = MDValueList.getValueFwdRef(Record[i+1]); 1773e580952dSDimitry Andric Inst->setMetadata(I->second, cast<MDNode>(Node)); 1774f22ef01cSRoman Divacky } 1775f22ef01cSRoman Divacky break; 1776f22ef01cSRoman Divacky } 1777f22ef01cSRoman Divacky } 1778f22ef01cSRoman Divacky } 1779f22ef01cSRoman Divacky return false; 1780f22ef01cSRoman Divacky } 1781f22ef01cSRoman Divacky 1782f22ef01cSRoman Divacky /// ParseFunctionBody - Lazily parse the specified function body block. 1783f22ef01cSRoman Divacky bool BitcodeReader::ParseFunctionBody(Function *F) { 1784f22ef01cSRoman Divacky if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) 1785f22ef01cSRoman Divacky return Error("Malformed block record"); 1786f22ef01cSRoman Divacky 1787f22ef01cSRoman Divacky InstructionList.clear(); 1788f22ef01cSRoman Divacky unsigned ModuleValueListSize = ValueList.size(); 1789e580952dSDimitry Andric unsigned ModuleMDValueListSize = MDValueList.size(); 1790f22ef01cSRoman Divacky 1791f22ef01cSRoman Divacky // Add all the function arguments to the value table. 1792f22ef01cSRoman Divacky for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) 1793f22ef01cSRoman Divacky ValueList.push_back(I); 1794f22ef01cSRoman Divacky 1795f22ef01cSRoman Divacky unsigned NextValueNo = ValueList.size(); 1796f22ef01cSRoman Divacky BasicBlock *CurBB = 0; 1797f22ef01cSRoman Divacky unsigned CurBBNo = 0; 1798f22ef01cSRoman Divacky 1799f22ef01cSRoman Divacky DebugLoc LastLoc; 1800f22ef01cSRoman Divacky 1801f22ef01cSRoman Divacky // Read all the records. 1802f22ef01cSRoman Divacky SmallVector<uint64_t, 64> Record; 1803f22ef01cSRoman Divacky while (1) { 1804f22ef01cSRoman Divacky unsigned Code = Stream.ReadCode(); 1805f22ef01cSRoman Divacky if (Code == bitc::END_BLOCK) { 1806f22ef01cSRoman Divacky if (Stream.ReadBlockEnd()) 1807f22ef01cSRoman Divacky return Error("Error at end of function block"); 1808f22ef01cSRoman Divacky break; 1809f22ef01cSRoman Divacky } 1810f22ef01cSRoman Divacky 1811f22ef01cSRoman Divacky if (Code == bitc::ENTER_SUBBLOCK) { 1812f22ef01cSRoman Divacky switch (Stream.ReadSubBlockID()) { 1813f22ef01cSRoman Divacky default: // Skip unknown content. 1814f22ef01cSRoman Divacky if (Stream.SkipBlock()) 1815f22ef01cSRoman Divacky return Error("Malformed block record"); 1816f22ef01cSRoman Divacky break; 1817f22ef01cSRoman Divacky case bitc::CONSTANTS_BLOCK_ID: 1818f22ef01cSRoman Divacky if (ParseConstants()) return true; 1819f22ef01cSRoman Divacky NextValueNo = ValueList.size(); 1820f22ef01cSRoman Divacky break; 1821f22ef01cSRoman Divacky case bitc::VALUE_SYMTAB_BLOCK_ID: 1822f22ef01cSRoman Divacky if (ParseValueSymbolTable()) return true; 1823f22ef01cSRoman Divacky break; 1824f22ef01cSRoman Divacky case bitc::METADATA_ATTACHMENT_ID: 1825f22ef01cSRoman Divacky if (ParseMetadataAttachment()) return true; 1826f22ef01cSRoman Divacky break; 1827f22ef01cSRoman Divacky case bitc::METADATA_BLOCK_ID: 1828f22ef01cSRoman Divacky if (ParseMetadata()) return true; 1829f22ef01cSRoman Divacky break; 1830f22ef01cSRoman Divacky } 1831f22ef01cSRoman Divacky continue; 1832f22ef01cSRoman Divacky } 1833f22ef01cSRoman Divacky 1834f22ef01cSRoman Divacky if (Code == bitc::DEFINE_ABBREV) { 1835f22ef01cSRoman Divacky Stream.ReadAbbrevRecord(); 1836f22ef01cSRoman Divacky continue; 1837f22ef01cSRoman Divacky } 1838f22ef01cSRoman Divacky 1839f22ef01cSRoman Divacky // Read a record. 1840f22ef01cSRoman Divacky Record.clear(); 1841f22ef01cSRoman Divacky Instruction *I = 0; 1842f22ef01cSRoman Divacky unsigned BitCode = Stream.ReadRecord(Code, Record); 1843f22ef01cSRoman Divacky switch (BitCode) { 1844f22ef01cSRoman Divacky default: // Default behavior: reject 1845f22ef01cSRoman Divacky return Error("Unknown instruction"); 1846f22ef01cSRoman Divacky case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks] 1847f22ef01cSRoman Divacky if (Record.size() < 1 || Record[0] == 0) 1848f22ef01cSRoman Divacky return Error("Invalid DECLAREBLOCKS record"); 1849f22ef01cSRoman Divacky // Create all the basic blocks for the function. 1850f22ef01cSRoman Divacky FunctionBBs.resize(Record[0]); 1851f22ef01cSRoman Divacky for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) 1852f22ef01cSRoman Divacky FunctionBBs[i] = BasicBlock::Create(Context, "", F); 1853f22ef01cSRoman Divacky CurBB = FunctionBBs[0]; 1854f22ef01cSRoman Divacky continue; 1855f22ef01cSRoman Divacky 1856f22ef01cSRoman Divacky case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN 1857f22ef01cSRoman Divacky // This record indicates that the last instruction is at the same 1858f22ef01cSRoman Divacky // location as the previous instruction with a location. 1859f22ef01cSRoman Divacky I = 0; 1860f22ef01cSRoman Divacky 1861f22ef01cSRoman Divacky // Get the last instruction emitted. 1862f22ef01cSRoman Divacky if (CurBB && !CurBB->empty()) 1863f22ef01cSRoman Divacky I = &CurBB->back(); 1864f22ef01cSRoman Divacky else if (CurBBNo && FunctionBBs[CurBBNo-1] && 1865f22ef01cSRoman Divacky !FunctionBBs[CurBBNo-1]->empty()) 1866f22ef01cSRoman Divacky I = &FunctionBBs[CurBBNo-1]->back(); 1867f22ef01cSRoman Divacky 1868f22ef01cSRoman Divacky if (I == 0) return Error("Invalid DEBUG_LOC_AGAIN record"); 1869f22ef01cSRoman Divacky I->setDebugLoc(LastLoc); 1870f22ef01cSRoman Divacky I = 0; 1871f22ef01cSRoman Divacky continue; 1872f22ef01cSRoman Divacky 1873e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 1874e580952dSDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC: 1875e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 1876e580952dSDimitry Andric case bitc::FUNC_CODE_DEBUG_LOC2: { // DEBUG_LOC: [line, col, scope, ia] 1877f22ef01cSRoman Divacky I = 0; // Get the last instruction emitted. 1878f22ef01cSRoman Divacky if (CurBB && !CurBB->empty()) 1879f22ef01cSRoman Divacky I = &CurBB->back(); 1880f22ef01cSRoman Divacky else if (CurBBNo && FunctionBBs[CurBBNo-1] && 1881f22ef01cSRoman Divacky !FunctionBBs[CurBBNo-1]->empty()) 1882f22ef01cSRoman Divacky I = &FunctionBBs[CurBBNo-1]->back(); 1883f22ef01cSRoman Divacky if (I == 0 || Record.size() < 4) 1884f22ef01cSRoman Divacky return Error("Invalid FUNC_CODE_DEBUG_LOC record"); 1885f22ef01cSRoman Divacky 1886f22ef01cSRoman Divacky unsigned Line = Record[0], Col = Record[1]; 1887f22ef01cSRoman Divacky unsigned ScopeID = Record[2], IAID = Record[3]; 1888f22ef01cSRoman Divacky 1889f22ef01cSRoman Divacky MDNode *Scope = 0, *IA = 0; 1890f22ef01cSRoman Divacky if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1)); 1891f22ef01cSRoman Divacky if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1)); 1892f22ef01cSRoman Divacky LastLoc = DebugLoc::get(Line, Col, Scope, IA); 1893f22ef01cSRoman Divacky I->setDebugLoc(LastLoc); 1894f22ef01cSRoman Divacky I = 0; 1895f22ef01cSRoman Divacky continue; 1896f22ef01cSRoman Divacky } 1897f22ef01cSRoman Divacky 1898f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] 1899f22ef01cSRoman Divacky unsigned OpNum = 0; 1900f22ef01cSRoman Divacky Value *LHS, *RHS; 1901f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 1902f22ef01cSRoman Divacky getValue(Record, OpNum, LHS->getType(), RHS) || 1903f22ef01cSRoman Divacky OpNum+1 > Record.size()) 1904f22ef01cSRoman Divacky return Error("Invalid BINOP record"); 1905f22ef01cSRoman Divacky 1906f22ef01cSRoman Divacky int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); 1907f22ef01cSRoman Divacky if (Opc == -1) return Error("Invalid BINOP record"); 1908f22ef01cSRoman Divacky I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 1909f22ef01cSRoman Divacky InstructionList.push_back(I); 1910f22ef01cSRoman Divacky if (OpNum < Record.size()) { 1911f22ef01cSRoman Divacky if (Opc == Instruction::Add || 1912f22ef01cSRoman Divacky Opc == Instruction::Sub || 19132754fe60SDimitry Andric Opc == Instruction::Mul || 19142754fe60SDimitry Andric Opc == Instruction::Shl) { 1915f22ef01cSRoman Divacky if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 1916f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setHasNoSignedWrap(true); 1917f22ef01cSRoman Divacky if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 1918f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); 19192754fe60SDimitry Andric } else if (Opc == Instruction::SDiv || 19202754fe60SDimitry Andric Opc == Instruction::UDiv || 19212754fe60SDimitry Andric Opc == Instruction::LShr || 19222754fe60SDimitry Andric Opc == Instruction::AShr) { 19232754fe60SDimitry Andric if (Record[OpNum] & (1 << bitc::PEO_EXACT)) 1924f22ef01cSRoman Divacky cast<BinaryOperator>(I)->setIsExact(true); 1925f22ef01cSRoman Divacky } 1926f22ef01cSRoman Divacky } 1927f22ef01cSRoman Divacky break; 1928f22ef01cSRoman Divacky } 1929f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] 1930f22ef01cSRoman Divacky unsigned OpNum = 0; 1931f22ef01cSRoman Divacky Value *Op; 1932f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 1933f22ef01cSRoman Divacky OpNum+2 != Record.size()) 1934f22ef01cSRoman Divacky return Error("Invalid CAST record"); 1935f22ef01cSRoman Divacky 1936f22ef01cSRoman Divacky const Type *ResTy = getTypeByID(Record[OpNum]); 1937f22ef01cSRoman Divacky int Opc = GetDecodedCastOpcode(Record[OpNum+1]); 1938f22ef01cSRoman Divacky if (Opc == -1 || ResTy == 0) 1939f22ef01cSRoman Divacky return Error("Invalid CAST record"); 1940f22ef01cSRoman Divacky I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy); 1941f22ef01cSRoman Divacky InstructionList.push_back(I); 1942f22ef01cSRoman Divacky break; 1943f22ef01cSRoman Divacky } 1944f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INBOUNDS_GEP: 1945f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] 1946f22ef01cSRoman Divacky unsigned OpNum = 0; 1947f22ef01cSRoman Divacky Value *BasePtr; 1948f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) 1949f22ef01cSRoman Divacky return Error("Invalid GEP record"); 1950f22ef01cSRoman Divacky 1951f22ef01cSRoman Divacky SmallVector<Value*, 16> GEPIdx; 1952f22ef01cSRoman Divacky while (OpNum != Record.size()) { 1953f22ef01cSRoman Divacky Value *Op; 1954f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 1955f22ef01cSRoman Divacky return Error("Invalid GEP record"); 1956f22ef01cSRoman Divacky GEPIdx.push_back(Op); 1957f22ef01cSRoman Divacky } 1958f22ef01cSRoman Divacky 1959f22ef01cSRoman Divacky I = GetElementPtrInst::Create(BasePtr, GEPIdx.begin(), GEPIdx.end()); 1960f22ef01cSRoman Divacky InstructionList.push_back(I); 1961f22ef01cSRoman Divacky if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP) 1962f22ef01cSRoman Divacky cast<GetElementPtrInst>(I)->setIsInBounds(true); 1963f22ef01cSRoman Divacky break; 1964f22ef01cSRoman Divacky } 1965f22ef01cSRoman Divacky 1966f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_EXTRACTVAL: { 1967f22ef01cSRoman Divacky // EXTRACTVAL: [opty, opval, n x indices] 1968f22ef01cSRoman Divacky unsigned OpNum = 0; 1969f22ef01cSRoman Divacky Value *Agg; 1970f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 1971f22ef01cSRoman Divacky return Error("Invalid EXTRACTVAL record"); 1972f22ef01cSRoman Divacky 1973f22ef01cSRoman Divacky SmallVector<unsigned, 4> EXTRACTVALIdx; 1974f22ef01cSRoman Divacky for (unsigned RecSize = Record.size(); 1975f22ef01cSRoman Divacky OpNum != RecSize; ++OpNum) { 1976f22ef01cSRoman Divacky uint64_t Index = Record[OpNum]; 1977f22ef01cSRoman Divacky if ((unsigned)Index != Index) 1978f22ef01cSRoman Divacky return Error("Invalid EXTRACTVAL index"); 1979f22ef01cSRoman Divacky EXTRACTVALIdx.push_back((unsigned)Index); 1980f22ef01cSRoman Divacky } 1981f22ef01cSRoman Divacky 1982f22ef01cSRoman Divacky I = ExtractValueInst::Create(Agg, 1983f22ef01cSRoman Divacky EXTRACTVALIdx.begin(), EXTRACTVALIdx.end()); 1984f22ef01cSRoman Divacky InstructionList.push_back(I); 1985f22ef01cSRoman Divacky break; 1986f22ef01cSRoman Divacky } 1987f22ef01cSRoman Divacky 1988f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INSERTVAL: { 1989f22ef01cSRoman Divacky // INSERTVAL: [opty, opval, opty, opval, n x indices] 1990f22ef01cSRoman Divacky unsigned OpNum = 0; 1991f22ef01cSRoman Divacky Value *Agg; 1992f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 1993f22ef01cSRoman Divacky return Error("Invalid INSERTVAL record"); 1994f22ef01cSRoman Divacky Value *Val; 1995f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Val)) 1996f22ef01cSRoman Divacky return Error("Invalid INSERTVAL record"); 1997f22ef01cSRoman Divacky 1998f22ef01cSRoman Divacky SmallVector<unsigned, 4> INSERTVALIdx; 1999f22ef01cSRoman Divacky for (unsigned RecSize = Record.size(); 2000f22ef01cSRoman Divacky OpNum != RecSize; ++OpNum) { 2001f22ef01cSRoman Divacky uint64_t Index = Record[OpNum]; 2002f22ef01cSRoman Divacky if ((unsigned)Index != Index) 2003f22ef01cSRoman Divacky return Error("Invalid INSERTVAL index"); 2004f22ef01cSRoman Divacky INSERTVALIdx.push_back((unsigned)Index); 2005f22ef01cSRoman Divacky } 2006f22ef01cSRoman Divacky 2007f22ef01cSRoman Divacky I = InsertValueInst::Create(Agg, Val, 2008f22ef01cSRoman Divacky INSERTVALIdx.begin(), INSERTVALIdx.end()); 2009f22ef01cSRoman Divacky InstructionList.push_back(I); 2010f22ef01cSRoman Divacky break; 2011f22ef01cSRoman Divacky } 2012f22ef01cSRoman Divacky 2013f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] 2014f22ef01cSRoman Divacky // obsolete form of select 2015f22ef01cSRoman Divacky // handles select i1 ... in old bitcode 2016f22ef01cSRoman Divacky unsigned OpNum = 0; 2017f22ef01cSRoman Divacky Value *TrueVal, *FalseVal, *Cond; 2018f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 2019f22ef01cSRoman Divacky getValue(Record, OpNum, TrueVal->getType(), FalseVal) || 2020f22ef01cSRoman Divacky getValue(Record, OpNum, Type::getInt1Ty(Context), Cond)) 2021f22ef01cSRoman Divacky return Error("Invalid SELECT record"); 2022f22ef01cSRoman Divacky 2023f22ef01cSRoman Divacky I = SelectInst::Create(Cond, TrueVal, FalseVal); 2024f22ef01cSRoman Divacky InstructionList.push_back(I); 2025f22ef01cSRoman Divacky break; 2026f22ef01cSRoman Divacky } 2027f22ef01cSRoman Divacky 2028f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] 2029f22ef01cSRoman Divacky // new form of select 2030f22ef01cSRoman Divacky // handles select i1 or select [N x i1] 2031f22ef01cSRoman Divacky unsigned OpNum = 0; 2032f22ef01cSRoman Divacky Value *TrueVal, *FalseVal, *Cond; 2033f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 2034f22ef01cSRoman Divacky getValue(Record, OpNum, TrueVal->getType(), FalseVal) || 2035f22ef01cSRoman Divacky getValueTypePair(Record, OpNum, NextValueNo, Cond)) 2036f22ef01cSRoman Divacky return Error("Invalid SELECT record"); 2037f22ef01cSRoman Divacky 2038f22ef01cSRoman Divacky // select condition can be either i1 or [N x i1] 2039f22ef01cSRoman Divacky if (const VectorType* vector_type = 2040f22ef01cSRoman Divacky dyn_cast<const VectorType>(Cond->getType())) { 2041f22ef01cSRoman Divacky // expect <n x i1> 2042f22ef01cSRoman Divacky if (vector_type->getElementType() != Type::getInt1Ty(Context)) 2043f22ef01cSRoman Divacky return Error("Invalid SELECT condition type"); 2044f22ef01cSRoman Divacky } else { 2045f22ef01cSRoman Divacky // expect i1 2046f22ef01cSRoman Divacky if (Cond->getType() != Type::getInt1Ty(Context)) 2047f22ef01cSRoman Divacky return Error("Invalid SELECT condition type"); 2048f22ef01cSRoman Divacky } 2049f22ef01cSRoman Divacky 2050f22ef01cSRoman Divacky I = SelectInst::Create(Cond, TrueVal, FalseVal); 2051f22ef01cSRoman Divacky InstructionList.push_back(I); 2052f22ef01cSRoman Divacky break; 2053f22ef01cSRoman Divacky } 2054f22ef01cSRoman Divacky 2055f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] 2056f22ef01cSRoman Divacky unsigned OpNum = 0; 2057f22ef01cSRoman Divacky Value *Vec, *Idx; 2058f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || 2059f22ef01cSRoman Divacky getValue(Record, OpNum, Type::getInt32Ty(Context), Idx)) 2060f22ef01cSRoman Divacky return Error("Invalid EXTRACTELT record"); 2061f22ef01cSRoman Divacky I = ExtractElementInst::Create(Vec, Idx); 2062f22ef01cSRoman Divacky InstructionList.push_back(I); 2063f22ef01cSRoman Divacky break; 2064f22ef01cSRoman Divacky } 2065f22ef01cSRoman Divacky 2066f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] 2067f22ef01cSRoman Divacky unsigned OpNum = 0; 2068f22ef01cSRoman Divacky Value *Vec, *Elt, *Idx; 2069f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || 2070f22ef01cSRoman Divacky getValue(Record, OpNum, 2071f22ef01cSRoman Divacky cast<VectorType>(Vec->getType())->getElementType(), Elt) || 2072f22ef01cSRoman Divacky getValue(Record, OpNum, Type::getInt32Ty(Context), Idx)) 2073f22ef01cSRoman Divacky return Error("Invalid INSERTELT record"); 2074f22ef01cSRoman Divacky I = InsertElementInst::Create(Vec, Elt, Idx); 2075f22ef01cSRoman Divacky InstructionList.push_back(I); 2076f22ef01cSRoman Divacky break; 2077f22ef01cSRoman Divacky } 2078f22ef01cSRoman Divacky 2079f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] 2080f22ef01cSRoman Divacky unsigned OpNum = 0; 2081f22ef01cSRoman Divacky Value *Vec1, *Vec2, *Mask; 2082f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || 2083f22ef01cSRoman Divacky getValue(Record, OpNum, Vec1->getType(), Vec2)) 2084f22ef01cSRoman Divacky return Error("Invalid SHUFFLEVEC record"); 2085f22ef01cSRoman Divacky 2086f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) 2087f22ef01cSRoman Divacky return Error("Invalid SHUFFLEVEC record"); 2088f22ef01cSRoman Divacky I = new ShuffleVectorInst(Vec1, Vec2, Mask); 2089f22ef01cSRoman Divacky InstructionList.push_back(I); 2090f22ef01cSRoman Divacky break; 2091f22ef01cSRoman Divacky } 2092f22ef01cSRoman Divacky 2093f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] 2094f22ef01cSRoman Divacky // Old form of ICmp/FCmp returning bool 2095f22ef01cSRoman Divacky // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were 2096f22ef01cSRoman Divacky // both legal on vectors but had different behaviour. 2097f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] 2098f22ef01cSRoman Divacky // FCmp/ICmp returning bool or vector of bool 2099f22ef01cSRoman Divacky 2100f22ef01cSRoman Divacky unsigned OpNum = 0; 2101f22ef01cSRoman Divacky Value *LHS, *RHS; 2102f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 2103f22ef01cSRoman Divacky getValue(Record, OpNum, LHS->getType(), RHS) || 2104f22ef01cSRoman Divacky OpNum+1 != Record.size()) 2105f22ef01cSRoman Divacky return Error("Invalid CMP record"); 2106f22ef01cSRoman Divacky 2107f22ef01cSRoman Divacky if (LHS->getType()->isFPOrFPVectorTy()) 2108f22ef01cSRoman Divacky I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); 2109f22ef01cSRoman Divacky else 2110f22ef01cSRoman Divacky I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS); 2111f22ef01cSRoman Divacky InstructionList.push_back(I); 2112f22ef01cSRoman Divacky break; 2113f22ef01cSRoman Divacky } 2114f22ef01cSRoman Divacky 2115f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_GETRESULT: { // GETRESULT: [ty, val, n] 2116f22ef01cSRoman Divacky if (Record.size() != 2) 2117f22ef01cSRoman Divacky return Error("Invalid GETRESULT record"); 2118f22ef01cSRoman Divacky unsigned OpNum = 0; 2119f22ef01cSRoman Divacky Value *Op; 2120f22ef01cSRoman Divacky getValueTypePair(Record, OpNum, NextValueNo, Op); 2121f22ef01cSRoman Divacky unsigned Index = Record[1]; 2122f22ef01cSRoman Divacky I = ExtractValueInst::Create(Op, Index); 2123f22ef01cSRoman Divacky InstructionList.push_back(I); 2124f22ef01cSRoman Divacky break; 2125f22ef01cSRoman Divacky } 2126f22ef01cSRoman Divacky 2127f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] 2128f22ef01cSRoman Divacky { 2129f22ef01cSRoman Divacky unsigned Size = Record.size(); 2130f22ef01cSRoman Divacky if (Size == 0) { 2131f22ef01cSRoman Divacky I = ReturnInst::Create(Context); 2132f22ef01cSRoman Divacky InstructionList.push_back(I); 2133f22ef01cSRoman Divacky break; 2134f22ef01cSRoman Divacky } 2135f22ef01cSRoman Divacky 2136f22ef01cSRoman Divacky unsigned OpNum = 0; 2137f22ef01cSRoman Divacky SmallVector<Value *,4> Vs; 2138f22ef01cSRoman Divacky do { 2139f22ef01cSRoman Divacky Value *Op = NULL; 2140f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 2141f22ef01cSRoman Divacky return Error("Invalid RET record"); 2142f22ef01cSRoman Divacky Vs.push_back(Op); 2143f22ef01cSRoman Divacky } while(OpNum != Record.size()); 2144f22ef01cSRoman Divacky 2145f22ef01cSRoman Divacky const Type *ReturnType = F->getReturnType(); 2146e580952dSDimitry Andric // Handle multiple return values. FIXME: Remove in LLVM 3.0. 2147f22ef01cSRoman Divacky if (Vs.size() > 1 || 2148f22ef01cSRoman Divacky (ReturnType->isStructTy() && 2149f22ef01cSRoman Divacky (Vs.empty() || Vs[0]->getType() != ReturnType))) { 2150f22ef01cSRoman Divacky Value *RV = UndefValue::get(ReturnType); 2151f22ef01cSRoman Divacky for (unsigned i = 0, e = Vs.size(); i != e; ++i) { 2152f22ef01cSRoman Divacky I = InsertValueInst::Create(RV, Vs[i], i, "mrv"); 2153f22ef01cSRoman Divacky InstructionList.push_back(I); 2154f22ef01cSRoman Divacky CurBB->getInstList().push_back(I); 2155f22ef01cSRoman Divacky ValueList.AssignValue(I, NextValueNo++); 2156f22ef01cSRoman Divacky RV = I; 2157f22ef01cSRoman Divacky } 2158f22ef01cSRoman Divacky I = ReturnInst::Create(Context, RV); 2159f22ef01cSRoman Divacky InstructionList.push_back(I); 2160f22ef01cSRoman Divacky break; 2161f22ef01cSRoman Divacky } 2162f22ef01cSRoman Divacky 2163f22ef01cSRoman Divacky I = ReturnInst::Create(Context, Vs[0]); 2164f22ef01cSRoman Divacky InstructionList.push_back(I); 2165f22ef01cSRoman Divacky break; 2166f22ef01cSRoman Divacky } 2167f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] 2168f22ef01cSRoman Divacky if (Record.size() != 1 && Record.size() != 3) 2169f22ef01cSRoman Divacky return Error("Invalid BR record"); 2170f22ef01cSRoman Divacky BasicBlock *TrueDest = getBasicBlock(Record[0]); 2171f22ef01cSRoman Divacky if (TrueDest == 0) 2172f22ef01cSRoman Divacky return Error("Invalid BR record"); 2173f22ef01cSRoman Divacky 2174f22ef01cSRoman Divacky if (Record.size() == 1) { 2175f22ef01cSRoman Divacky I = BranchInst::Create(TrueDest); 2176f22ef01cSRoman Divacky InstructionList.push_back(I); 2177f22ef01cSRoman Divacky } 2178f22ef01cSRoman Divacky else { 2179f22ef01cSRoman Divacky BasicBlock *FalseDest = getBasicBlock(Record[1]); 2180f22ef01cSRoman Divacky Value *Cond = getFnValueByID(Record[2], Type::getInt1Ty(Context)); 2181f22ef01cSRoman Divacky if (FalseDest == 0 || Cond == 0) 2182f22ef01cSRoman Divacky return Error("Invalid BR record"); 2183f22ef01cSRoman Divacky I = BranchInst::Create(TrueDest, FalseDest, Cond); 2184f22ef01cSRoman Divacky InstructionList.push_back(I); 2185f22ef01cSRoman Divacky } 2186f22ef01cSRoman Divacky break; 2187f22ef01cSRoman Divacky } 2188f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] 2189f22ef01cSRoman Divacky if (Record.size() < 3 || (Record.size() & 1) == 0) 2190f22ef01cSRoman Divacky return Error("Invalid SWITCH record"); 2191f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[0]); 2192f22ef01cSRoman Divacky Value *Cond = getFnValueByID(Record[1], OpTy); 2193f22ef01cSRoman Divacky BasicBlock *Default = getBasicBlock(Record[2]); 2194f22ef01cSRoman Divacky if (OpTy == 0 || Cond == 0 || Default == 0) 2195f22ef01cSRoman Divacky return Error("Invalid SWITCH record"); 2196f22ef01cSRoman Divacky unsigned NumCases = (Record.size()-3)/2; 2197f22ef01cSRoman Divacky SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 2198f22ef01cSRoman Divacky InstructionList.push_back(SI); 2199f22ef01cSRoman Divacky for (unsigned i = 0, e = NumCases; i != e; ++i) { 2200f22ef01cSRoman Divacky ConstantInt *CaseVal = 2201f22ef01cSRoman Divacky dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); 2202f22ef01cSRoman Divacky BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); 2203f22ef01cSRoman Divacky if (CaseVal == 0 || DestBB == 0) { 2204f22ef01cSRoman Divacky delete SI; 2205f22ef01cSRoman Divacky return Error("Invalid SWITCH record!"); 2206f22ef01cSRoman Divacky } 2207f22ef01cSRoman Divacky SI->addCase(CaseVal, DestBB); 2208f22ef01cSRoman Divacky } 2209f22ef01cSRoman Divacky I = SI; 2210f22ef01cSRoman Divacky break; 2211f22ef01cSRoman Divacky } 2212f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] 2213f22ef01cSRoman Divacky if (Record.size() < 2) 2214f22ef01cSRoman Divacky return Error("Invalid INDIRECTBR record"); 2215f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[0]); 2216f22ef01cSRoman Divacky Value *Address = getFnValueByID(Record[1], OpTy); 2217f22ef01cSRoman Divacky if (OpTy == 0 || Address == 0) 2218f22ef01cSRoman Divacky return Error("Invalid INDIRECTBR record"); 2219f22ef01cSRoman Divacky unsigned NumDests = Record.size()-2; 2220f22ef01cSRoman Divacky IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); 2221f22ef01cSRoman Divacky InstructionList.push_back(IBI); 2222f22ef01cSRoman Divacky for (unsigned i = 0, e = NumDests; i != e; ++i) { 2223f22ef01cSRoman Divacky if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { 2224f22ef01cSRoman Divacky IBI->addDestination(DestBB); 2225f22ef01cSRoman Divacky } else { 2226f22ef01cSRoman Divacky delete IBI; 2227f22ef01cSRoman Divacky return Error("Invalid INDIRECTBR record!"); 2228f22ef01cSRoman Divacky } 2229f22ef01cSRoman Divacky } 2230f22ef01cSRoman Divacky I = IBI; 2231f22ef01cSRoman Divacky break; 2232f22ef01cSRoman Divacky } 2233f22ef01cSRoman Divacky 2234f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_INVOKE: { 2235f22ef01cSRoman Divacky // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] 2236f22ef01cSRoman Divacky if (Record.size() < 4) return Error("Invalid INVOKE record"); 2237f22ef01cSRoman Divacky AttrListPtr PAL = getAttributes(Record[0]); 2238f22ef01cSRoman Divacky unsigned CCInfo = Record[1]; 2239f22ef01cSRoman Divacky BasicBlock *NormalBB = getBasicBlock(Record[2]); 2240f22ef01cSRoman Divacky BasicBlock *UnwindBB = getBasicBlock(Record[3]); 2241f22ef01cSRoman Divacky 2242f22ef01cSRoman Divacky unsigned OpNum = 4; 2243f22ef01cSRoman Divacky Value *Callee; 2244f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 2245f22ef01cSRoman Divacky return Error("Invalid INVOKE record"); 2246f22ef01cSRoman Divacky 2247f22ef01cSRoman Divacky const PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); 2248f22ef01cSRoman Divacky const FunctionType *FTy = !CalleeTy ? 0 : 2249f22ef01cSRoman Divacky dyn_cast<FunctionType>(CalleeTy->getElementType()); 2250f22ef01cSRoman Divacky 2251f22ef01cSRoman Divacky // Check that the right number of fixed parameters are here. 2252f22ef01cSRoman Divacky if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 || 2253f22ef01cSRoman Divacky Record.size() < OpNum+FTy->getNumParams()) 2254f22ef01cSRoman Divacky return Error("Invalid INVOKE record"); 2255f22ef01cSRoman Divacky 2256f22ef01cSRoman Divacky SmallVector<Value*, 16> Ops; 2257f22ef01cSRoman Divacky for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 2258f22ef01cSRoman Divacky Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); 2259f22ef01cSRoman Divacky if (Ops.back() == 0) return Error("Invalid INVOKE record"); 2260f22ef01cSRoman Divacky } 2261f22ef01cSRoman Divacky 2262f22ef01cSRoman Divacky if (!FTy->isVarArg()) { 2263f22ef01cSRoman Divacky if (Record.size() != OpNum) 2264f22ef01cSRoman Divacky return Error("Invalid INVOKE record"); 2265f22ef01cSRoman Divacky } else { 2266f22ef01cSRoman Divacky // Read type/value pairs for varargs params. 2267f22ef01cSRoman Divacky while (OpNum != Record.size()) { 2268f22ef01cSRoman Divacky Value *Op; 2269f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 2270f22ef01cSRoman Divacky return Error("Invalid INVOKE record"); 2271f22ef01cSRoman Divacky Ops.push_back(Op); 2272f22ef01cSRoman Divacky } 2273f22ef01cSRoman Divacky } 2274f22ef01cSRoman Divacky 2275f22ef01cSRoman Divacky I = InvokeInst::Create(Callee, NormalBB, UnwindBB, 2276f22ef01cSRoman Divacky Ops.begin(), Ops.end()); 2277f22ef01cSRoman Divacky InstructionList.push_back(I); 2278f22ef01cSRoman Divacky cast<InvokeInst>(I)->setCallingConv( 2279f22ef01cSRoman Divacky static_cast<CallingConv::ID>(CCInfo)); 2280f22ef01cSRoman Divacky cast<InvokeInst>(I)->setAttributes(PAL); 2281f22ef01cSRoman Divacky break; 2282f22ef01cSRoman Divacky } 2283f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_UNWIND: // UNWIND 2284f22ef01cSRoman Divacky I = new UnwindInst(Context); 2285f22ef01cSRoman Divacky InstructionList.push_back(I); 2286f22ef01cSRoman Divacky break; 2287f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE 2288f22ef01cSRoman Divacky I = new UnreachableInst(Context); 2289f22ef01cSRoman Divacky InstructionList.push_back(I); 2290f22ef01cSRoman Divacky break; 2291f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] 2292f22ef01cSRoman Divacky if (Record.size() < 1 || ((Record.size()-1)&1)) 2293f22ef01cSRoman Divacky return Error("Invalid PHI record"); 2294f22ef01cSRoman Divacky const Type *Ty = getTypeByID(Record[0]); 2295f22ef01cSRoman Divacky if (!Ty) return Error("Invalid PHI record"); 2296f22ef01cSRoman Divacky 22973b0f4066SDimitry Andric PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); 2298f22ef01cSRoman Divacky InstructionList.push_back(PN); 2299f22ef01cSRoman Divacky 2300f22ef01cSRoman Divacky for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { 2301f22ef01cSRoman Divacky Value *V = getFnValueByID(Record[1+i], Ty); 2302f22ef01cSRoman Divacky BasicBlock *BB = getBasicBlock(Record[2+i]); 2303f22ef01cSRoman Divacky if (!V || !BB) return Error("Invalid PHI record"); 2304f22ef01cSRoman Divacky PN->addIncoming(V, BB); 2305f22ef01cSRoman Divacky } 2306f22ef01cSRoman Divacky I = PN; 2307f22ef01cSRoman Divacky break; 2308f22ef01cSRoman Divacky } 2309f22ef01cSRoman Divacky 2310f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_MALLOC: { // MALLOC: [instty, op, align] 2311f22ef01cSRoman Divacky // Autoupgrade malloc instruction to malloc call. 2312f22ef01cSRoman Divacky // FIXME: Remove in LLVM 3.0. 2313f22ef01cSRoman Divacky if (Record.size() < 3) 2314f22ef01cSRoman Divacky return Error("Invalid MALLOC record"); 2315f22ef01cSRoman Divacky const PointerType *Ty = 2316f22ef01cSRoman Divacky dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); 2317f22ef01cSRoman Divacky Value *Size = getFnValueByID(Record[1], Type::getInt32Ty(Context)); 2318f22ef01cSRoman Divacky if (!Ty || !Size) return Error("Invalid MALLOC record"); 2319f22ef01cSRoman Divacky if (!CurBB) return Error("Invalid malloc instruction with no BB"); 2320f22ef01cSRoman Divacky const Type *Int32Ty = IntegerType::getInt32Ty(CurBB->getContext()); 2321f22ef01cSRoman Divacky Constant *AllocSize = ConstantExpr::getSizeOf(Ty->getElementType()); 2322f22ef01cSRoman Divacky AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, Int32Ty); 2323f22ef01cSRoman Divacky I = CallInst::CreateMalloc(CurBB, Int32Ty, Ty->getElementType(), 2324f22ef01cSRoman Divacky AllocSize, Size, NULL); 2325f22ef01cSRoman Divacky InstructionList.push_back(I); 2326f22ef01cSRoman Divacky break; 2327f22ef01cSRoman Divacky } 2328f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_FREE: { // FREE: [op, opty] 2329f22ef01cSRoman Divacky unsigned OpNum = 0; 2330f22ef01cSRoman Divacky Value *Op; 2331f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 2332f22ef01cSRoman Divacky OpNum != Record.size()) 2333f22ef01cSRoman Divacky return Error("Invalid FREE record"); 2334f22ef01cSRoman Divacky if (!CurBB) return Error("Invalid free instruction with no BB"); 2335f22ef01cSRoman Divacky I = CallInst::CreateFree(Op, CurBB); 2336f22ef01cSRoman Divacky InstructionList.push_back(I); 2337f22ef01cSRoman Divacky break; 2338f22ef01cSRoman Divacky } 2339ffd1746dSEd Schouten case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] 2340ffd1746dSEd Schouten // For backward compatibility, tolerate a lack of an opty, and use i32. 2341e580952dSDimitry Andric // Remove this in LLVM 3.0. 2342ffd1746dSEd Schouten if (Record.size() < 3 || Record.size() > 4) 2343f22ef01cSRoman Divacky return Error("Invalid ALLOCA record"); 2344ffd1746dSEd Schouten unsigned OpNum = 0; 2345f22ef01cSRoman Divacky const PointerType *Ty = 2346ffd1746dSEd Schouten dyn_cast_or_null<PointerType>(getTypeByID(Record[OpNum++])); 2347ffd1746dSEd Schouten const Type *OpTy = Record.size() == 4 ? getTypeByID(Record[OpNum++]) : 2348ffd1746dSEd Schouten Type::getInt32Ty(Context); 2349ffd1746dSEd Schouten Value *Size = getFnValueByID(Record[OpNum++], OpTy); 2350ffd1746dSEd Schouten unsigned Align = Record[OpNum++]; 2351f22ef01cSRoman Divacky if (!Ty || !Size) return Error("Invalid ALLOCA record"); 2352f22ef01cSRoman Divacky I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1); 2353f22ef01cSRoman Divacky InstructionList.push_back(I); 2354f22ef01cSRoman Divacky break; 2355f22ef01cSRoman Divacky } 2356f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] 2357f22ef01cSRoman Divacky unsigned OpNum = 0; 2358f22ef01cSRoman Divacky Value *Op; 2359f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 2360f22ef01cSRoman Divacky OpNum+2 != Record.size()) 2361f22ef01cSRoman Divacky return Error("Invalid LOAD record"); 2362f22ef01cSRoman Divacky 2363f22ef01cSRoman Divacky I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1); 2364f22ef01cSRoman Divacky InstructionList.push_back(I); 2365f22ef01cSRoman Divacky break; 2366f22ef01cSRoman Divacky } 2367f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_STORE2: { // STORE2:[ptrty, ptr, val, align, vol] 2368f22ef01cSRoman Divacky unsigned OpNum = 0; 2369f22ef01cSRoman Divacky Value *Val, *Ptr; 2370f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 2371f22ef01cSRoman Divacky getValue(Record, OpNum, 2372f22ef01cSRoman Divacky cast<PointerType>(Ptr->getType())->getElementType(), Val) || 2373f22ef01cSRoman Divacky OpNum+2 != Record.size()) 2374f22ef01cSRoman Divacky return Error("Invalid STORE record"); 2375f22ef01cSRoman Divacky 2376f22ef01cSRoman Divacky I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); 2377f22ef01cSRoman Divacky InstructionList.push_back(I); 2378f22ef01cSRoman Divacky break; 2379f22ef01cSRoman Divacky } 2380f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_STORE: { // STORE:[val, valty, ptr, align, vol] 2381f22ef01cSRoman Divacky // FIXME: Legacy form of store instruction. Should be removed in LLVM 3.0. 2382f22ef01cSRoman Divacky unsigned OpNum = 0; 2383f22ef01cSRoman Divacky Value *Val, *Ptr; 2384f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Val) || 2385f22ef01cSRoman Divacky getValue(Record, OpNum, 2386f22ef01cSRoman Divacky PointerType::getUnqual(Val->getType()), Ptr)|| 2387f22ef01cSRoman Divacky OpNum+2 != Record.size()) 2388f22ef01cSRoman Divacky return Error("Invalid STORE record"); 2389f22ef01cSRoman Divacky 2390f22ef01cSRoman Divacky I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); 2391f22ef01cSRoman Divacky InstructionList.push_back(I); 2392f22ef01cSRoman Divacky break; 2393f22ef01cSRoman Divacky } 2394e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 2395e580952dSDimitry Andric case bitc::FUNC_CODE_INST_CALL: 2396e580952dSDimitry Andric LLVM2_7MetadataDetected = true; 2397e580952dSDimitry Andric case bitc::FUNC_CODE_INST_CALL2: { 2398f22ef01cSRoman Divacky // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...] 2399f22ef01cSRoman Divacky if (Record.size() < 3) 2400f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2401f22ef01cSRoman Divacky 2402f22ef01cSRoman Divacky AttrListPtr PAL = getAttributes(Record[0]); 2403f22ef01cSRoman Divacky unsigned CCInfo = Record[1]; 2404f22ef01cSRoman Divacky 2405f22ef01cSRoman Divacky unsigned OpNum = 2; 2406f22ef01cSRoman Divacky Value *Callee; 2407f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 2408f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2409f22ef01cSRoman Divacky 2410f22ef01cSRoman Divacky const PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 2411f22ef01cSRoman Divacky const FunctionType *FTy = 0; 2412f22ef01cSRoman Divacky if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType()); 2413f22ef01cSRoman Divacky if (!FTy || Record.size() < FTy->getNumParams()+OpNum) 2414f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2415f22ef01cSRoman Divacky 2416f22ef01cSRoman Divacky SmallVector<Value*, 16> Args; 2417f22ef01cSRoman Divacky // Read the fixed params. 2418f22ef01cSRoman Divacky for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 2419f22ef01cSRoman Divacky if (FTy->getParamType(i)->getTypeID()==Type::LabelTyID) 2420f22ef01cSRoman Divacky Args.push_back(getBasicBlock(Record[OpNum])); 2421f22ef01cSRoman Divacky else 2422f22ef01cSRoman Divacky Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); 2423f22ef01cSRoman Divacky if (Args.back() == 0) return Error("Invalid CALL record"); 2424f22ef01cSRoman Divacky } 2425f22ef01cSRoman Divacky 2426f22ef01cSRoman Divacky // Read type/value pairs for varargs params. 2427f22ef01cSRoman Divacky if (!FTy->isVarArg()) { 2428f22ef01cSRoman Divacky if (OpNum != Record.size()) 2429f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2430f22ef01cSRoman Divacky } else { 2431f22ef01cSRoman Divacky while (OpNum != Record.size()) { 2432f22ef01cSRoman Divacky Value *Op; 2433f22ef01cSRoman Divacky if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 2434f22ef01cSRoman Divacky return Error("Invalid CALL record"); 2435f22ef01cSRoman Divacky Args.push_back(Op); 2436f22ef01cSRoman Divacky } 2437f22ef01cSRoman Divacky } 2438f22ef01cSRoman Divacky 2439f22ef01cSRoman Divacky I = CallInst::Create(Callee, Args.begin(), Args.end()); 2440f22ef01cSRoman Divacky InstructionList.push_back(I); 2441f22ef01cSRoman Divacky cast<CallInst>(I)->setCallingConv( 2442f22ef01cSRoman Divacky static_cast<CallingConv::ID>(CCInfo>>1)); 2443f22ef01cSRoman Divacky cast<CallInst>(I)->setTailCall(CCInfo & 1); 2444f22ef01cSRoman Divacky cast<CallInst>(I)->setAttributes(PAL); 2445f22ef01cSRoman Divacky break; 2446f22ef01cSRoman Divacky } 2447f22ef01cSRoman Divacky case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] 2448f22ef01cSRoman Divacky if (Record.size() < 3) 2449f22ef01cSRoman Divacky return Error("Invalid VAARG record"); 2450f22ef01cSRoman Divacky const Type *OpTy = getTypeByID(Record[0]); 2451f22ef01cSRoman Divacky Value *Op = getFnValueByID(Record[1], OpTy); 2452f22ef01cSRoman Divacky const Type *ResTy = getTypeByID(Record[2]); 2453f22ef01cSRoman Divacky if (!OpTy || !Op || !ResTy) 2454f22ef01cSRoman Divacky return Error("Invalid VAARG record"); 2455f22ef01cSRoman Divacky I = new VAArgInst(Op, ResTy); 2456f22ef01cSRoman Divacky InstructionList.push_back(I); 2457f22ef01cSRoman Divacky break; 2458f22ef01cSRoman Divacky } 2459f22ef01cSRoman Divacky } 2460f22ef01cSRoman Divacky 2461f22ef01cSRoman Divacky // Add instruction to end of current BB. If there is no current BB, reject 2462f22ef01cSRoman Divacky // this file. 2463f22ef01cSRoman Divacky if (CurBB == 0) { 2464f22ef01cSRoman Divacky delete I; 2465f22ef01cSRoman Divacky return Error("Invalid instruction with no BB"); 2466f22ef01cSRoman Divacky } 2467f22ef01cSRoman Divacky CurBB->getInstList().push_back(I); 2468f22ef01cSRoman Divacky 2469f22ef01cSRoman Divacky // If this was a terminator instruction, move to the next block. 2470f22ef01cSRoman Divacky if (isa<TerminatorInst>(I)) { 2471f22ef01cSRoman Divacky ++CurBBNo; 2472f22ef01cSRoman Divacky CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0; 2473f22ef01cSRoman Divacky } 2474f22ef01cSRoman Divacky 2475f22ef01cSRoman Divacky // Non-void values get registered in the value table for future use. 2476f22ef01cSRoman Divacky if (I && !I->getType()->isVoidTy()) 2477f22ef01cSRoman Divacky ValueList.AssignValue(I, NextValueNo++); 2478f22ef01cSRoman Divacky } 2479f22ef01cSRoman Divacky 2480f22ef01cSRoman Divacky // Check the function list for unresolved values. 2481f22ef01cSRoman Divacky if (Argument *A = dyn_cast<Argument>(ValueList.back())) { 2482f22ef01cSRoman Divacky if (A->getParent() == 0) { 2483f22ef01cSRoman Divacky // We found at least one unresolved value. Nuke them all to avoid leaks. 2484f22ef01cSRoman Divacky for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ 2485e580952dSDimitry Andric if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) { 2486f22ef01cSRoman Divacky A->replaceAllUsesWith(UndefValue::get(A->getType())); 2487f22ef01cSRoman Divacky delete A; 2488f22ef01cSRoman Divacky } 2489f22ef01cSRoman Divacky } 2490f22ef01cSRoman Divacky return Error("Never resolved value found in function!"); 2491f22ef01cSRoman Divacky } 2492f22ef01cSRoman Divacky } 2493f22ef01cSRoman Divacky 2494e580952dSDimitry Andric // FIXME: Check for unresolved forward-declared metadata references 2495e580952dSDimitry Andric // and clean up leaks. 2496e580952dSDimitry Andric 2497f22ef01cSRoman Divacky // See if anything took the address of blocks in this function. If so, 2498f22ef01cSRoman Divacky // resolve them now. 2499f22ef01cSRoman Divacky DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI = 2500f22ef01cSRoman Divacky BlockAddrFwdRefs.find(F); 2501f22ef01cSRoman Divacky if (BAFRI != BlockAddrFwdRefs.end()) { 2502f22ef01cSRoman Divacky std::vector<BlockAddrRefTy> &RefList = BAFRI->second; 2503f22ef01cSRoman Divacky for (unsigned i = 0, e = RefList.size(); i != e; ++i) { 2504f22ef01cSRoman Divacky unsigned BlockIdx = RefList[i].first; 2505f22ef01cSRoman Divacky if (BlockIdx >= FunctionBBs.size()) 2506f22ef01cSRoman Divacky return Error("Invalid blockaddress block #"); 2507f22ef01cSRoman Divacky 2508f22ef01cSRoman Divacky GlobalVariable *FwdRef = RefList[i].second; 2509f22ef01cSRoman Divacky FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx])); 2510f22ef01cSRoman Divacky FwdRef->eraseFromParent(); 2511f22ef01cSRoman Divacky } 2512f22ef01cSRoman Divacky 2513f22ef01cSRoman Divacky BlockAddrFwdRefs.erase(BAFRI); 2514f22ef01cSRoman Divacky } 2515f22ef01cSRoman Divacky 2516e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 2517e580952dSDimitry Andric unsigned NewMDValueListSize = MDValueList.size(); 2518e580952dSDimitry Andric 2519f22ef01cSRoman Divacky // Trim the value list down to the size it was before we parsed this function. 2520f22ef01cSRoman Divacky ValueList.shrinkTo(ModuleValueListSize); 2521e580952dSDimitry Andric MDValueList.shrinkTo(ModuleMDValueListSize); 2522e580952dSDimitry Andric 2523e580952dSDimitry Andric // Backwards compatibility hack: Function-local metadata numbers 2524e580952dSDimitry Andric // were previously not reset between functions. This is now fixed, 2525e580952dSDimitry Andric // however we still need to understand the old numbering in order 2526e580952dSDimitry Andric // to be able to read old bitcode files. 2527e580952dSDimitry Andric // FIXME: Remove this in LLVM 3.0. 2528e580952dSDimitry Andric if (LLVM2_7MetadataDetected) 2529e580952dSDimitry Andric MDValueList.resize(NewMDValueListSize); 2530e580952dSDimitry Andric 2531f22ef01cSRoman Divacky std::vector<BasicBlock*>().swap(FunctionBBs); 2532f22ef01cSRoman Divacky 2533f22ef01cSRoman Divacky return false; 2534f22ef01cSRoman Divacky } 2535f22ef01cSRoman Divacky 2536f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2537f22ef01cSRoman Divacky // GVMaterializer implementation 2538f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2539f22ef01cSRoman Divacky 2540f22ef01cSRoman Divacky 2541f22ef01cSRoman Divacky bool BitcodeReader::isMaterializable(const GlobalValue *GV) const { 2542f22ef01cSRoman Divacky if (const Function *F = dyn_cast<Function>(GV)) { 2543f22ef01cSRoman Divacky return F->isDeclaration() && 2544f22ef01cSRoman Divacky DeferredFunctionInfo.count(const_cast<Function*>(F)); 2545f22ef01cSRoman Divacky } 2546f22ef01cSRoman Divacky return false; 2547f22ef01cSRoman Divacky } 2548f22ef01cSRoman Divacky 2549f22ef01cSRoman Divacky bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) { 2550f22ef01cSRoman Divacky Function *F = dyn_cast<Function>(GV); 2551f22ef01cSRoman Divacky // If it's not a function or is already material, ignore the request. 2552f22ef01cSRoman Divacky if (!F || !F->isMaterializable()) return false; 2553f22ef01cSRoman Divacky 2554f22ef01cSRoman Divacky DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 2555f22ef01cSRoman Divacky assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 2556f22ef01cSRoman Divacky 2557f22ef01cSRoman Divacky // Move the bit stream to the saved position of the deferred function body. 2558f22ef01cSRoman Divacky Stream.JumpToBit(DFII->second); 2559f22ef01cSRoman Divacky 2560f22ef01cSRoman Divacky if (ParseFunctionBody(F)) { 2561f22ef01cSRoman Divacky if (ErrInfo) *ErrInfo = ErrorString; 2562f22ef01cSRoman Divacky return true; 2563f22ef01cSRoman Divacky } 2564f22ef01cSRoman Divacky 2565f22ef01cSRoman Divacky // Upgrade any old intrinsic calls in the function. 2566f22ef01cSRoman Divacky for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(), 2567f22ef01cSRoman Divacky E = UpgradedIntrinsics.end(); I != E; ++I) { 2568f22ef01cSRoman Divacky if (I->first != I->second) { 2569f22ef01cSRoman Divacky for (Value::use_iterator UI = I->first->use_begin(), 2570f22ef01cSRoman Divacky UE = I->first->use_end(); UI != UE; ) { 2571f22ef01cSRoman Divacky if (CallInst* CI = dyn_cast<CallInst>(*UI++)) 2572f22ef01cSRoman Divacky UpgradeIntrinsicCall(CI, I->second); 2573f22ef01cSRoman Divacky } 2574f22ef01cSRoman Divacky } 2575f22ef01cSRoman Divacky } 2576f22ef01cSRoman Divacky 2577f22ef01cSRoman Divacky return false; 2578f22ef01cSRoman Divacky } 2579f22ef01cSRoman Divacky 2580f22ef01cSRoman Divacky bool BitcodeReader::isDematerializable(const GlobalValue *GV) const { 2581f22ef01cSRoman Divacky const Function *F = dyn_cast<Function>(GV); 2582f22ef01cSRoman Divacky if (!F || F->isDeclaration()) 2583f22ef01cSRoman Divacky return false; 2584f22ef01cSRoman Divacky return DeferredFunctionInfo.count(const_cast<Function*>(F)); 2585f22ef01cSRoman Divacky } 2586f22ef01cSRoman Divacky 2587f22ef01cSRoman Divacky void BitcodeReader::Dematerialize(GlobalValue *GV) { 2588f22ef01cSRoman Divacky Function *F = dyn_cast<Function>(GV); 2589f22ef01cSRoman Divacky // If this function isn't dematerializable, this is a noop. 2590f22ef01cSRoman Divacky if (!F || !isDematerializable(F)) 2591f22ef01cSRoman Divacky return; 2592f22ef01cSRoman Divacky 2593f22ef01cSRoman Divacky assert(DeferredFunctionInfo.count(F) && "No info to read function later?"); 2594f22ef01cSRoman Divacky 2595f22ef01cSRoman Divacky // Just forget the function body, we can remat it later. 2596f22ef01cSRoman Divacky F->deleteBody(); 2597f22ef01cSRoman Divacky } 2598f22ef01cSRoman Divacky 2599f22ef01cSRoman Divacky 2600f22ef01cSRoman Divacky bool BitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) { 2601f22ef01cSRoman Divacky assert(M == TheModule && 2602f22ef01cSRoman Divacky "Can only Materialize the Module this BitcodeReader is attached to."); 2603f22ef01cSRoman Divacky // Iterate over the module, deserializing any functions that are still on 2604f22ef01cSRoman Divacky // disk. 2605f22ef01cSRoman Divacky for (Module::iterator F = TheModule->begin(), E = TheModule->end(); 2606f22ef01cSRoman Divacky F != E; ++F) 2607f22ef01cSRoman Divacky if (F->isMaterializable() && 2608f22ef01cSRoman Divacky Materialize(F, ErrInfo)) 2609f22ef01cSRoman Divacky return true; 2610f22ef01cSRoman Divacky 2611f22ef01cSRoman Divacky // Upgrade any intrinsic calls that slipped through (should not happen!) and 2612f22ef01cSRoman Divacky // delete the old functions to clean up. We can't do this unless the entire 2613f22ef01cSRoman Divacky // module is materialized because there could always be another function body 2614f22ef01cSRoman Divacky // with calls to the old function. 2615f22ef01cSRoman Divacky for (std::vector<std::pair<Function*, Function*> >::iterator I = 2616f22ef01cSRoman Divacky UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) { 2617f22ef01cSRoman Divacky if (I->first != I->second) { 2618f22ef01cSRoman Divacky for (Value::use_iterator UI = I->first->use_begin(), 2619f22ef01cSRoman Divacky UE = I->first->use_end(); UI != UE; ) { 2620f22ef01cSRoman Divacky if (CallInst* CI = dyn_cast<CallInst>(*UI++)) 2621f22ef01cSRoman Divacky UpgradeIntrinsicCall(CI, I->second); 2622f22ef01cSRoman Divacky } 2623f22ef01cSRoman Divacky if (!I->first->use_empty()) 2624f22ef01cSRoman Divacky I->first->replaceAllUsesWith(I->second); 2625f22ef01cSRoman Divacky I->first->eraseFromParent(); 2626f22ef01cSRoman Divacky } 2627f22ef01cSRoman Divacky } 2628f22ef01cSRoman Divacky std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics); 2629f22ef01cSRoman Divacky 2630f22ef01cSRoman Divacky // Check debug info intrinsics. 2631f22ef01cSRoman Divacky CheckDebugInfoIntrinsics(TheModule); 2632f22ef01cSRoman Divacky 2633f22ef01cSRoman Divacky return false; 2634f22ef01cSRoman Divacky } 2635f22ef01cSRoman Divacky 2636f22ef01cSRoman Divacky 2637f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2638f22ef01cSRoman Divacky // External interface 2639f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2640f22ef01cSRoman Divacky 2641f22ef01cSRoman Divacky /// getLazyBitcodeModule - lazy function-at-a-time loading from a file. 2642f22ef01cSRoman Divacky /// 2643f22ef01cSRoman Divacky Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer, 2644f22ef01cSRoman Divacky LLVMContext& Context, 2645f22ef01cSRoman Divacky std::string *ErrMsg) { 2646f22ef01cSRoman Divacky Module *M = new Module(Buffer->getBufferIdentifier(), Context); 2647f22ef01cSRoman Divacky BitcodeReader *R = new BitcodeReader(Buffer, Context); 2648f22ef01cSRoman Divacky M->setMaterializer(R); 2649f22ef01cSRoman Divacky if (R->ParseBitcodeInto(M)) { 2650f22ef01cSRoman Divacky if (ErrMsg) 2651f22ef01cSRoman Divacky *ErrMsg = R->getErrorString(); 2652f22ef01cSRoman Divacky 2653f22ef01cSRoman Divacky delete M; // Also deletes R. 2654f22ef01cSRoman Divacky return 0; 2655f22ef01cSRoman Divacky } 2656f22ef01cSRoman Divacky // Have the BitcodeReader dtor delete 'Buffer'. 2657f22ef01cSRoman Divacky R->setBufferOwned(true); 2658f22ef01cSRoman Divacky return M; 2659f22ef01cSRoman Divacky } 2660f22ef01cSRoman Divacky 2661f22ef01cSRoman Divacky /// ParseBitcodeFile - Read the specified bitcode file, returning the module. 2662f22ef01cSRoman Divacky /// If an error occurs, return null and fill in *ErrMsg if non-null. 2663f22ef01cSRoman Divacky Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context, 2664f22ef01cSRoman Divacky std::string *ErrMsg){ 2665f22ef01cSRoman Divacky Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg); 2666f22ef01cSRoman Divacky if (!M) return 0; 2667f22ef01cSRoman Divacky 2668f22ef01cSRoman Divacky // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether 2669f22ef01cSRoman Divacky // there was an error. 2670f22ef01cSRoman Divacky static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false); 2671f22ef01cSRoman Divacky 2672f22ef01cSRoman Divacky // Read in the entire module, and destroy the BitcodeReader. 2673f22ef01cSRoman Divacky if (M->MaterializeAllPermanently(ErrMsg)) { 2674f22ef01cSRoman Divacky delete M; 26752754fe60SDimitry Andric return 0; 2676f22ef01cSRoman Divacky } 26772754fe60SDimitry Andric 2678f22ef01cSRoman Divacky return M; 2679f22ef01cSRoman Divacky } 26802754fe60SDimitry Andric 26812754fe60SDimitry Andric std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer, 26822754fe60SDimitry Andric LLVMContext& Context, 26832754fe60SDimitry Andric std::string *ErrMsg) { 26842754fe60SDimitry Andric BitcodeReader *R = new BitcodeReader(Buffer, Context); 26852754fe60SDimitry Andric // Don't let the BitcodeReader dtor delete 'Buffer'. 26862754fe60SDimitry Andric R->setBufferOwned(false); 26872754fe60SDimitry Andric 26882754fe60SDimitry Andric std::string Triple(""); 26892754fe60SDimitry Andric if (R->ParseTriple(Triple)) 26902754fe60SDimitry Andric if (ErrMsg) 26912754fe60SDimitry Andric *ErrMsg = R->getErrorString(); 26922754fe60SDimitry Andric 26932754fe60SDimitry Andric delete R; 26942754fe60SDimitry Andric return Triple; 26952754fe60SDimitry Andric } 2696