Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/CodeGen/ConstantInitBuilder.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ConstantInitBuilder.cpp - Global initializer builder -------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file defines out-of-line routines for building initializers for
10
// global variables, in particular the kind of globals that are implicitly
11
// introduced by various language ABIs.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/CodeGen/ConstantInitBuilder.h"
16
#include "CodeGenModule.h"
17
18
using namespace clang;
19
using namespace CodeGen;
20
21
0
llvm::Type *ConstantInitFuture::getType() const {
22
0
  assert(Data && "dereferencing null future");
23
0
  if (Data.is<llvm::Constant*>()) {
24
0
    return Data.get<llvm::Constant*>()->getType();
25
0
  } else {
26
0
    return Data.get<ConstantInitBuilderBase*>()->Buffer[0]->getType();
27
0
  }
28
0
}
29
30
0
void ConstantInitFuture::abandon() {
31
0
  assert(Data && "abandoning null future");
32
0
  if (auto builder = Data.dyn_cast<ConstantInitBuilderBase*>()) {
33
0
    builder->abandon(0);
34
0
  }
35
0
  Data = nullptr;
36
0
}
37
38
0
void ConstantInitFuture::installInGlobal(llvm::GlobalVariable *GV) {
39
0
  assert(Data && "installing null future");
40
0
  if (Data.is<llvm::Constant*>()) {
41
0
    GV->setInitializer(Data.get<llvm::Constant*>());
42
0
  } else {
43
0
    auto &builder = *Data.get<ConstantInitBuilderBase*>();
44
0
    assert(builder.Buffer.size() == 1);
45
0
    builder.setGlobalInitializer(GV, builder.Buffer[0]);
46
0
    builder.Buffer.clear();
47
0
    Data = nullptr;
48
0
  }
49
0
}
50
51
ConstantInitFuture
52
0
ConstantInitBuilderBase::createFuture(llvm::Constant *initializer) {
53
0
  assert(Buffer.empty() && "buffer not current empty");
54
0
  Buffer.push_back(initializer);
55
0
  return ConstantInitFuture(this);
56
0
}
57
58
// Only used in this file.
59
inline ConstantInitFuture::ConstantInitFuture(ConstantInitBuilderBase *builder)
60
0
    : Data(builder) {
61
0
  assert(!builder->Frozen);
62
0
  assert(builder->Buffer.size() == 1);
63
0
  assert(builder->Buffer[0] != nullptr);
64
0
}
65
66
llvm::GlobalVariable *
67
ConstantInitBuilderBase::createGlobal(llvm::Constant *initializer,
68
                                      const llvm::Twine &name,
69
                                      CharUnits alignment,
70
                                      bool constant,
71
                                      llvm::GlobalValue::LinkageTypes linkage,
72
18.2k
                                      unsigned addressSpace) {
73
18.2k
  auto GV = new llvm::GlobalVariable(CGM.getModule(),
74
18.2k
                                     initializer->getType(),
75
18.2k
                                     constant,
76
18.2k
                                     linkage,
77
18.2k
                                     initializer,
78
18.2k
                                     name,
79
18.2k
                                     /*insert before*/ nullptr,
80
18.2k
                                     llvm::GlobalValue::NotThreadLocal,
81
18.2k
                                     addressSpace);
82
18.2k
  GV->setAlignment(alignment.getQuantity());
83
18.2k
  resolveSelfReferences(GV);
84
18.2k
  return GV;
85
18.2k
}
86
87
void ConstantInitBuilderBase::setGlobalInitializer(llvm::GlobalVariable *GV,
88
6.11k
                                                   llvm::Constant *initializer){
89
6.11k
  GV->setInitializer(initializer);
90
6.11k
91
6.11k
  if (!SelfReferences.empty())
92
0
    resolveSelfReferences(GV);
93
6.11k
}
94
95
18.2k
void ConstantInitBuilderBase::resolveSelfReferences(llvm::GlobalVariable *GV) {
96
18.2k
  for (auto &entry : SelfReferences) {
97
0
    llvm::Constant *resolvedReference =
98
0
      llvm::ConstantExpr::getInBoundsGetElementPtr(
99
0
        GV->getValueType(), GV, entry.Indices);
100
0
    auto dummy = entry.Dummy;
101
0
    dummy->replaceAllUsesWith(resolvedReference);
102
0
    dummy->eraseFromParent();
103
0
  }
104
18.2k
  SelfReferences.clear();
105
18.2k
}
106
107
608
void ConstantInitBuilderBase::abandon(size_t newEnd) {
108
608
  // Remove all the entries we've added.
109
608
  Buffer.erase(Buffer.begin() + newEnd, Buffer.end());
110
608
111
608
  // If we're abandoning all the way to the beginning, destroy
112
608
  // all the self-references, because we might not get another
113
608
  // opportunity.
114
608
  if (newEnd == 0) {
115
304
    for (auto &entry : SelfReferences) {
116
0
      auto dummy = entry.Dummy;
117
0
      dummy->replaceAllUsesWith(llvm::UndefValue::get(dummy->getType()));
118
0
      dummy->eraseFromParent();
119
0
    }
120
304
    SelfReferences.clear();
121
304
  }
122
608
}
123
124
0
void ConstantAggregateBuilderBase::addSize(CharUnits size) {
125
0
  add(Builder.CGM.getSize(size));
126
0
}
127
128
llvm::Constant *
129
ConstantAggregateBuilderBase::getRelativeOffset(llvm::IntegerType *offsetType,
130
0
                                                llvm::Constant *target) {
131
0
  // Compute the address of the relative-address slot.
132
0
  auto base = getAddrOfCurrentPosition(offsetType);
133
0
134
0
  // Subtract.
135
0
  base = llvm::ConstantExpr::getPtrToInt(base, Builder.CGM.IntPtrTy);
136
0
  target = llvm::ConstantExpr::getPtrToInt(target, Builder.CGM.IntPtrTy);
137
0
  llvm::Constant *offset = llvm::ConstantExpr::getSub(target, base);
138
0
139
0
  // Truncate to the relative-address type if necessary.
140
0
  if (Builder.CGM.IntPtrTy != offsetType) {
141
0
    offset = llvm::ConstantExpr::getTrunc(offset, offsetType);
142
0
  }
143
0
144
0
  return offset;
145
0
}
146
147
llvm::Constant *
148
0
ConstantAggregateBuilderBase::getAddrOfCurrentPosition(llvm::Type *type) {
149
0
  // Make a global variable.  We will replace this with a GEP to this
150
0
  // position after installing the initializer.
151
0
  auto dummy =
152
0
    new llvm::GlobalVariable(Builder.CGM.getModule(), type, true,
153
0
                             llvm::GlobalVariable::PrivateLinkage,
154
0
                             nullptr, "");
155
0
  Builder.SelfReferences.emplace_back(dummy);
156
0
  auto &entry = Builder.SelfReferences.back();
157
0
  (void) getGEPIndicesToCurrentPosition(entry.Indices);
158
0
  return dummy;
159
0
}
160
161
void ConstantAggregateBuilderBase::getGEPIndicesTo(
162
                               llvm::SmallVectorImpl<llvm::Constant*> &indices,
163
0
                               size_t position) const {
164
0
  // Recurse on the parent builder if present.
165
0
  if (Parent) {
166
0
    Parent->getGEPIndicesTo(indices, Begin);
167
0
168
0
  // Otherwise, add an index to drill into the first level of pointer.
169
0
  } else {
170
0
    assert(indices.empty());
171
0
    indices.push_back(llvm::ConstantInt::get(Builder.CGM.Int32Ty, 0));
172
0
  }
173
0
174
0
  assert(position >= Begin);
175
0
  // We have to use i32 here because struct GEPs demand i32 indices.
176
0
  // It's rather unlikely to matter in practice.
177
0
  indices.push_back(llvm::ConstantInt::get(Builder.CGM.Int32Ty,
178
0
                                           position - Begin));
179
0
}
180
181
ConstantAggregateBuilderBase::PlaceholderPosition
182
0
ConstantAggregateBuilderBase::addPlaceholderWithSize(llvm::Type *type) {
183
0
  // Bring the offset up to the last field.
184
0
  CharUnits offset = getNextOffsetFromGlobal();
185
0
186
0
  // Create the placeholder.
187
0
  auto position = addPlaceholder();
188
0
189
0
  // Advance the offset past that field.
190
0
  auto &layout = Builder.CGM.getDataLayout();
191
0
  if (!Packed)
192
0
    offset = offset.alignTo(CharUnits::fromQuantity(
193
0
                                layout.getABITypeAlignment(type)));
194
0
  offset += CharUnits::fromQuantity(layout.getTypeStoreSize(type));
195
0
196
0
  CachedOffsetEnd = Builder.Buffer.size();
197
0
  CachedOffsetFromGlobal = offset;
198
0
199
0
  return position;
200
0
}
201
202
0
CharUnits ConstantAggregateBuilderBase::getOffsetFromGlobalTo(size_t end) const{
203
0
  size_t cacheEnd = CachedOffsetEnd;
204
0
  assert(cacheEnd <= end);
205
0
206
0
  // Fast path: if the cache is valid, just use it.
207
0
  if (cacheEnd == end) {
208
0
    return CachedOffsetFromGlobal;
209
0
  }
210
0
211
0
  // If the cached range ends before the index at which the current
212
0
  // aggregate starts, recurse for the parent.
213
0
  CharUnits offset;
214
0
  if (cacheEnd < Begin) {
215
0
    assert(cacheEnd == 0);
216
0
    assert(Parent && "Begin != 0 for root builder");
217
0
    cacheEnd = Begin;
218
0
    offset = Parent->getOffsetFromGlobalTo(Begin);
219
0
  } else {
220
0
    offset = CachedOffsetFromGlobal;
221
0
  }
222
0
223
0
  // Perform simple layout on the elements in cacheEnd..<end.
224
0
  if (cacheEnd != end) {
225
0
    auto &layout = Builder.CGM.getDataLayout();
226
0
    do {
227
0
      llvm::Constant *element = Builder.Buffer[cacheEnd];
228
0
      assert(element != nullptr &&
229
0
             "cannot compute offset when a placeholder is present");
230
0
      llvm::Type *elementType = element->getType();
231
0
      if (!Packed)
232
0
        offset = offset.alignTo(CharUnits::fromQuantity(
233
0
                                  layout.getABITypeAlignment(elementType)));
234
0
      offset += CharUnits::fromQuantity(layout.getTypeStoreSize(elementType));
235
0
    } while (++cacheEnd != end);
236
0
  }
237
0
238
0
  // Cache and return.
239
0
  CachedOffsetEnd = cacheEnd;
240
0
  CachedOffsetFromGlobal = offset;
241
0
  return offset;
242
0
}
243
244
10.8k
llvm::Constant *ConstantAggregateBuilderBase::finishArray(llvm::Type *eltTy) {
245
10.8k
  markFinished();
246
10.8k
247
10.8k
  auto &buffer = getBuffer();
248
10.8k
  assert((Begin < buffer.size() ||
249
10.8k
          (Begin == buffer.size() && eltTy))
250
10.8k
         && "didn't add any array elements without element type");
251
10.8k
  auto elts = llvm::makeArrayRef(buffer).slice(Begin);
252
10.8k
  if (!eltTy) 
eltTy = elts[0]->getType()17
;
253
10.8k
  auto type = llvm::ArrayType::get(eltTy, elts.size());
254
10.8k
  auto constant = llvm::ConstantArray::get(type, elts);
255
10.8k
  buffer.erase(buffer.begin() + Begin, buffer.end());
256
10.8k
  return constant;
257
10.8k
}
258
259
llvm::Constant *
260
28.6k
ConstantAggregateBuilderBase::finishStruct(llvm::StructType *ty) {
261
28.6k
  markFinished();
262
28.6k
263
28.6k
  auto &buffer = getBuffer();
264
28.6k
  auto elts = llvm::makeArrayRef(buffer).slice(Begin);
265
28.6k
266
28.6k
  if (ty == nullptr && 
elts.empty()8.04k
)
267
0
    ty = llvm::StructType::get(Builder.CGM.getLLVMContext(), {}, Packed);
268
28.6k
269
28.6k
  llvm::Constant *constant;
270
28.6k
  if (ty) {
271
20.6k
    assert(ty->isPacked() == Packed);
272
20.6k
    constant = llvm::ConstantStruct::get(ty, elts);
273
20.6k
  } else {
274
8.04k
    constant = llvm::ConstantStruct::getAnon(elts, Packed);
275
8.04k
  }
276
28.6k
277
28.6k
  buffer.erase(buffer.begin() + Begin, buffer.end());
278
28.6k
  return constant;
279
28.6k
}