[LLVMdev] Proposal for Adding MetaData to a BasicBlock

kalyan ponnala ponnala.kalyan at gmail.com
Sat Apr 24 02:21:54 PDT 2010

Hello group,

Per my posting on the Clang Newsgroup.

I'm interested in doing some loop optimizations, towards this end I would
like to add some custom Metadata to a Loop header.

Loops in LLVM are represented using BasicBlocks, but unfortunately you can
NOT add MetaData to a BasicBlock.  Although you can add Metadata to an
instruction.  So I'm proposing to add the Metadata manipulation methods and
state from class "Instruction" to class "BasicBlock".

The class "Instruction" stores a private enumeration:

  // HasMetadataBit - This is a bit stored in the SubClassData field which
  // indicates whether this instruction has metadata attached to it or not.

  HasMetadataBit = 1 << 15

This enumeration is then utilized by hasMetadata() which is inlined in class

// hasMetadata() - Return true if this instruction has any metadata attached
to it.

bool hasMetadata() const
  return (getSubclassDataFromValue() & HasMetadataBit) != 0;

>> First Question:

Since I do not expect to require any subclasses of BasicBlock, should I just
implement in BasicBlock the following:

bool hasMetadata() const
  return (HasMetadataBit) != 0;

>> Second Question:

Additionally, after studying the way Metadata is manipulated in class
"Instruction", I propose to add the following methods from class
"Instruction" to class "BasicBlock":

    bool hasMetadata() const
    MDNode *getMetadata(unsigned KindID) const;
    MDNode *getMetadata(const char *Kind) const;
    void getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode*> >
    void setMetadata(unsigned KindID, MDNode *Node);
    void setMetadata(const char *Kind, MDNode *Node);

    // These are all implemented in Metadata.cpp.
    MDNode *getMetadataImpl(unsigned KindID) const;
    MDNode *getMetadataImpl(const char *Kind) const;
    void getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,MDNode*> >
    void removeAllMetadata();

I noticed that BasicBlock seems to be referenced by almost every class in
LLVM, so I wanted to ask which particular client classes I will need to
update so I don't break any existing code.  It seems to me, that all I'm
doing is adding code, so I shouldn't break any of the client classes, but I
will need to update the classes that will need to utilize these new methods
in class BasicBlock.

Per Chris's earlier comment, I realize that BitcodeReader / BitcodeWriter &
LLParser will need to be updated.  For the moment I am just trying to make
sure that I can procedurally add Metadata to a Loop Header via adding
Metadata to a BasicBlock.

>> Third Question:

I had some trouble understanding the implementation of SetMetadata in the
Instruction class, is this where Metadata is "attached" to an Instruction?

Any detailed comments / suggestions as to which client LLVM IR classes I
need to dig into as well as what all the changes I will have to make would
be greatly appreciated.

Kalyan Ponnala
phone: 8163772059
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100424/f0486816/attachment.html>

More information about the llvm-dev mailing list