jesusgumbau
jesusgumbau

Reputation: 108

LLVM: How to traverse Module metadata to find a value?

I have this metadata tree in my LLVM module:

!meta.test = !{!0}    
!0 = !{"str1", "str2", !1}
!1 = !{!2, !3, null}
!2 = !{"str3", i8 5}

I want to be able to get the value: i8 5.

I am trying it using M->getNamedMetadata("meta.test"), but I'm unable to traverse the metadata tree using the LLVM API to reach that value.

How should I do this?

Cheers.

Upvotes: 4

Views: 1709

Answers (1)

Chirag Patel
Chirag Patel

Reputation: 1161

For LLVM 3.6 onwards

getNamedMetadata returns NamedMetadata, you can use getOperand(unsigned) to get MDNode and can cast to your appropriate type as per your use.

so M->getNamedMetadata("meta.test")->getOperand(0) will get you metadataNode !0 MDNode.

you can use cast< ValueAsMetadata >(MDNode)->getvalue() to get Value i8 5

or you can use cast< MDString >(MDNode)->getString() to get Value str1.

so in short you can traverse metadata MDNodes using getOperand() call and cast it to your use as per hierarchy. see this for more info.

Upvotes: 4

Related Questions