Coverage Report

Created: 2022-07-16 07:03

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