Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/IR/MDBuilder.cpp
Line
Count
Source (jump to first uncovered line)
1
//===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file defines the MDBuilder class, which is used as a convenient way to
11
// create LLVM metadata with a consistent and simplified interface.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "llvm/IR/MDBuilder.h"
16
#include "llvm/IR/Constants.h"
17
#include "llvm/IR/Metadata.h"
18
using namespace llvm;
19
20
125k
MDString *MDBuilder::createString(StringRef Str) {
21
125k
  return MDString::get(Context, Str);
22
125k
}
23
24
455k
ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
25
455k
  return ConstantAsMetadata::get(C);
26
455k
}
27
28
17
MDNode *MDBuilder::createFPMath(float Accuracy) {
29
17
  if (Accuracy == 0.0)
30
1
    return nullptr;
31
17
  assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
32
16
  auto *Op =
33
16
      createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
34
16
  return MDNode::get(Context, Op);
35
16
}
36
37
MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
38
7.44k
                                       uint32_t FalseWeight) {
39
7.44k
  return createBranchWeights({TrueWeight, FalseWeight});
40
7.44k
}
41
42
9.29k
MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
43
9.29k
  assert(Weights.size() >= 1 && "Need at least one branch weights!");
44
9.29k
45
9.29k
  SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
46
9.29k
  Vals[0] = createString("branch_weights");
47
9.29k
48
9.29k
  Type *Int32Ty = Type::getInt32Ty(Context);
49
29.4k
  for (unsigned i = 0, e = Weights.size(); 
i != e29.4k
;
++i20.1k
)
50
20.1k
    Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
51
9.29k
52
9.29k
  return MDNode::get(Context, Vals);
53
9.29k
}
54
55
2
MDNode *MDBuilder::createUnpredictable() {
56
2
  return MDNode::get(Context, None);
57
2
}
58
59
MDNode *MDBuilder::createFunctionEntryCount(
60
349
    uint64_t Count, const DenseSet<GlobalValue::GUID> *Imports) {
61
349
  Type *Int64Ty = Type::getInt64Ty(Context);
62
349
  SmallVector<Metadata *, 8> Ops;
63
349
  Ops.push_back(createString("function_entry_count"));
64
349
  Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
65
349
  if (
Imports349
) {
66
70
    SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
67
70
    std::stable_sort(OrderID.begin(), OrderID.end(),
68
3
      [] (GlobalValue::GUID A, GlobalValue::GUID B) {
69
3
        return A < B;});
70
70
    for (auto ID : OrderID)
71
8
      Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
72
70
  }
73
349
  return MDNode::get(Context, Ops);
74
349
}
75
76
14
MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
77
14
  return MDNode::get(Context,
78
14
                     {createString("function_section_prefix"),
79
14
                      createString(Prefix)});
80
14
}
81
82
9.16k
MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
83
9.16k
  assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
84
9.16k
85
9.16k
  Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
86
9.16k
  return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
87
9.16k
}
88
89
9.16k
MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
90
9.16k
  // If the range is everything then it is useless.
91
9.16k
  if (Hi == Lo)
92
4
    return nullptr;
93
9.16k
94
9.16k
  // Return the range [Lo, Hi).
95
9.16k
  return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
96
9.16k
}
97
98
10.2k
MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
99
10.2k
  // To ensure uniqueness the root node is self-referential.
100
10.2k
  auto Dummy = MDNode::getTemporary(Context, None);
101
10.2k
102
10.2k
  SmallVector<Metadata *, 3> Args(1, Dummy.get());
103
10.2k
  if (Extra)
104
6.97k
    Args.push_back(Extra);
105
10.2k
  if (!Name.empty())
106
4.34k
    Args.push_back(createString(Name));
107
10.2k
  MDNode *Root = MDNode::get(Context, Args);
108
10.2k
109
10.2k
  // At this point we have
110
10.2k
  //   !0 = metadata !{}            <- dummy
111
10.2k
  //   !1 = metadata !{metadata !0} <- root
112
10.2k
  // Replace the dummy operand with the root node itself and delete the dummy.
113
10.2k
  Root->replaceOperandWith(0, Root);
114
10.2k
115
10.2k
  // We now have
116
10.2k
  //   !1 = metadata !{metadata !1} <- self-referential root
117
10.2k
  return Root;
118
10.2k
}
119
120
7.96k
MDNode *MDBuilder::createTBAARoot(StringRef Name) {
121
7.96k
  return MDNode::get(Context, createString(Name));
122
7.96k
}
123
124
/// \brief Return metadata for a non-root TBAA node with the given name,
125
/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
126
MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
127
8
                                  bool isConstant) {
128
8
  if (
isConstant8
) {
129
1
    Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
130
1
    return MDNode::get(Context,
131
1
                       {createString(Name), Parent, createConstant(Flags)});
132
1
  }
133
7
  return MDNode::get(Context, {createString(Name), Parent});
134
7
}
135
136
0
MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
137
0
  return MDNode::get(Context, createString(Name));
138
0
}
139
140
0
MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
141
0
  return MDNode::get(Context, {createString(Name), Domain});
142
0
}
143
144
/// \brief Return metadata for a tbaa.struct node with the given
145
/// struct field descriptions.
146
4.20k
MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
147
4.20k
  SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
148
4.20k
  Type *Int64 = Type::getInt64Ty(Context);
149
23.2k
  for (unsigned i = 0, e = Fields.size(); 
i != e23.2k
;
++i19.0k
) {
150
19.0k
    Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
151
19.0k
    Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
152
19.0k
    Vals[i * 3 + 2] = Fields[i].TBAA;
153
19.0k
  }
154
4.20k
  return MDNode::get(Context, Vals);
155
4.20k
}
156
157
/// \brief Return metadata for a TBAA struct node in the type DAG
158
/// with the given name, a list of pairs (offset, field type in the type DAG).
159
MDNode *MDBuilder::createTBAAStructTypeNode(
160
16.6k
    StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
161
16.6k
  SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
162
16.6k
  Type *Int64 = Type::getInt64Ty(Context);
163
16.6k
  Ops[0] = createString(Name);
164
186k
  for (unsigned i = 0, e = Fields.size(); 
i != e186k
;
++i169k
) {
165
169k
    Ops[i * 2 + 1] = Fields[i].first;
166
169k
    Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
167
169k
  }
168
16.6k
  return MDNode::get(Context, Ops);
169
16.6k
}
170
171
/// \brief Return metadata for a TBAA scalar type node with the
172
/// given name, an offset and a parent in the TBAA type DAG.
173
MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
174
87.2k
                                            uint64_t Offset) {
175
87.2k
  ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
176
87.2k
  return MDNode::get(Context,
177
87.2k
                     {createString(Name), Parent, createConstant(Off)});
178
87.2k
}
179
180
/// \brief Return metadata for a TBAA tag node with the given
181
/// base type, access type and offset relative to the base type.
182
MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
183
121k
                                           uint64_t Offset, bool IsConstant) {
184
121k
  IntegerType *Int64 = Type::getInt64Ty(Context);
185
121k
  ConstantInt *Off = ConstantInt::get(Int64, Offset);
186
121k
  if (
IsConstant121k
) {
187
0
    return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
188
0
                                 createConstant(ConstantInt::get(Int64, 1))});
189
0
  }
190
121k
  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
191
121k
}