IR: MDNode => Value: NamedMDNode::getOperator()

Change `NamedMDNode::getOperator()` from returning `MDNode *` to
returning `Value *`.  To reduce boilerplate at some call sites, add a
`getOperatorAsMDNode()` for named metadata that's expected to only
return `MDNode` -- for now, that's everything, but debug node named
metadata (such as llvm.dbg.cu and llvm.dbg.sp) will soon change.  This
is part of PR21433.

Note that there's a follow-up patch to clang for the API change.

llvm-svn: 221375
This commit is contained in:
Duncan P. N. Exon Smith 2014-11-05 18:16:03 +00:00
parent 13b8368b56
commit fb1e9e11dd
17 changed files with 47 additions and 42 deletions

View File

@ -237,10 +237,10 @@ private:
};
//===----------------------------------------------------------------------===//
/// \brief A tuple of MDNodes.
/// \brief A tuple of metadata nodes.
///
/// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
/// to modules, have names, and contain lists of MDNodes.
/// to modules, have names, and contain lists of metadata nodes.
class NamedMDNode : public ilist_node<NamedMDNode> {
friend class SymbolTableListTraits<NamedMDNode, Module>;
friend struct ilist_traits<NamedMDNode>;
@ -250,7 +250,7 @@ class NamedMDNode : public ilist_node<NamedMDNode> {
std::string Name;
Module *Parent;
void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
void *Operands; // SmallVector<TrackingVH<Value>, 4>
void setParent(Module *M) { Parent = M; }
@ -305,7 +305,10 @@ public:
inline Module *getParent() { return Parent; }
inline const Module *getParent() const { return Parent; }
MDNode *getOperand(unsigned i) const;
Value *getOperand(unsigned i) const;
MDNode *getOperandAsMDNode(unsigned i) const {
return cast_or_null<MDNode>(getOperand(i));
}
unsigned getNumOperands() const;
void addOperand(Value *M);
StringRef getName() const;
@ -315,11 +318,11 @@ public:
// ---------------------------------------------------------------------------
// Operand Iterator interface...
//
typedef op_iterator_impl<MDNode*, MDNode> op_iterator;
typedef op_iterator_impl<Value *, Value> op_iterator;
op_iterator op_begin() { return op_iterator(this, 0); }
op_iterator op_end() { return op_iterator(this, getNumOperands()); }
typedef op_iterator_impl<const MDNode*, MDNode> const_op_iterator;
typedef op_iterator_impl<const Value *, Value> const_op_iterator;
const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }

View File

@ -1398,7 +1398,7 @@ void AsmPrinter::EmitModuleIdents(Module &M) {
if (const NamedMDNode *NMD = M.getNamedMetadata("llvm.ident")) {
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
const MDNode *N = NMD->getOperand(i);
const MDNode *N = NMD->getOperandAsMDNode(i);
assert(N->getNumOperands() == 1 &&
"llvm.ident metadata entry can have only one operand");
const MDString *S = cast<MDString>(N->getOperand(0));

View File

@ -450,8 +450,8 @@ void DwarfDebug::beginModule() {
SingleCU = CU_Nodes->getNumOperands() == 1;
for (MDNode *N : CU_Nodes->operands()) {
DICompileUnit CUNode(N);
for (Value *N : CU_Nodes->operands()) {
DICompileUnit CUNode(cast<MDNode>(N));
DwarfCompileUnit &CU = constructDwarfCompileUnit(CUNode);
DIArray ImportedEntities = CUNode.getImportedEntities();
for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
@ -526,8 +526,8 @@ void DwarfDebug::collectDeadVariables() {
const Module *M = MMI->getModule();
if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
for (MDNode *N : CU_Nodes->operands()) {
DICompileUnit TheCU(N);
for (Value *N : CU_Nodes->operands()) {
DICompileUnit TheCU(cast<MDNode>(N));
// Construct subprogram DIE and add variables DIEs.
DwarfCompileUnit *SPCU =
static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));

View File

@ -560,8 +560,8 @@ LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
}
void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
unwrap<Instruction>(Inst)->setMetadata(KindID,
MD ? unwrap<MDNode>(MD) : nullptr);
unwrap<Instruction>(Inst)
->setMetadata(KindID, MD ? unwrap<Value>(MD) : nullptr);
}
/*--.. Conversion functions ................................................--*/
@ -720,7 +720,7 @@ void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
if (!N)
return;
MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
Value *Op = Val ? unwrap<Value>(Val) : nullptr;
if (Op)
N->addOperand(Op);
}

View File

@ -949,7 +949,7 @@ DITypeIdentifierMap
llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
DITypeIdentifierMap Map;
for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
DICompileUnit CU(CU_Nodes->getOperand(CUi));
DICompileUnit CU(CU_Nodes->getOperandAsMDNode(CUi));
DIArray Retain = CU.getRetainedTypes();
for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
if (!Retain.getElement(Ti).isCompositeType())
@ -997,7 +997,7 @@ void DebugInfoFinder::processModule(const Module &M) {
InitializeTypeMap(M);
if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
DICompileUnit CU(CU_Nodes->getOperand(i));
DICompileUnit CU(CU_Nodes->getOperandAsMDNode(i));
addCompileUnit(CU);
DIArray GVs = CU.getGlobalVariables();
for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
@ -1542,8 +1542,8 @@ llvm::makeSubprogramMap(const Module &M) {
if (!CU_Nodes)
return R;
for (MDNode *N : CU_Nodes->operands()) {
DICompileUnit CUNode(N);
for (Value *N : CU_Nodes->operands()) {
DICompileUnit CUNode(cast<MDNode>(N));
DIArray SPs = CUNode.getSubprograms();
for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
DISubprogram SP(SPs.getElement(i));

View File

@ -555,14 +555,13 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
// NamedMDNode implementation.
//
static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
static SmallVector<TrackingVH<Value>, 4> &getNMDOps(void *Operands) {
return *(SmallVector<TrackingVH<Value>, 4> *)Operands;
}
NamedMDNode::NamedMDNode(const Twine &N)
: Name(N.str()), Parent(nullptr),
Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
}
: Name(N.str()), Parent(nullptr),
Operands(new SmallVector<TrackingVH<Value>, 4>()) {}
NamedMDNode::~NamedMDNode() {
dropAllReferences();
@ -573,7 +572,7 @@ unsigned NamedMDNode::getNumOperands() const {
return (unsigned)getNMDOps(Operands).size();
}
MDNode *NamedMDNode::getOperand(unsigned i) const {
Value *NamedMDNode::getOperand(unsigned i) const {
assert(i < getNumOperands() && "Invalid Operand number!");
return &*getNMDOps(Operands)[i];
}
@ -582,7 +581,7 @@ void NamedMDNode::addOperand(Value *V) {
auto *M = cast<MDNode>(V);
assert(!M->isFunctionLocal() &&
"NamedMDNode operands must not be function-local!");
getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
getNMDOps(Operands).push_back(TrackingVH<Value>(M));
}
void NamedMDNode::eraseFromParent() {

View File

@ -276,7 +276,8 @@ getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
const NamedMDNode *ModFlags = getModuleFlagsMetadata();
if (!ModFlags) return;
for (const MDNode *Flag : ModFlags->operands()) {
for (const Value *FlagMD : ModFlags->operands()) {
const MDNode *Flag = cast<MDNode>(FlagMD);
ModFlagBehavior MFB;
if (Flag->getNumOperands() >= 3 &&
isValidModFlagBehavior(Flag->getOperand(0), MFB) &&

View File

@ -81,7 +81,7 @@ void TypeFinder::run(const Module &M, bool onlyNamed) {
E = M.named_metadata_end(); I != E; ++I) {
const NamedMDNode *NMD = I;
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
incorporateMDNode(NMD->getOperand(i));
incorporateMDNode(NMD->getOperandAsMDNode(i));
}
}

View File

@ -555,7 +555,7 @@ void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) {
MDNode *MD = NMD.getOperand(i);
MDNode *MD = NMD.getOperandAsMDNode(i);
if (!MD)
continue;
@ -624,7 +624,7 @@ void Verifier::visitModuleIdents(const Module &M) {
// llvm.ident takes a list of metadata entry. Each entry has only one string.
// Scan each llvm.ident entry and make sure that this requirement is met.
for (unsigned i = 0, e = Idents->getNumOperands(); i != e; ++i) {
const MDNode *N = Idents->getOperand(i);
const MDNode *N = Idents->getOperandAsMDNode(i);
Assert1(N->getNumOperands() == 1,
"incorrect number of operands in llvm.ident metadata", N);
Assert1(isa<MDString>(N->getOperand(0)),
@ -642,7 +642,7 @@ void Verifier::visitModuleFlags(const Module &M) {
DenseMap<const MDString*, const MDNode*> SeenIDs;
SmallVector<const MDNode*, 16> Requirements;
for (unsigned I = 0, E = Flags->getNumOperands(); I != E; ++I) {
visitModuleFlag(Flags->getOperand(I), SeenIDs, Requirements);
visitModuleFlag(Flags->getOperandAsMDNode(I), SeenIDs, Requirements);
}
// Validate that the requirements in the module are valid.

View File

@ -1287,7 +1287,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
DenseMap<MDString*, MDNode*> Flags;
SmallSetVector<MDNode*, 16> Requirements;
for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
MDNode *Op = DstModFlags->getOperand(I);
MDNode *Op = DstModFlags->getOperandAsMDNode(I);
ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0));
MDString *ID = cast<MDString>(Op->getOperand(1));
@ -1302,7 +1302,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
// requirements.
bool HasErr = false;
for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
MDNode *SrcOp = SrcModFlags->getOperand(I);
MDNode *SrcOp = SrcModFlags->getOperandAsMDNode(I);
ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0));
MDString *ID = cast<MDString>(SrcOp->getOperand(1));
MDNode *DstOp = Flags.lookup(ID);

View File

@ -371,7 +371,7 @@ void GenericToNVVM::remapNamedMDNode(Module *M, NamedMDNode *N) {
// Check if any operand is or contains a global variable in GVMap, and thus
// converted to another value.
for (unsigned i = 0; i < NumOperands; ++i) {
MDNode *Operand = N->getOperand(i);
MDNode *Operand = cast<MDNode>(N->getOperand(i));
MDNode *NewOperand = remapMDNode(M, Operand);
OperandChanged |= Operand != NewOperand;
NewOperands.push_back(NewOperand);

View File

@ -73,7 +73,7 @@ static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
return;
key_val_pair_t tmp;
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
const MDNode *elem = NMD->getOperand(i);
const MDNode *elem = NMD->getOperandAsMDNode(i);
Value *entity = elem->getOperand(0);
// entity may be null due to DCE

View File

@ -256,7 +256,9 @@ class GlobalsMetadata {
NamedMDNode *Globals = M.getNamedMetadata("llvm.asan.globals");
if (!Globals)
return;
for (auto MDN : Globals->operands()) {
for (const Value *MDV : Globals->operands()) {
const MDNode *MDN = cast<MDNode>(MDV);
// Metadata node contains the global and the fields of "Entry".
assert(MDN->getNumOperands() == 5);
Value *V = MDN->getOperand(0);

View File

@ -474,7 +474,7 @@ bool getSourceInfoFromDI(const Module &M, std::string &Directory,
if (!CUNode || CUNode->getNumOperands() == 0)
return false;
DICompileUnit CU(CUNode->getOperand(0));
DICompileUnit CU(cast<MDNode>(CUNode->getOperand(0)));
if (!CU.Verify())
return false;

View File

@ -417,7 +417,7 @@ namespace {
std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) {
if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
MDNode *N = GCov->getOperand(i);
MDNode *N = GCov->getOperandAsMDNode(i);
if (N->getNumOperands() != 2) continue;
MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
MDNode *CompileUnit = dyn_cast<MDNode>(N->getOperand(1));
@ -479,7 +479,7 @@ void GCOVProfiler::emitProfileNotes() {
// this pass over the original .o's as they're produced, or run it after
// LTO, we'll generate the same .gcno files.
DICompileUnit CU(CU_Nodes->getOperand(i));
DICompileUnit CU(CU_Nodes->getOperandAsMDNode(i));
std::error_code EC;
raw_fd_ostream out(mangleName(CU, "gcno"), EC, sys::fs::F_None);
std::string EdgeDestinations;
@ -565,7 +565,7 @@ bool GCOVProfiler::emitProfileArcs() {
bool Result = false;
bool InsertIndCounterIncrCode = false;
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
DICompileUnit CU(CU_Nodes->getOperand(i));
DICompileUnit CU(CU_Nodes->getOperandAsMDNode(i));
DIArray SPs = CU.getSubprograms();
SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
@ -846,7 +846,7 @@ Function *GCOVProfiler::insertCounterWriteout(
NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
if (CU_Nodes) {
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
DICompileUnit CU(CU_Nodes->getOperand(i));
DICompileUnit CU(CU_Nodes->getOperandAsMDNode(i));
std::string FilenameGcda = mangleName(CU, "gcda");
uint32_t CfgChecksum = FileChecksums.empty() ? 0 : FileChecksums[i];
Builder.CreateCall3(StartFile,

View File

@ -305,7 +305,7 @@ bool ObjCARCContract::doInitialization(Module &M) {
if (NamedMDNode *NMD =
M.getNamedMetadata("clang.arc.retainAutoreleasedReturnValueMarker"))
if (NMD->getNumOperands() == 1) {
const MDNode *N = NMD->getOperand(0);
const MDNode *N = NMD->getOperandAsMDNode(0);
if (N->getNumOperands() == 1)
if (const MDString *S = dyn_cast<MDString>(N->getOperand(0)))
RetainRVMarker = S;

View File

@ -55,7 +55,7 @@ struct BreakpointPrinter : public ModulePass {
if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
std::string Name;
DISubprogram SP(NMD->getOperand(i));
DISubprogram SP(cast_or_null<MDNode>(NMD->getOperand(i)));
assert((!SP || SP.isSubprogram()) &&
"A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
if (!SP)