diff options
author | Chris Lattner <sabre@nondot.org> | 2009-12-28 23:41:32 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-12-28 23:41:32 +0000 |
commit | 3990b121cf4a0b280ed3e54cf13870cbf4259e78 (patch) | |
tree | 9d5ea8aa8a5f0b166334346e372f143b832b9d03 /lib/VMCore | |
parent | f309880ad86114cda05037538c46123f6cda1a7e (diff) | |
download | external_llvm-3990b121cf4a0b280ed3e54cf13870cbf4259e78.zip external_llvm-3990b121cf4a0b280ed3e54cf13870cbf4259e78.tar.gz external_llvm-3990b121cf4a0b280ed3e54cf13870cbf4259e78.tar.bz2 |
This is a major cleanup of the instruction metadata interfaces that
I asked Devang to do back on Sep 27. Instead of going through the
MetadataContext class with methods like getMD() and getMDs(), just
ask the instruction directly for its metadata with getMetadata()
and getAllMetadata().
This includes a variety of other fixes and improvements: previously
all Value*'s were bloated because the HasMetadata bit was thrown into
value, adding a 9th bit to a byte. Now this is properly sunk down to
the Instruction class (the only place where it makes sense) and it
will be folded away somewhere soon.
This also fixes some confusion in getMDs and its clients about
whether the returned list is indexed by the MDID or densely packed.
This is now returned sorted and densely packed and the comments make
this clear.
This introduces a number of fixme's which I'll follow up on.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92235 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/AsmWriter.cpp | 43 | ||||
-rw-r--r-- | lib/VMCore/IRBuilder.cpp | 2 | ||||
-rw-r--r-- | lib/VMCore/Instruction.cpp | 14 | ||||
-rw-r--r-- | lib/VMCore/Metadata.cpp | 248 | ||||
-rw-r--r-- | lib/VMCore/Value.cpp | 19 |
5 files changed, 164 insertions, 162 deletions
diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index c9c9bf0..824eac0 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -21,11 +21,9 @@ #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/InlineAsm.h" -#include "llvm/Instruction.h" -#include "llvm/Instructions.h" +#include "llvm/IntrinsicInst.h" #include "llvm/LLVMContext.h" #include "llvm/Operator.h" -#include "llvm/Metadata.h" #include "llvm/Module.h" #include "llvm/ValueSymbolTable.h" #include "llvm/TypeSymbolTable.h" @@ -680,30 +678,30 @@ void SlotTracker::processFunction() { ST_DEBUG("Inserting Instructions:\n"); - MetadataContext &TheMetadata = TheFunction->getContext().getMetadata(); - typedef SmallVector<std::pair<unsigned, MDNode*>, 2> MDMapTy; - MDMapTy MDs; + SmallVector<std::pair<unsigned, MDNode*>, 2> MDForInst; // Add all of the basic blocks and instructions with no names. for (Function::const_iterator BB = TheFunction->begin(), E = TheFunction->end(); BB != E; ++BB) { if (!BB->hasName()) CreateFunctionSlot(BB); + for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { - if (I->getType() != Type::getVoidTy(TheFunction->getContext()) && - !I->hasName()) + if (!I->getType()->isVoidTy() && !I->hasName()) CreateFunctionSlot(I); - for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) - if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i))) - CreateMetadataSlot(N); + + // Intrinsics can directly use metadata. + if (isa<IntrinsicInst>(I)) + for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) + if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i))) + CreateMetadataSlot(N); // Process metadata attached with this instruction. - MDs.clear(); - TheMetadata.getMDs(I, MDs); - for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME; - ++MI) - CreateMetadataSlot(MI->second); + MDForInst.clear(); + I->getAllMetadata(MDForInst); + for (unsigned i = 0, e = MDForInst.size(); i != e; ++i) + CreateMetadataSlot(MDForInst[i].second); } } @@ -2076,14 +2074,11 @@ void AssemblyWriter::printInstruction(const Instruction &I) { // Print Metadata info. if (!MDNames.empty()) { - MetadataContext &TheMetadata = I.getContext().getMetadata(); - typedef SmallVector<std::pair<unsigned, MDNode*>, 2> MDMapTy; - MDMapTy MDs; - TheMetadata.getMDs(&I, MDs); - for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME; - ++MI) - Out << ", !" << MDNames[MI->first] - << " !" << Machine.getMetadataSlot(MI->second); + SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD; + I.getAllMetadata(InstMD); + for (unsigned i = 0, e = InstMD.size(); i != e; ++i) + Out << ", !" << MDNames[InstMD[i].first] + << " !" << Machine.getMetadataSlot(InstMD[i].second); } printInfoComment(I); } diff --git a/lib/VMCore/IRBuilder.cpp b/lib/VMCore/IRBuilder.cpp index 0b4a109..4c0299c 100644 --- a/lib/VMCore/IRBuilder.cpp +++ b/lib/VMCore/IRBuilder.cpp @@ -43,7 +43,7 @@ void IRBuilderBase::SetCurrentDebugLocation(MDNode *L) { void IRBuilderBase::SetInstDebugLocation(Instruction *I) const { if (CurDbgLocation) - Context.getMetadata().addMD(DbgMDKind, CurDbgLocation, I); + I->setMetadata(DbgMDKind, CurDbgLocation); } const Type *IRBuilderBase::getCurrentFunctionReturnType() const { diff --git a/lib/VMCore/Instruction.cpp b/lib/VMCore/Instruction.cpp index ce253d6..f468c1b 100644 --- a/lib/VMCore/Instruction.cpp +++ b/lib/VMCore/Instruction.cpp @@ -24,7 +24,8 @@ using namespace llvm; Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, Instruction *InsertBefore) - : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { + : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0), + HasMetadata(false) { // Make sure that we get added to a basicblock LeakDetector::addGarbageObject(this); @@ -38,7 +39,8 @@ Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd) - : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { + : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0), + HasMetadata(false) { // Make sure that we get added to a basicblock LeakDetector::addGarbageObject(this); @@ -51,10 +53,8 @@ Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, // Out of line virtual method, so the vtable, etc has a home. Instruction::~Instruction() { assert(Parent == 0 && "Instruction still linked in the program!"); - if (hasMetadata()) { - LLVMContext &Context = getContext(); - Context.pImpl->TheMetadata.ValueIsDeleted(this); - } + if (HasMetadata) + getContext().pImpl->TheMetadata.ValueIsDeleted(this); } @@ -464,7 +464,7 @@ bool Instruction::isSafeToSpeculativelyExecute() const { Instruction *Instruction::clone() const { Instruction *New = clone_impl(); New->SubclassOptionalData = SubclassOptionalData; - if (hasMetadata()) + if (HasMetadata) getContext().pImpl->TheMetadata.ValueIsCloned(this, New); return New; } diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index a516d7b..1374754 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -261,32 +261,27 @@ private: StringMap<unsigned> MDHandlerNames; public: + // Name <-> ID mapping methods. unsigned getMDKindID(StringRef Name); - - /// getMD - Get the metadata of given kind attached to an Instruction. - /// If the metadata is not found then return 0. - MDNode *getMD(unsigned Kind, const Instruction *Inst); - - /// getMDs - Get the metadata attached to an Instruction. - void getMDs(const Instruction *Inst, - SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const; - - /// addMD - Attach the metadata of given kind to an Instruction. - void addMD(unsigned Kind, MDNode *Node, Instruction *Inst); + void getMDKindNames(SmallVectorImpl<StringRef> &) const; - /// removeMD - Remove metadata of given kind attached with an instruction. - void removeMD(unsigned Kind, Instruction *Inst); + // Instruction metadata methods. + MDNode *getMetadata(const Instruction *Inst, unsigned Kind); + void getAllMetadata(const Instruction *Inst, + SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs)const; + + void setMetadata(Instruction *Inst, unsigned Kind, MDNode *Node); + /// removeAllMetadata - Remove all metadata attached with an instruction. void removeAllMetadata(Instruction *Inst); - + + + /// copyMD - If metadata is attached with Instruction In1 then attach /// the same metadata to In2. void copyMD(Instruction *In1, Instruction *In2); - - /// getMDKindNames - Populate client-supplied smallvector using custom - /// metadata name and ID. - void getMDKindNames(SmallVectorImpl<StringRef> &) const; + /// ValueIsDeleted - This handler is used to update metadata store /// when a value is deleted. @@ -308,49 +303,96 @@ unsigned MetadataContextImpl::getMDKindID(StringRef Name) { // If this is new, assign it its ID. if (Entry == 0) Entry = MDHandlerNames.size(); - return Entry; } -/// addMD - Attach the metadata of given kind to an Instruction. -void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node, - Instruction *Inst) { - assert(Node && "Invalid null MDNode"); - Inst->HasMetadata = true; - MDMapTy &Info = MetadataStore[Inst]; - if (Info.empty()) { - Info.push_back(std::make_pair(MDKind, Node)); - MetadataStore.insert(std::make_pair(Inst, Info)); - return; - } +/// getHandlerNames - Populate client supplied smallvector using custome +/// metadata name and ID. +void MetadataContextImpl:: +getMDKindNames(SmallVectorImpl<StringRef> &Names) const { + Names.resize(MDHandlerNames.size()+1); + Names[0] = ""; + for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(), + E = MDHandlerNames.end(); I != E; ++I) + // MD Handlers are numbered from 1. + Names[I->second] = I->first(); +} - // If there is an entry for this MDKind then replace it. - for (unsigned i = 0, e = Info.size(); i != e; ++i) { - MDPairTy &P = Info[i]; - if (P.first == MDKind) { - Info[i] = std::make_pair(MDKind, Node); - return; - } - } - // Otherwise add a new entry. - Info.push_back(std::make_pair(MDKind, Node)); +/// getMetadata - Get the metadata of given kind attached to an Instruction. +/// If the metadata is not found then return 0. +MDNode *MetadataContextImpl:: +getMetadata(const Instruction *Inst, unsigned MDKind) { + MDMapTy &Info = MetadataStore[Inst]; + assert(Inst->hasMetadata() && !Info.empty() && "Shouldn't have called this"); + + for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I) + if (I->first == MDKind) + return I->second; + return 0; } -/// removeMD - Remove metadata of given kind attached with an instruction. -void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) { - MDStoreTy::iterator I = MetadataStore.find(Inst); - if (I == MetadataStore.end()) +/// getAllMetadata - Get all of the metadata attached to an Instruction. +void MetadataContextImpl:: +getAllMetadata(const Instruction *Inst, + SmallVectorImpl<std::pair<unsigned, MDNode*> > &Result) const { + assert(Inst->hasMetadata() && MetadataStore.find(Inst) != MetadataStore.end() + && "Shouldn't have called this"); + const MDMapTy &Info = MetadataStore.find(Inst)->second; + assert(!Info.empty() && "Shouldn't have called this"); + + Result.clear(); + Result.append(Info.begin(), Info.end()); + + // Sort the resulting array so it is stable. + if (Result.size() > 1) + array_pod_sort(Result.begin(), Result.end()); +} + + +void MetadataContextImpl::setMetadata(Instruction *Inst, unsigned Kind, + MDNode *Node) { + // Handle the case when we're adding/updating metadata on an instruction. + if (Node) { + MDMapTy &Info = MetadataStore[Inst]; + assert(!Info.empty() == Inst->HasMetadata && "HasMetadata bit is wonked"); + if (Info.empty()) { + Inst->HasMetadata = true; + } else { + // Handle replacement of an existing value. + for (unsigned i = 0, e = Info.size(); i != e; ++i) + if (Info[i].first == Kind) { + Info[i].second = Node; + return; + } + } + + // No replacement, just add it to the list. + Info.push_back(std::make_pair(Kind, Node)); return; + } + + // Otherwise, we're removing metadata from an instruction. + assert(Inst->HasMetadata && MetadataStore.count(Inst) && + "HasMetadata bit out of date!"); + MDMapTy &Info = MetadataStore[Inst]; - MDMapTy &Info = I->second; - for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) { - MDPairTy &P = *MI; - if (P.first == Kind) { - Info.erase(MI); + // Common case is removing the only entry. + if (Info.size() == 1 && Info[0].first == Kind) { + MetadataStore.erase(Inst); + Inst->HasMetadata = false; + return; + } + + // Handle replacement of an existing value. + for (unsigned i = 0, e = Info.size(); i != e; ++i) + if (Info[i].first == Kind) { + Info[i] = Info.back(); + Info.pop_back(); + assert(!Info.empty() && "Removing last entry should be handled above"); return; } - } + // Otherwise, removing an entry that doesn't exist on the instruction. } /// removeAllMetadata - Remove all metadata attached with an instruction. @@ -359,6 +401,7 @@ void MetadataContextImpl::removeAllMetadata(Instruction *Inst) { Inst->HasMetadata = false; } + /// copyMD - If metadata is attached with Instruction In1 then attach /// the same metadata to In2. void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) { @@ -366,48 +409,9 @@ void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) { MDMapTy &In1Info = MetadataStore[In1]; if (In1Info.empty()) return; - + for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I) - addMD(I->first, I->second, In2); -} - -/// getMD - Get the metadata of given kind attached to an Instruction. -/// If the metadata is not found then return 0. -MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) { - MDMapTy &Info = MetadataStore[Inst]; - if (Info.empty()) - return NULL; - - for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I) - if (I->first == MDKind) - return I->second; - return NULL; -} - -/// getMDs - Get the metadata attached to an Instruction. -void MetadataContextImpl:: -getMDs(const Instruction *Inst, - SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const { - MDStoreTy::const_iterator I = MetadataStore.find(Inst); - if (I == MetadataStore.end()) - return; - MDs.resize(I->second.size()); - for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end(); - MI != ME; ++MI) - // MD kinds are numbered from 1. - MDs[MI->first - 1] = std::make_pair(MI->first, MI->second); -} - -/// getHandlerNames - Populate client supplied smallvector using custome -/// metadata name and ID. -void MetadataContextImpl:: -getMDKindNames(SmallVectorImpl<StringRef> &Names) const { - Names.resize(MDHandlerNames.size()+1); - Names[0] = ""; - for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(), - E = MDHandlerNames.end(); I != E; ++I) - // MD Handlers are numbered from 1. - Names[I->second] = I->first(); + In2->setMetadata(I->first, I->second); } /// ValueIsCloned - This handler is used to update metadata store @@ -421,7 +425,7 @@ void MetadataContextImpl::ValueIsCloned(const Instruction *In1, // FIXME: Give all metadata handlers a chance to adjust. MDMapTy &In1Info = I->second; for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I) - addMD(I->first, I->second, In2); + In2->setMetadata(I->first, I->second); } /// ValueIsRAUWd - This handler is used when V1's all uses are replaced by @@ -463,34 +467,6 @@ unsigned MetadataContext::getMDKindID(StringRef Name) const { return pImpl->getMDKindID(Name); } -/// getMD - Get the metadata of given kind attached to an Instruction. -/// If the metadata is not found then return 0. -MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) { - return pImpl->getMD(Kind, Inst); -} - -/// getMDs - Get the metadata attached to an Instruction. -void MetadataContext:: -getMDs(const Instruction *Inst, - SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const { - return pImpl->getMDs(Inst, MDs); -} - -/// addMD - Attach the metadata of given kind to an Instruction. -void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) { - pImpl->addMD(Kind, Node, Inst); -} - -/// removeMD - Remove metadata of given kind attached with an instruction. -void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) { - pImpl->removeMD(Kind, Inst); -} - -/// removeAllMetadata - Remove all metadata attached with an instruction. -void MetadataContext::removeAllMetadata(Instruction *Inst) { - pImpl->removeAllMetadata(Inst); -} - /// copyMD - If metadata is attached with Instruction In1 then attach /// the same metadata to In2. void MetadataContext::copyMD(Instruction *In1, Instruction *In2) { @@ -517,3 +493,35 @@ void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) { void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) { pImpl->ValueIsCloned(In1, In2); } + +//===----------------------------------------------------------------------===// +// Instruction Metadata method implementations. +// + +void Instruction::setMetadata(const char *Kind, MDNode *Node) { + if (Node == 0 && !hasMetadata()) return; + setMetadata(getContext().getMetadata().getMDKindID(Kind), Node); +} + +MDNode *Instruction::getMetadataImpl(const char *Kind) const { + return getMetadataImpl(getContext().getMetadata().getMDKindID(Kind)); +} + +/// setMetadata - Set the metadata of of the specified kind to the specified +/// node. This updates/replaces metadata if already present, or removes it if +/// Node is null. +void Instruction::setMetadata(unsigned KindID, MDNode *Node) { + if (Node == 0 && !hasMetadata()) return; + + getContext().getMetadata().pImpl->setMetadata(this, KindID, Node); +} + +MDNode *Instruction::getMetadataImpl(unsigned KindID) const { + return getContext().getMetadata().pImpl->getMetadata(this, KindID); +} + +void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, + MDNode*> > &Result)const { + getContext().getMetadata().pImpl->getAllMetadata(this, Result); +} + diff --git a/lib/VMCore/Value.cpp b/lib/VMCore/Value.cpp index 826e8a1..0155fa5 100644 --- a/lib/VMCore/Value.cpp +++ b/lib/VMCore/Value.cpp @@ -41,7 +41,7 @@ static inline const Type *checkType(const Type *Ty) { } Value::Value(const Type *ty, unsigned scid) - : SubclassID(scid), HasValueHandle(0), HasMetadata(0), + : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), VTy(checkType(ty)), UseList(0), Name(0) { if (isa<CallInst>(this) || isa<InvokeInst>(this)) @@ -57,11 +57,6 @@ Value::Value(const Type *ty, unsigned scid) } Value::~Value() { - if (HasMetadata) { - LLVMContext &Context = getContext(); - Context.pImpl->TheMetadata.ValueIsDeleted(this); - } - // Notify all ValueHandles (if present) that this value is going away. if (HasValueHandle) ValueHandleBase::ValueIsDeleted(this); @@ -306,10 +301,14 @@ void Value::uncheckedReplaceAllUsesWith(Value *New) { // Notify all ValueHandles (if present) that this value is going away. if (HasValueHandle) ValueHandleBase::ValueIsRAUWd(this, New); - if (HasMetadata) { - LLVMContext &Context = getContext(); - Context.pImpl->TheMetadata.ValueIsRAUWd(this, New); - } + + // FIXME: It doesn't make sense at all for metadata to follow RAUW. + if (Instruction *I = dyn_cast<Instruction>(this)) + if (I->hasMetadata()) { + LLVMContext &Context = getContext(); + // FIXME: NUKE ValueIsRAUWd?? + Context.pImpl->TheMetadata.ValueIsRAUWd(this, New); + } while (!use_empty()) { Use &U = *UseList; |