Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGExprConstant.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
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 contains code to emit Constant Expr nodes as LLVM code.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CGCXXABI.h"
14
#include "CGObjCRuntime.h"
15
#include "CGRecordLayout.h"
16
#include "CodeGenFunction.h"
17
#include "CodeGenModule.h"
18
#include "ConstantEmitter.h"
19
#include "TargetInfo.h"
20
#include "clang/AST/APValue.h"
21
#include "clang/AST/ASTContext.h"
22
#include "clang/AST/Attr.h"
23
#include "clang/AST/RecordLayout.h"
24
#include "clang/AST/StmtVisitor.h"
25
#include "clang/Basic/Builtins.h"
26
#include "llvm/ADT/STLExtras.h"
27
#include "llvm/ADT/Sequence.h"
28
#include "llvm/IR/Constants.h"
29
#include "llvm/IR/DataLayout.h"
30
#include "llvm/IR/Function.h"
31
#include "llvm/IR/GlobalVariable.h"
32
using namespace clang;
33
using namespace CodeGen;
34
35
//===----------------------------------------------------------------------===//
36
//                            ConstantAggregateBuilder
37
//===----------------------------------------------------------------------===//
38
39
namespace {
40
class ConstExprEmitter;
41
42
struct ConstantAggregateBuilderUtils {
43
  CodeGenModule &CGM;
44
45
10.9k
  ConstantAggregateBuilderUtils(CodeGenModule &CGM) : CGM(CGM) {}
46
47
21.6k
  CharUnits getAlignment(const llvm::Constant *C) const {
48
21.6k
    return CharUnits::fromQuantity(
49
21.6k
        CGM.getDataLayout().getABITypeAlignment(C->getType()));
50
21.6k
  }
51
52
16.5k
  CharUnits getSize(llvm::Type *Ty) const {
53
16.5k
    return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(Ty));
54
16.5k
  }
55
56
11.1k
  CharUnits getSize(const llvm::Constant *C) const {
57
11.1k
    return getSize(C->getType());
58
11.1k
  }
59
60
414
  llvm::Constant *getPadding(CharUnits PadSize) const {
61
414
    llvm::Type *Ty = CGM.Int8Ty;
62
414
    if (PadSize > CharUnits::One())
63
381
      Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
64
414
    return llvm::UndefValue::get(Ty);
65
414
  }
66
67
8
  llvm::Constant *getZeroes(CharUnits ZeroSize) const {
68
8
    llvm::Type *Ty = llvm::ArrayType::get(CGM.Int8Ty, ZeroSize.getQuantity());
69
8
    return llvm::ConstantAggregateZero::get(Ty);
70
8
  }
71
};
72
73
/// Incremental builder for an llvm::Constant* holding a struct or array
74
/// constant.
75
class ConstantAggregateBuilder : private ConstantAggregateBuilderUtils {
76
  /// The elements of the constant. These two arrays must have the same size;
77
  /// Offsets[i] describes the offset of Elems[i] within the constant. The
78
  /// elements are kept in increasing offset order, and we ensure that there
79
  /// is no overlap: Offsets[i+1] >= Offsets[i] + getSize(Elemes[i]).
80
  ///
81
  /// This may contain explicit padding elements (in order to create a
82
  /// natural layout), but need not. Gaps between elements are implicitly
83
  /// considered to be filled with undef.
84
  llvm::SmallVector<llvm::Constant*, 32> Elems;
85
  llvm::SmallVector<CharUnits, 32> Offsets;
86
87
  /// The size of the constant (the maximum end offset of any added element).
88
  /// May be larger than the end of Elems.back() if we split the last element
89
  /// and removed some trailing undefs.
90
  CharUnits Size = CharUnits::Zero();
91
92
  /// This is true only if laying out Elems in order as the elements of a
93
  /// non-packed LLVM struct will give the correct layout.
94
  bool NaturalLayout = true;
95
96
  bool split(size_t Index, CharUnits Hint);
97
  Optional<size_t> splitAt(CharUnits Pos);
98
99
  static llvm::Constant *buildFrom(CodeGenModule &CGM,
100
                                   ArrayRef<llvm::Constant *> Elems,
101
                                   ArrayRef<CharUnits> Offsets,
102
                                   CharUnits StartOffset, CharUnits Size,
103
                                   bool NaturalLayout, llvm::Type *DesiredTy,
104
                                   bool AllowOversized);
105
106
public:
107
  ConstantAggregateBuilder(CodeGenModule &CGM)
108
5.49k
      : ConstantAggregateBuilderUtils(CGM) {}
109
110
  /// Update or overwrite the value starting at \p Offset with \c C.
111
  ///
112
  /// \param AllowOverwrite If \c true, this constant might overwrite (part of)
113
  ///        a constant that has already been added. This flag is only used to
114
  ///        detect bugs.
115
  bool add(llvm::Constant *C, CharUnits Offset, bool AllowOverwrite);
116
117
  /// Update or overwrite the bits starting at \p OffsetInBits with \p Bits.
118
  bool addBits(llvm::APInt Bits, uint64_t OffsetInBits, bool AllowOverwrite);
119
120
  /// Attempt to condense the value starting at \p Offset to a constant of type
121
  /// \p DesiredTy.
122
  void condense(CharUnits Offset, llvm::Type *DesiredTy);
123
124
  /// Produce a constant representing the entire accumulated value, ideally of
125
  /// the specified type. If \p AllowOversized, the constant might be larger
126
  /// than implied by \p DesiredTy (eg, if there is a flexible array member).
127
  /// Otherwise, the constant will be of exactly the same size as \p DesiredTy
128
  /// even if we can't represent it as that type.
129
5.46k
  llvm::Constant *build(llvm::Type *DesiredTy, bool AllowOversized) const {
130
5.46k
    return buildFrom(CGM, Elems, Offsets, CharUnits::Zero(), Size,
131
5.46k
                     NaturalLayout, DesiredTy, AllowOversized);
132
5.46k
  }
133
};
134
135
template<typename Container, typename Range = std::initializer_list<
136
                                 typename Container::value_type>>
137
274
static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
138
274
  assert(BeginOff <= EndOff && "invalid replacement range");
139
274
  llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
140
274
}
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<llvm::Constant*, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_0, llvm::Constant*> > >(llvm::SmallVector<llvm::Constant*, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_0, llvm::Constant*> >)
Line
Count
Source
137
34
static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
138
34
  assert(BeginOff <= EndOff && "invalid replacement range");
139
34
  llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
140
34
}
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<clang::CharUnits, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_1, clang::CharUnits> > >(llvm::SmallVector<clang::CharUnits, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_1, clang::CharUnits> >)
Line
Count
Source
137
2
static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
138
2
  assert(BeginOff <= EndOff && "invalid replacement range");
139
2
  llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
140
2
}
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<clang::CharUnits, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_2, clang::CharUnits> > >(llvm::SmallVector<clang::CharUnits, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_2, clang::CharUnits> >)
Line
Count
Source
137
32
static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
138
32
  assert(BeginOff <= EndOff && "invalid replacement range");
139
32
  llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
140
32
}
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<llvm::Constant*, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_3, llvm::Constant*> > >(llvm::SmallVector<llvm::Constant*, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_3, llvm::Constant*> >)
Line
Count
Source
137
15
static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
138
15
  assert(BeginOff <= EndOff && "invalid replacement range");
139
15
  llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
140
15
}
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<clang::CharUnits, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_4, clang::CharUnits> > >(llvm::SmallVector<clang::CharUnits, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_4, clang::CharUnits> >)
Line
Count
Source
137
15
static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
138
15
  assert(BeginOff <= EndOff && "invalid replacement range");
139
15
  llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
140
15
}
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<llvm::Constant*, 32u>, std::initializer_list<llvm::Constant*> >(llvm::SmallVector<llvm::Constant*, 32u>&, unsigned long, unsigned long, std::initializer_list<llvm::Constant*>)
Line
Count
Source
137
88
static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
138
88
  assert(BeginOff <= EndOff && "invalid replacement range");
139
88
  llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
140
88
}
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<clang::CharUnits, 32u>, std::initializer_list<clang::CharUnits> >(llvm::SmallVector<clang::CharUnits, 32u>&, unsigned long, unsigned long, std::initializer_list<clang::CharUnits>)
Line
Count
Source
137
88
static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
138
88
  assert(BeginOff <= EndOff && "invalid replacement range");
139
88
  llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
140
88
}
141
142
bool ConstantAggregateBuilder::add(llvm::Constant *C, CharUnits Offset,
143
10.7k
                          bool AllowOverwrite) {
144
10.7k
  // Common case: appending to a layout.
145
10.7k
  if (Offset >= Size) {
146
10.6k
    CharUnits Align = getAlignment(C);
147
10.6k
    CharUnits AlignedSize = Size.alignTo(Align);
148
10.6k
    if (AlignedSize > Offset || 
Offset.alignTo(Align) != Offset10.5k
)
149
104
      NaturalLayout = false;
150
10.5k
    else if (AlignedSize < Offset) {
151
51
      Elems.push_back(getPadding(Offset - Size));
152
51
      Offsets.push_back(Size);
153
51
    }
154
10.6k
    Elems.push_back(C);
155
10.6k
    Offsets.push_back(Offset);
156
10.6k
    Size = Offset + getSize(C);
157
10.6k
    return true;
158
10.6k
  }
159
70
160
70
  // Uncommon case: constant overlaps what we've already created.
161
70
  llvm::Optional<size_t> FirstElemToReplace = splitAt(Offset);
162
70
  if (!FirstElemToReplace)
163
0
    return false;
164
70
165
70
  CharUnits CSize = getSize(C);
166
70
  llvm::Optional<size_t> LastElemToReplace = splitAt(Offset + CSize);
167
70
  if (!LastElemToReplace)
168
0
    return false;
169
70
170
70
  assert((FirstElemToReplace == LastElemToReplace || AllowOverwrite) &&
171
70
         "unexpectedly overwriting field");
172
70
173
70
  replace(Elems, *FirstElemToReplace, *LastElemToReplace, {C});
174
70
  replace(Offsets, *FirstElemToReplace, *LastElemToReplace, {Offset});
175
70
  Size = std::max(Size, Offset + CSize);
176
70
  NaturalLayout = false;
177
70
  return true;
178
70
}
179
180
bool ConstantAggregateBuilder::addBits(llvm::APInt Bits, uint64_t OffsetInBits,
181
202
                              bool AllowOverwrite) {
182
202
  const ASTContext &Context = CGM.getContext();
183
202
  const uint64_t CharWidth = CGM.getContext().getCharWidth();
184
202
185
202
  // Offset of where we want the first bit to go within the bits of the
186
202
  // current char.
187
202
  unsigned OffsetWithinChar = OffsetInBits % CharWidth;
188
202
189
202
  // We split bit-fields up into individual bytes. Walk over the bytes and
190
202
  // update them.
191
202
  for (CharUnits OffsetInChars =
192
202
           Context.toCharUnitsFromBits(OffsetInBits - OffsetWithinChar);
193
352
       /**/; 
++OffsetInChars150
) {
194
352
    // Number of bits we want to fill in this char.
195
352
    unsigned WantedBits =
196
352
        std::min((uint64_t)Bits.getBitWidth(), CharWidth - OffsetWithinChar);
197
352
198
352
    // Get a char containing the bits we want in the right places. The other
199
352
    // bits have unspecified values.
200
352
    llvm::APInt BitsThisChar = Bits;
201
352
    if (BitsThisChar.getBitWidth() < CharWidth)
202
182
      BitsThisChar = BitsThisChar.zext(CharWidth);
203
352
    if (CGM.getDataLayout().isBigEndian()) {
204
0
      // Figure out how much to shift by. We may need to left-shift if we have
205
0
      // less than one byte of Bits left.
206
0
      int Shift = Bits.getBitWidth() - CharWidth + OffsetWithinChar;
207
0
      if (Shift > 0)
208
0
        BitsThisChar.lshrInPlace(Shift);
209
0
      else if (Shift < 0)
210
0
        BitsThisChar = BitsThisChar.shl(-Shift);
211
352
    } else {
212
352
      BitsThisChar = BitsThisChar.shl(OffsetWithinChar);
213
352
    }
214
352
    if (BitsThisChar.getBitWidth() > CharWidth)
215
147
      BitsThisChar = BitsThisChar.trunc(CharWidth);
216
352
217
352
    if (WantedBits == CharWidth) {
218
159
      // Got a full byte: just add it directly.
219
159
      add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
220
159
          OffsetInChars, AllowOverwrite);
221
193
    } else {
222
193
      // Partial byte: update the existing integer if there is one. If we
223
193
      // can't split out a 1-CharUnit range to update, then we can't add
224
193
      // these bits and fail the entire constant emission.
225
193
      llvm::Optional<size_t> FirstElemToUpdate = splitAt(OffsetInChars);
226
193
      if (!FirstElemToUpdate)
227
0
        return false;
228
193
      llvm::Optional<size_t> LastElemToUpdate =
229
193
          splitAt(OffsetInChars + CharUnits::One());
230
193
      if (!LastElemToUpdate)
231
0
        return false;
232
193
      assert(*LastElemToUpdate - *FirstElemToUpdate < 2 &&
233
193
             "should have at most one element covering one byte");
234
193
235
193
      // Figure out which bits we want and discard the rest.
236
193
      llvm::APInt UpdateMask(CharWidth, 0);
237
193
      if (CGM.getDataLayout().isBigEndian())
238
0
        UpdateMask.setBits(CharWidth - OffsetWithinChar - WantedBits,
239
0
                           CharWidth - OffsetWithinChar);
240
193
      else
241
193
        UpdateMask.setBits(OffsetWithinChar, OffsetWithinChar + WantedBits);
242
193
      BitsThisChar &= UpdateMask;
243
193
244
193
      if (*FirstElemToUpdate == *LastElemToUpdate ||
245
193
          
Elems[*FirstElemToUpdate]->isNullValue()55
||
246
193
          
isa<llvm::UndefValue>(Elems[*FirstElemToUpdate])36
) {
247
159
        // All existing bits are either zero or undef.
248
159
        add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
249
159
            OffsetInChars, /*AllowOverwrite*/ true);
250
159
      } else {
251
34
        llvm::Constant *&ToUpdate = Elems[*FirstElemToUpdate];
252
34
        // In order to perform a partial update, we need the existing bitwise
253
34
        // value, which we can only extract for a constant int.
254
34
        auto *CI = dyn_cast<llvm::ConstantInt>(ToUpdate);
255
34
        if (!CI)
256
0
          return false;
257
34
        // Because this is a 1-CharUnit range, the constant occupying it must
258
34
        // be exactly one CharUnit wide.
259
34
        assert(CI->getBitWidth() == CharWidth && "splitAt failed");
260
34
        assert((!(CI->getValue() & UpdateMask) || AllowOverwrite) &&
261
34
               "unexpectedly overwriting bitfield");
262
34
        BitsThisChar |= (CI->getValue() & ~UpdateMask);
263
34
        ToUpdate = llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar);
264
34
      }
265
193
    }
266
352
267
352
    // Stop if we've added all the bits.
268
352
    if (WantedBits == Bits.getBitWidth())
269
202
      break;
270
150
271
150
    // Remove the consumed bits from Bits.
272
150
    if (!CGM.getDataLayout().isBigEndian())
273
150
      Bits.lshrInPlace(WantedBits);
274
150
    Bits = Bits.trunc(Bits.getBitWidth() - WantedBits);
275
150
276
150
    // The remanining bits go at the start of the following bytes.
277
150
    OffsetWithinChar = 0;
278
150
  }
279
202
280
202
  return true;
281
202
}
282
283
/// Returns a position within Elems and Offsets such that all elements
284
/// before the returned index end before Pos and all elements at or after
285
/// the returned index begin at or after Pos. Splits elements as necessary
286
/// to ensure this. Returns None if we find something we can't split.
287
562
Optional<size_t> ConstantAggregateBuilder::splitAt(CharUnits Pos) {
288
562
  if (Pos >= Size)
289
374
    return Offsets.size();
290
188
291
241
  
while (188
true) {
292
241
    auto FirstAfterPos = llvm::upper_bound(Offsets, Pos);
293
241
    if (FirstAfterPos == Offsets.begin())
294
0
      return 0;
295
241
296
241
    // If we already have an element starting at Pos, we're done.
297
241
    size_t LastAtOrBeforePosIndex = FirstAfterPos - Offsets.begin() - 1;
298
241
    if (Offsets[LastAtOrBeforePosIndex] == Pos)
299
184
      return LastAtOrBeforePosIndex;
300
57
301
57
    // We found an element starting before Pos. Check for overlap.
302
57
    if (Offsets[LastAtOrBeforePosIndex] +
303
57
        getSize(Elems[LastAtOrBeforePosIndex]) <= Pos)
304
4
      return LastAtOrBeforePosIndex + 1;
305
53
306
53
    // Try to decompose it into smaller constants.
307
53
    if (!split(LastAtOrBeforePosIndex, Pos))
308
0
      return None;
309
53
  }
310
188
}
311
312
/// Split the constant at index Index, if possible. Return true if we did.
313
/// Hint indicates the location at which we'd like to split, but may be
314
/// ignored.
315
53
bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
316
53
  NaturalLayout = false;
317
53
  llvm::Constant *C = Elems[Index];
318
53
  CharUnits Offset = Offsets[Index];
319
53
320
53
  if (auto *CA = dyn_cast<llvm::ConstantAggregate>(C)) {
321
34
    replace(Elems, Index, Index + 1,
322
34
            llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
323
75
                            [&](unsigned Op) { return CA->getOperand(Op); }));
324
34
    if (auto *Seq = dyn_cast<llvm::SequentialType>(CA->getType())) {
325
2
      // Array or vector.
326
2
      CharUnits ElemSize = getSize(Seq->getElementType());
327
2
      replace(
328
2
          Offsets, Index, Index + 1,
329
2
          llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
330
6
                          [&](unsigned Op) { return Offset + Op * ElemSize; }));
331
32
    } else {
332
32
      // Must be a struct.
333
32
      auto *ST = cast<llvm::StructType>(CA->getType());
334
32
      const llvm::StructLayout *Layout =
335
32
          CGM.getDataLayout().getStructLayout(ST);
336
32
      replace(Offsets, Index, Index + 1,
337
32
              llvm::map_range(
338
69
                  llvm::seq(0u, CA->getNumOperands()), [&](unsigned Op) {
339
69
                    return Offset + CharUnits::fromQuantity(
340
69
                                        Layout->getElementOffset(Op));
341
69
                  }));
342
32
    }
343
34
    return true;
344
34
  }
345
19
346
19
  if (auto *CDS = dyn_cast<llvm::ConstantDataSequential>(C)) {
347
15
    // FIXME: If possible, split into two ConstantDataSequentials at Hint.
348
15
    CharUnits ElemSize = getSize(CDS->getElementType());
349
15
    replace(Elems, Index, Index + 1,
350
15
            llvm::map_range(llvm::seq(0u, CDS->getNumElements()),
351
87
                            [&](unsigned Elem) {
352
87
                              return CDS->getElementAsConstant(Elem);
353
87
                            }));
354
15
    replace(Offsets, Index, Index + 1,
355
15
            llvm::map_range(
356
15
                llvm::seq(0u, CDS->getNumElements()),
357
87
                [&](unsigned Elem) { return Offset + Elem * ElemSize; }));
358
15
    return true;
359
15
  }
360
4
361
4
  if (isa<llvm::ConstantAggregateZero>(C)) {
362
4
    CharUnits ElemSize = getSize(C);
363
4
    assert(Hint > Offset && Hint < Offset + ElemSize && "nothing to split");
364
4
    replace(Elems, Index, Index + 1,
365
4
            {getZeroes(Hint - Offset), getZeroes(Offset + ElemSize - Hint)});
366
4
    replace(Offsets, Index, Index + 1, {Offset, Hint});
367
4
    return true;
368
4
  }
369
0
370
0
  if (isa<llvm::UndefValue>(C)) {
371
0
    replace(Elems, Index, Index + 1, {});
372
0
    replace(Offsets, Index, Index + 1, {});
373
0
    return true;
374
0
  }
375
0
376
0
  // FIXME: We could split a ConstantInt if the need ever arose.
377
0
  // We don't need to do this to handle bit-fields because we always eagerly
378
0
  // split them into 1-byte chunks.
379
0
380
0
  return false;
381
0
}
382
383
static llvm::Constant *
384
EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
385
                  llvm::Type *CommonElementType, unsigned ArrayBound,
386
                  SmallVectorImpl<llvm::Constant *> &Elements,
387
                  llvm::Constant *Filler);
388
389
llvm::Constant *ConstantAggregateBuilder::buildFrom(
390
    CodeGenModule &CGM, ArrayRef<llvm::Constant *> Elems,
391
    ArrayRef<CharUnits> Offsets, CharUnits StartOffset, CharUnits Size,
392
5.48k
    bool NaturalLayout, llvm::Type *DesiredTy, bool AllowOversized) {
393
5.48k
  ConstantAggregateBuilderUtils Utils(CGM);
394
5.48k
395
5.48k
  if (Elems.empty())
396
101
    return llvm::UndefValue::get(DesiredTy);
397
5.38k
398
5.38k
  auto Offset = [&](size_t I) 
{ return Offsets[I] - StartOffset; }465
;
399
5.38k
400
5.38k
  // If we want an array type, see if all the elements are the same type and
401
5.38k
  // appropriately spaced.
402
5.38k
  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(DesiredTy)) {
403
21
    assert(!AllowOversized && "oversized array emission not supported");
404
21
405
21
    bool CanEmitArray = true;
406
21
    llvm::Type *CommonType = Elems[0]->getType();
407
21
    llvm::Constant *Filler = llvm::Constant::getNullValue(CommonType);
408
21
    CharUnits ElemSize = Utils.getSize(ATy->getElementType());
409
21
    SmallVector<llvm::Constant*, 32> ArrayElements;
410
128
    for (size_t I = 0; I != Elems.size(); 
++I107
) {
411
109
      // Skip zeroes; we'll use a zero value as our array filler.
412
109
      if (Elems[I]->isNullValue())
413
35
        continue;
414
74
415
74
      // All remaining elements must be the same type.
416
74
      if (Elems[I]->getType() != CommonType ||
417
74
          
Offset(I) % ElemSize != 072
) {
418
2
        CanEmitArray = false;
419
2
        break;
420
2
      }
421
72
      ArrayElements.resize(Offset(I) / ElemSize + 1, Filler);
422
72
      ArrayElements.back() = Elems[I];
423
72
    }
424
21
425
21
    if (CanEmitArray) {
426
19
      return EmitArrayConstant(CGM, ATy, CommonType, ATy->getNumElements(),
427
19
                               ArrayElements, Filler);
428
19
    }
429
5.36k
430
5.36k
    // Can't emit as an array, carry on to emit as a struct.
431
5.36k
  }
432
5.36k
433
5.36k
  CharUnits DesiredSize = Utils.getSize(DesiredTy);
434
5.36k
  CharUnits Align = CharUnits::One();
435
5.36k
  for (llvm::Constant *C : Elems)
436
10.7k
    Align = std::max(Align, Utils.getAlignment(C));
437
5.36k
  CharUnits AlignedSize = Size.alignTo(Align);
438
5.36k
439
5.36k
  bool Packed = false;
440
5.36k
  ArrayRef<llvm::Constant*> UnpackedElems = Elems;
441
5.36k
  llvm::SmallVector<llvm::Constant*, 32> UnpackedElemStorage;
442
5.36k
  if ((DesiredSize < AlignedSize && 
!AllowOversized116
) ||
443
5.36k
      
DesiredSize.alignTo(Align) != DesiredSize5.25k
) {
444
113
    // The natural layout would be the wrong size; force use of a packed layout.
445
113
    NaturalLayout = false;
446
113
    Packed = true;
447
5.24k
  } else if (DesiredSize > AlignedSize) {
448
351
    // The constant would be too small. Add padding to fix it.
449
351
    UnpackedElemStorage.assign(Elems.begin(), Elems.end());
450
351
    UnpackedElemStorage.push_back(Utils.getPadding(DesiredSize - Size));
451
351
    UnpackedElems = UnpackedElemStorage;
452
351
  }
453
5.36k
454
5.36k
  // If we don't have a natural layout, insert padding as necessary.
455
5.36k
  // As we go, double-check to see if we can actually just emit Elems
456
5.36k
  // as a non-packed struct and do so opportunistically if possible.
457
5.36k
  llvm::SmallVector<llvm::Constant*, 32> PackedElems;
458
5.36k
  if (!NaturalLayout) {
459
156
    CharUnits SizeSoFar = CharUnits::Zero();
460
477
    for (size_t I = 0; I != Elems.size(); 
++I321
) {
461
321
      CharUnits Align = Utils.getAlignment(Elems[I]);
462
321
      CharUnits NaturalOffset = SizeSoFar.alignTo(Align);
463
321
      CharUnits DesiredOffset = Offset(I);
464
321
      assert(DesiredOffset >= SizeSoFar && "elements out of order");
465
321
466
321
      if (DesiredOffset != NaturalOffset)
467
106
        Packed = true;
468
321
      if (DesiredOffset != SizeSoFar)
469
3
        PackedElems.push_back(Utils.getPadding(DesiredOffset - SizeSoFar));
470
321
      PackedElems.push_back(Elems[I]);
471
321
      SizeSoFar = DesiredOffset + Utils.getSize(Elems[I]);
472
321
    }
473
156
    // If we're using the packed layout, pad it out to the desired size if
474
156
    // necessary.
475
156
    if (Packed) {
476
116
      assert((SizeSoFar <= DesiredSize || AllowOversized) &&
477
116
             "requested size is too small for contents");
478
116
      if (SizeSoFar < DesiredSize)
479
9
        PackedElems.push_back(Utils.getPadding(DesiredSize - SizeSoFar));
480
116
    }
481
156
  }
482
5.36k
483
5.36k
  llvm::StructType *STy = llvm::ConstantStruct::getTypeForElements(
484
5.36k
      CGM.getLLVMContext(), Packed ? 
PackedElems116
:
UnpackedElems5.24k
, Packed);
485
5.36k
486
5.36k
  // Pick the type to use.  If the type is layout identical to the desired
487
5.36k
  // type then use it, otherwise use whatever the builder produced for us.
488
5.36k
  if (llvm::StructType *DesiredSTy = dyn_cast<llvm::StructType>(DesiredTy)) {
489
5.36k
    if (DesiredSTy->isLayoutIdentical(STy))
490
4.60k
      STy = DesiredSTy;
491
5.36k
  }
492
5.36k
493
5.36k
  return llvm::ConstantStruct::get(STy, Packed ? 
PackedElems116
:
UnpackedElems5.24k
);
494
5.36k
}
495
496
void ConstantAggregateBuilder::condense(CharUnits Offset,
497
18
                                        llvm::Type *DesiredTy) {
498
18
  CharUnits Size = getSize(DesiredTy);
499
18
500
18
  llvm::Optional<size_t> FirstElemToReplace = splitAt(Offset);
501
18
  if (!FirstElemToReplace)
502
0
    return;
503
18
  size_t First = *FirstElemToReplace;
504
18
505
18
  llvm::Optional<size_t> LastElemToReplace = splitAt(Offset + Size);
506
18
  if (!LastElemToReplace)
507
0
    return;
508
18
  size_t Last = *LastElemToReplace;
509
18
510
18
  size_t Length = Last - First;
511
18
  if (Length == 0)
512
0
    return;
513
18
514
18
  if (Length == 1 && 
Offsets[First] == Offset4
&&
515
18
      
getSize(Elems[First]) == Size4
) {
516
4
    // Re-wrap single element structs if necessary. Otherwise, leave any single
517
4
    // element constant of the right size alone even if it has the wrong type.
518
4
    auto *STy = dyn_cast<llvm::StructType>(DesiredTy);
519
4
    if (STy && STy->getNumElements() == 1 &&
520
4
        STy->getElementType(0) == Elems[First]->getType())
521
2
      Elems[First] = llvm::ConstantStruct::get(STy, Elems[First]);
522
4
    return;
523
4
  }
524
14
525
14
  llvm::Constant *Replacement = buildFrom(
526
14
      CGM, makeArrayRef(Elems).slice(First, Length),
527
14
      makeArrayRef(Offsets).slice(First, Length), Offset, getSize(DesiredTy),
528
14
      /*known to have natural layout=*/false, DesiredTy, false);
529
14
  replace(Elems, First, Last, {Replacement});
530
14
  replace(Offsets, First, Last, {Offset});
531
14
}
532
533
//===----------------------------------------------------------------------===//
534
//                            ConstStructBuilder
535
//===----------------------------------------------------------------------===//
536
537
class ConstStructBuilder {
538
  CodeGenModule &CGM;
539
  ConstantEmitter &Emitter;
540
  ConstantAggregateBuilder &Builder;
541
  CharUnits StartOffset;
542
543
public:
544
  static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
545
                                     InitListExpr *ILE, QualType StructTy);
546
  static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
547
                                     const APValue &Value, QualType ValTy);
548
  static bool UpdateStruct(ConstantEmitter &Emitter,
549
                           ConstantAggregateBuilder &Const, CharUnits Offset,
550
                           InitListExpr *Updater);
551
552
private:
553
  ConstStructBuilder(ConstantEmitter &Emitter,
554
                     ConstantAggregateBuilder &Builder, CharUnits StartOffset)
555
      : CGM(Emitter.CGM), Emitter(Emitter), Builder(Builder),
556
5.49k
        StartOffset(StartOffset) {}
557
558
  bool AppendField(const FieldDecl *Field, uint64_t FieldOffset,
559
                   llvm::Constant *InitExpr, bool AllowOverwrite = false);
560
561
  bool AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst,
562
                   bool AllowOverwrite = false);
563
564
  bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
565
                      llvm::ConstantInt *InitExpr, bool AllowOverwrite = false);
566
567
  bool Build(InitListExpr *ILE, bool AllowOverwrite);
568
  bool Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
569
             const CXXRecordDecl *VTableClass, CharUnits BaseOffset);
570
  llvm::Constant *Finalize(QualType Ty);
571
};
572
573
bool ConstStructBuilder::AppendField(
574
    const FieldDecl *Field, uint64_t FieldOffset, llvm::Constant *InitCst,
575
10.1k
    bool AllowOverwrite) {
576
10.1k
  const ASTContext &Context = CGM.getContext();
577
10.1k
578
10.1k
  CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
579
10.1k
580
10.1k
  return AppendBytes(FieldOffsetInChars, InitCst, AllowOverwrite);
581
10.1k
}
582
583
bool ConstStructBuilder::AppendBytes(CharUnits FieldOffsetInChars,
584
                                     llvm::Constant *InitCst,
585
10.3k
                                     bool AllowOverwrite) {
586
10.3k
  return Builder.add(InitCst, StartOffset + FieldOffsetInChars, AllowOverwrite);
587
10.3k
}
588
589
bool ConstStructBuilder::AppendBitField(
590
    const FieldDecl *Field, uint64_t FieldOffset, llvm::ConstantInt *CI,
591
202
    bool AllowOverwrite) {
592
202
  uint64_t FieldSize = Field->getBitWidthValue(CGM.getContext());
593
202
  llvm::APInt FieldValue = CI->getValue();
594
202
595
202
  // Promote the size of FieldValue if necessary
596
202
  // FIXME: This should never occur, but currently it can because initializer
597
202
  // constants are cast to bool, and because clang is not enforcing bitfield
598
202
  // width limits.
599
202
  if (FieldSize > FieldValue.getBitWidth())
600
0
    FieldValue = FieldValue.zext(FieldSize);
601
202
602
202
  // Truncate the size of FieldValue to the bit field size.
603
202
  if (FieldSize < FieldValue.getBitWidth())
604
195
    FieldValue = FieldValue.trunc(FieldSize);
605
202
606
202
  return Builder.addBits(FieldValue,
607
202
                         CGM.getContext().toBits(StartOffset) + FieldOffset,
608
202
                         AllowOverwrite);
609
202
}
610
611
static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter,
612
                                      ConstantAggregateBuilder &Const,
613
                                      CharUnits Offset, QualType Type,
614
46
                                      InitListExpr *Updater) {
615
46
  if (Type->isRecordType())
616
25
    return ConstStructBuilder::UpdateStruct(Emitter, Const, Offset, Updater);
617
21
618
21
  auto CAT = Emitter.CGM.getContext().getAsConstantArrayType(Type);
619
21
  if (!CAT)
620
0
    return false;
621
21
  QualType ElemType = CAT->getElementType();
622
21
  CharUnits ElemSize = Emitter.CGM.getContext().getTypeSizeInChars(ElemType);
623
21
  llvm::Type *ElemTy = Emitter.CGM.getTypes().ConvertTypeForMem(ElemType);
624
21
625
21
  llvm::Constant *FillC = nullptr;
626
21
  if (Expr *Filler = Updater->getArrayFiller()) {
627
21
    if (!isa<NoInitExpr>(Filler)) {
628
3
      FillC = Emitter.tryEmitAbstractForMemory(Filler, ElemType);
629
3
      if (!FillC)
630
0
        return false;
631
21
    }
632
21
  }
633
21
634
21
  unsigned NumElementsToUpdate =
635
21
      FillC ? 
CAT->getSize().getZExtValue()3
:
Updater->getNumInits()18
;
636
111
  for (unsigned I = 0; I != NumElementsToUpdate; 
++I, Offset += ElemSize90
) {
637
90
    Expr *Init = nullptr;
638
90
    if (I < Updater->getNumInits())
639
78
      Init = Updater->getInit(I);
640
90
641
90
    if (!Init && 
FillC12
) {
642
12
      if (!Const.add(FillC, Offset, true))
643
0
        return false;
644
78
    } else if (!Init || isa<NoInitExpr>(Init)) {
645
52
      continue;
646
52
    } else 
if (InitListExpr *26
ChildILE26
= dyn_cast<InitListExpr>(Init)) {
647
4
      if (!EmitDesignatedInitUpdater(Emitter, Const, Offset, ElemType,
648
4
                                     ChildILE))
649
0
        return false;
650
4
      // Attempt to reduce the array element to a single constant if necessary.
651
4
      Const.condense(Offset, ElemTy);
652
22
    } else {
653
22
      llvm::Constant *Val = Emitter.tryEmitPrivateForMemory(Init, ElemType);
654
22
      if (!Const.add(Val, Offset, true))
655
0
        return false;
656
22
    }
657
90
  }
658
21
659
21
  return true;
660
21
}
661
662
2.42k
bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) {
663
2.42k
  RecordDecl *RD = ILE->getType()->castAs<RecordType>()->getDecl();
664
2.42k
  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
665
2.42k
666
2.42k
  unsigned FieldNo = -1;
667
2.42k
  unsigned ElementNo = 0;
668
2.42k
669
2.42k
  // Bail out if we have base classes. We could support these, but they only
670
2.42k
  // arise in C++1z where we will have already constant folded most interesting
671
2.42k
  // cases. FIXME: There are still a few more cases we can handle this way.
672
2.42k
  if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
673
134
    if (CXXRD->getNumBases())
674
5
      return false;
675
2.42k
676
5.39k
  
for (FieldDecl *Field : RD->fields())2.42k
{
677
5.39k
    ++FieldNo;
678
5.39k
679
5.39k
    // If this is a union, skip all the fields that aren't being initialized.
680
5.39k
    if (RD->isUnion() &&
681
5.39k
        
!declaresSameEntity(ILE->getInitializedFieldInUnion(), Field)254
)
682
125
      continue;
683
5.27k
684
5.27k
    // Don't emit anonymous bitfields or zero-sized fields.
685
5.27k
    if (Field->isUnnamedBitfield() || 
Field->isZeroSize(CGM.getContext())5.24k
)
686
24
      continue;
687
5.24k
688
5.24k
    // Get the initializer.  A struct can include fields without initializers,
689
5.24k
    // we just use explicit null values for them.
690
5.24k
    Expr *Init = nullptr;
691
5.24k
    if (ElementNo < ILE->getNumInits())
692
5.20k
      Init = ILE->getInit(ElementNo++);
693
5.24k
    if (Init && 
isa<NoInitExpr>(Init)5.20k
)
694
15
      continue;
695
5.23k
696
5.23k
    // When emitting a DesignatedInitUpdateExpr, a nested InitListExpr
697
5.23k
    // represents additional overwriting of our current constant value, and not
698
5.23k
    // a new constant to emit independently.
699
5.23k
    if (AllowOverwrite &&
700
5.23k
        
(27
Field->getType()->isArrayType()27
||
Field->getType()->isRecordType()13
)) {
701
15
      if (auto *SubILE = dyn_cast<InitListExpr>(Init)) {
702
14
        CharUnits Offset = CGM.getContext().toCharUnitsFromBits(
703
14
            Layout.getFieldOffset(FieldNo));
704
14
        if (!EmitDesignatedInitUpdater(Emitter, Builder, StartOffset + Offset,
705
14
                                       Field->getType(), SubILE))
706
0
          return false;
707
14
        // If we split apart the field's value, try to collapse it down to a
708
14
        // single value now.
709
14
        Builder.condense(StartOffset + Offset,
710
14
                         CGM.getTypes().ConvertTypeForMem(Field->getType()));
711
14
        continue;
712
14
      }
713
15
    }
714
5.22k
715
5.22k
    llvm::Constant *EltInit =
716
5.22k
        Init ? 
Emitter.tryEmitPrivateForMemory(Init, Field->getType())5.17k
717
5.22k
             : 
Emitter.emitNullForMemory(Field->getType())46
;
718
5.22k
    if (!EltInit)
719
23
      return false;
720
5.19k
721
5.19k
    if (!Field->isBitField()) {
722
5.07k
      // Handle non-bitfield members.
723
5.07k
      if (!AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit,
724
5.07k
                       AllowOverwrite))
725
0
        return false;
726
5.07k
      // After emitting a non-empty field with [[no_unique_address]], we may
727
5.07k
      // need to overwrite its tail padding.
728
5.07k
      if (Field->hasAttr<NoUniqueAddressAttr>())
729
0
        AllowOverwrite = true;
730
5.07k
    } else {
731
123
      // Otherwise we have a bitfield.
732
123
      if (auto *CI = dyn_cast<llvm::ConstantInt>(EltInit)) {
733
120
        if (!AppendBitField(Field, Layout.getFieldOffset(FieldNo), CI,
734
120
                            AllowOverwrite))
735
0
          return false;
736
3
      } else {
737
3
        // We are trying to initialize a bitfield with a non-trivial constant,
738
3
        // this must require run-time code.
739
3
        return false;
740
3
      }
741
123
    }
742
5.19k
  }
743
2.42k
744
2.42k
  
return true2.39k
;
745
2.42k
}
746
747
namespace {
748
struct BaseInfo {
749
  BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
750
382
    : Decl(Decl), Offset(Offset), Index(Index) {
751
382
  }
752
753
  const CXXRecordDecl *Decl;
754
  CharUnits Offset;
755
  unsigned Index;
756
757
62
  bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
758
};
759
}
760
761
bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
762
                               bool IsPrimaryBase,
763
                               const CXXRecordDecl *VTableClass,
764
3.45k
                               CharUnits Offset) {
765
3.45k
  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
766
3.45k
767
3.45k
  if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
768
3.45k
    // Add a vtable pointer, if we need one and it hasn't already been added.
769
3.45k
    if (CD->isDynamicClass() && 
!IsPrimaryBase278
) {
770
202
      llvm::Constant *VTableAddressPoint =
771
202
          CGM.getCXXABI().getVTableAddressPointForConstExpr(
772
202
              BaseSubobject(CD, Offset), VTableClass);
773
202
      if (!AppendBytes(Offset, VTableAddressPoint))
774
0
        return false;
775
3.45k
    }
776
3.45k
777
3.45k
    // Accumulate and sort bases, in order to visit them in address order, which
778
3.45k
    // may not be the same as declaration order.
779
3.45k
    SmallVector<BaseInfo, 8> Bases;
780
3.45k
    Bases.reserve(CD->getNumBases());
781
3.45k
    unsigned BaseNo = 0;
782
3.45k
    for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(),
783
3.83k
         BaseEnd = CD->bases_end(); Base != BaseEnd; 
++Base, ++BaseNo382
) {
784
382
      assert(!Base->isVirtual() && "should not have virtual bases here");
785
382
      const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
786
382
      CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
787
382
      Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
788
382
    }
789
3.45k
    llvm::stable_sort(Bases);
790
3.45k
791
3.83k
    for (unsigned I = 0, N = Bases.size(); I != N; 
++I382
) {
792
382
      BaseInfo &Base = Bases[I];
793
382
794
382
      bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
795
382
      Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
796
382
            VTableClass, Offset + Base.Offset);
797
382
    }
798
3.45k
  }
799
3.45k
800
3.45k
  unsigned FieldNo = 0;
801
3.45k
  uint64_t OffsetBits = CGM.getContext().toBits(Offset);
802
3.45k
803
3.45k
  bool AllowOverwrite = false;
804
3.45k
  for (RecordDecl::field_iterator Field = RD->field_begin(),
805
9.07k
       FieldEnd = RD->field_end(); Field != FieldEnd; 
++Field, ++FieldNo5.62k
) {
806
5.62k
    // If this is a union, skip all the fields that aren't being initialized.
807
5.62k
    if (RD->isUnion() && 
!declaresSameEntity(Val.getUnionField(), *Field)863
)
808
429
      continue;
809
5.19k
810
5.19k
    // Don't emit anonymous bitfields or zero-sized fields.
811
5.19k
    if (Field->isUnnamedBitfield() || 
Field->isZeroSize(CGM.getContext())5.17k
)
812
43
      continue;
813
5.15k
814
5.15k
    // Emit the value of the initializer.
815
5.15k
    const APValue &FieldValue =
816
5.15k
      RD->isUnion() ? 
Val.getUnionValue()434
:
Val.getStructField(FieldNo)4.71k
;
817
5.15k
    llvm::Constant *EltInit =
818
5.15k
      Emitter.tryEmitPrivateForMemory(FieldValue, Field->getType());
819
5.15k
    if (!EltInit)
820
0
      return false;
821
5.15k
822
5.15k
    if (!Field->isBitField()) {
823
5.06k
      // Handle non-bitfield members.
824
5.06k
      if (!AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
825
5.06k
                       EltInit, AllowOverwrite))
826
0
        return false;
827
5.06k
      // After emitting a non-empty field with [[no_unique_address]], we may
828
5.06k
      // need to overwrite its tail padding.
829
5.06k
      if (Field->hasAttr<NoUniqueAddressAttr>())
830
6
        AllowOverwrite = true;
831
5.06k
    } else {
832
82
      // Otherwise we have a bitfield.
833
82
      if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
834
82
                          cast<llvm::ConstantInt>(EltInit), AllowOverwrite))
835
0
        return false;
836
82
    }
837
5.15k
  }
838
3.45k
839
3.45k
  return true;
840
3.45k
}
841
842
5.44k
llvm::Constant *ConstStructBuilder::Finalize(QualType Type) {
843
5.44k
  RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
844
5.44k
  llvm::Type *ValTy = CGM.getTypes().ConvertType(Type);
845
5.44k
  return Builder.build(ValTy, RD->hasFlexibleArrayMember());
846
5.44k
}
847
848
llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
849
                                                InitListExpr *ILE,
850
2.40k
                                                QualType ValTy) {
851
2.40k
  ConstantAggregateBuilder Const(Emitter.CGM);
852
2.40k
  ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero());
853
2.40k
854
2.40k
  if (!Builder.Build(ILE, /*AllowOverwrite*/false))
855
31
    return nullptr;
856
2.36k
857
2.36k
  return Builder.Finalize(ValTy);
858
2.36k
}
859
860
llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
861
                                                const APValue &Val,
862
3.07k
                                                QualType ValTy) {
863
3.07k
  ConstantAggregateBuilder Const(Emitter.CGM);
864
3.07k
  ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero());
865
3.07k
866
3.07k
  const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl();
867
3.07k
  const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
868
3.07k
  if (!Builder.Build(Val, RD, false, CD, CharUnits::Zero()))
869
0
    return nullptr;
870
3.07k
871
3.07k
  return Builder.Finalize(ValTy);
872
3.07k
}
873
874
bool ConstStructBuilder::UpdateStruct(ConstantEmitter &Emitter,
875
                                      ConstantAggregateBuilder &Const,
876
25
                                      CharUnits Offset, InitListExpr *Updater) {
877
25
  return ConstStructBuilder(Emitter, Const, Offset)
878
25
      .Build(Updater, /*AllowOverwrite*/ true);
879
25
}
880
881
//===----------------------------------------------------------------------===//
882
//                             ConstExprEmitter
883
//===----------------------------------------------------------------------===//
884
885
static ConstantAddress tryEmitGlobalCompoundLiteral(CodeGenModule &CGM,
886
                                                    CodeGenFunction *CGF,
887
17
                                              const CompoundLiteralExpr *E) {
888
17
  CharUnits Align = CGM.getContext().getTypeAlignInChars(E->getType());
889
17
  if (llvm::GlobalVariable *Addr =
890
2
          CGM.getAddrOfConstantCompoundLiteralIfEmitted(E))
891
2
    return ConstantAddress(Addr, Align);
892
15
893
15
  LangAS addressSpace = E->getType().getAddressSpace();
894
15
895
15
  ConstantEmitter emitter(CGM, CGF);
896
15
  llvm::Constant *C = emitter.tryEmitForInitializer(E->getInitializer(),
897
15
                                                    addressSpace, E->getType());
898
15
  if (!C) {
899
0
    assert(!E->isFileScope() &&
900
0
           "file-scope compound literal did not have constant initializer!");
901
0
    return ConstantAddress::invalid();
902
0
  }
903
15
904
15
  auto GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
905
15
                                     CGM.isTypeConstant(E->getType(), true),
906
15
                                     llvm::GlobalValue::InternalLinkage,
907
15
                                     C, ".compoundliteral", nullptr,
908
15
                                     llvm::GlobalVariable::NotThreadLocal,
909
15
                    CGM.getContext().getTargetAddressSpace(addressSpace));
910
15
  emitter.finalize(GV);
911
15
  GV->setAlignment(Align.getAsAlign());
912
15
  CGM.setAddrOfConstantCompoundLiteral(E, GV);
913
15
  return ConstantAddress(GV, Align);
914
15
}
915
916
static llvm::Constant *
917
EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
918
                  llvm::Type *CommonElementType, unsigned ArrayBound,
919
                  SmallVectorImpl<llvm::Constant *> &Elements,
920
3.59k
                  llvm::Constant *Filler) {
921
3.59k
  // Figure out how long the initial prefix of non-zero elements is.
922
3.59k
  unsigned NonzeroLength = ArrayBound;
923
3.59k
  if (Elements.size() < NonzeroLength && 
Filler->isNullValue()672
)
924
667
    NonzeroLength = Elements.size();
925
3.59k
  if (NonzeroLength == Elements.size()) {
926
4.06k
    while (NonzeroLength > 0 && 
Elements[NonzeroLength - 1]->isNullValue()3.48k
)
927
480
      --NonzeroLength;
928
3.58k
  }
929
3.59k
930
3.59k
  if (NonzeroLength == 0)
931
579
    return llvm::ConstantAggregateZero::get(DesiredType);
932
3.01k
933
3.01k
  // Add a zeroinitializer array filler if we have lots of trailing zeroes.
934
3.01k
  unsigned TrailingZeroes = ArrayBound - NonzeroLength;
935
3.01k
  if (TrailingZeroes >= 8) {
936
41
    assert(Elements.size() >= NonzeroLength &&
937
41
           "missing initializer for non-zero element");
938
41
939
41
    // If all the elements had the same type up to the trailing zeroes, emit a
940
41
    // struct of two arrays (the nonzero data and the zeroinitializer).
941
41
    if (CommonElementType && 
NonzeroLength >= 840
) {
942
13
      llvm::Constant *Initial = llvm::ConstantArray::get(
943
13
          llvm::ArrayType::get(CommonElementType, NonzeroLength),
944
13
          makeArrayRef(Elements).take_front(NonzeroLength));
945
13
      Elements.resize(2);
946
13
      Elements[0] = Initial;
947
28
    } else {
948
28
      Elements.resize(NonzeroLength + 1);
949
28
    }
950
41
951
41
    auto *FillerType =
952
41
        CommonElementType ? 
CommonElementType40
:
DesiredType->getElementType()1
;
953
41
    FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes);
954
41
    Elements.back() = llvm::ConstantAggregateZero::get(FillerType);
955
41
    CommonElementType = nullptr;
956
2.97k
  } else if (Elements.size() != ArrayBound) {
957
134
    // Otherwise pad to the right size with the filler if necessary.
958
134
    Elements.resize(ArrayBound, Filler);
959
134
    if (Filler->getType() != CommonElementType)
960
5
      CommonElementType = nullptr;
961
134
  }
962
3.01k
963
3.01k
  // If all elements have the same type, just emit an array constant.
964
3.01k
  if (CommonElementType)
965
2.96k
    return llvm::ConstantArray::get(
966
2.96k
        llvm::ArrayType::get(CommonElementType, ArrayBound), Elements);
967
51
968
51
  // We have mixed types. Use a packed struct.
969
51
  llvm::SmallVector<llvm::Type *, 16> Types;
970
51
  Types.reserve(Elements.size());
971
51
  for (llvm::Constant *Elt : Elements)
972
161
    Types.push_back(Elt->getType());
973
51
  llvm::StructType *SType =
974
51
      llvm::StructType::get(CGM.getLLVMContext(), Types, true);
975
51
  return llvm::ConstantStruct::get(SType, Elements);
976
51
}
977
978
// This class only needs to handle arrays, structs and unions. Outside C++11
979
// mode, we don't currently constant fold those types.  All other types are
980
// handled by constant folding.
981
//
982
// Constant folding is currently missing support for a few features supported
983
// here: CK_ToUnion, CK_ReinterpretMemberPointer, and DesignatedInitUpdateExpr.
984
class ConstExprEmitter :
985
  public StmtVisitor<ConstExprEmitter, llvm::Constant*, QualType> {
986
  CodeGenModule &CGM;
987
  ConstantEmitter &Emitter;
988
  llvm::LLVMContext &VMContext;
989
public:
990
  ConstExprEmitter(ConstantEmitter &emitter)
991
20.5k
    : CGM(emitter.CGM), Emitter(emitter), VMContext(CGM.getLLVMContext()) {
992
20.5k
  }
993
994
  //===--------------------------------------------------------------------===//
995
  //                            Visitor Methods
996
  //===--------------------------------------------------------------------===//
997
998
13.3k
  llvm::Constant *VisitStmt(Stmt *S, QualType T) {
999
13.3k
    return nullptr;
1000
13.3k
  }
1001
1002
16
  llvm::Constant *VisitConstantExpr(ConstantExpr *CE, QualType T) {
1003
16
    return Visit(CE->getSubExpr(), T);
1004
16
  }
1005
1006
125
  llvm::Constant *VisitParenExpr(ParenExpr *PE, QualType T) {
1007
125
    return Visit(PE->getSubExpr(), T);
1008
125
  }
1009
1010
  llvm::Constant *
1011
  VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE,
1012
0
                                    QualType T) {
1013
0
    return Visit(PE->getReplacement(), T);
1014
0
  }
1015
1016
  llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE,
1017
1
                                            QualType T) {
1018
1
    return Visit(GE->getResultExpr(), T);
1019
1
  }
1020
1021
1
  llvm::Constant *VisitChooseExpr(ChooseExpr *CE, QualType T) {
1022
1
    return Visit(CE->getChosenSubExpr(), T);
1023
1
  }
1024
1025
35
  llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E, QualType T) {
1026
35
    return Visit(E->getInitializer(), T);
1027
35
  }
1028
1029
5.01k
  llvm::Constant *VisitCastExpr(CastExpr *E, QualType destType) {
1030
5.01k
    if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
1031
643
      CGM.EmitExplicitCastExprType(ECE, Emitter.CGF);
1032
5.01k
    Expr *subExpr = E->getSubExpr();
1033
5.01k
1034
5.01k
    switch (E->getCastKind()) {
1035
4
    case CK_ToUnion: {
1036
4
      // GCC cast to union extension
1037
4
      assert(E->getType()->isUnionType() &&
1038
4
             "Destination type is not union type!");
1039
4
1040
4
      auto field = E->getTargetUnionField();
1041
4
1042
4
      auto C = Emitter.tryEmitPrivateForMemory(subExpr, field->getType());
1043
4
      if (!C) 
return nullptr0
;
1044
4
1045
4
      auto destTy = ConvertType(destType);
1046
4
      if (C->getType() == destTy) 
return C0
;
1047
4
1048
4
      // Build a struct with the union sub-element as the first member,
1049
4
      // and padded to the appropriate size.
1050
4
      SmallVector<llvm::Constant*, 2> Elts;
1051
4
      SmallVector<llvm::Type*, 2> Types;
1052
4
      Elts.push_back(C);
1053
4
      Types.push_back(C->getType());
1054
4
      unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
1055
4
      unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destTy);
1056
4
1057
4
      assert(CurSize <= TotalSize && "Union size mismatch!");
1058
4
      if (unsigned NumPadBytes = TotalSize - CurSize) {
1059
2
        llvm::Type *Ty = CGM.Int8Ty;
1060
2
        if (NumPadBytes > 1)
1061
2
          Ty = llvm::ArrayType::get(Ty, NumPadBytes);
1062
2
1063
2
        Elts.push_back(llvm::UndefValue::get(Ty));
1064
2
        Types.push_back(Ty);
1065
2
      }
1066
4
1067
4
      llvm::StructType *STy = llvm::StructType::get(VMContext, Types, false);
1068
4
      return llvm::ConstantStruct::get(STy, Elts);
1069
4
    }
1070
4
1071
4
    case CK_AddressSpaceConversion: {
1072
0
      auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
1073
0
      if (!C) return nullptr;
1074
0
      LangAS destAS = E->getType()->getPointeeType().getAddressSpace();
1075
0
      LangAS srcAS = subExpr->getType()->getPointeeType().getAddressSpace();
1076
0
      llvm::Type *destTy = ConvertType(E->getType());
1077
0
      return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, srcAS,
1078
0
                                                             destAS, destTy);
1079
0
    }
1080
0
1081
4.60k
    case CK_LValueToRValue:
1082
4.60k
    case CK_AtomicToNonAtomic:
1083
4.60k
    case CK_NonAtomicToAtomic:
1084
4.60k
    case CK_NoOp:
1085
4.60k
    case CK_ConstructorConversion:
1086
4.60k
      return Visit(subExpr, destType);
1087
4.60k
1088
4.60k
    case CK_IntToOCLSampler:
1089
0
      llvm_unreachable("global sampler variables are not generated");
1090
4.60k
1091
4.60k
    
case CK_Dependent: 0
llvm_unreachable0
("saw dependent cast!");
1092
4.60k
1093
4.60k
    case CK_BuiltinFnToFnPtr:
1094
0
      llvm_unreachable("builtin functions are handled elsewhere");
1095
4.60k
1096
4.60k
    case CK_ReinterpretMemberPointer:
1097
57
    case CK_DerivedToBaseMemberPointer:
1098
57
    case CK_BaseToDerivedMemberPointer: {
1099
57
      auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
1100
57
      if (!C) 
return nullptr0
;
1101
57
      return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
1102
57
    }
1103
57
1104
57
    // These will never be supported.
1105
57
    case CK_ObjCObjectLValueCast:
1106
3
    case CK_ARCProduceObject:
1107
3
    case CK_ARCConsumeObject:
1108
3
    case CK_ARCReclaimReturnedObject:
1109
3
    case CK_ARCExtendBlockObject:
1110
3
    case CK_CopyAndAutoreleaseBlockObject:
1111
3
      return nullptr;
1112
3
1113
3
    // These don't need to be handled here because Evaluate knows how to
1114
3
    // evaluate them in the cases where they can be folded.
1115
348
    case CK_BitCast:
1116
348
    case CK_ToVoid:
1117
348
    case CK_Dynamic:
1118
348
    case CK_LValueBitCast:
1119
348
    case CK_LValueToRValueBitCast:
1120
348
    case CK_NullToMemberPointer:
1121
348
    case CK_UserDefinedConversion:
1122
348
    case CK_CPointerToObjCPointerCast:
1123
348
    case CK_BlockPointerToObjCPointerCast:
1124
348
    case CK_AnyPointerToBlockPointerCast:
1125
348
    case CK_ArrayToPointerDecay:
1126
348
    case CK_FunctionToPointerDecay:
1127
348
    case CK_BaseToDerived:
1128
348
    case CK_DerivedToBase:
1129
348
    case CK_UncheckedDerivedToBase:
1130
348
    case CK_MemberPointerToBoolean:
1131
348
    case CK_VectorSplat:
1132
348
    case CK_FloatingRealToComplex:
1133
348
    case CK_FloatingComplexToReal:
1134
348
    case CK_FloatingComplexToBoolean:
1135
348
    case CK_FloatingComplexCast:
1136
348
    case CK_FloatingComplexToIntegralComplex:
1137
348
    case CK_IntegralRealToComplex:
1138
348
    case CK_IntegralComplexToReal:
1139
348
    case CK_IntegralComplexToBoolean:
1140
348
    case CK_IntegralComplexCast:
1141
348
    case CK_IntegralComplexToFloatingComplex:
1142
348
    case CK_PointerToIntegral:
1143
348
    case CK_PointerToBoolean:
1144
348
    case CK_NullToPointer:
1145
348
    case CK_IntegralCast:
1146
348
    case CK_BooleanToSignedIntegral:
1147
348
    case CK_IntegralToPointer:
1148
348
    case CK_IntegralToBoolean:
1149
348
    case CK_IntegralToFloating:
1150
348
    case CK_FloatingToIntegral:
1151
348
    case CK_FloatingToBoolean:
1152
348
    case CK_FloatingCast:
1153
348
    case CK_FixedPointCast:
1154
348
    case CK_FixedPointToBoolean:
1155
348
    case CK_FixedPointToIntegral:
1156
348
    case CK_IntegralToFixedPoint:
1157
348
    case CK_ZeroToOCLOpaqueType:
1158
348
      return nullptr;
1159
0
    }
1160
0
    llvm_unreachable("Invalid CastKind");
1161
0
  }
1162
1163
3
  llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE, QualType T) {
1164
3
    // No need for a DefaultInitExprScope: we don't handle 'this' in a
1165
3
    // constant expression.
1166
3
    return Visit(DIE->getExpr(), T);
1167
3
  }
1168
1169
709
  llvm::Constant *VisitExprWithCleanups(ExprWithCleanups *E, QualType T) {
1170
709
    if (!E->cleanupsHaveSideEffects())
1171
273
      return Visit(E->getSubExpr(), T);
1172
436
    return nullptr;
1173
436
  }
1174
1175
  llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E,
1176
91
                                                QualType T) {
1177
91
    return Visit(E->getSubExpr(), T);
1178
91
  }
1179
1180
838
  llvm::Constant *EmitArrayInitialization(InitListExpr *ILE, QualType T) {
1181
838
    auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType());
1182
838
    assert(CAT && "can't emit array init for non-constant-bound array");
1183
838
    unsigned NumInitElements = ILE->getNumInits();
1184
838
    unsigned NumElements = CAT->getSize().getZExtValue();
1185
838
1186
838
    // Initialising an array requires us to automatically
1187
838
    // initialise any elements that have not been initialised explicitly
1188
838
    unsigned NumInitableElts = std::min(NumInitElements, NumElements);
1189
838
1190
838
    QualType EltType = CAT->getElementType();
1191
838
1192
838
    // Initialize remaining array elements.
1193
838
    llvm::Constant *fillC = nullptr;
1194
838
    if (Expr *filler = ILE->getArrayFiller()) {
1195
140
      fillC = Emitter.tryEmitAbstractForMemory(filler, EltType);
1196
140
      if (!fillC)
1197
8
        return nullptr;
1198
830
    }
1199
830
1200
830
    // Copy initializer elements.
1201
830
    SmallVector<llvm::Constant*, 16> Elts;
1202
830
    if (fillC && 
fillC->isNullValue()132
)
1203
132
      Elts.reserve(NumInitableElts + 1);
1204
698
    else
1205
698
      Elts.reserve(NumElements);
1206
830
1207
830
    llvm::Type *CommonElementType = nullptr;
1208
6.45k
    for (unsigned i = 0; i < NumInitableElts; 
++i5.62k
) {
1209
5.70k
      Expr *Init = ILE->getInit(i);
1210
5.70k
      llvm::Constant *C = Emitter.tryEmitPrivateForMemory(Init, EltType);
1211
5.70k
      if (!C)
1212
83
        return nullptr;
1213
5.62k
      if (i == 0)
1214
728
        CommonElementType = C->getType();
1215
4.89k
      else if (C->getType() != CommonElementType)
1216
4
        CommonElementType = nullptr;
1217
5.62k
      Elts.push_back(C);
1218
5.62k
    }
1219
830
1220
830
    llvm::ArrayType *Desired =
1221
747
        cast<llvm::ArrayType>(CGM.getTypes().ConvertType(ILE->getType()));
1222
747
    return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
1223
747
                             fillC);
1224
830
  }
1225
1226
2.40k
  llvm::Constant *EmitRecordInitialization(InitListExpr *ILE, QualType T) {
1227
2.40k
    return ConstStructBuilder::BuildStruct(Emitter, ILE, T);
1228
2.40k
  }
1229
1230
  llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E,
1231
74
                                             QualType T) {
1232
74
    return CGM.EmitNullConstant(T);
1233
74
  }
1234
1235
3.25k
  llvm::Constant *VisitInitListExpr(InitListExpr *ILE, QualType T) {
1236
3.25k
    if (ILE->isTransparent())
1237
15
      return Visit(ILE->getInit(0), T);
1238
3.23k
1239
3.23k
    if (ILE->getType()->isArrayType())
1240
838
      return EmitArrayInitialization(ILE, T);
1241
2.40k
1242
2.40k
    if (ILE->getType()->isRecordType())
1243
2.40k
      return EmitRecordInitialization(ILE, T);
1244
0
1245
0
    return nullptr;
1246
0
  }
1247
1248
  llvm::Constant *VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E,
1249
28
                                                QualType destType) {
1250
28
    auto C = Visit(E->getBase(), destType);
1251
28
    if (!C)
1252
0
      return nullptr;
1253
28
1254
28
    ConstantAggregateBuilder Const(CGM);
1255
28
    Const.add(C, CharUnits::Zero(), false);
1256
28
1257
28
    if (!EmitDesignatedInitUpdater(Emitter, Const, CharUnits::Zero(), destType,
1258
28
                                   E->getUpdater()))
1259
0
      return nullptr;
1260
28
1261
28
    llvm::Type *ValTy = CGM.getTypes().ConvertType(destType);
1262
28
    bool HasFlexibleArray = false;
1263
28
    if (auto *RT = destType->getAs<RecordType>())
1264
20
      HasFlexibleArray = RT->getDecl()->hasFlexibleArrayMember();
1265
28
    return Const.build(ValTy, HasFlexibleArray);
1266
28
  }
1267
1268
3.13k
  llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E, QualType Ty) {
1269
3.13k
    if (!E->getConstructor()->isTrivial())
1270
3.00k
      return nullptr;
1271
126
1272
126
    // Only default and copy/move constructors can be trivial.
1273
126
    if (E->getNumArgs()) {
1274
124
      assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
1275
124
      assert(E->getConstructor()->isCopyOrMoveConstructor() &&
1276
124
             "trivial ctor has argument but isn't a copy/move ctor");
1277
124
1278
124
      Expr *Arg = E->getArg(0);
1279
124
      assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
1280
124
             "argument to copy ctor is of wrong type");
1281
124
1282
124
      return Visit(Arg, Ty);
1283
124
    }
1284
2
1285
2
    return CGM.EmitNullConstant(Ty);
1286
2
  }
1287
1288
91
  llvm::Constant *VisitStringLiteral(StringLiteral *E, QualType T) {
1289
91
    // This is a string literal initializing an array in an initializer.
1290
91
    return CGM.GetConstantArrayFromStringLiteral(E);
1291
91
  }
1292
1293
36
  llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E, QualType T) {
1294
36
    // This must be an @encode initializing an array in a static initializer.
1295
36
    // Don't emit it as the address of the string, emit the string data itself
1296
36
    // as an inline array.
1297
36
    std::string Str;
1298
36
    CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1299
36
    const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T);
1300
36
1301
36
    // Resize the string to the right size, adding zeros at the end, or
1302
36
    // truncating as needed.
1303
36
    Str.resize(CAT->getSize().getZExtValue(), '\0');
1304
36
    return llvm::ConstantDataArray::getString(VMContext, Str, false);
1305
36
  }
1306
1307
1
  llvm::Constant *VisitUnaryExtension(const UnaryOperator *E, QualType T) {
1308
1
    return Visit(E->getSubExpr(), T);
1309
1
  }
1310
1311
  // Utility methods
1312
4
  llvm::Type *ConvertType(QualType T) {
1313
4
    return CGM.getTypes().ConvertType(T);
1314
4
  }
1315
};
1316
1317
}  // end anonymous namespace.
1318
1319
llvm::Constant *ConstantEmitter::validateAndPopAbstract(llvm::Constant *C,
1320
23.3k
                                                        AbstractState saved) {
1321
23.3k
  Abstract = saved.OldValue;
1322
23.3k
1323
23.3k
  assert(saved.OldPlaceholdersSize == PlaceholderAddresses.size() &&
1324
23.3k
         "created a placeholder while doing an abstract emission?");
1325
23.3k
1326
23.3k
  // No validation necessary for now.
1327
23.3k
  // No cleanup to do for now.
1328
23.3k
  return C;
1329
23.3k
}
1330
1331
llvm::Constant *
1332
14.2k
ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &D) {
1333
14.2k
  auto state = pushAbstract();
1334
14.2k
  auto C = tryEmitPrivateForVarInit(D);
1335
14.2k
  return validateAndPopAbstract(C, state);
1336
14.2k
}
1337
1338
llvm::Constant *
1339
549
ConstantEmitter::tryEmitAbstract(const Expr *E, QualType destType) {
1340
549
  auto state = pushAbstract();
1341
549
  auto C = tryEmitPrivate(E, destType);
1342
549
  return validateAndPopAbstract(C, state);
1343
549
}
1344
1345
llvm::Constant *
1346
533
ConstantEmitter::tryEmitAbstract(const APValue &value, QualType destType) {
1347
533
  auto state = pushAbstract();
1348
533
  auto C = tryEmitPrivate(value, destType);
1349
533
  return validateAndPopAbstract(C, state);
1350
533
}
1351
1352
llvm::Constant *
1353
312
ConstantEmitter::emitAbstract(const Expr *E, QualType destType) {
1354
312
  auto state = pushAbstract();
1355
312
  auto C = tryEmitPrivate(E, destType);
1356
312
  C = validateAndPopAbstract(C, state);
1357
312
  if (!C) {
1358
0
    CGM.Error(E->getExprLoc(),
1359
0
              "internal error: could not emit constant value \"abstractly\"");
1360
0
    C = CGM.EmitNullConstant(destType);
1361
0
  }
1362
312
  return C;
1363
312
}
1364
1365
llvm::Constant *
1366
ConstantEmitter::emitAbstract(SourceLocation loc, const APValue &value,
1367
7.66k
                              QualType destType) {
1368
7.66k
  auto state = pushAbstract();
1369
7.66k
  auto C = tryEmitPrivate(value, destType);
1370
7.66k
  C = validateAndPopAbstract(C, state);
1371
7.66k
  if (!C) {
1372
0
    CGM.Error(loc,
1373
0
              "internal error: could not emit constant value \"abstractly\"");
1374
0
    C = CGM.EmitNullConstant(destType);
1375
0
  }
1376
7.66k
  return C;
1377
7.66k
}
1378
1379
21.1k
llvm::Constant *ConstantEmitter::tryEmitForInitializer(const VarDecl &D) {
1380
21.1k
  initializeNonAbstract(D.getType().getAddressSpace());
1381
21.1k
  return markIfFailed(tryEmitPrivateForVarInit(D));
1382
21.1k
}
1383
1384
llvm::Constant *ConstantEmitter::tryEmitForInitializer(const Expr *E,
1385
                                                       LangAS destAddrSpace,
1386
99
                                                       QualType destType) {
1387
99
  initializeNonAbstract(destAddrSpace);
1388
99
  return markIfFailed(tryEmitPrivateForMemory(E, destType));
1389
99
}
1390
1391
llvm::Constant *ConstantEmitter::emitForInitializer(const APValue &value,
1392
                                                    LangAS destAddrSpace,
1393
126
                                                    QualType destType) {
1394
126
  initializeNonAbstract(destAddrSpace);
1395
126
  auto C = tryEmitPrivateForMemory(value, destType);
1396
126
  assert(C && "couldn't emit constant value non-abstractly?");
1397
126
  return C;
1398
126
}
1399
1400
0
llvm::GlobalValue *ConstantEmitter::getCurrentAddrPrivate() {
1401
0
  assert(!Abstract && "cannot get current address for abstract constant");
1402
0
1403
0
1404
0
1405
0
  // Make an obviously ill-formed global that should blow up compilation
1406
0
  // if it survives.
1407
0
  auto global = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, true,
1408
0
                                         llvm::GlobalValue::PrivateLinkage,
1409
0
                                         /*init*/ nullptr,
1410
0
                                         /*name*/ "",
1411
0
                                         /*before*/ nullptr,
1412
0
                                         llvm::GlobalVariable::NotThreadLocal,
1413
0
                                         CGM.getContext().getTargetAddressSpace(DestAddressSpace));
1414
0
1415
0
  PlaceholderAddresses.push_back(std::make_pair(nullptr, global));
1416
0
1417
0
  return global;
1418
0
}
1419
1420
void ConstantEmitter::registerCurrentAddrPrivate(llvm::Constant *signal,
1421
0
                                           llvm::GlobalValue *placeholder) {
1422
0
  assert(!PlaceholderAddresses.empty());
1423
0
  assert(PlaceholderAddresses.back().first == nullptr);
1424
0
  assert(PlaceholderAddresses.back().second == placeholder);
1425
0
  PlaceholderAddresses.back().first = signal;
1426
0
}
1427
1428
namespace {
1429
  struct ReplacePlaceholders {
1430
    CodeGenModule &CGM;
1431
1432
    /// The base address of the global.
1433
    llvm::Constant *Base;
1434
    llvm::Type *BaseValueTy = nullptr;
1435
1436
    /// The placeholder addresses that were registered during emission.
1437
    llvm::DenseMap<llvm::Constant*, llvm::GlobalVariable*> PlaceholderAddresses;
1438
1439
    /// The locations of the placeholder signals.
1440
    llvm::DenseMap<llvm::GlobalVariable*, llvm::Constant*> Locations;
1441
1442
    /// The current index stack.  We use a simple unsigned stack because
1443
    /// we assume that placeholders will be relatively sparse in the
1444
    /// initializer, but we cache the index values we find just in case.
1445
    llvm::SmallVector<unsigned, 8> Indices;
1446
    llvm::SmallVector<llvm::Constant*, 8> IndexValues;
1447
1448
    ReplacePlaceholders(CodeGenModule &CGM, llvm::Constant *base,
1449
                        ArrayRef<std::pair<llvm::Constant*,
1450
                                           llvm::GlobalVariable*>> addresses)
1451
        : CGM(CGM), Base(base),
1452
0
          PlaceholderAddresses(addresses.begin(), addresses.end()) {
1453
0
    }
1454
1455
    void replaceInInitializer(llvm::Constant *init) {
1456
      // Remember the type of the top-most initializer.
1457
      BaseValueTy = init->getType();
1458
1459
      // Initialize the stack.
1460
      Indices.push_back(0);
1461
      IndexValues.push_back(nullptr);
1462
1463
      // Recurse into the initializer.
1464
      findLocations(init);
1465
1466
      // Check invariants.
1467
      assert(IndexValues.size() == Indices.size() && "mismatch");
1468
      assert(Indices.size() == 1 && "didn't pop all indices");
1469
1470
      // Do the replacement; this basically invalidates 'init'.
1471
      assert(Locations.size() == PlaceholderAddresses.size() &&
1472
             "missed a placeholder?");
1473
1474
      // We're iterating over a hashtable, so this would be a source of
1475
      // non-determinism in compiler output *except* that we're just
1476
      // messing around with llvm::Constant structures, which never itself
1477
      // does anything that should be visible in compiler output.
1478
      for (auto &entry : Locations) {
1479
        assert(entry.first->getParent() == nullptr && "not a placeholder!");
1480
        entry.first->replaceAllUsesWith(entry.second);
1481
        entry.first->eraseFromParent();
1482
      }
1483
    }
1484
1485
  private:
1486
0
    void findLocations(llvm::Constant *init) {
1487
0
      // Recurse into aggregates.
1488
0
      if (auto agg = dyn_cast<llvm::ConstantAggregate>(init)) {
1489
0
        for (unsigned i = 0, e = agg->getNumOperands(); i != e; ++i) {
1490
0
          Indices.push_back(i);
1491
0
          IndexValues.push_back(nullptr);
1492
0
1493
0
          findLocations(agg->getOperand(i));
1494
0
1495
0
          IndexValues.pop_back();
1496
0
          Indices.pop_back();
1497
0
        }
1498
0
        return;
1499
0
      }
1500
0
1501
0
      // Otherwise, check for registered constants.
1502
0
      while (true) {
1503
0
        auto it = PlaceholderAddresses.find(init);
1504
0
        if (it != PlaceholderAddresses.end()) {
1505
0
          setLocation(it->second);
1506
0
          break;
1507
0
        }
1508
0
1509
0
        // Look through bitcasts or other expressions.
1510
0
        if (auto expr = dyn_cast<llvm::ConstantExpr>(init)) {
1511
0
          init = expr->getOperand(0);
1512
0
        } else {
1513
0
          break;
1514
0
        }
1515
0
      }
1516
0
    }
1517
1518
0
    void setLocation(llvm::GlobalVariable *placeholder) {
1519
0
      assert(Locations.find(placeholder) == Locations.end() &&
1520
0
             "already found location for placeholder!");
1521
0
1522
0
      // Lazily fill in IndexValues with the values from Indices.
1523
0
      // We do this in reverse because we should always have a strict
1524
0
      // prefix of indices from the start.
1525
0
      assert(Indices.size() == IndexValues.size());
1526
0
      for (size_t i = Indices.size() - 1; i != size_t(-1); --i) {
1527
0
        if (IndexValues[i]) {
1528
0
#ifndef NDEBUG
1529
0
          for (size_t j = 0; j != i + 1; ++j) {
1530
0
            assert(IndexValues[j] &&
1531
0
                   isa<llvm::ConstantInt>(IndexValues[j]) &&
1532
0
                   cast<llvm::ConstantInt>(IndexValues[j])->getZExtValue()
1533
0
                     == Indices[j]);
1534
0
          }
1535
0
#endif
1536
0
          break;
1537
0
        }
1538
0
1539
0
        IndexValues[i] = llvm::ConstantInt::get(CGM.Int32Ty, Indices[i]);
1540
0
      }
1541
0
1542
0
      // Form a GEP and then bitcast to the placeholder type so that the
1543
0
      // replacement will succeed.
1544
0
      llvm::Constant *location =
1545
0
        llvm::ConstantExpr::getInBoundsGetElementPtr(BaseValueTy,
1546
0
                                                     Base, IndexValues);
1547
0
      location = llvm::ConstantExpr::getBitCast(location,
1548
0
                                                placeholder->getType());
1549
0
1550
0
      Locations.insert({placeholder, location});
1551
0
    }
1552
  };
1553
}
1554
1555
14.6k
void ConstantEmitter::finalize(llvm::GlobalVariable *global) {
1556
14.6k
  assert(InitializedNonAbstract &&
1557
14.6k
         "finalizing emitter that was used for abstract emission?");
1558
14.6k
  assert(!Finalized && "finalizing emitter multiple times");
1559
14.6k
  assert(global->getInitializer());
1560
14.6k
1561
14.6k
  // Note that we might also be Failed.
1562
14.6k
  Finalized = true;
1563
14.6k
1564
14.6k
  if (!PlaceholderAddresses.empty()) {
1565
0
    ReplacePlaceholders(CGM, global, PlaceholderAddresses)
1566
0
      .replaceInInitializer(global->getInitializer());
1567
0
    PlaceholderAddresses.clear(); // satisfy
1568
0
  }
1569
14.6k
}
1570
1571
44.0k
ConstantEmitter::~ConstantEmitter() {
1572
44.0k
  assert((!InitializedNonAbstract || Finalized || Failed) &&
1573
44.0k
         "not finalized after being initialized for non-abstract emission");
1574
44.0k
  assert(PlaceholderAddresses.empty() && "unhandled placeholders");
1575
44.0k
}
1576
1577
57.1k
static QualType getNonMemoryType(CodeGenModule &CGM, QualType type) {
1578
57.1k
  if (auto AT = type->getAs<AtomicType>()) {
1579
99
    return CGM.getContext().getQualifiedType(AT->getValueType(),
1580
99
                                             type.getQualifiers());
1581
99
  }
1582
57.0k
  return type;
1583
57.0k
}
1584
1585
35.4k
llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
1586
35.4k
  // Make a quick check if variable can be default NULL initialized
1587
35.4k
  // and avoid going through rest of code which may do, for c++11,
1588
35.4k
  // initialization of memory to all NULLs.
1589
35.4k
  if (!D.hasLocalStorage()) {
1590
21.1k
    QualType Ty = CGM.getContext().getBaseElementType(D.getType());
1591
21.1k
    if (Ty->isRecordType())
1592
5.35k
      if (const CXXConstructExpr *E =
1593
4.38k
          dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
1594
4.38k
        const CXXConstructorDecl *CD = E->getConstructor();
1595
4.38k
        if (CD->isTrivial() && 
CD->isDefaultConstructor()954
)
1596
929
          return CGM.EmitNullConstant(D.getType());
1597
20.2k
      }
1598
20.2k
    InConstantContext = true;
1599
20.2k
  }
1600
35.4k
1601
35.4k
  QualType destType = D.getType();
1602
34.5k
1603
34.5k
  // Try to emit the initializer.  Note that this can allow some things that
1604
34.5k
  // are not allowed by tryEmitPrivateForMemory alone.
1605
34.5k
  if (auto value = D.evaluateValue()) {
1606
12.1k
    return tryEmitPrivateForMemory(*value, destType);
1607
12.1k
  }
1608
22.4k
1609
22.4k
  // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
1610
22.4k
  // reference is a constant expression, and the reference binds to a temporary,
1611
22.4k
  // then constant initialization is performed. ConstExprEmitter will
1612
22.4k
  // incorrectly emit a prvalue constant in this case, and the calling code
1613
22.4k
  // interprets that as the (pointer) value of the reference, rather than the
1614
22.4k
  // desired value of the referee.
1615
22.4k
  if (destType->isReferenceType())
1616
3.22k
    return nullptr;
1617
19.1k
1618
19.1k
  const Expr *E = D.getInit();
1619
19.1k
  assert(E && "No initializer to emit");
1620
19.1k
1621
19.1k
  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1622
19.1k
  auto C =
1623
19.1k
    ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), nonMemoryDestType);
1624
19.1k
  return (C ? 
emitForMemory(C, destType)2.27k
:
nullptr16.9k
);
1625
19.1k
}
1626
1627
llvm::Constant *
1628
143
ConstantEmitter::tryEmitAbstractForMemory(const Expr *E, QualType destType) {
1629
143
  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1630
143
  auto C = tryEmitAbstract(E, nonMemoryDestType);
1631
143
  return (C ? 
emitForMemory(C, destType)135
:
nullptr8
);
1632
143
}
1633
1634
llvm::Constant *
1635
ConstantEmitter::tryEmitAbstractForMemory(const APValue &value,
1636
533
                                          QualType destType) {
1637
533
  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1638
533
  auto C = tryEmitAbstract(value, nonMemoryDestType);
1639
533
  return (C ? emitForMemory(C, destType) : 
nullptr0
);
1640
533
}
1641
1642
llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const Expr *E,
1643
11.0k
                                                         QualType destType) {
1644
11.0k
  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1645
11.0k
  llvm::Constant *C = tryEmitPrivate(E, nonMemoryDestType);
1646
11.0k
  return (C ? 
emitForMemory(C, destType)10.8k
:
nullptr162
);
1647
11.0k
}
1648
1649
llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const APValue &value,
1650
26.2k
                                                         QualType destType) {
1651
26.2k
  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1652
26.2k
  auto C = tryEmitPrivate(value, nonMemoryDestType);
1653
26.2k
  return (C ? emitForMemory(C, destType) : 
nullptr0
);
1654
26.2k
}
1655
1656
llvm::Constant *ConstantEmitter::emitForMemory(CodeGenModule &CGM,
1657
                                               llvm::Constant *C,
1658
40.1k
                                               QualType destType) {
1659
40.1k
  // For an _Atomic-qualified constant, we may need to add tail padding.
1660
40.1k
  if (auto AT = destType->getAs<AtomicType>()) {
1661
95
    QualType destValueType = AT->getValueType();
1662
95
    C = emitForMemory(CGM, C, destValueType);
1663
95
1664
95
    uint64_t innerSize = CGM.getContext().getTypeSize(destValueType);
1665
95
    uint64_t outerSize = CGM.getContext().getTypeSize(destType);
1666
95
    if (innerSize == outerSize)
1667
94
      return C;
1668
1
1669
1
    assert(innerSize < outerSize && "emitted over-large constant for atomic");
1670
1
    llvm::Constant *elts[] = {
1671
1
      C,
1672
1
      llvm::ConstantAggregateZero::get(
1673
1
          llvm::ArrayType::get(CGM.Int8Ty, (outerSize - innerSize) / 8))
1674
1
    };
1675
1
    return llvm::ConstantStruct::getAnon(elts);
1676
1
  }
1677
40.0k
1678
40.0k
  // Zero-extend bool.
1679
40.0k
  if (C->getType()->isIntegerTy(1)) {
1680
359
    llvm::Type *boolTy = CGM.getTypes().ConvertTypeForMem(destType);
1681
359
    return llvm::ConstantExpr::getZExt(C, boolTy);
1682
359
  }
1683
39.7k
1684
39.7k
  return C;
1685
39.7k
}
1686
1687
llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E,
1688
11.9k
                                                QualType destType) {
1689
11.9k
  Expr::EvalResult Result;
1690
11.9k
1691
11.9k
  bool Success = false;
1692
11.9k
1693
11.9k
  if (destType->isReferenceType())
1694
9
    Success = E->EvaluateAsLValue(Result, CGM.getContext());
1695
11.9k
  else
1696
11.9k
    Success = E->EvaluateAsRValue(Result, CGM.getContext(), InConstantContext);
1697
11.9k
1698
11.9k
  llvm::Constant *C;
1699
11.9k
  if (Success && 
!Result.HasSideEffects10.5k
)
1700
10.5k
    C = tryEmitPrivate(Result.Val, destType);
1701
1.41k
  else
1702
1.41k
    C = ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), destType);
1703
11.9k
1704
11.9k
  return C;
1705
11.9k
}
1706
1707
28.6k
llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) {
1708
28.6k
  return getTargetCodeGenInfo().getNullPointer(*this, T, QT);
1709
28.6k
}
1710
1711
namespace {
1712
/// A struct which can be used to peephole certain kinds of finalization
1713
/// that normally happen during l-value emission.
1714
struct ConstantLValue {
1715
  llvm::Constant *Value;
1716
  bool HasOffsetApplied;
1717
1718
  /*implicit*/ ConstantLValue(llvm::Constant *value,
1719
                              bool hasOffsetApplied = false)
1720
6.90k
    : Value(value), HasOffsetApplied(hasOffsetApplied) {}
1721
1722
  /*implicit*/ ConstantLValue(ConstantAddress address)
1723
3.82k
    : ConstantLValue(address.getPointer()) {}
1724
};
1725
1726
/// A helper class for emitting constant l-values.
1727
class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter,
1728
                                                      ConstantLValue> {
1729
  CodeGenModule &CGM;
1730
  ConstantEmitter &Emitter;
1731
  const APValue &Value;
1732
  QualType DestType;
1733
1734
  // Befriend StmtVisitorBase so that we don't have to expose Visit*.
1735
  friend StmtVisitorBase;
1736
1737
public:
1738
  ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value,
1739
                        QualType destType)
1740
7.58k
    : CGM(emitter.CGM), Emitter(emitter), Value(value), DestType(destType) {}
1741
1742
  llvm::Constant *tryEmit();
1743
1744
private:
1745
  llvm::Constant *tryEmitAbsolute(llvm::Type *destTy);
1746
  ConstantLValue tryEmitBase(const APValue::LValueBase &base);
1747
1748
0
  ConstantLValue VisitStmt(const Stmt *S) { return nullptr; }
1749
  ConstantLValue VisitConstantExpr(const ConstantExpr *E);
1750
  ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
1751
  ConstantLValue VisitStringLiteral(const StringLiteral *E);
1752
  ConstantLValue VisitObjCBoxedExpr(const ObjCBoxedExpr *E);
1753
  ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
1754
  ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *E);
1755
  ConstantLValue VisitPredefinedExpr(const PredefinedExpr *E);
1756
  ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *E);
1757
  ConstantLValue VisitCallExpr(const CallExpr *E);
1758
  ConstantLValue VisitBlockExpr(const BlockExpr *E);
1759
  ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *E);
1760
  ConstantLValue VisitCXXUuidofExpr(const CXXUuidofExpr *E);
1761
  ConstantLValue VisitMaterializeTemporaryExpr(
1762
                                         const MaterializeTemporaryExpr *E);
1763
1764
6.90k
  bool hasNonZeroOffset() const {
1765
6.90k
    return !Value.getLValueOffset().isZero();
1766
6.90k
  }
1767
1768
  /// Return the value offset.
1769
256
  llvm::Constant *getOffset() {
1770
256
    return llvm::ConstantInt::get(CGM.Int64Ty,
1771
256
                                  Value.getLValueOffset().getQuantity());
1772
256
  }
1773
1774
  /// Apply the value offset to the given constant.
1775
6.90k
  llvm::Constant *applyOffset(llvm::Constant *C) {
1776
6.90k
    if (!hasNonZeroOffset())
1777
6.85k
      return C;
1778
50
1779
50
    llvm::Type *origPtrTy = C->getType();
1780
50
    unsigned AS = origPtrTy->getPointerAddressSpace();
1781
50
    llvm::Type *charPtrTy = CGM.Int8Ty->getPointerTo(AS);
1782
50
    C = llvm::ConstantExpr::getBitCast(C, charPtrTy);
1783
50
    C = llvm::ConstantExpr::getGetElementPtr(CGM.Int8Ty, C, getOffset());
1784
50
    C = llvm::ConstantExpr::getPointerCast(C, origPtrTy);
1785
50
    return C;
1786
50
  }
1787
};
1788
1789
}
1790
1791
7.58k
llvm::Constant *ConstantLValueEmitter::tryEmit() {
1792
7.58k
  const APValue::LValueBase &base = Value.getLValueBase();
1793
7.58k
1794
7.58k
  // The destination type should be a pointer or reference
1795
7.58k
  // type, but it might also be a cast thereof.
1796
7.58k
  //
1797
7.58k
  // FIXME: the chain of casts required should be reflected in the APValue.
1798
7.58k
  // We need this in order to correctly handle things like a ptrtoint of a
1799
7.58k
  // non-zero null pointer and addrspace casts that aren't trivially
1800
7.58k
  // represented in LLVM IR.
1801
7.58k
  auto destTy = CGM.getTypes().ConvertTypeForMem(DestType);
1802
7.58k
  assert(isa<llvm::IntegerType>(destTy) || isa<llvm::PointerType>(destTy));
1803
7.58k
1804
7.58k
  // If there's no base at all, this is a null or absolute pointer,
1805
7.58k
  // possibly cast back to an integer type.
1806
7.58k
  if (!base) {
1807
681
    return tryEmitAbsolute(destTy);
1808
681
  }
1809
6.90k
1810
6.90k
  // Otherwise, try to emit the base.
1811
6.90k
  ConstantLValue result = tryEmitBase(base);
1812
6.90k
1813
6.90k
  // If that failed, we're done.
1814
6.90k
  llvm::Constant *value = result.Value;
1815
6.90k
  if (!value) 
return nullptr0
;
1816
6.90k
1817
6.90k
  // Apply the offset if necessary and not already done.
1818
6.90k
  if (!result.HasOffsetApplied) {
1819
6.90k
    value = applyOffset(value);
1820
6.90k
  }
1821
6.90k
1822
6.90k
  // Convert to the appropriate type; this could be an lvalue for
1823
6.90k
  // an integer.  FIXME: performAddrSpaceCast
1824
6.90k
  if (isa<llvm::PointerType>(destTy))
1825
6.88k
    return llvm::ConstantExpr::getPointerCast(value, destTy);
1826
18
1827
18
  return llvm::ConstantExpr::getPtrToInt(value, destTy);
1828
18
}
1829
1830
/// Try to emit an absolute l-value, such as a null pointer or an integer
1831
/// bitcast to pointer type.
1832
llvm::Constant *
1833
681
ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) {
1834
681
  // If we're producing a pointer, this is easy.
1835
681
  auto destPtrTy = cast<llvm::PointerType>(destTy);
1836
681
  if (Value.isNullPointer()) {
1837
475
    // FIXME: integer offsets from non-zero null pointers.
1838
475
    return CGM.getNullPointer(destPtrTy, DestType);
1839
475
  }
1840
206
1841
206
  // Convert the integer to a pointer-sized integer before converting it
1842
206
  // to a pointer.
1843
206
  // FIXME: signedness depends on the original integer type.
1844
206
  auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy);
1845
206
  llvm::Constant *C;
1846
206
  C = llvm::ConstantExpr::getIntegerCast(getOffset(), intptrTy,
1847
206
                                         /*isSigned*/ false);
1848
206
  C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy);
1849
206
  return C;
1850
206
}
1851
1852
ConstantLValue
1853
6.90k
ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
1854
6.90k
  // Handle values.
1855
6.90k
  if (const ValueDecl *D = base.dyn_cast<const ValueDecl*>()) {
1856
2.88k
    if (D->hasAttr<WeakRefAttr>())
1857
5
      return CGM.GetWeakRefReference(D).getPointer();
1858
2.87k
1859
2.87k
    if (auto FD = dyn_cast<FunctionDecl>(D))
1860
2.17k
      return CGM.GetAddrOfFunction(FD);
1861
705
1862
705
    if (auto VD = dyn_cast<VarDecl>(D)) {
1863
705
      // We can never refer to a variable with local storage.
1864
705
      if (!VD->hasLocalStorage()) {
1865
705
        if (VD->isFileVarDecl() || 
VD->hasExternalStorage()16
)
1866
690
          return CGM.GetAddrOfGlobalVar(VD);
1867
15
1868
15
        if (VD->isLocalVarDecl()) {
1869
15
          return CGM.getOrCreateStaticVarDecl(
1870
15
              *VD, CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false));
1871
15
        }
1872
0
      }
1873
705
    }
1874
0
1875
0
    return nullptr;
1876
0
  }
1877
4.01k
1878
4.01k
  // Handle typeid(T).
1879
4.01k
  if (TypeInfoLValue TI = base.dyn_cast<TypeInfoLValue>()) {
1880
75
    llvm::Type *StdTypeInfoPtrTy =
1881
75
        CGM.getTypes().ConvertType(base.getTypeInfoType())->getPointerTo();
1882
75
    llvm::Constant *TypeInfo =
1883
75
        CGM.GetAddrOfRTTIDescriptor(QualType(TI.getType(), 0));
1884
75
    if (TypeInfo->getType() != StdTypeInfoPtrTy)
1885
75
      TypeInfo = llvm::ConstantExpr::getBitCast(TypeInfo, StdTypeInfoPtrTy);
1886
75
    return TypeInfo;
1887
75
  }
1888
3.94k
1889
3.94k
  // Otherwise, it must be an expression.
1890
3.94k
  return Visit(base.get<const Expr*>());
1891
3.94k
}
1892
1893
ConstantLValue
1894
0
ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *E) {
1895
0
  return Visit(E->getSubExpr());
1896
0
}
1897
1898
ConstantLValue
1899
14
ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
1900
14
  return tryEmitGlobalCompoundLiteral(CGM, Emitter.CGF, E);
1901
14
}
1902
1903
ConstantLValue
1904
3.08k
ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *E) {
1905
3.08k
  return CGM.GetAddrOfConstantStringFromLiteral(E);
1906
3.08k
}
1907
1908
ConstantLValue
1909
9
ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
1910
9
  return CGM.GetAddrOfConstantStringFromObjCEncode(E);
1911
9
}
1912
1913
static ConstantLValue emitConstantObjCStringLiteral(const StringLiteral *S,
1914
                                                    QualType T,
1915
184
                                                    CodeGenModule &CGM) {
1916
184
  auto C = CGM.getObjCRuntime().GenerateConstantString(S);
1917
184
  return C.getElementBitCast(CGM.getTypes().ConvertTypeForMem(T));
1918
184
}
1919
1920
ConstantLValue
1921
181
ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
1922
181
  return emitConstantObjCStringLiteral(E->getString(), E->getType(), CGM);
1923
181
}
1924
1925
ConstantLValue
1926
3
ConstantLValueEmitter::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
1927
3
  assert(E->isExpressibleAsConstantInitializer() &&
1928
3
         "this boxed expression can't be emitted as a compile-time constant");
1929
3
  auto *SL = cast<StringLiteral>(E->getSubExpr()->IgnoreParenCasts());
1930
3
  return emitConstantObjCStringLiteral(SL, E->getType(), CGM);
1931
3
}
1932
1933
ConstantLValue
1934
3
ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *E) {
1935
3
  return CGM.GetAddrOfConstantStringFromLiteral(E->getFunctionName());
1936
3
}
1937
1938
ConstantLValue
1939
36
ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *E) {
1940
36
  assert(Emitter.CGF && "Invalid address of label expression outside function");
1941
36
  llvm::Constant *Ptr = Emitter.CGF->GetAddrOfLabel(E->getLabel());
1942
36
  Ptr = llvm::ConstantExpr::getBitCast(Ptr,
1943
36
                                   CGM.getTypes().ConvertType(E->getType()));
1944
36
  return Ptr;
1945
36
}
1946
1947
ConstantLValue
1948
365
ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) {
1949
365
  unsigned builtin = E->getBuiltinCallee();
1950
365
  if (builtin != Builtin::BI__builtin___CFStringMakeConstantString &&
1951
365
      
builtin != Builtin::BI__builtin___NSStringMakeConstantString0
)
1952
0
    return nullptr;
1953
365
1954
365
  auto literal = cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts());
1955
365
  if (builtin == Builtin::BI__builtin___NSStringMakeConstantString) {
1956
0
    return CGM.getObjCRuntime().GenerateConstantString(literal);
1957
365
  } else {
1958
365
    // FIXME: need to deal with UCN conversion issues.
1959
365
    return CGM.GetAddrOfConstantCFString(literal);
1960
365
  }
1961
365
}
1962
1963
ConstantLValue
1964
80
ConstantLValueEmitter::VisitBlockExpr(const BlockExpr *E) {
1965
80
  StringRef functionName;
1966
80
  if (auto CGF = Emitter.CGF)
1967
38
    functionName = CGF->CurFn->getName();
1968
42
  else
1969
42
    functionName = "global";
1970
80
1971
80
  return CGM.GetAddrOfGlobalBlock(E, functionName);
1972
80
}
1973
1974
ConstantLValue
1975
0
ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
1976
0
  QualType T;
1977
0
  if (E->isTypeOperand())
1978
0
    T = E->getTypeOperand(CGM.getContext());
1979
0
  else
1980
0
    T = E->getExprOperand()->getType();
1981
0
  return CGM.GetAddrOfRTTIDescriptor(T);
1982
0
}
1983
1984
ConstantLValue
1985
40
ConstantLValueEmitter::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
1986
40
  return CGM.GetAddrOfUuidDescriptor(E);
1987
40
}
1988
1989
ConstantLValue
1990
ConstantLValueEmitter::VisitMaterializeTemporaryExpr(
1991
130
                                            const MaterializeTemporaryExpr *E) {
1992
130
  assert(E->getStorageDuration() == SD_Static);
1993
130
  SmallVector<const Expr *, 2> CommaLHSs;
1994
130
  SmallVector<SubobjectAdjustment, 2> Adjustments;
1995
130
  const Expr *Inner =
1996
130
      E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
1997
130
  return CGM.GetAddrOfGlobalTemporary(E, Inner);
1998
130
}
1999
2000
llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value,
2001
44.9k
                                                QualType DestType) {
2002
44.9k
  switch (Value.getKind()) {
2003
0
  case APValue::None:
2004
0
  case APValue::Indeterminate:
2005
0
    // Out-of-lifetime and indeterminate values can be modeled as 'undef'.
2006
0
    return llvm::UndefValue::get(CGM.getTypes().ConvertType(DestType));
2007
7.58k
  case APValue::LValue:
2008
7.58k
    return ConstantLValueEmitter(*this, Value, DestType).tryEmit();
2009
27.2k
  case APValue::Int:
2010
27.2k
    return llvm::ConstantInt::get(CGM.getLLVMContext(), Value.getInt());
2011
152
  case APValue::FixedPoint:
2012
152
    return llvm::ConstantInt::get(CGM.getLLVMContext(),
2013
152
                                  Value.getFixedPoint().getValue());
2014
7
  case APValue::ComplexInt: {
2015
7
    llvm::Constant *Complex[2];
2016
7
2017
7
    Complex[0] = llvm::ConstantInt::get(CGM.getLLVMContext(),
2018
7
                                        Value.getComplexIntReal());
2019
7
    Complex[1] = llvm::ConstantInt::get(CGM.getLLVMContext(),
2020
7
                                        Value.getComplexIntImag());
2021
7
2022
7
    // FIXME: the target may want to specify that this is packed.
2023
7
    llvm::StructType *STy =
2024
7
        llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
2025
7
    return llvm::ConstantStruct::get(STy, Complex);
2026
0
  }
2027
2.65k
  case APValue::Float: {
2028
2.65k
    const llvm::APFloat &Init = Value.getFloat();
2029
2.65k
    if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf() &&
2030
2.65k
        
!CGM.getContext().getLangOpts().NativeHalfType73
&&
2031
2.65k
        
CGM.getContext().getTargetInfo().useFP16ConversionIntrinsics()65
)
2032
1
      return llvm::ConstantInt::get(CGM.getLLVMContext(),
2033
1
                                    Init.bitcastToAPInt());
2034
2.65k
    else
2035
2.65k
      return llvm::ConstantFP::get(CGM.getLLVMContext(), Init);
2036
0
  }
2037
33
  case APValue::ComplexFloat: {
2038
33
    llvm::Constant *Complex[2];
2039
33
2040
33
    Complex[0] = llvm::ConstantFP::get(CGM.getLLVMContext(),
2041
33
                                       Value.getComplexFloatReal());
2042
33
    Complex[1] = llvm::ConstantFP::get(CGM.getLLVMContext(),
2043
33
                                       Value.getComplexFloatImag());
2044
33
2045
33
    // FIXME: the target may want to specify that this is packed.
2046
33
    llvm::StructType *STy =
2047
33
        llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
2048
33
    return llvm::ConstantStruct::get(STy, Complex);
2049
0
  }
2050
1.11k
  case APValue::Vector: {
2051
1.11k
    unsigned NumElts = Value.getVectorLength();
2052
1.11k
    SmallVector<llvm::Constant *, 4> Inits(NumElts);
2053
1.11k
2054
8.56k
    for (unsigned I = 0; I != NumElts; 
++I7.44k
) {
2055
7.44k
      const APValue &Elt = Value.getVectorElt(I);
2056
7.44k
      if (Elt.isInt())
2057
6.50k
        Inits[I] = llvm::ConstantInt::get(CGM.getLLVMContext(), Elt.getInt());
2058
937
      else if (Elt.isFloat())
2059
937
        Inits[I] = llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat());
2060
937
      else
2061
937
        
llvm_unreachable0
("unsupported vector element type");
2062
7.44k
    }
2063
1.11k
    return llvm::ConstantVector::get(Inits);
2064
1.11k
  }
2065
1.11k
  case APValue::AddrLabelDiff: {
2066
5
    const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
2067
5
    const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
2068
5
    llvm::Constant *LHS = tryEmitPrivate(LHSExpr, LHSExpr->getType());
2069
5
    llvm::Constant *RHS = tryEmitPrivate(RHSExpr, RHSExpr->getType());
2070
5
    if (!LHS || !RHS) 
return nullptr0
;
2071
5
2072
5
    // Compute difference
2073
5
    llvm::Type *ResultType = CGM.getTypes().ConvertType(DestType);
2074
5
    LHS = llvm::ConstantExpr::getPtrToInt(LHS, CGM.IntPtrTy);
2075
5
    RHS = llvm::ConstantExpr::getPtrToInt(RHS, CGM.IntPtrTy);
2076
5
    llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
2077
5
2078
5
    // LLVM is a bit sensitive about the exact format of the
2079
5
    // address-of-label difference; make sure to truncate after
2080
5
    // the subtraction.
2081
5
    return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
2082
5
  }
2083
3.07k
  case APValue::Struct:
2084
3.07k
  case APValue::Union:
2085
3.07k
    return ConstStructBuilder::BuildStruct(*this, Value, DestType);
2086
3.07k
  case APValue::Array: {
2087
2.83k
    const ConstantArrayType *CAT =
2088
2.83k
        CGM.getContext().getAsConstantArrayType(DestType);
2089
2.83k
    unsigned NumElements = Value.getArraySize();
2090
2.83k
    unsigned NumInitElts = Value.getArrayInitializedElts();
2091
2.83k
2092
2.83k
    // Emit array filler, if there is one.
2093
2.83k
    llvm::Constant *Filler = nullptr;
2094
2.83k
    if (Value.hasArrayFiller()) {
2095
533
      Filler = tryEmitAbstractForMemory(Value.getArrayFiller(),
2096
533
                                        CAT->getElementType());
2097
533
      if (!Filler)
2098
0
        return nullptr;
2099
2.83k
    }
2100
2.83k
2101
2.83k
    // Emit initializer elements.
2102
2.83k
    SmallVector<llvm::Constant*, 16> Elts;
2103
2.83k
    if (Filler && 
Filler->isNullValue()533
)
2104
528
      Elts.reserve(NumInitElts + 1);
2105
2.30k
    else
2106
2.30k
      Elts.reserve(NumElements);
2107
2.83k
2108
2.83k
    llvm::Type *CommonElementType = nullptr;
2109
11.7k
    for (unsigned I = 0; I < NumInitElts; 
++I8.87k
) {
2110
8.87k
      llvm::Constant *C = tryEmitPrivateForMemory(
2111
8.87k
          Value.getArrayInitializedElt(I), CAT->getElementType());
2112
8.87k
      if (!C) 
return nullptr0
;
2113
8.87k
2114
8.87k
      if (I == 0)
2115
2.61k
        CommonElementType = C->getType();
2116
6.25k
      else if (C->getType() != CommonElementType)
2117
7
        CommonElementType = nullptr;
2118
8.87k
      Elts.push_back(C);
2119
8.87k
    }
2120
2.83k
2121
2.83k
    // This means that the array type is probably "IncompleteType" or some
2122
2.83k
    // type that is not ConstantArray.
2123
2.83k
    if (CAT == nullptr && 
CommonElementType == nullptr3
&&
!NumInitElts3
) {
2124
3
      const ArrayType *AT = CGM.getContext().getAsArrayType(DestType);
2125
3
      CommonElementType = CGM.getTypes().ConvertType(AT->getElementType());
2126
3
      llvm::ArrayType *AType = llvm::ArrayType::get(CommonElementType,
2127
3
                                                    NumElements);
2128
3
      return llvm::ConstantAggregateZero::get(AType);
2129
3
    }
2130
2.82k
2131
2.82k
    llvm::ArrayType *Desired =
2132
2.82k
        cast<llvm::ArrayType>(CGM.getTypes().ConvertType(DestType));
2133
2.82k
    return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
2134
2.82k
                             Filler);
2135
2.82k
  }
2136
2.82k
  case APValue::MemberPointer:
2137
263
    return CGM.getCXXABI().EmitMemberPointer(Value, DestType);
2138
0
  }
2139
0
  llvm_unreachable("Unknown APValue kind");
2140
0
}
2141
2142
llvm::GlobalVariable *CodeGenModule::getAddrOfConstantCompoundLiteralIfEmitted(
2143
17
    const CompoundLiteralExpr *E) {
2144
17
  return EmittedCompoundLiterals.lookup(E);
2145
17
}
2146
2147
void CodeGenModule::setAddrOfConstantCompoundLiteral(
2148
15
    const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV) {
2149
15
  bool Ok = EmittedCompoundLiterals.insert(std::make_pair(CLE, GV)).second;
2150
15
  (void)Ok;
2151
15
  assert(Ok && "CLE has already been emitted!");
2152
15
}
2153
2154
ConstantAddress
2155
3
CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
2156
3
  assert(E->isFileScope() && "not a file-scope compound literal expr");
2157
3
  return tryEmitGlobalCompoundLiteral(*this, nullptr, E);
2158
3
}
2159
2160
llvm::Constant *
2161
606
CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
2162
606
  // Member pointer constants always have a very particular form.
2163
606
  const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
2164
606
  const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
2165
606
2166
606
  // A member function pointer.
2167
606
  if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
2168
553
    return getCXXABI().EmitMemberFunctionPointer(method);
2169
53
2170
53
  // Otherwise, a member data pointer.
2171
53
  uint64_t fieldOffset = getContext().getFieldOffset(decl);
2172
53
  CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
2173
53
  return getCXXABI().EmitMemberDataPointer(type, chars);
2174
53
}
2175
2176
static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2177
                                               llvm::Type *baseType,
2178
                                               const CXXRecordDecl *base);
2179
2180
static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
2181
                                        const RecordDecl *record,
2182
65
                                        bool asCompleteObject) {
2183
65
  const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
2184
65
  llvm::StructType *structure =
2185
65
    (asCompleteObject ? 
layout.getLLVMType()38
2186
65
                      : 
layout.getBaseSubobjectLLVMType()27
);
2187
65
2188
65
  unsigned numElements = structure->getNumElements();
2189
65
  std::vector<llvm::Constant *> elements(numElements);
2190
65
2191
65
  auto CXXR = dyn_cast<CXXRecordDecl>(record);
2192
65
  // Fill in all the bases.
2193
65
  if (CXXR) {
2194
53
    for (const auto &I : CXXR->bases()) {
2195
25
      if (I.isVirtual()) {
2196
17
        // Ignore virtual bases; if we're laying out for a complete
2197
17
        // object, we'll lay these out later.
2198
17
        continue;
2199
17
      }
2200
8
2201
8
      const CXXRecordDecl *base =
2202
8
        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2203
8
2204
8
      // Ignore empty bases.
2205
8
      if (base->isEmpty() ||
2206
8
          CGM.getContext().getASTRecordLayout(base).getNonVirtualSize()
2207
8
              .isZero())
2208
1
        continue;
2209
7
2210
7
      unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
2211
7
      llvm::Type *baseType = structure->getElementType(fieldIndex);
2212
7
      elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2213
7
    }
2214
53
  }
2215
65
2216
65
  // Fill in all the fields.
2217
119
  for (const auto *Field : record->fields()) {
2218
119
    // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
2219
119
    // will fill in later.)
2220
119
    if (!Field->isBitField() && 
!Field->isZeroSize(CGM.getContext())117
) {
2221
117
      unsigned fieldIndex = layout.getLLVMFieldNo(Field);
2222
117
      elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
2223
117
    }
2224
119
2225
119
    // For unions, stop after the first named field.
2226
119
    if (record->isUnion()) {
2227
4
      if (Field->getIdentifier())
2228
3
        break;
2229
1
      if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
2230
1
        if (FieldRD->findFirstNamedDataMember())
2231
1
          break;
2232
1
    }
2233
119
  }
2234
65
2235
65
  // Fill in the virtual bases, if we're working with the complete object.
2236
65
  if (CXXR && 
asCompleteObject53
) {
2237
26
    for (const auto &I : CXXR->vbases()) {
2238
5
      const CXXRecordDecl *base =
2239
5
        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2240
5
2241
5
      // Ignore empty bases.
2242
5
      if (base->isEmpty())
2243
0
        continue;
2244
5
2245
5
      unsigned fieldIndex = layout.getVirtualBaseIndex(base);
2246
5
2247
5
      // We might have already laid this field out.
2248
5
      if (elements[fieldIndex]) 
continue0
;
2249
5
2250
5
      llvm::Type *baseType = structure->getElementType(fieldIndex);
2251
5
      elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2252
5
    }
2253
26
  }
2254
65
2255
65
  // Now go through all other fields and zero them out.
2256
223
  for (unsigned i = 0; i != numElements; 
++i158
) {
2257
158
    if (!elements[i])
2258
29
      elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
2259
158
  }
2260
65
2261
65
  return llvm::ConstantStruct::get(structure, elements);
2262
65
}
2263
2264
/// Emit the null constant for a base subobject.
2265
static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2266
                                               llvm::Type *baseType,
2267
12
                                               const CXXRecordDecl *base) {
2268
12
  const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
2269
12
2270
12
  // Just zero out bases that don't have any pointer to data members.
2271
12
  if (baseLayout.isZeroInitializableAsBase())
2272
4
    return llvm::Constant::getNullValue(baseType);
2273
8
2274
8
  // Otherwise, we can just use its null constant.
2275
8
  return EmitNullConstant(CGM, base, /*asCompleteObject=*/false);
2276
8
}
2277
2278
llvm::Constant *ConstantEmitter::emitNullForMemory(CodeGenModule &CGM,
2279
56
                                                   QualType T) {
2280
56
  return emitForMemory(CGM, CGM.EmitNullConstant(T), T);
2281
56
}
2282
2283
28.3k
llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
2284
28.3k
  if (T->getAs<PointerType>())
2285
7.05k
    return getNullPointer(
2286
7.05k
        cast<llvm::PointerType>(getTypes().ConvertTypeForMem(T)), T);
2287
21.3k
2288
21.3k
  if (getTypes().isZeroInitializable(T))
2289
21.1k
    return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
2290
137
2291
137
  if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
2292
10
    llvm::ArrayType *ATy =
2293
10
      cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
2294
10
2295
10
    QualType ElementTy = CAT->getElementType();
2296
10
2297
10
    llvm::Constant *Element =
2298
10
      ConstantEmitter::emitNullForMemory(*this, ElementTy);
2299
10
    unsigned NumElements = CAT->getSize().getZExtValue();
2300
10
    SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
2301
10
    return llvm::ConstantArray::get(ATy, Array);
2302
10
  }
2303
127
2304
127
  if (const RecordType *RT = T->getAs<RecordType>())
2305
38
    return ::EmitNullConstant(*this, RT->getDecl(), /*complete object*/ true);
2306
89
2307
89
  assert(T->isMemberDataPointerType() &&
2308
89
         "Should only see pointers to data members here!");
2309
89
2310
89
  return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
2311
89
}
2312
2313
llvm::Constant *
2314
19
CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
2315
19
  return ::EmitNullConstant(*this, Record, false);
2316
19
}