rename getMDKind -> getMDKindID, make it autoinsert if an MD Kind

doesn't exist already, eliminate registerMDKind.  Tidy up a bunch
of random stuff.

llvm-svn: 92225
This commit is contained in:
Chris Lattner 2009-12-28 20:45:51 +00:00
parent 2b65f2d7d4
commit cd3aa9d1ff
11 changed files with 59 additions and 108 deletions

View File

@ -197,32 +197,25 @@ public:
}; };
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// MetadataContext - /// MetadataContext - MetadataContext handles uniquing and assignment of IDs for
/// MetadataContext handles uniquing and assignment of IDs for custom metadata /// custom metadata types.
/// types. Custom metadata handler names do not contain spaces. And the name ///
/// must start with an alphabet. The regular expression used to check name
/// is [a-zA-Z$._][a-zA-Z$._0-9]*
class MetadataContext { class MetadataContext {
// DO NOT IMPLEMENT MetadataContext(MetadataContext&); // DO NOT IMPLEMENT
MetadataContext(MetadataContext&); void operator=(MetadataContext&); // DO NOT IMPLEMENT
void operator=(MetadataContext&);
MetadataContextImpl *const pImpl; MetadataContextImpl *const pImpl;
public: public:
MetadataContext(); MetadataContext();
~MetadataContext(); ~MetadataContext();
/// registerMDKind - Register a new metadata kind and return its ID. /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
/// A metadata kind can be registered only once. unsigned getMDKindID(StringRef Name) const;
unsigned registerMDKind(StringRef Name);
/// getMDKind - Return metadata kind. If the requested metadata kind
/// is not registered then return 0.
unsigned getMDKind(StringRef Name) const;
/// isValidName - Return true if Name is a valid custom metadata handler name. /// isValidName - Return true if Name is a valid custom metadata handler name.
static bool isValidName(StringRef Name); static bool isValidName(StringRef Name);
#if 1
/// getMD - Get the metadata of given kind attached to an Instruction. /// getMD - Get the metadata of given kind attached to an Instruction.
/// If the metadata is not found then return 0. /// If the metadata is not found then return 0.
MDNode *getMD(unsigned Kind, const Instruction *Inst); MDNode *getMD(unsigned Kind, const Instruction *Inst);
@ -239,6 +232,7 @@ public:
/// removeAllMetadata - Remove all metadata attached with an instruction. /// removeAllMetadata - Remove all metadata attached with an instruction.
void removeAllMetadata(Instruction *Inst); void removeAllMetadata(Instruction *Inst);
#endif
/// copyMD - If metadata is attached with Instruction In1 then attach /// copyMD - If metadata is attached with Instruction In1 then attach
/// the same metadata to In2. /// the same metadata to In2.

View File

@ -137,9 +137,7 @@ public:
/// information. /// information.
void SetCurrentDebugLocation(MDNode *L) { void SetCurrentDebugLocation(MDNode *L) {
if (MDKind == 0) if (MDKind == 0)
MDKind = Context.getMetadata().getMDKind("dbg"); MDKind = Context.getMetadata().getMDKindID("dbg");
if (MDKind == 0)
MDKind = Context.getMetadata().registerMDKind("dbg");
CurDbgLocation = L; CurDbgLocation = L;
} }
@ -154,9 +152,7 @@ public:
/// SetDebugLocation - Set location information for the given instruction. /// SetDebugLocation - Set location information for the given instruction.
void SetDebugLocation(Instruction *I, MDNode *Loc) { void SetDebugLocation(Instruction *I, MDNode *Loc) {
if (MDKind == 0) if (MDKind == 0)
MDKind = Context.getMetadata().getMDKind("dbg"); MDKind = Context.getMetadata().getMDKindID("dbg");
if (MDKind == 0)
MDKind = Context.getMetadata().registerMDKind("dbg");
Context.getMetadata().addMD(MDKind, Loc, I); Context.getMetadata().addMD(MDKind, Loc, I);
} }

View File

@ -1119,7 +1119,7 @@ Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, Value *Offset,
void DebugInfoFinder::processModule(Module &M) { void DebugInfoFinder::processModule(Module &M) {
MetadataContext &TheMetadata = M.getContext().getMetadata(); MetadataContext &TheMetadata = M.getContext().getMetadata();
unsigned MDDbgKind = TheMetadata.getMDKind("dbg"); unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI) for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
@ -1127,9 +1127,8 @@ void DebugInfoFinder::processModule(Module &M) {
++BI) { ++BI) {
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
processDeclare(DDI); processDeclare(DDI);
else if (MDDbgKind) else if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI))
if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI)) processLocation(DILocation(L));
processLocation(DILocation(L));
} }
NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv"); NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");

View File

@ -1123,11 +1123,8 @@ bool LLParser::ParseOptionalCustomMetadata() {
if (ParseMDNode(Node)) return true; if (ParseMDNode(Node)) return true;
MetadataContext &TheMetadata = M->getContext().getMetadata(); MetadataContext &TheMetadata = M->getContext().getMetadata();
unsigned MDK = TheMetadata.getMDKind(Name.c_str()); unsigned MDK = TheMetadata.getMDKindID(Name.c_str());
if (!MDK)
MDK = TheMetadata.registerMDKind(Name.c_str());
MDsOnInst.push_back(std::make_pair(MDK, cast<MDNode>(Node))); MDsOnInst.push_back(std::make_pair(MDK, cast<MDNode>(Node)));
return false; return false;
} }

View File

@ -840,17 +840,10 @@ bool BitcodeReader::ParseMetadata() {
(void) Kind; (void) Kind;
for (unsigned i = 1; i != RecordLength; ++i) for (unsigned i = 1; i != RecordLength; ++i)
Name[i-1] = Record[i]; Name[i-1] = Record[i];
MetadataContext &TheMetadata = Context.getMetadata();
unsigned ExistingKind = TheMetadata.getMDKind(Name.str()); unsigned NewKind = Context.getMetadata().getMDKindID(Name.str());
if (ExistingKind == 0) { assert(Kind == NewKind &&
unsigned NewKind = TheMetadata.registerMDKind(Name.str()); "FIXME: Unable to handle custom metadata mismatch!");(void)NewKind;
(void) NewKind;
assert (Kind == NewKind
&& "Unable to handle custom metadata mismatch!");
} else {
assert (ExistingKind == Kind
&& "Unable to handle custom metadata mismatch!");
}
break; break;
} }
} }

View File

@ -351,9 +351,9 @@ bool FastISel::SelectCall(User *I) {
if (MMI) { if (MMI) {
MetadataContext &TheMetadata = MetadataContext &TheMetadata =
DI->getParent()->getContext().getMetadata(); DI->getParent()->getContext().getMetadata();
unsigned MDDbgKind = TheMetadata.getMDKind("dbg"); unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
MDNode *Dbg = TheMetadata.getMD(MDDbgKind, DI); if (MDNode *Dbg = TheMetadata.getMD(MDDbgKind, DI))
MMI->setVariableDbgInfo(DI->getVariable(), FI, Dbg); MMI->setVariableDbgInfo(DI->getVariable(), FI, Dbg);
} }
return true; return true;
} }

View File

@ -4383,9 +4383,9 @@ SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
if (MMI) { if (MMI) {
MetadataContext &TheMetadata = MetadataContext &TheMetadata =
DI.getParent()->getContext().getMetadata(); DI.getParent()->getContext().getMetadata();
unsigned MDDbgKind = TheMetadata.getMDKind("dbg"); unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI); if (MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI))
MMI->setVariableDbgInfo(Variable, FI, Dbg); MMI->setVariableDbgInfo(Variable, FI, Dbg);
} }
return 0; return 0;
} }

View File

@ -362,27 +362,25 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
/// SetDebugLoc - Update MF's and SDB's DebugLocs if debug information is /// SetDebugLoc - Update MF's and SDB's DebugLocs if debug information is
/// attached with this instruction. /// attached with this instruction.
static void SetDebugLoc(unsigned MDDbgKind, static void SetDebugLoc(unsigned MDDbgKind, MetadataContext &TheMetadata,
MetadataContext &TheMetadata, Instruction *I, SelectionDAGBuilder *SDB,
Instruction *I, FastISel *FastIS, MachineFunction *MF) {
SelectionDAGBuilder *SDB, if (isa<DbgInfoIntrinsic>(I)) return;
FastISel *FastIS,
MachineFunction *MF) { if (MDNode *Dbg = TheMetadata.getMD(MDDbgKind, I)) {
if (!isa<DbgInfoIntrinsic>(I)) DILocation DILoc(Dbg);
if (MDNode *Dbg = TheMetadata.getMD(MDDbgKind, I)) { DebugLoc Loc = ExtractDebugLocation(DILoc, MF->getDebugLocInfo());
DILocation DILoc(Dbg);
DebugLoc Loc = ExtractDebugLocation(DILoc, MF->getDebugLocInfo());
SDB->setCurDebugLoc(Loc); SDB->setCurDebugLoc(Loc);
if (FastIS) if (FastIS)
FastIS->setCurDebugLoc(Loc); FastIS->setCurDebugLoc(Loc);
// If the function doesn't have a default debug location yet, set // If the function doesn't have a default debug location yet, set
// it. This is kind of a hack. // it. This is kind of a hack.
if (MF->getDefaultDebugLoc().isUnknown()) if (MF->getDefaultDebugLoc().isUnknown())
MF->setDefaultDebugLoc(Loc); MF->setDefaultDebugLoc(Loc);
} }
} }
/// ResetDebugLoc - Set MF's and SDB's DebugLocs to Unknown. /// ResetDebugLoc - Set MF's and SDB's DebugLocs to Unknown.
@ -398,14 +396,13 @@ void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB,
BasicBlock::iterator End, BasicBlock::iterator End,
bool &HadTailCall) { bool &HadTailCall) {
SDB->setCurrentBasicBlock(BB); SDB->setCurrentBasicBlock(BB);
MetadataContext &TheMetadata = LLVMBB->getParent()->getContext().getMetadata(); MetadataContext &TheMetadata =LLVMBB->getParent()->getContext().getMetadata();
unsigned MDDbgKind = TheMetadata.getMDKind("dbg"); unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
// Lower all of the non-terminator instructions. If a call is emitted // Lower all of the non-terminator instructions. If a call is emitted
// as a tail call, cease emitting nodes for this block. // as a tail call, cease emitting nodes for this block.
for (BasicBlock::iterator I = Begin; I != End && !SDB->HasTailCall; ++I) { for (BasicBlock::iterator I = Begin; I != End && !SDB->HasTailCall; ++I) {
if (MDDbgKind) SetDebugLoc(MDDbgKind, TheMetadata, I, SDB, 0, MF);
SetDebugLoc(MDDbgKind, TheMetadata, I, SDB, 0, MF);
if (!isa<TerminatorInst>(I)) { if (!isa<TerminatorInst>(I)) {
SDB->visit(*I); SDB->visit(*I);
@ -681,7 +678,7 @@ void SelectionDAGISel::SelectAllBasicBlocks(Function &Fn,
); );
MetadataContext &TheMetadata = Fn.getContext().getMetadata(); MetadataContext &TheMetadata = Fn.getContext().getMetadata();
unsigned MDDbgKind = TheMetadata.getMDKind("dbg"); unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
// Iterate over all basic blocks in the function. // Iterate over all basic blocks in the function.
for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
@ -779,8 +776,7 @@ void SelectionDAGISel::SelectAllBasicBlocks(Function &Fn,
break; break;
} }
if (MDDbgKind) SetDebugLoc(MDDbgKind, TheMetadata, BI, SDB, FastIS, &MF);
SetDebugLoc(MDDbgKind, TheMetadata, BI, SDB, FastIS, &MF);
// First try normal tablegen-generated "fast" selection. // First try normal tablegen-generated "fast" selection.
if (FastIS->SelectInstruction(BI)) { if (FastIS->SelectInstruction(BI)) {

View File

@ -221,9 +221,7 @@ static bool StripDebugInfo(Module &M) {
NMD->eraseFromParent(); NMD->eraseFromParent();
} }
MetadataContext &TheMetadata = M.getContext().getMetadata(); MetadataContext &TheMetadata = M.getContext().getMetadata();
unsigned MDDbgKind = TheMetadata.getMDKind("dbg"); unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
if (!MDDbgKind)
return Changed;
for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;

View File

@ -422,7 +422,7 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
BasicBlock::iterator I = NewBB->begin(); BasicBlock::iterator I = NewBB->begin();
LLVMContext &Context = OldFunc->getContext(); LLVMContext &Context = OldFunc->getContext();
unsigned DbgKind = Context.getMetadata().getMDKind("dbg"); unsigned DbgKind = Context.getMetadata().getMDKindID("dbg");
MDNode *TheCallMD = NULL; MDNode *TheCallMD = NULL;
SmallVector<Value *, 4> MDVs; SmallVector<Value *, 4> MDVs;
if (TheCall && TheCall->hasMetadata()) if (TheCall && TheCall->hasMetadata())

View File

@ -261,13 +261,7 @@ private:
StringMap<unsigned> MDHandlerNames; StringMap<unsigned> MDHandlerNames;
public: public:
/// registerMDKind - Register a new metadata kind and return its ID. unsigned getMDKindID(StringRef Name);
/// A metadata kind can be registered only once.
unsigned registerMDKind(StringRef Name);
/// getMDKind - Return metadata kind. If the requested metadata kind
/// is not registered then return 0.
unsigned getMDKind(StringRef Name) const;
/// getMD - Get the metadata of given kind attached to an Instruction. /// getMD - Get the metadata of given kind attached to an Instruction.
/// If the metadata is not found then return 0. /// If the metadata is not found then return 0.
@ -308,22 +302,14 @@ public:
}; };
} }
/// registerMDKind - Register a new metadata kind and return its ID. /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
/// A metadata kind can be registered only once. unsigned MetadataContextImpl::getMDKindID(StringRef Name) {
unsigned MetadataContextImpl::registerMDKind(StringRef Name) { unsigned &Entry = MDHandlerNames[Name];
unsigned Count = MDHandlerNames.size();
assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
return MDHandlerNames[Name] = Count + 1;
}
/// getMDKind - Return metadata kind. If the requested metadata kind // If this is new, assign it its ID.
/// is not registered then return 0. if (Entry == 0) Entry = MDHandlerNames.size();
unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name); return Entry;
if (I == MDHandlerNames.end())
return 0;
return I->getValue();
} }
/// addMD - Attach the metadata of given kind to an Instruction. /// addMD - Attach the metadata of given kind to an Instruction.
@ -472,17 +458,9 @@ bool MetadataContext::isValidName(StringRef MDName) {
return true; return true;
} }
/// registerMDKind - Register a new metadata kind and return its ID. /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
/// A metadata kind can be registered only once. unsigned MetadataContext::getMDKindID(StringRef Name) const {
unsigned MetadataContext::registerMDKind(StringRef Name) { return pImpl->getMDKindID(Name);
assert(isValidName(Name) && "Invalid custome metadata name!");
return pImpl->registerMDKind(Name);
}
/// getMDKind - Return metadata kind. If the requested metadata kind
/// is not registered then return 0.
unsigned MetadataContext::getMDKind(StringRef Name) const {
return pImpl->getMDKind(Name);
} }
/// getMD - Get the metadata of given kind attached to an Instruction. /// getMD - Get the metadata of given kind attached to an Instruction.