StringMapEntry<MDString *> &Entry =
pImpl->MDStringCache.GetOrCreateValue(Str);
MDString *&S = Entry.getValue();
- if (S) return S;
-
- return S =
- new MDString(Context, Entry.getKey());
+ if (!S) S = new MDString(Context, Entry.getKey());
+ return S;
+}
+
+MDString *MDString::get(LLVMContext &Context, const char *Str) {
+ LLVMContextImpl *pImpl = Context.pImpl;
+ StringMapEntry<MDString *> &Entry =
+ pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
+ MDString *&S = Entry.getValue();
+ if (!S) S = new MDString(Context, Entry.getKey());
+ return S;
}
//===----------------------------------------------------------------------===//
// MDNode implementation.
//
-MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals)
+MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
+ Function *LocalFunc)
: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
NodeSize = NumVals;
Node = new ElementVH[NodeSize];
ElementVH *Ptr = Node;
for (unsigned i = 0; i != NumVals; ++i)
*Ptr++ = ElementVH(Vals[i], this);
+ LocalFunction = LocalFunc;
}
void MDNode::Profile(FoldingSetNodeID &ID) const {
ID.AddPointer(getElement(i));
}
-MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
+MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
+ Function *LocalFunction) {
LLVMContextImpl *pImpl = Context.pImpl;
FoldingSetNodeID ID;
for (unsigned i = 0; i != NumVals; ++i)
ID.AddPointer(Vals[i]);
+ if (LocalFunction)
+ ID.AddPointer(LocalFunction);
void *InsertPoint;
- MDNode *N;
- {
- N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
- }
- if (N) return N;
-
- N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
+ MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
if (!N) {
// InsertPoint will have been set by the FindNodeOrInsertPos call.
- N = new MDNode(Context, Vals, NumVals);
+ N = new MDNode(Context, Vals, NumVals, LocalFunction);
pImpl->MDNodeSet.InsertNode(N, InsertPoint);
}
-
return N;
}
/// ~MDNode - Destroy MDNode.
MDNode::~MDNode() {
- {
- LLVMContextImpl *pImpl = getType()->getContext().pImpl;
- pImpl->MDNodeSet.RemoveNode(this);
- }
+ LLVMContextImpl *pImpl = getType()->getContext().pImpl;
+ pImpl->MDNodeSet.RemoveNode(this);
delete [] Node;
Node = NULL;
}
/// addMD - Attach the metadata of given kind to an Instruction.
void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
- /// removeMD - Remove metadata of given kind attached with an instuction.
+ /// removeMD - Remove metadata of given kind attached with an instruction.
void removeMD(unsigned Kind, Instruction *Inst);
/// removeAllMetadata - Remove all metadata attached with an instruction.
/// the same metadata to In2.
void copyMD(Instruction *In1, Instruction *In2);
- /// getHandlerNames - Populate client supplied smallvector using custome
+ /// getHandlerNames - Populate client-supplied smallvector using custom
/// metadata name and ID.
void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
Info.push_back(std::make_pair(MDKind, Node));
}
-/// removeMD - Remove metadata of given kind attached with an instuction.
+/// 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())
}
}
}
-
+
/// removeAllMetadata - Remove all metadata attached with an instruction.
void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
MetadataStore.erase(Inst);
/// getMDs - Get the metadata attached to an Instruction.
void MetadataContextImpl::
getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const {
- MDStoreTy::iterator I = MetadataStore.find(Inst);
+ MDStoreTy::const_iterator I = MetadataStore.find(Inst);
if (I == MetadataStore.end())
return;
- for (MDMapTy::iterator MI = I->second.begin(), ME = I->second.end();
+ MDs.resize(I->second.size());
+ for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
MI != ME; ++MI)
- MDs.push_back(std::make_pair(MI->first, MI->second));
- std::sort(MDs.begin(), MDs.end());
+ // 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::
getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
+ Names.resize(MDHandlerNames.size());
for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
E = MDHandlerNames.end(); I != E; ++I)
- Names.push_back(std::make_pair(I->second, I->first()));
- std::sort(Names.begin(), Names.end());
+ // MD Handlers are numbered from 1.
+ Names[I->second - 1] = std::make_pair(I->second, I->first());
}
/// ValueIsCloned - This handler is used to update metadata store
void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
pImpl->addMD(Kind, Node, Inst);
}
-
-/// removeMD - Remove metadata of given kind attached with an instuction.
+
+/// 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);