[PATCH] D136637: [llvm-c] add LLVMReplaceMDNodeOperandWith

Davide Bertola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 24 13:38:00 PDT 2022


davibe created this revision.
davibe added a reviewer: lattner.
Herald added a reviewer: deadalnix.
Herald added a subscriber: hiraditya.
Herald added a project: All.
davibe requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Hello!

I'm working on a tool that visits debug info and massages it using the llvm-c API. I noticed that `LLVMGetOperand` special cases `MDNode`s so I can get their operands, but I can't replace them. This patch adds `LLVMReplaceMDNodeOperandWith` which boils down to `MDNode::replaceOperandWith`.

The name was chosen for consistency with `LLVMGetMDNodeOperands` and `LLVMGetMDNodeNumOperands`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136637

Files:
  llvm/include/llvm-c/Core.h
  llvm/lib/IR/Core.cpp


Index: llvm/lib/IR/Core.cpp
===================================================================
--- llvm/lib/IR/Core.cpp
+++ llvm/lib/IR/Core.cpp
@@ -1248,6 +1248,13 @@
     Dest[i] = getMDNodeOperandImpl(Context, N, i);
 }
 
+void LLVMReplaceMDNodeOperandWith(LLVMValueRef V, unsigned Index,
+                                  LLVMMetadataRef Replacement) {
+  auto *MD = cast<MetadataAsValue>(unwrap(V));
+  auto *N = cast<MDNode>(MD->getMetadata());
+  N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
+}
+
 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
   if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
     return N->getNumOperands();
Index: llvm/include/llvm-c/Core.h
===================================================================
--- llvm/include/llvm-c/Core.h
+++ llvm/include/llvm-c/Core.h
@@ -2915,6 +2915,14 @@
  */
 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest);
 
+/**
+ * Replace an operand at a specific index in a llvm::MDNode value.
+ *
+ * @see llvm::MDNode::replaceOperandWith()
+ */
+void LLVMReplaceMDNodeOperandWith(LLVMValueRef V, unsigned Index,
+                                  LLVMMetadataRef Replacement);
+
 /** Deprecated: Use LLVMMDStringInContext2 instead. */
 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
                                    unsigned SLen);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136637.470273.patch
Type: text/x-patch
Size: 1393 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221024/ba6ff9a6/attachment.bin>


More information about the llvm-commits mailing list