Coverage Report

Created: 2019-07-24 05:18

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