diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2010-09-17 15:48:55 +0000 | 
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2010-09-17 15:48:55 +0000 | 
| commit | d39c594d39df7f283c2fb8a704a3f31c501180d9 (patch) | |
| tree | 36453626c792cccd91f783a38a169d610a6b9db9 /lib/Bitcode/Reader/BitcodeReader.cpp | |
| parent | 6144c1de6a7674dad94290650e4e14f24d42e421 (diff) | |
Notes
Diffstat (limited to 'lib/Bitcode/Reader/BitcodeReader.cpp')
| -rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.cpp | 119 | 
1 files changed, 78 insertions, 41 deletions
| diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index b3f0776d29d54..830c79aa3b54e 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -39,6 +39,7 @@ void BitcodeReader::FreeState() {    std::vector<BasicBlock*>().swap(FunctionBBs);    std::vector<Function*>().swap(FunctionsWithBodies);    DeferredFunctionInfo.clear(); +  MDKindMap.clear();  }  //===----------------------------------------------------------------------===// @@ -76,6 +77,7 @@ static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {    case 12: return GlobalValue::AvailableExternallyLinkage;    case 13: return GlobalValue::LinkerPrivateLinkage;    case 14: return GlobalValue::LinkerPrivateWeakLinkage; +  case 15: return GlobalValue::LinkerPrivateWeakDefAutoLinkage;    }  } @@ -295,8 +297,6 @@ void BitcodeReaderValueList::ResolveConstantForwardRefs() {        } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {          NewC = ConstantStruct::get(Context, &NewOps[0], NewOps.size(),                                           UserCS->getType()->isPacked()); -      } else if (ConstantUnion *UserCU = dyn_cast<ConstantUnion>(UserC)) { -        NewC = ConstantUnion::get(UserCU->getType(), NewOps[0]);        } else if (isa<ConstantVector>(UserC)) {          NewC = ConstantVector::get(&NewOps[0], NewOps.size());        } else { @@ -332,9 +332,9 @@ void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {    }    // If there was a forward reference to this value, replace it. -  Value *PrevVal = OldV; +  MDNode *PrevVal = cast<MDNode>(OldV);    OldV->replaceAllUsesWith(V); -  delete PrevVal; +  MDNode::deleteTemporary(PrevVal);    // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new    // value for Idx.    MDValuePtrs[Idx] = V; @@ -350,7 +350,7 @@ Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {    }    // Create and return a placeholder, which will later be RAUW'd. -  Value *V = new Argument(Type::getMetadataTy(Context)); +  Value *V = MDNode::getTemporary(Context, 0, 0);    MDValuePtrs[Idx] = V;    return V;  } @@ -589,13 +589,6 @@ bool BitcodeReader::ParseTypeTable() {        ResultTy = StructType::get(Context, EltTys, Record[0]);        break;      } -    case bitc::TYPE_CODE_UNION: {  // UNION: [eltty x N] -      SmallVector<const Type*, 8> EltTys; -      for (unsigned i = 0, e = Record.size(); i != e; ++i) -        EltTys.push_back(getTypeByID(Record[i], true)); -      ResultTy = UnionType::get(&EltTys[0], EltTys.size()); -      break; -    }      case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]        if (Record.size() < 2)          return Error("Invalid ARRAY type record"); @@ -781,7 +774,8 @@ bool BitcodeReader::ParseMetadata() {      bool IsFunctionLocal = false;      // Read a record.      Record.clear(); -    switch (Stream.ReadRecord(Code, Record)) { +    Code = Stream.ReadRecord(Code, Record); +    switch (Code) {      default:  // Default behavior: ignore.        break;      case bitc::METADATA_NAME: { @@ -794,34 +788,46 @@ bool BitcodeReader::ParseMetadata() {        Record.clear();        Code = Stream.ReadCode(); -      // METADATA_NAME is always followed by METADATA_NAMED_NODE. -      if (Stream.ReadRecord(Code, Record) != bitc::METADATA_NAMED_NODE) +      // METADATA_NAME is always followed by METADATA_NAMED_NODE2. +      // Or METADATA_NAMED_NODE in LLVM 2.7. FIXME: Remove this in LLVM 3.0. +      unsigned NextBitCode = Stream.ReadRecord(Code, Record); +      if (NextBitCode == bitc::METADATA_NAMED_NODE) { +        LLVM2_7MetadataDetected = true; +      } else if (NextBitCode != bitc::METADATA_NAMED_NODE2)          assert ( 0 && "Inavlid Named Metadata record");        // Read named metadata elements.        unsigned Size = Record.size(); -      SmallVector<MDNode *, 8> Elts; +      NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);        for (unsigned i = 0; i != Size; ++i) { -        if (Record[i] == ~0U) { -          Elts.push_back(NULL); -          continue; -        }          MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i]));          if (MD == 0)            return Error("Malformed metadata record"); -        Elts.push_back(MD); +        NMD->addOperand(MD);        } -      Value *V = NamedMDNode::Create(Context, Name.str(), Elts.data(), -                                     Elts.size(), TheModule); -      MDValueList.AssignValue(V, NextMDValueNo++); +      // Backwards compatibility hack: NamedMDValues used to be Values, +      // and they got their own slots in the value numbering. They are no +      // longer Values, however we still need to account for them in the +      // numbering in order to be able to read old bitcode files. +      // FIXME: Remove this in LLVM 3.0. +      if (LLVM2_7MetadataDetected) +        MDValueList.AssignValue(0, NextMDValueNo++);        break;      } -    case bitc::METADATA_FN_NODE: +    case bitc::METADATA_FN_NODE: // FIXME: Remove in LLVM 3.0. +    case bitc::METADATA_FN_NODE2:        IsFunctionLocal = true;        // fall-through -    case bitc::METADATA_NODE: { +    case bitc::METADATA_NODE:    // FIXME: Remove in LLVM 3.0. +    case bitc::METADATA_NODE2: { + +      // Detect 2.7-era metadata. +      // FIXME: Remove in LLVM 3.0. +      if (Code == bitc::METADATA_FN_NODE || Code == bitc::METADATA_NODE) +        LLVM2_7MetadataDetected = true; +        if (Record.size() % 2 == 1) -        return Error("Invalid METADATA_NODE record"); +        return Error("Invalid METADATA_NODE2 record");        unsigned Size = Record.size();        SmallVector<Value*, 8> Elts; @@ -859,13 +865,12 @@ bool BitcodeReader::ParseMetadata() {        SmallString<8> Name;        Name.resize(RecordLength-1);        unsigned Kind = Record[0]; -      (void) Kind;        for (unsigned i = 1; i != RecordLength; ++i)          Name[i-1] = Record[i];        unsigned NewKind = TheModule->getMDKindID(Name.str()); -      assert(Kind == NewKind && -             "FIXME: Unable to handle custom metadata mismatch!");(void)NewKind; +      if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) +        return Error("Conflicting METADATA_KIND records");        break;      }      } @@ -1020,11 +1025,6 @@ bool BitcodeReader::ParseConstants() {            Elts.push_back(ValueList.getConstantFwdRef(Record[i],                                                       STy->getElementType(i)));          V = ConstantStruct::get(STy, Elts); -      } else if (const UnionType *UnTy = dyn_cast<UnionType>(CurTy)) { -        uint64_t Index = Record[0]; -        Constant *Val = ValueList.getConstantFwdRef(Record[1], -                                        UnTy->getElementType(Index)); -        V = ConstantUnion::get(UnTy, Val);        } else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {          const Type *EltTy = ATy->getElementType();          for (unsigned i = 0; i != Size; ++i) @@ -1297,6 +1297,12 @@ bool BitcodeReader::ParseModule() {            UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));        } +      // Look for global variables which need to be renamed. +      for (Module::global_iterator +             GI = TheModule->global_begin(), GE = TheModule->global_end(); +           GI != GE; ++GI) +        UpgradeGlobalVariable(GI); +        // Force deallocation of memory for these vectors to favor the client that        // want lazy deserialization.        std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); @@ -1614,15 +1620,22 @@ bool BitcodeReader::ParseMetadataAttachment() {      switch (Stream.ReadRecord(Code, Record)) {      default:  // Default behavior: ignore.        break; -    case bitc::METADATA_ATTACHMENT: { +    // FIXME: Remove in LLVM 3.0. +    case bitc::METADATA_ATTACHMENT: +      LLVM2_7MetadataDetected = true; +    case bitc::METADATA_ATTACHMENT2: {        unsigned RecordLength = Record.size();        if (Record.empty() || (RecordLength - 1) % 2 == 1)          return Error ("Invalid METADATA_ATTACHMENT reader!");        Instruction *Inst = InstructionList[Record[0]];        for (unsigned i = 1; i != RecordLength; i = i+2) {          unsigned Kind = Record[i]; +        DenseMap<unsigned, unsigned>::iterator I = +          MDKindMap.find(Kind); +        if (I == MDKindMap.end()) +          return Error("Invalid metadata kind ID");          Value *Node = MDValueList.getValueFwdRef(Record[i+1]); -        Inst->setMetadata(Kind, cast<MDNode>(Node)); +        Inst->setMetadata(I->second, cast<MDNode>(Node));        }        break;      } @@ -1638,6 +1651,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {    InstructionList.clear();    unsigned ModuleValueListSize = ValueList.size(); +  unsigned ModuleMDValueListSize = MDValueList.size();    // Add all the function arguments to the value table.    for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) @@ -1722,7 +1736,10 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {        I = 0;        continue; -    case bitc::FUNC_CODE_DEBUG_LOC: {      // DEBUG_LOC: [line, col, scope, ia] +    // FIXME: Remove this in LLVM 3.0. +    case bitc::FUNC_CODE_DEBUG_LOC: +      LLVM2_7MetadataDetected = true; +    case bitc::FUNC_CODE_DEBUG_LOC2: {      // DEBUG_LOC: [line, col, scope, ia]        I = 0;     // Get the last instruction emitted.        if (CurBB && !CurBB->empty())          I = &CurBB->back(); @@ -1988,6 +2005,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {          } while(OpNum != Record.size());          const Type *ReturnType = F->getReturnType(); +        // Handle multiple return values. FIXME: Remove in LLVM 3.0.          if (Vs.size() > 1 ||              (ReturnType->isStructTy() &&               (Vs.empty() || Vs[0]->getType() != ReturnType))) { @@ -2183,7 +2201,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {      }      case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]        // For backward compatibility, tolerate a lack of an opty, and use i32. -      // LLVM 3.0: Remove this. +      // Remove this in LLVM 3.0.        if (Record.size() < 3 || Record.size() > 4)          return Error("Invalid ALLOCA record");        unsigned OpNum = 0; @@ -2236,7 +2254,10 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {        InstructionList.push_back(I);        break;      } -    case bitc::FUNC_CODE_INST_CALL: { +    // FIXME: Remove this in LLVM 3.0. +    case bitc::FUNC_CODE_INST_CALL: +      LLVM2_7MetadataDetected = true; +    case bitc::FUNC_CODE_INST_CALL2: {        // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]        if (Record.size() < 3)          return Error("Invalid CALL record"); @@ -2324,7 +2345,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {      if (A->getParent() == 0) {        // We found at least one unresolved value.  Nuke them all to avoid leaks.        for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ -        if ((A = dyn_cast<Argument>(ValueList.back())) && A->getParent() == 0) { +        if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) {            A->replaceAllUsesWith(UndefValue::get(A->getType()));            delete A;          } @@ -2333,6 +2354,9 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {      }    } +  // FIXME: Check for unresolved forward-declared metadata references +  // and clean up leaks. +    // See if anything took the address of blocks in this function.  If so,    // resolve them now.    DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI = @@ -2352,8 +2376,21 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {      BlockAddrFwdRefs.erase(BAFRI);    } +  // FIXME: Remove this in LLVM 3.0. +  unsigned NewMDValueListSize = MDValueList.size(); +    // Trim the value list down to the size it was before we parsed this function.    ValueList.shrinkTo(ModuleValueListSize); +  MDValueList.shrinkTo(ModuleMDValueListSize); + +  // Backwards compatibility hack: Function-local metadata numbers +  // were previously not reset between functions. This is now fixed, +  // however we still need to understand the old numbering in order +  // to be able to read old bitcode files. +  // FIXME: Remove this in LLVM 3.0. +  if (LLVM2_7MetadataDetected) +    MDValueList.resize(NewMDValueListSize); +    std::vector<BasicBlock*>().swap(FunctionBBs);    return false; | 
