llvm.org GIT mirror llvm / bad06b1
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221375 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 4 years ago
17 changed file(s) with 47 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
236236 };
237237
238238 //===----------------------------------------------------------------------===//
239 /// \brief A tuple of MDNodes.
239 /// \brief A tuple of metadata nodes.
240240 ///
241241 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
242 /// to modules, have names, and contain lists of MDNodes.
242 /// to modules, have names, and contain lists of metadata nodes.
243243 class NamedMDNode : public ilist_node {
244244 friend class SymbolTableListTraits;
245245 friend struct ilist_traits;
249249
250250 std::string Name;
251251 Module *Parent;
252 void *Operands; // SmallVectorMDNode>, 4>
252 void *Operands; // SmallVectorValue>, 4>
253253
254254 void setParent(Module *M) { Parent = M; }
255255
304304 inline Module *getParent() { return Parent; }
305305 inline const Module *getParent() const { return Parent; }
306306
307 MDNode *getOperand(unsigned i) const;
307 Value *getOperand(unsigned i) const;
308 MDNode *getOperandAsMDNode(unsigned i) const {
309 return cast_or_null(getOperand(i));
310 }
308311 unsigned getNumOperands() const;
309312 void addOperand(Value *M);
310313 StringRef getName() const;
314317 // ---------------------------------------------------------------------------
315318 // Operand Iterator interface...
316319 //
317 typedef op_iterator_impl<MDNode*, MDNode> op_iterator;
320 typedef op_iterator_impl<Value *, Value> op_iterator;
318321 op_iterator op_begin() { return op_iterator(this, 0); }
319322 op_iterator op_end() { return op_iterator(this, getNumOperands()); }
320323
321 typedef op_iterator_implMDNode*, MDNode> const_op_iterator;
324 typedef op_iterator_implValue *, Value> const_op_iterator;
322325 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
323326 const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
324327
13971397
13981398 if (const NamedMDNode *NMD = M.getNamedMetadata("llvm.ident")) {
13991399 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1400 const MDNode *N = NMD->getOperand(i);
1400 const MDNode *N = NMD->getOperandAsMDNode(i);
14011401 assert(N->getNumOperands() == 1 &&
14021402 "llvm.ident metadata entry can have only one operand");
14031403 const MDString *S = cast(N->getOperand(0));
449449
450450 SingleCU = CU_Nodes->getNumOperands() == 1;
451451
452 for (MDNode *N : CU_Nodes->operands()) {
453 DICompileUnit CUNode(N);
452 for (Value *N : CU_Nodes->operands()) {
453 DICompileUnit CUNode(cast(N));
454454 DwarfCompileUnit &CU = constructDwarfCompileUnit(CUNode);
455455 DIArray ImportedEntities = CUNode.getImportedEntities();
456456 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
525525 const Module *M = MMI->getModule();
526526
527527 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
528 for (MDNode *N : CU_Nodes->operands()) {
529 DICompileUnit TheCU(N);
528 for (Value *N : CU_Nodes->operands()) {
529 DICompileUnit TheCU(cast(N));
530530 // Construct subprogram DIE and add variables DIEs.
531531 DwarfCompileUnit *SPCU =
532532 static_cast(CUMap.lookup(TheCU));
559559 }
560560
561561 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
562 unwrap(Inst)->setMetadata(KindID,
563 MD ? unwrap(MD) : nullptr);
562 unwrap(Inst)
563 ->setMetadata(KindID, MD ? unwrap(MD) : nullptr);
564564 }
565565
566566 /*--.. Conversion functions ................................................--*/
719719 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
720720 if (!N)
721721 return;
722 MDNode *Op = Val ? unwrape>(Val) : nullptr;
722 Value *Op = Val ? unwrape>(Val) : nullptr;
723723 if (Op)
724724 N->addOperand(Op);
725725 }
948948 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
949949 DITypeIdentifierMap Map;
950950 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
951 DICompileUnit CU(CU_Nodes->getOperand(CUi));
951 DICompileUnit CU(CU_Nodes->getOperandAsMDNode(CUi));
952952 DIArray Retain = CU.getRetainedTypes();
953953 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
954954 if (!Retain.getElement(Ti).isCompositeType())
996996 InitializeTypeMap(M);
997997 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
998998 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
999 DICompileUnit CU(CU_Nodes->getOperand(i));
999 DICompileUnit CU(CU_Nodes->getOperandAsMDNode(i));
10001000 addCompileUnit(CU);
10011001 DIArray GVs = CU.getGlobalVariables();
10021002 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
15411541 if (!CU_Nodes)
15421542 return R;
15431543
1544 for (MDNode *N : CU_Nodes->operands()) {
1545 DICompileUnit CUNode(N);
1544 for (Value *N : CU_Nodes->operands()) {
1545 DICompileUnit CUNode(cast(N));
15461546 DIArray SPs = CUNode.getSubprograms();
15471547 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
15481548 DISubprogram SP(SPs.getElement(i));
554554 // NamedMDNode implementation.
555555 //
556556
557 static SmallVector, 4> &getNMDOps(void *Operands) {
558 return *(SmallVector, 4>*)Operands;
557 static SmallVector, 4> &getNMDOps(void *Operands) {
558 return *(SmallVector, 4> *)Operands;
559559 }
560560
561561 NamedMDNode::NamedMDNode(const Twine &N)
562 : Name(N.str()), Parent(nullptr),
563 Operands(new SmallVector, 4>()) {
564 }
562 : Name(N.str()), Parent(nullptr),
563 Operands(new SmallVector, 4>()) {}
565564
566565 NamedMDNode::~NamedMDNode() {
567566 dropAllReferences();
572571 return (unsigned)getNMDOps(Operands).size();
573572 }
574573
575 MDNode *NamedMDNode::getOperand(unsigned i) const {
574 Value *NamedMDNode::getOperand(unsigned i) const {
576575 assert(i < getNumOperands() && "Invalid Operand number!");
577576 return &*getNMDOps(Operands)[i];
578577 }
581580 auto *M = cast(V);
582581 assert(!M->isFunctionLocal() &&
583582 "NamedMDNode operands must not be function-local!");
584 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
583 getNMDOps(Operands).push_back(TrackingVH<Value>(M));
585584 }
586585
587586 void NamedMDNode::eraseFromParent() {
275275 const NamedMDNode *ModFlags = getModuleFlagsMetadata();
276276 if (!ModFlags) return;
277277
278 for (const MDNode *Flag : ModFlags->operands()) {
278 for (const Value *FlagMD : ModFlags->operands()) {
279 const MDNode *Flag = cast(FlagMD);
279280 ModFlagBehavior MFB;
280281 if (Flag->getNumOperands() >= 3 &&
281282 isValidModFlagBehavior(Flag->getOperand(0), MFB) &&
8080 E = M.named_metadata_end(); I != E; ++I) {
8181 const NamedMDNode *NMD = I;
8282 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
83 incorporateMDNode(NMD->getOperand(i));
83 incorporateMDNode(NMD->getOperandAsMDNode(i));
8484 }
8585 }
8686
554554
555555 void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
556556 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) {
557 MDNode *MD = NMD.getOperand(i);
557 MDNode *MD = NMD.getOperandAsMDNode(i);
558558 if (!MD)
559559 continue;
560560
623623 // llvm.ident takes a list of metadata entry. Each entry has only one string.
624624 // Scan each llvm.ident entry and make sure that this requirement is met.
625625 for (unsigned i = 0, e = Idents->getNumOperands(); i != e; ++i) {
626 const MDNode *N = Idents->getOperand(i);
626 const MDNode *N = Idents->getOperandAsMDNode(i);
627627 Assert1(N->getNumOperands() == 1,
628628 "incorrect number of operands in llvm.ident metadata", N);
629629 Assert1(isa(N->getOperand(0)),
641641 DenseMap SeenIDs;
642642 SmallVector Requirements;
643643 for (unsigned I = 0, E = Flags->getNumOperands(); I != E; ++I) {
644 visitModuleFlag(Flags->getOperand(I), SeenIDs, Requirements);
644 visitModuleFlag(Flags->getOperandAsMDNode(I), SeenIDs, Requirements);
645645 }
646646
647647 // Validate that the requirements in the module are valid.
12861286 DenseMap Flags;
12871287 SmallSetVector Requirements;
12881288 for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1289 MDNode *Op = DstModFlags->getOperand(I);
1289 MDNode *Op = DstModFlags->getOperandAsMDNode(I);
12901290 ConstantInt *Behavior = cast(Op->getOperand(0));
12911291 MDString *ID = cast(Op->getOperand(1));
12921292
13011301 // requirements.
13021302 bool HasErr = false;
13031303 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1304 MDNode *SrcOp = SrcModFlags->getOperand(I);
1304 MDNode *SrcOp = SrcModFlags->getOperandAsMDNode(I);
13051305 ConstantInt *SrcBehavior = cast(SrcOp->getOperand(0));
13061306 MDString *ID = cast(SrcOp->getOperand(1));
13071307 MDNode *DstOp = Flags.lookup(ID);
370370 // Check if any operand is or contains a global variable in GVMap, and thus
371371 // converted to another value.
372372 for (unsigned i = 0; i < NumOperands; ++i) {
373 MDNode *Operand = N->getOperand(i);
373 MDNode *Operand = cast(N->getOperand(i));
374374 MDNode *NewOperand = remapMDNode(M, Operand);
375375 OperandChanged |= Operand != NewOperand;
376376 NewOperands.push_back(NewOperand);
7272 return;
7373 key_val_pair_t tmp;
7474 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
75 const MDNode *elem = NMD->getOperand(i);
75 const MDNode *elem = NMD->getOperandAsMDNode(i);
7676
7777 Value *entity = elem->getOperand(0);
7878 // entity may be null due to DCE
255255 NamedMDNode *Globals = M.getNamedMetadata("llvm.asan.globals");
256256 if (!Globals)
257257 return;
258 for (auto MDN : Globals->operands()) {
258 for (const Value *MDV : Globals->operands()) {
259 const MDNode *MDN = cast(MDV);
260
259261 // Metadata node contains the global and the fields of "Entry".
260262 assert(MDN->getNumOperands() == 5);
261263 Value *V = MDN->getOperand(0);
473473 if (!CUNode || CUNode->getNumOperands() == 0)
474474 return false;
475475
476 DICompileUnit CU(CUNode->getOperand(0));
476 DICompileUnit CU(cast(CUNode->getOperand(0)));
477477 if (!CU.Verify())
478478 return false;
479479
416416 std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) {
417417 if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
418418 for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
419 MDNode *N = GCov->getOperand(i);
419 MDNode *N = GCov->getOperandAsMDNode(i);
420420 if (N->getNumOperands() != 2) continue;
421421 MDString *GCovFile = dyn_cast(N->getOperand(0));
422422 MDNode *CompileUnit = dyn_cast(N->getOperand(1));
478478 // this pass over the original .o's as they're produced, or run it after
479479 // LTO, we'll generate the same .gcno files.
480480
481 DICompileUnit CU(CU_Nodes->getOperand(i));
481 DICompileUnit CU(CU_Nodes->getOperandAsMDNode(i));
482482 std::error_code EC;
483483 raw_fd_ostream out(mangleName(CU, "gcno"), EC, sys::fs::F_None);
484484 std::string EdgeDestinations;
564564 bool Result = false;
565565 bool InsertIndCounterIncrCode = false;
566566 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
567 DICompileUnit CU(CU_Nodes->getOperand(i));
567 DICompileUnit CU(CU_Nodes->getOperandAsMDNode(i));
568568 DIArray SPs = CU.getSubprograms();
569569 SmallVector, 8> CountersBySP;
570570 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
845845 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
846846 if (CU_Nodes) {
847847 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
848 DICompileUnit CU(CU_Nodes->getOperand(i));
848 DICompileUnit CU(CU_Nodes->getOperandAsMDNode(i));
849849 std::string FilenameGcda = mangleName(CU, "gcda");
850850 uint32_t CfgChecksum = FileChecksums.empty() ? 0 : FileChecksums[i];
851851 Builder.CreateCall3(StartFile,
304304 if (NamedMDNode *NMD =
305305 M.getNamedMetadata("clang.arc.retainAutoreleasedReturnValueMarker"))
306306 if (NMD->getNumOperands() == 1) {
307 const MDNode *N = NMD->getOperand(0);
307 const MDNode *N = NMD->getOperandAsMDNode(0);
308308 if (N->getNumOperands() == 1)
309309 if (const MDString *S = dyn_cast(N->getOperand(0)))
310310 RetainRVMarker = S;
5454 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
5555 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
5656 std::string Name;
57 DISubprogram SP(NMD->getOperand(i));
57 DISubprogram SP(cast_or_null(NMD->getOperand(i)));
5858 assert((!SP || SP.isSubprogram()) &&
5959 "A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
6060 if (!SP)