llvm.org GIT mirror llvm / 656dc62
Add convenience methods to MDBuilder for attaching metadata to instructions, and retrieving it from instructions. I don't have a use for this but is seems logical for it to exist. While there, remove some 'const' markings from methods which are in fact 'const' in practice, but aren't logically 'const'. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154811 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 8 years ago
1 changed file(s) with 27 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
1616
1717 #include "llvm/Constants.h"
1818 #include "llvm/DerivedTypes.h"
19 #include "llvm/Instructions.h"
1920 #include "llvm/LLVMContext.h"
2021 #include "llvm/Metadata.h"
2122 #include "llvm/ADT/APInt.h"
2930 MDBuilder(LLVMContext &context) : Context(context) {}
3031
3132 /// CreateString - Return the given string as metadata.
32 MDString *CreateString(StringRef Str) const {
33 MDString *CreateString(StringRef Str) {
3334 return MDString::get(Context, Str);
3435 }
3536
3839 //===------------------------------------------------------------------===//
3940
4041 /// CreateRange - Return metadata describing the range [Lo, Hi).
41 MDNode *CreateRange(const APInt &Lo, const APInt &Hi) const {
42 MDNode *CreateRange(const APInt &Lo, const APInt &Hi) {
4243 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
4344 // If the range is everything then it is useless.
4445 if (Hi == Lo)
5051 return MDNode::get(Context, Range);
5152 }
5253
54 /// GetRangeMetadata - Get range metadata attached to an instruction.
55 MDNode *GetRangeMetadata(Instruction *I) const {
56 return I->getMetadata(LLVMContext::MD_range);
57 }
58
59 /// SetRangeMetadata - Attach range metadata to an instruction.
60 void SetRangeMetadata(Instruction *I, MDNode *RangeTag) {
61 I->setMetadata(LLVMContext::MD_range, RangeTag);
62 }
63
5364
5465 //===------------------------------------------------------------------===//
5566 // TBAA metadata.
5869 /// CreateAnonymousTBAARoot - Return metadata appropriate for a TBAA root
5970 /// node. Each returned node is distinct from all other metadata and will
6071 /// never be identified (uniqued) with anything else.
61 MDNode *CreateAnonymousTBAARoot() const {
72 MDNode *CreateAnonymousTBAARoot() {
6273 // To ensure uniqueness the root node is self-referential.
6374 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef());
6475 MDNode *Root = MDNode::get(Context, Dummy);
7687 /// CreateTBAARoot - Return metadata appropriate for a TBAA root node with
7788 /// the given name. This may be identified (uniqued) with other roots with
7889 /// the same name.
79 MDNode *CreateTBAARoot(StringRef Name) const {
90 MDNode *CreateTBAARoot(StringRef Name) {
8091 return MDNode::get(Context, CreateString(Name));
8192 }
8293
8394 /// CreateTBAANode - Return metadata for a non-root TBAA node with the given
8495 /// name, parent in the TBAA tree, and value for 'pointsToConstantMemory'.
8596 MDNode *CreateTBAANode(StringRef Name, MDNode *Parent,
86 bool isConstant = false) const {
97 bool isConstant = false) {
8798 if (isConstant) {
8899 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
89100 Value *Ops[3] = { CreateString(Name), Parent, Flags };
93104 return MDNode::get(Context, Ops);
94105 }
95106 }
107
108 /// GetTBAAMetadata - Get tbaa metadata attached to an instruction.
109 MDNode *GetTBAAMetadata(Instruction *I) const {
110 return I->getMetadata(LLVMContext::MD_tbaa);
111 }
112
113 /// SetTBAAMetadata - Attach tbaa metadata to an instruction.
114 void SetTBAAMetadata(Instruction *I, MDNode *TBAATag) {
115 I->setMetadata(LLVMContext::MD_tbaa, TBAATag);
116 }
117
96118 };
97119
98120 } // end namespace llvm