diff options
author | Mark Seaborn <mseaborn@chromium.org> | 2013-06-26 11:19:15 -0700 |
---|---|---|
committer | Mark Seaborn <mseaborn@chromium.org> | 2013-06-26 11:19:15 -0700 |
commit | bb186d9ec3a98cec5290410982309e0b35765231 (patch) | |
tree | 6b1ddc83d236dbd5a01a33063353b81b472b103a /lib/Bitcode | |
parent | afa589d27ad90bcfebae9566dd31785d4ba9696f (diff) |
PNaCl wire format: Remove the METADATA_BLOCK block from pexes
Despite removing metadata at the IR level, the writer was still
outputting a METADATA_BLOCK containing built-in strings such as "dbg",
"tbaa" and "fpmath".
Remove this by removing metadata support from the reader and writer.
I've removed most references to "metadata" and "MD".
I've also removed DEBUG_LOC handling, since that depends on
MDValueList in the reader.
BUG=https://code.google.com/p/nativeclient/issues/detail?id=3518
TEST=PNaCl toolchain trybots + check hello_world.final.pexe in bcanalyzer
Review URL: https://codereview.chromium.org/17761005
Diffstat (limited to 'lib/Bitcode')
-rw-r--r-- | lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp | 253 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h | 46 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp | 221 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp | 156 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Writer/NaClValueEnumerator.h | 19 |
5 files changed, 5 insertions, 690 deletions
diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp index 8defdb5d1b..593727a2f4 100644 --- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp +++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp @@ -45,12 +45,10 @@ void NaClBitcodeReader::FreeState() { Buffer = 0; std::vector<Type*>().swap(TypeList); ValueList.clear(); - MDValueList.clear(); std::vector<BasicBlock*>().swap(FunctionBBs); std::vector<Function*>().swap(FunctionsWithBodies); DeferredFunctionInfo.clear(); - MDKindMap.clear(); assert(BlockAddrFwdRefs.empty() && "Unresolved blockaddress fwd references"); } @@ -393,45 +391,6 @@ void NaClBitcodeReaderValueList::ResolveConstantForwardRefs() { } } -void NaClBitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) { - if (Idx == size()) { - push_back(V); - return; - } - - if (Idx >= size()) - resize(Idx+1); - - WeakVH &OldV = MDValuePtrs[Idx]; - if (OldV == 0) { - OldV = V; - return; - } - - // If there was a forward reference to this value, replace it. - MDNode *PrevVal = cast<MDNode>(OldV); - OldV->replaceAllUsesWith(V); - MDNode::deleteTemporary(PrevVal); - // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new - // value for Idx. - MDValuePtrs[Idx] = V; -} - -Value *NaClBitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { - if (Idx >= size()) - resize(Idx + 1); - - if (Value *V = MDValuePtrs[Idx]) { - assert(V->getType()->isMetadataTy() && "Type mismatch in value table!"); - return V; - } - - // Create and return a placeholder, which will later be RAUW'd. - Value *V = MDNode::getTemporary(Context, ArrayRef<Value*>()); - MDValuePtrs[Idx] = V; - return V; -} - Type *NaClBitcodeReader::getTypeByID(unsigned ID) { // The type table size is always specified correctly. if (ID >= TypeList.size()) @@ -525,9 +484,6 @@ bool NaClBitcodeReader::ParseTypeTableBody() { case naclbitc::TYPE_CODE_LABEL: // LABEL ResultTy = Type::getLabelTy(Context); break; - case naclbitc::TYPE_CODE_METADATA: // METADATA - ResultTy = Type::getMetadataTy(Context); - break; case naclbitc::TYPE_CODE_X86_MMX: // X86_MMX ResultTy = Type::getX86_MMXTy(Context); break; @@ -737,106 +693,6 @@ bool NaClBitcodeReader::ParseValueSymbolTable() { } } -bool NaClBitcodeReader::ParseMetadata() { - unsigned NextMDValueNo = MDValueList.size(); - - DEBUG(dbgs() << "-> ParseMetadata\n"); - if (Stream.EnterSubBlock(naclbitc::METADATA_BLOCK_ID)) - return Error("Malformed block record"); - - SmallVector<uint64_t, 64> Record; - - // Read all the records. - while (1) { - NaClBitstreamEntry Entry = Stream.advanceSkippingSubblocks(); - - switch (Entry.Kind) { - case NaClBitstreamEntry::SubBlock: // Handled for us already. - case NaClBitstreamEntry::Error: - Error("malformed metadata block"); - return true; - case NaClBitstreamEntry::EndBlock: - DEBUG(dbgs() << "<- ParseMetadata\n"); - return false; - case NaClBitstreamEntry::Record: - // The interesting case. - break; - } - - bool IsFunctionLocal = false; - // Read a record. - Record.clear(); - unsigned Code = Stream.readRecord(Entry.ID, Record); - switch (Code) { - default: // Default behavior: ignore. - break; - case naclbitc::METADATA_NAME: { - // Read name of the named metadata. - SmallString<8> Name(Record.begin(), Record.end()); - Record.clear(); - Code = Stream.ReadCode(); - - // METADATA_NAME is always followed by METADATA_NAMED_NODE. - unsigned NextBitCode = Stream.readRecord(Code, Record); - assert(NextBitCode == naclbitc::METADATA_NAMED_NODE); (void)NextBitCode; - - // Read named metadata elements. - unsigned Size = Record.size(); - NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name); - for (unsigned i = 0; i != Size; ++i) { - MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i])); - if (MD == 0) - return Error("Malformed metadata record"); - NMD->addOperand(MD); - } - break; - } - case naclbitc::METADATA_FN_NODE: - IsFunctionLocal = true; - // fall-through - case naclbitc::METADATA_NODE: { - if (Record.size() % 2 == 1) - return Error("Invalid METADATA_NODE record"); - - unsigned Size = Record.size(); - SmallVector<Value*, 8> Elts; - for (unsigned i = 0; i != Size; i += 2) { - Type *Ty = getTypeByID(Record[i]); - if (!Ty) return Error("Invalid METADATA_NODE record"); - if (Ty->isMetadataTy()) - Elts.push_back(MDValueList.getValueFwdRef(Record[i+1])); - else if (!Ty->isVoidTy()) - Elts.push_back(ValueList.getOrCreateValueFwdRef(Record[i+1], Ty)); - else - Elts.push_back(NULL); - } - Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal); - IsFunctionLocal = false; - MDValueList.AssignValue(V, NextMDValueNo++); - break; - } - case naclbitc::METADATA_STRING: { - SmallString<8> String(Record.begin(), Record.end()); - Value *V = MDString::get(Context, String); - MDValueList.AssignValue(V, NextMDValueNo++); - break; - } - case naclbitc::METADATA_KIND: { - if (Record.size() < 2) - return Error("Invalid METADATA_KIND record"); - - unsigned Kind = Record[0]; - SmallString<8> Name(Record.begin()+1, Record.end()); - - unsigned NewKind = TheModule->getMDKindID(Name.str()); - if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) - return Error("Conflicting METADATA_KIND records"); - break; - } - } - } -} - /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global /// values and aliases that we can. bool NaClBitcodeReader::ResolveGlobalAndAliasInits() { @@ -1416,10 +1272,6 @@ bool NaClBitcodeReader::ParseModule(bool Resume) { if (ParseConstants() || ResolveGlobalAndAliasInits()) return true; break; - case naclbitc::METADATA_BLOCK_ID: - if (ParseMetadata()) - return true; - break; case naclbitc::FUNCTION_BLOCK_ID: // If this is the first function body we've seen, reverse the // FunctionsWithBodies list. @@ -1688,53 +1540,6 @@ bool NaClBitcodeReader::ParseBitcodeInto(Module *M) { } } -/// ParseMetadataAttachment - Parse metadata attachments. -bool NaClBitcodeReader::ParseMetadataAttachment() { - DEBUG(dbgs() << "-> ParseMetadataAttachment\n"); - if (Stream.EnterSubBlock(naclbitc::METADATA_ATTACHMENT_ID)) - return Error("Malformed block record"); - - SmallVector<uint64_t, 64> Record; - while (1) { - NaClBitstreamEntry Entry = Stream.advanceSkippingSubblocks(); - - switch (Entry.Kind) { - case NaClBitstreamEntry::SubBlock: // Handled for us already. - case NaClBitstreamEntry::Error: - return Error("malformed metadata block"); - case NaClBitstreamEntry::EndBlock: - DEBUG(dbgs() << "<- ParseMetadataAttachment\n"); - return false; - case NaClBitstreamEntry::Record: - // The interesting case. - break; - } - - // Read a metadata attachment record. - Record.clear(); - switch (Stream.readRecord(Entry.ID, Record)) { - default: // Default behavior: ignore. - break; - case naclbitc::METADATA_ATTACHMENT: { - 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(I->second, cast<MDNode>(Node)); - } - break; - } - } - } -} - /// ParseFunctionBody - Lazily parse the specified function body block. bool NaClBitcodeReader::ParseFunctionBody(Function *F) { DEBUG(dbgs() << "-> ParseFunctionBody\n"); @@ -1743,7 +1548,6 @@ bool NaClBitcodeReader::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) @@ -1753,8 +1557,6 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { BasicBlock *CurBB = 0; unsigned CurBBNo = 0; - DebugLoc LastLoc; - // Read all the records. SmallVector<uint64_t, 64> Record; while (1) { @@ -1782,14 +1584,6 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { if (ParseValueSymbolTable()) return true; break; - case naclbitc::METADATA_ATTACHMENT_ID: - if (ParseMetadataAttachment()) - return true; - break; - case naclbitc::METADATA_BLOCK_ID: - if (ParseMetadata()) - return true; - break; } continue; @@ -1815,45 +1609,6 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { CurBB = FunctionBBs[0]; continue; - case naclbitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN - // This record indicates that the last instruction is at the same - // location as the previous instruction with a location. - I = 0; - - // Get the last instruction emitted. - if (CurBB && !CurBB->empty()) - I = &CurBB->back(); - else if (CurBBNo && FunctionBBs[CurBBNo-1] && - !FunctionBBs[CurBBNo-1]->empty()) - I = &FunctionBBs[CurBBNo-1]->back(); - - if (I == 0) return Error("Invalid DEBUG_LOC_AGAIN record"); - I->setDebugLoc(LastLoc); - I = 0; - continue; - - case naclbitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] - I = 0; // Get the last instruction emitted. - if (CurBB && !CurBB->empty()) - I = &CurBB->back(); - else if (CurBBNo && FunctionBBs[CurBBNo-1] && - !FunctionBBs[CurBBNo-1]->empty()) - I = &FunctionBBs[CurBBNo-1]->back(); - if (I == 0 || Record.size() < 4) - return Error("Invalid FUNC_CODE_DEBUG_LOC record"); - - unsigned Line = Record[0], Col = Record[1]; - unsigned ScopeID = Record[2], IAID = Record[3]; - - MDNode *Scope = 0, *IA = 0; - if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1)); - if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1)); - LastLoc = DebugLoc::get(Line, Col, Scope, IA); - I->setDebugLoc(LastLoc); - I = 0; - continue; - } - case naclbitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, opval, opcode[, flags]] unsigned OpNum = 0; @@ -2521,9 +2276,6 @@ OutOfRecordLoop: } } - // 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 = @@ -2545,7 +2297,6 @@ OutOfRecordLoop: // Trim the value list down to the size it was before we parsed this function. ValueList.shrinkTo(ModuleValueListSize); - MDValueList.shrinkTo(ModuleMDValueListSize); std::vector<BasicBlock*>().swap(FunctionBBs); DEBUG(dbgs() << "-> ParseFunctionBody\n"); return false; @@ -2738,8 +2489,6 @@ Module *llvm::getNaClLazyBitcodeModule(MemoryBuffer *Buffer, R->materializeForwardReferencedFunctions(); - M->convertMetadataToLibraryList(); // @LOCALMOD - return M; } @@ -2763,8 +2512,6 @@ Module *llvm::getNaClStreamedBitcodeModule(const std::string &name, R->materializeForwardReferencedFunctions(); - M->convertMetadataToLibraryList(); // @LOCALMOD - return M; } diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h index e6ea6266be..5256e07134 100644 --- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h +++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h @@ -93,40 +93,6 @@ public: }; -//===----------------------------------------------------------------------===// -// NaClBitcodeReaderMDValueList Class -//===----------------------------------------------------------------------===// - -class NaClBitcodeReaderMDValueList { - std::vector<WeakVH> MDValuePtrs; - - LLVMContext &Context; -public: - NaClBitcodeReaderMDValueList(LLVMContext& C) : Context(C) {} - - // vector compatibility methods - unsigned size() const { return MDValuePtrs.size(); } - void resize(unsigned N) { MDValuePtrs.resize(N); } - void push_back(Value *V) { MDValuePtrs.push_back(V); } - void clear() { MDValuePtrs.clear(); } - Value *back() const { return MDValuePtrs.back(); } - void pop_back() { MDValuePtrs.pop_back(); } - bool empty() const { return MDValuePtrs.empty(); } - - Value *operator[](unsigned i) const { - assert(i < MDValuePtrs.size()); - return MDValuePtrs[i]; - } - - void shrinkTo(unsigned N) { - assert(N <= size() && "Invalid shrinkTo request!"); - MDValuePtrs.resize(N); - } - - Value *getValueFwdRef(unsigned Idx); - void AssignValue(Value *V, unsigned Idx); -}; - class NaClBitcodeReader : public GVMaterializer { NaClBitcodeHeader Header; // Header fields of the PNaCl bitcode file. LLVMContext &Context; @@ -143,7 +109,6 @@ class NaClBitcodeReader : public GVMaterializer { std::vector<Type*> TypeList; NaClBitcodeReaderValueList ValueList; - NaClBitcodeReaderMDValueList MDValueList; SmallVector<Instruction *, 64> InstructionList; SmallVector<SmallVector<uint64_t, 64>, 64> UseListRecords; @@ -163,9 +128,6 @@ class NaClBitcodeReader : public GVMaterializer { typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; UpgradedIntrinsicMap UpgradedIntrinsics; - // Map the bitcode's custom MDKind ID to the Module's MDKind ID. - DenseMap<unsigned, unsigned> MDKindMap; - // Several operations happen after the module header has been read, but // before function bodies are processed. This keeps track of whether // we've done this yet. @@ -198,7 +160,7 @@ public: bool AcceptSupportedOnly = true) : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false), LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false), - ErrorString(0), ValueList(C), MDValueList(C), + ErrorString(0), ValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), AcceptSupportedBitcodeOnly(AcceptSupportedOnly) { } @@ -206,7 +168,7 @@ public: bool AcceptSupportedOnly = true) : Context(C), TheModule(0), Buffer(0), BufferOwned(false), LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false), - ErrorString(0), ValueList(C), MDValueList(C), + ErrorString(0), ValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), AcceptSupportedBitcodeOnly(AcceptSupportedOnly) { } @@ -248,8 +210,6 @@ private: // Gets or creates the (function-level) forward referenced value for // ID with the given type. Value *getOrCreateFnValueByID(unsigned ID, Type *Ty) { - if (Ty && Ty->isMetadataTy()) - return MDValueList.getValueFwdRef(ID); return ValueList.getOrCreateValueFwdRef(ID, Ty); } // Returns the value associated with ID. The value must already exist, @@ -330,8 +290,6 @@ private: bool ParseFunctionBody(Function *F); bool GlobalCleanup(); bool ResolveGlobalAndAliasInits(); - bool ParseMetadata(); - bool ParseMetadataAttachment(); bool ParseUseLists(); bool InitStream(); bool InitStreamFromBuffer(); diff --git a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp index bfd12c55a9..be75af1e40 100644 --- a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp +++ b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp @@ -89,10 +89,6 @@ enum { TYPE_ARRAY_ABBREV, TYPE_MAX_ABBREV = TYPE_ARRAY_ABBREV, - // META_DATA_BLOCK abbrev id's. - METADATA_STRING_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, - METADATA_MAX_ABBREV = METADATA_STRING_ABBREV, - // MODULE_BLOCK abbrev id's. MODULE_GLOBALVAR_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, MODULE_MAX_ABBREV = MODULE_GLOBALVAR_ABBREV, @@ -300,7 +296,6 @@ static void WriteTypeTable(const NaClValueEnumerator &VE, case Type::FP128TyID: Code = naclbitc::TYPE_CODE_FP128; break; case Type::PPC_FP128TyID: Code = naclbitc::TYPE_CODE_PPC_FP128; break; case Type::LabelTyID: Code = naclbitc::TYPE_CODE_LABEL; break; - case Type::MetadataTyID: Code = naclbitc::TYPE_CODE_METADATA; break; case Type::X86_MMXTyID: Code = naclbitc::TYPE_CODE_X86_MMX; break; case Type::IntegerTyID: // INTEGER: [width] @@ -593,167 +588,6 @@ static uint64_t GetOptimizationFlags(const Value *V) { return Flags; } -static void WriteMDNode(const MDNode *N, - const NaClValueEnumerator &VE, - NaClBitstreamWriter &Stream, - SmallVector<uint64_t, 64> &Record) { - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { - if (N->getOperand(i)) { - Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); - Record.push_back(VE.getValueID(N->getOperand(i))); - } else { - Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext()))); - Record.push_back(0); - } - } - unsigned MDCode = N->isFunctionLocal() ? naclbitc::METADATA_FN_NODE : - naclbitc::METADATA_NODE; - Stream.EmitRecord(MDCode, Record, 0); - Record.clear(); -} - -static void WriteModuleMetadata(const Module *M, - const NaClValueEnumerator &VE, - NaClBitstreamWriter &Stream) { - DEBUG(dbgs() << "-> WriteModuleMetadata\n"); - const NaClValueEnumerator::ValueList &Vals = VE.getMDValues(); - bool StartedMetadataBlock = false; - SmallVector<uint64_t, 64> Record; - for (unsigned i = 0, e = Vals.size(); i != e; ++i) { - - if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { - if (!N->isFunctionLocal() || !N->getFunction()) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID); - StartedMetadataBlock = true; - } - WriteMDNode(N, VE, Stream, Record); - } - } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID); - StartedMetadataBlock = true; - } - - // Code: [strchar x N] - Record.append(MDS->begin(), MDS->end()); - - // Emit the finished record. - Stream.EmitRecord(naclbitc::METADATA_STRING, Record, - METADATA_STRING_ABBREV); - Record.clear(); - } - } - - // Write named metadata. - for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), - E = M->named_metadata_end(); I != E; ++I) { - const NamedMDNode *NMD = I; - if (!StartedMetadataBlock) { - Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID); - StartedMetadataBlock = true; - } - - // Write name. - StringRef Str = NMD->getName(); - for (unsigned i = 0, e = Str.size(); i != e; ++i) - Record.push_back(Str[i]); - Stream.EmitRecord(naclbitc::METADATA_NAME, Record, 0/*TODO*/); - Record.clear(); - - // Write named metadata operands. - for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) - Record.push_back(VE.getValueID(NMD->getOperand(i))); - Stream.EmitRecord(naclbitc::METADATA_NAMED_NODE, Record, 0); - Record.clear(); - } - - if (StartedMetadataBlock) - Stream.ExitBlock(); - - DEBUG(dbgs() << "<- WriteModuleMetadata\n"); -} - -static void WriteFunctionLocalMetadata(const Function &F, - const NaClValueEnumerator &VE, - NaClBitstreamWriter &Stream) { - DEBUG(dbgs() << "-> WriteFunctionLocalMetadata\n"); - bool StartedMetadataBlock = false; - SmallVector<uint64_t, 64> Record; - const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); - for (unsigned i = 0, e = Vals.size(); i != e; ++i) - if (const MDNode *N = Vals[i]) - if (N->isFunctionLocal() && N->getFunction() == &F) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID); - StartedMetadataBlock = true; - } - WriteMDNode(N, VE, Stream, Record); - } - - if (StartedMetadataBlock) - Stream.ExitBlock(); - DEBUG(dbgs() << "<- WriteFunctionLocalMetadata\n"); -} - -static void WriteMetadataAttachment(const Function &F, - const NaClValueEnumerator &VE, - NaClBitstreamWriter &Stream) { - Stream.EnterSubblock(naclbitc::METADATA_ATTACHMENT_ID); - - SmallVector<uint64_t, 64> Record; - - // Write metadata attachments - // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] - SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; - - for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) - for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); - I != E; ++I) { - MDs.clear(); - I->getAllMetadataOtherThanDebugLoc(MDs); - - // If no metadata, ignore instruction. - if (MDs.empty()) continue; - - Record.push_back(VE.getInstructionID(I)); - - for (unsigned i = 0, e = MDs.size(); i != e; ++i) { - Record.push_back(MDs[i].first); - Record.push_back(VE.getValueID(MDs[i].second)); - } - Stream.EmitRecord(naclbitc::METADATA_ATTACHMENT, Record, 0); - Record.clear(); - } - - Stream.ExitBlock(); -} - -static void WriteModuleMetadataStore(const Module *M, - NaClBitstreamWriter &Stream) { - SmallVector<uint64_t, 64> Record; - - // Write metadata kinds - // METADATA_KIND - [n x [id, name]] - SmallVector<StringRef, 8> Names; - M->getMDKindNames(Names); - - if (Names.empty()) return; - - Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID); - - for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { - Record.push_back(MDKindID); - StringRef KName = Names[MDKindID]; - Record.append(KName.begin(), KName.end()); - - Stream.EmitRecord(naclbitc::METADATA_KIND, Record, 0); - Record.clear(); - } - - Stream.ExitBlock(); -} - static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { Vals.push_back(NaClEncodeSignRotatedValue((int64_t)V)); } @@ -1536,16 +1370,9 @@ static void WriteFunction(const Function &F, NaClValueEnumerator &VE, VE.getFunctionConstantRange(CstStart, CstEnd); WriteConstants(CstStart, CstEnd, VE, Stream, false); - // If there is function-local metadata, emit it now. - WriteFunctionLocalMetadata(F, VE, Stream); - // Keep a running idea of what the instruction ID is. unsigned InstID = CstEnd; - bool NeedsMetadataAttachment = false; - - DebugLoc LastDL; - // Finally, emit all the instructions, in order. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); @@ -1554,37 +1381,11 @@ static void WriteFunction(const Function &F, NaClValueEnumerator &VE, if (!I->getType()->isVoidTy()) ++InstID; - - // If the instruction has metadata, write a metadata attachment later. - NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc(); - - // If the instruction has a debug location, emit it. - DebugLoc DL = I->getDebugLoc(); - if (DL.isUnknown()) { - // nothing todo. - } else if (DL == LastDL) { - // Just repeat the same debug loc as last time. - Stream.EmitRecord(naclbitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals); - } else { - MDNode *Scope, *IA; - DL.getScopeAndInlinedAt(Scope, IA, I->getContext()); - - Vals.push_back(DL.getLine()); - Vals.push_back(DL.getCol()); - Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0); - Vals.push_back(IA ? VE.getValueID(IA)+1 : 0); - Stream.EmitRecord(naclbitc::FUNC_CODE_DEBUG_LOC, Vals); - Vals.clear(); - - LastDL = DL; - } } // Emit names for all the instructions etc. WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); - if (NeedsMetadataAttachment) - WriteMetadataAttachment(F, VE, Stream); VE.purgeFunction(); Stream.ExitBlock(); } @@ -1593,8 +1394,7 @@ static void WriteFunction(const Function &F, NaClValueEnumerator &VE, static void WriteBlockInfo(const NaClValueEnumerator &VE, NaClBitstreamWriter &Stream) { // We only want to emit block info records for blocks that have multiple - // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK, - // and METADATA_BLOCK_ID. + // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. // Other blocks can define their abbrevs inline. Stream.EnterBlockInfoBlock(); @@ -1761,16 +1561,6 @@ static void WriteBlockInfo(const NaClValueEnumerator &VE, llvm_unreachable("Unexpected abbrev ordering!"); } - { // Abbrev for METADATA_STRING. - NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); - Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::METADATA_STRING)); - Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); - Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); - if (Stream.EmitBlockInfoAbbrev(naclbitc::METADATA_BLOCK_ID, - Abbv) != METADATA_STRING_ABBREV) - llvm_unreachable("Unexpected abbrev ordering!"); - } - Stream.ExitBlock(); } @@ -1800,12 +1590,6 @@ static void WriteModule(const Module *M, NaClBitstreamWriter &Stream) { // Emit constants. WriteModuleConstants(VE, Stream); - // Emit metadata. - WriteModuleMetadata(M, VE, Stream); - - // Emit metadata. - WriteModuleMetadataStore(M, Stream); - // Emit names for globals/functions etc. WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); @@ -1867,9 +1651,6 @@ void llvm::NaClWriteBitcodeToFile(const Module *M, raw_ostream &Out) { SmallVector<char, 0> Buffer; Buffer.reserve(256*1024); - // Convert Deplib info to metadata - M->convertLibraryListToMetadata(); // @LOCALMOD - // Emit the module into the buffer. { NaClBitstreamWriter Stream(Buffer); diff --git a/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp b/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp index 4ff9b47bcc..feb14cdb0c 100644 --- a/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp +++ b/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp @@ -71,12 +71,9 @@ NaClValueEnumerator::NaClValueEnumerator(const Module *M) { I != E; ++I) EnumerateValue(I->getAliasee()); - // Insert constants and metadata that are named at module level into the slot + // Insert constants that are named at module level into the slot // pool so that the module symbol table can refer to them... EnumerateValueSymbolTable(M->getValueSymbolTable()); - EnumerateNamedMetadata(M); - - SmallVector<std::pair<unsigned, MDNode*>, 8> MDs; // Enumerate types used by function bodies and argument lists. for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { @@ -89,26 +86,9 @@ NaClValueEnumerator::NaClValueEnumerator(const Module *M) { for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){ for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) { - if (MDNode *MD = dyn_cast<MDNode>(*OI)) - if (MD->isFunctionLocal() && MD->getFunction()) - // These will get enumerated during function-incorporation. - continue; EnumerateOperandType(*OI); } EnumerateType(I->getType()); - - // Enumerate metadata attached with this instruction. - MDs.clear(); - I->getAllMetadataOtherThanDebugLoc(MDs); - for (unsigned i = 0, e = MDs.size(); i != e; ++i) - EnumerateMetadata(MDs[i].second); - - if (!I->getDebugLoc().isUnknown()) { - MDNode *Scope, *IA; - I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext()); - if (Scope) EnumerateMetadata(Scope); - if (IA) EnumerateMetadata(IA); - } } } @@ -164,12 +144,6 @@ void NaClValueEnumerator::setInstructionID(const Instruction *I) { } unsigned NaClValueEnumerator::getValueID(const Value *V) const { - if (isa<MDNode>(V) || isa<MDString>(V)) { - ValueMapType::const_iterator I = MDValueMap.find(V); - assert(I != MDValueMap.end() && "Value not in slotcalculator!"); - return I->second-1; - } - ValueMapType::const_iterator I = ValueMap.find(V); assert(I != ValueMap.end() && "Value not in slotcalculator!"); return I->second-1; @@ -178,8 +152,6 @@ unsigned NaClValueEnumerator::getValueID(const Value *V) const { void NaClValueEnumerator::dump() const { print(dbgs(), ValueMap, "Default"); dbgs() << '\n'; - print(dbgs(), MDValueMap, "MetaData"); - dbgs() << '\n'; } void NaClValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map, @@ -256,97 +228,6 @@ void NaClValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) EnumerateValue(VI->getValue()); } -/// EnumerateNamedMetadata - Insert all of the values referenced by -/// named metadata in the specified module. -void NaClValueEnumerator::EnumerateNamedMetadata(const Module *M) { - for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), - E = M->named_metadata_end(); I != E; ++I) - EnumerateNamedMDNode(I); -} - -void NaClValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) { - for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) - EnumerateMetadata(MD->getOperand(i)); -} - -/// EnumerateMDNodeOperands - Enumerate all non-function-local values -/// and types referenced by the given MDNode. -void NaClValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) { - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { - if (Value *V = N->getOperand(i)) { - if (isa<MDNode>(V) || isa<MDString>(V)) - EnumerateMetadata(V); - else if (!isa<Instruction>(V) && !isa<Argument>(V)) - EnumerateValue(V); - } else - EnumerateType(Type::getVoidTy(N->getContext())); - } -} - -void NaClValueEnumerator::EnumerateMetadata(const Value *MD) { - assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind"); - - // Enumerate the type of this value. - EnumerateType(MD->getType()); - - const MDNode *N = dyn_cast<MDNode>(MD); - - // In the module-level pass, skip function-local nodes themselves, but - // do walk their operands. - if (N && N->isFunctionLocal() && N->getFunction()) { - EnumerateMDNodeOperands(N); - return; - } - - // Check to see if it's already in! - unsigned &MDValueID = MDValueMap[MD]; - if (MDValueID) { - // Increment use count. - MDValues[MDValueID-1].second++; - return; - } - MDValues.push_back(std::make_pair(MD, 1U)); - MDValueID = MDValues.size(); - - // Enumerate all non-function-local operands. - if (N) - EnumerateMDNodeOperands(N); -} - -/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata -/// information reachable from the given MDNode. -void NaClValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) { - assert(N->isFunctionLocal() && N->getFunction() && - "EnumerateFunctionLocalMetadata called on non-function-local mdnode!"); - - // Enumerate the type of this value. - EnumerateType(N->getType()); - - // Check to see if it's already in! - unsigned &MDValueID = MDValueMap[N]; - if (MDValueID) { - // Increment use count. - MDValues[MDValueID-1].second++; - return; - } - MDValues.push_back(std::make_pair(N, 1U)); - MDValueID = MDValues.size(); - - // To incoroporate function-local information visit all function-local - // MDNodes and all function-local values they reference. - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) - if (Value *V = N->getOperand(i)) { - if (MDNode *O = dyn_cast<MDNode>(V)) { - if (O->isFunctionLocal() && O->getFunction()) - EnumerateFunctionLocalMetadata(O); - } else if (isa<Instruction>(V) || isa<Argument>(V)) - EnumerateValue(V); - } - - // Also, collect all function-local MDNodes for easy access. - FunctionLocalMDs.push_back(N); -} - void NaClValueEnumerator::EnumerateValue(const Value *V) { assert(!V->getType()->isVoidTy() && "Can't insert void values!"); assert(!isa<MDNode>(V) && !isa<MDString>(V) && @@ -482,20 +363,12 @@ void NaClValueEnumerator::EnumerateOperandType(const Value *V) { EnumerateOperandType(Op); } - - if (const MDNode *N = dyn_cast<MDNode>(V)) { - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) - if (Value *Elem = N->getOperand(i)) - EnumerateOperandType(Elem); - } - } else if (isa<MDString>(V) || isa<MDNode>(V)) - EnumerateMetadata(V); + } } void NaClValueEnumerator::incorporateFunction(const Function &F |