Creating TBAA metadata in LLVM – Advanced IR Generation


To create the metadata, we must use the llvm::MDBuilder class, which is declared in the llvm/IR/MDBuilder.h header file. The data itself is stored in instances of the llvm::MDNode and llvm::MDString classes. Using the builder class shields us from the internal details of the construction.

A root node is created with a call to the createTBAARoot() method, which expects the name of the type hierarchy as a parameter and returns the root node. An anonymous, unique root node can be created with the createAnonymousTBAARoot() method.

A scalar type is added to the hierarchy with the createTBAAScalarTypeNode() method, which takes the name of the type and the parent node as a parameter.

On the other hand, adding a type node for an aggregate type is slightly more complex. The createTBAAStructTypeNode() method takes the name of the type and a list of the fields as parameters. Specifically, the fields are given as a std::pair<llvm::MDNode*, uint64_t> instance, where the first element indicates the type of the member and the second element represents the offset in struct.

An access tag is created with the createTBAAStructTagNode() method, which takes the base type, the access type, and the offset as parameters.

Lastly, the metadata must be attached to a load or store instruction. The llvm::Instruction class contains a method called setMetadata(), which is used to add various type-based alias analysis metadata. The first parameter must be of the llvm::LLVMContext::MD_tbaa type and the second must be the access tag.

Equipped with this knowledge, we must add metadata for type-based alias analysis (TBAA) to tinylang.

Leave a Reply

Your email address will not be published. Required fields are marked *