How to insert metadata in llvm IR?

As title.
I try to write a pass to covert the bitcode with no debug info to the bitcode
debug info. I use DIBuilder as a LLVM API so that I can generate DI(Debug info).
Now I have some DIDescriptor , e.g. DIVariable ,DICompileUnit, DISubprogram , I
can use some function to insert metadata.

1. LLVM::Instruction::SetDebugLoc(...) => insert the debug info location like
DILexicalBlock ,and insert !dbg appending an instruction.
2. LLVM::Instruction::setMetadata(...) => insert the metadta and !dbg appending
3. DIBuilder::insertDeclare(...) => insert variable declare (like
@llvm.dbg.declare) and metadata.
4. llvm::NamedMDNode::addOperand(...) => insert and Named Metadata. Like

I can use above four function to insert metadata into bitcode. But I can't just
insert a metadata.
This problem is occur when I want to create an DISubprogram
metadata into bc.
I can't use above four function because this descriptor not
append any instruction and I can't use declare to generate it.
This descriptor is
not a NamedMDNode too. I have try many way to insert it, but I failed...

Can you tell me a right way to insert this MD?

Why would you do this in this way? Why not just do it at the time you create the module in the first place? Your summation of how the debug information is supposed to be added via the classes also isn't quite correct. I'd look into how clang emits debug information as a guideline.