llvm.org GIT mirror llvm / ec39f09
Fix a major source of compile-time slowness at -O0 -g by optimizing the storage of !dbg metadata kinds in the instruction themselves. The on-the-side hash table works great for metadata that not-all instructions get, or for metadata that only exists when optimizing. But when compile-time is everything, it isn't great. I'm not super thrilled with the fact that this plops a TrackingVH in Instruction, because it grows it by 3 words. I'm investigating alternatives, but this should be a step in the right direction in any case. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99957 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
6 changed file(s) with 62 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
1616
1717 #include "llvm/User.h"
1818 #include "llvm/ADT/ilist_node.h"
19 #include "llvm/Support/ValueHandle.h"
1920
2021 namespace llvm {
2122
3031 Instruction(const Instruction &); // Do not implement
3132
3233 BasicBlock *Parent;
34 TrackingVH DbgInfo; // 'dbg' Metadata cache.
3335
3436 enum {
3537 /// HasMetadataBit - This is a bit stored in the SubClassData field which
122124 /// hasMetadata() - Return true if this instruction has any metadata attached
123125 /// to it.
124126 bool hasMetadata() const {
125 return (getSubclassDataFromValue() & HasMetadataBit) != 0;
127 return DbgInfo != 0 || hasMetadataHashEntry();
126128 }
127129
128130 /// getMetadata - Get the metadata of given kind attached to this Instruction.
154156 void setMetadata(const char *Kind, MDNode *Node);
155157
156158 private:
159 /// hasMetadataHashEntry - Return true if we have an entry in the on-the-side
160 /// metadata hash.
161 bool hasMetadataHashEntry() const {
162 return (getSubclassDataFromValue() & HasMetadataBit) != 0;
163 }
164
157165 // These are all implemented in Metadata.cpp.
158166 MDNode *getMetadataImpl(unsigned KindID) const;
159167 MDNode *getMetadataImpl(const char *Kind) const;
314322 return Value::getSubclassDataFromValue();
315323 }
316324
317 void setHasMetadata(bool V) {
325 void setHasMetadataHashEntry(bool V) {
318326 setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
319327 (V ? HasMetadataBit : 0));
320328 }
3535 LLVMContext();
3636 ~LLVMContext();
3737
38 // Pinned metadata names, which always have the same value. This is a
39 // compile-time performance optimization, not a correctness optimization.
40 enum {
41 MD_dbg = 1 // "dbg" -> 1.
42 };
43
3844 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
3945 /// This ID is uniqued across modules in the current LLVMContext.
4046 unsigned getMDKindID(StringRef Name) const;
301301
302302 ValueTy *getValPtr() const {
303303 CheckValidity();
304 return static_cast(ValueHandleBase::getValPtr());
304 return (ValueTy*)ValueHandleBase::getValPtr();
305305 }
306306 void setValPtr(ValueTy *P) {
307307 CheckValidity();
2121
2222 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
2323 Instruction *InsertBefore)
24 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
24 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0), DbgInfo(0) {
2525 // Make sure that we get added to a basicblock
2626 LeakDetector::addGarbageObject(this);
2727
3535
3636 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
3737 BasicBlock *InsertAtEnd)
38 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
38 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0), DbgInfo(0) {
3939 // Make sure that we get added to a basicblock
4040 LeakDetector::addGarbageObject(this);
4141
2525 return *GlobalContext;
2626 }
2727
28 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
28 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
29 // Create the first metadata kind, which is always 'dbg'.
30 unsigned DbgID = getMDKindID("dbg");
31 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
32 }
2933 LLVMContext::~LLVMContext() { delete pImpl; }
3034
3135
429429 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
430430 if (Node == 0 && !hasMetadata()) return;
431431
432 // Handle 'dbg' as a special case since it is not stored in the hash table.
433 if (KindID == LLVMContext::MD_dbg) {
434 DbgInfo = Node;
435 return;
436 }
437
432438 // Handle the case when we're adding/updating metadata on an instruction.
433439 if (Node) {
434440 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
435 assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked");
441 assert(!Info.empty() == hasMetadataHashEntry() &&
442 "HasMetadata bit is wonked");
436443 if (Info.empty()) {
437 setHasMetadata(true);
444 setHasMetadataHashEntry(true);
438445 } else {
439446 // Handle replacement of an existing value.
440447 for (unsigned i = 0, e = Info.size(); i != e; ++i)
450457 }
451458
452459 // Otherwise, we're removing metadata from an instruction.
453 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
460 assert(hasMetadataHashEntry() &&
461 getContext().pImpl->MetadataStore.count(this) &&
454462 "HasMetadata bit out of date!");
455463 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
456464
457465 // Common case is removing the only entry.
458466 if (Info.size() == 1 && Info[0].first == KindID) {
459467 getContext().pImpl->MetadataStore.erase(this);
460 setHasMetadata(false);
468 setHasMetadataHashEntry(false);
461469 return;
462470 }
463471
464 // Handle replacement of an existing value.
472 // Handle removal of an existing value.
465473 for (unsigned i = 0, e = Info.size(); i != e; ++i)
466474 if (Info[i].first == KindID) {
467475 Info[i] = Info.back();
473481 }
474482
475483 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
484 // Handle 'dbg' as a special case since it is not stored in the hash table.
485 if (KindID == LLVMContext::MD_dbg)
486 return DbgInfo;
487
488 if (!hasMetadataHashEntry()) return 0;
489
476490 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
477 assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
491 assert(!Info.empty() && "bit out of sync with hash table");
478492
479493 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
480494 I != E; ++I)
484498 }
485499
486500 void Instruction::getAllMetadataImpl(SmallVectorImpl
487 MDNode*> > &Result)const {
488 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
501 MDNode*> > &Result) const {
502 Result.clear();
503
504 // Handle 'dbg' as a special case since it is not stored in the hash table.
505 if (DbgInfo) {
506 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg, DbgInfo));
507 if (!hasMetadataHashEntry()) return;
508 }
509
510 assert(hasMetadataHashEntry() &&
511 getContext().pImpl->MetadataStore.count(this) &&
489512 "Shouldn't have called this");
490513 const LLVMContextImpl::MDMapTy &Info =
491514 getContext().pImpl->MetadataStore.find(this)->second;
492515 assert(!Info.empty() && "Shouldn't have called this");
493516
494 Result.clear();
495517 Result.append(Info.begin(), Info.end());
496518
497519 // Sort the resulting array so it is stable.
502524 /// removeAllMetadata - Remove all metadata from this instruction.
503525 void Instruction::removeAllMetadata() {
504526 assert(hasMetadata() && "Caller should check");
505 getContext().pImpl->MetadataStore.erase(this);
506 setHasMetadata(false);
507 }
508
527 DbgInfo = 0;
528 if (hasMetadataHashEntry()) {
529 getContext().pImpl->MetadataStore.erase(this);
530 setHasMetadataHashEntry(false);
531 }
532 }
533