xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGNonTrivialStruct.cpp (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1 //===--- CGNonTrivialStruct.cpp - Emit Special Functions for C Structs ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines functions to generate various special functions for C
10 // structs.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "clang/AST/NonTrivialTypeVisitor.h"
17 #include "clang/CodeGen/CodeGenABITypes.h"
18 #include "llvm/Support/ScopedPrinter.h"
19 #include <array>
20 
21 using namespace clang;
22 using namespace CodeGen;
23 
24 // Return the size of a field in number of bits.
25 static uint64_t getFieldSize(const FieldDecl *FD, QualType FT,
26                              ASTContext &Ctx) {
27   if (FD && FD->isBitField())
28     return FD->getBitWidthValue(Ctx);
29   return Ctx.getTypeSize(FT);
30 }
31 
32 namespace {
33 enum { DstIdx = 0, SrcIdx = 1 };
34 const char *ValNameStr[2] = {"dst", "src"};
35 
36 template <class Derived> struct StructVisitor {
37   StructVisitor(ASTContext &Ctx) : Ctx(Ctx) {}
38 
39   template <class... Ts>
40   void visitStructFields(QualType QT, CharUnits CurStructOffset, Ts... Args) {
41     const RecordDecl *RD = QT->castAs<RecordType>()->getDecl();
42 
43     // Iterate over the fields of the struct.
44     for (const FieldDecl *FD : RD->fields()) {
45       QualType FT = FD->getType();
46       FT = QT.isVolatileQualified() ? FT.withVolatile() : FT;
47       asDerived().visit(FT, FD, CurStructOffset, Args...);
48     }
49 
50     asDerived().flushTrivialFields(Args...);
51   }
52 
53   template <class... Ts> void visitTrivial(Ts... Args) {}
54 
55   template <class... Ts> void visitCXXDestructor(Ts... Args) {
56     llvm_unreachable("field of a C++ struct type is not expected");
57   }
58 
59   template <class... Ts> void flushTrivialFields(Ts... Args) {}
60 
61   uint64_t getFieldOffsetInBits(const FieldDecl *FD) {
62     return FD ? Ctx.getASTRecordLayout(FD->getParent())
63                     .getFieldOffset(FD->getFieldIndex())
64               : 0;
65   }
66 
67   CharUnits getFieldOffset(const FieldDecl *FD) {
68     return Ctx.toCharUnitsFromBits(getFieldOffsetInBits(FD));
69   }
70 
71   Derived &asDerived() { return static_cast<Derived &>(*this); }
72 
73   ASTContext &getContext() { return Ctx; }
74   ASTContext &Ctx;
75 };
76 
77 template <class Derived, bool IsMove>
78 struct CopyStructVisitor : StructVisitor<Derived>,
79                            CopiedTypeVisitor<Derived, IsMove> {
80   using StructVisitor<Derived>::asDerived;
81   using Super = CopiedTypeVisitor<Derived, IsMove>;
82 
83   CopyStructVisitor(ASTContext &Ctx) : StructVisitor<Derived>(Ctx) {}
84 
85   template <class... Ts>
86   void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT,
87                 const FieldDecl *FD, CharUnits CurStructOffset, Ts &&... Args) {
88     if (PCK)
89       asDerived().flushTrivialFields(std::forward<Ts>(Args)...);
90   }
91 
92   template <class... Ts>
93   void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT,
94                      const FieldDecl *FD, CharUnits CurStructOffset,
95                      Ts &&... Args) {
96     if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) {
97       asDerived().visitArray(PCK, AT, FT.isVolatileQualified(), FD,
98                              CurStructOffset, std::forward<Ts>(Args)...);
99       return;
100     }
101 
102     Super::visitWithKind(PCK, FT, FD, CurStructOffset,
103                          std::forward<Ts>(Args)...);
104   }
105 
106   template <class... Ts>
107   void visitTrivial(QualType FT, const FieldDecl *FD, CharUnits CurStructOffset,
108                     Ts... Args) {
109     assert(!FT.isVolatileQualified() && "volatile field not expected");
110     ASTContext &Ctx = asDerived().getContext();
111     uint64_t FieldSize = getFieldSize(FD, FT, Ctx);
112 
113     // Ignore zero-sized fields.
114     if (FieldSize == 0)
115       return;
116 
117     uint64_t FStartInBits = asDerived().getFieldOffsetInBits(FD);
118     uint64_t FEndInBits = FStartInBits + FieldSize;
119     uint64_t RoundedFEnd = llvm::alignTo(FEndInBits, Ctx.getCharWidth());
120 
121     // Set Start if this is the first field of a sequence of trivial fields.
122     if (Start == End)
123       Start = CurStructOffset + Ctx.toCharUnitsFromBits(FStartInBits);
124     End = CurStructOffset + Ctx.toCharUnitsFromBits(RoundedFEnd);
125   }
126 
127   CharUnits Start = CharUnits::Zero(), End = CharUnits::Zero();
128 };
129 
130 // This function creates the mangled name of a special function of a non-trivial
131 // C struct. Since there is no ODR in C, the function is mangled based on the
132 // struct contents and not the name. The mangled name has the following
133 // structure:
134 //
135 // <function-name> ::= <prefix> <alignment-info> "_" <struct-field-info>
136 // <prefix> ::= "__destructor_" | "__default_constructor_" |
137 //              "__copy_constructor_" | "__move_constructor_" |
138 //              "__copy_assignment_" | "__move_assignment_"
139 // <alignment-info> ::= <dst-alignment> ["_" <src-alignment>]
140 // <struct-field-info> ::= <field-info>+
141 // <field-info> ::= <struct-or-scalar-field-info> | <array-field-info>
142 // <struct-or-scalar-field-info> ::= "_S" <struct-field-info> |
143 //                                   <strong-field-info> | <trivial-field-info>
144 // <array-field-info> ::= "_AB" <array-offset> "s" <element-size> "n"
145 //                        <num-elements> <innermost-element-info> "_AE"
146 // <innermost-element-info> ::= <struct-or-scalar-field-info>
147 // <strong-field-info> ::= "_s" ["b"] ["v"] <field-offset>
148 // <trivial-field-info> ::= "_t" ["v"] <field-offset> "_" <field-size>
149 
150 template <class Derived> struct GenFuncNameBase {
151   std::string getVolatileOffsetStr(bool IsVolatile, CharUnits Offset) {
152     std::string S;
153     if (IsVolatile)
154       S = "v";
155     S += llvm::to_string(Offset.getQuantity());
156     return S;
157   }
158 
159   void visitARCStrong(QualType FT, const FieldDecl *FD,
160                       CharUnits CurStructOffset) {
161     appendStr("_s");
162     if (FT->isBlockPointerType())
163       appendStr("b");
164     CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
165     appendStr(getVolatileOffsetStr(FT.isVolatileQualified(), FieldOffset));
166   }
167 
168   void visitARCWeak(QualType FT, const FieldDecl *FD,
169                     CharUnits CurStructOffset) {
170     appendStr("_w");
171     CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
172     appendStr(getVolatileOffsetStr(FT.isVolatileQualified(), FieldOffset));
173   }
174 
175   void visitStruct(QualType QT, const FieldDecl *FD,
176                    CharUnits CurStructOffset) {
177     CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
178     appendStr("_S");
179     asDerived().visitStructFields(QT, FieldOffset);
180   }
181 
182   template <class FieldKind>
183   void visitArray(FieldKind FK, const ArrayType *AT, bool IsVolatile,
184                   const FieldDecl *FD, CharUnits CurStructOffset) {
185     // String for non-volatile trivial fields is emitted when
186     // flushTrivialFields is called.
187     if (!FK)
188       return asDerived().visitTrivial(QualType(AT, 0), FD, CurStructOffset);
189 
190     asDerived().flushTrivialFields();
191     CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
192     ASTContext &Ctx = asDerived().getContext();
193     const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
194     unsigned NumElts = Ctx.getConstantArrayElementCount(CAT);
195     QualType EltTy = Ctx.getBaseElementType(CAT);
196     CharUnits EltSize = Ctx.getTypeSizeInChars(EltTy);
197     appendStr("_AB" + llvm::to_string(FieldOffset.getQuantity()) + "s" +
198               llvm::to_string(EltSize.getQuantity()) + "n" +
199               llvm::to_string(NumElts));
200     EltTy = IsVolatile ? EltTy.withVolatile() : EltTy;
201     asDerived().visitWithKind(FK, EltTy, nullptr, FieldOffset);
202     appendStr("_AE");
203   }
204 
205   void appendStr(StringRef Str) { Name += Str; }
206 
207   std::string getName(QualType QT, bool IsVolatile) {
208     QT = IsVolatile ? QT.withVolatile() : QT;
209     asDerived().visitStructFields(QT, CharUnits::Zero());
210     return Name;
211   }
212 
213   Derived &asDerived() { return static_cast<Derived &>(*this); }
214 
215   std::string Name;
216 };
217 
218 template <class Derived>
219 struct GenUnaryFuncName : StructVisitor<Derived>, GenFuncNameBase<Derived> {
220   GenUnaryFuncName(StringRef Prefix, CharUnits DstAlignment, ASTContext &Ctx)
221       : StructVisitor<Derived>(Ctx) {
222     this->appendStr(Prefix);
223     this->appendStr(llvm::to_string(DstAlignment.getQuantity()));
224   }
225 };
226 
227 // Helper function to create a null constant.
228 static llvm::Constant *getNullForVariable(Address Addr) {
229   llvm::Type *Ty = Addr.getElementType();
230   return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(Ty));
231 }
232 
233 template <bool IsMove>
234 struct GenBinaryFuncName : CopyStructVisitor<GenBinaryFuncName<IsMove>, IsMove>,
235                            GenFuncNameBase<GenBinaryFuncName<IsMove>> {
236 
237   GenBinaryFuncName(StringRef Prefix, CharUnits DstAlignment,
238                     CharUnits SrcAlignment, ASTContext &Ctx)
239       : CopyStructVisitor<GenBinaryFuncName<IsMove>, IsMove>(Ctx) {
240     this->appendStr(Prefix);
241     this->appendStr(llvm::to_string(DstAlignment.getQuantity()));
242     this->appendStr("_" + llvm::to_string(SrcAlignment.getQuantity()));
243   }
244 
245   void flushTrivialFields() {
246     if (this->Start == this->End)
247       return;
248 
249     this->appendStr("_t" + llvm::to_string(this->Start.getQuantity()) + "w" +
250                     llvm::to_string((this->End - this->Start).getQuantity()));
251 
252     this->Start = this->End = CharUnits::Zero();
253   }
254 
255   void visitVolatileTrivial(QualType FT, const FieldDecl *FD,
256                             CharUnits CurStructOffset) {
257     // Zero-length bit-fields don't need to be copied/assigned.
258     if (FD && FD->isZeroLengthBitField(this->Ctx))
259       return;
260 
261     // Because volatile fields can be bit-fields and are individually copied,
262     // their offset and width are in bits.
263     uint64_t OffsetInBits =
264         this->Ctx.toBits(CurStructOffset) + this->getFieldOffsetInBits(FD);
265     this->appendStr("_tv" + llvm::to_string(OffsetInBits) + "w" +
266                     llvm::to_string(getFieldSize(FD, FT, this->Ctx)));
267   }
268 };
269 
270 struct GenDefaultInitializeFuncName
271     : GenUnaryFuncName<GenDefaultInitializeFuncName>,
272       DefaultInitializedTypeVisitor<GenDefaultInitializeFuncName> {
273   using Super = DefaultInitializedTypeVisitor<GenDefaultInitializeFuncName>;
274   GenDefaultInitializeFuncName(CharUnits DstAlignment, ASTContext &Ctx)
275       : GenUnaryFuncName<GenDefaultInitializeFuncName>("__default_constructor_",
276                                                        DstAlignment, Ctx) {}
277   void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT,
278                      const FieldDecl *FD, CharUnits CurStructOffset) {
279     if (const auto *AT = getContext().getAsArrayType(FT)) {
280       visitArray(PDIK, AT, FT.isVolatileQualified(), FD, CurStructOffset);
281       return;
282     }
283 
284     Super::visitWithKind(PDIK, FT, FD, CurStructOffset);
285   }
286 };
287 
288 struct GenDestructorFuncName : GenUnaryFuncName<GenDestructorFuncName>,
289                                DestructedTypeVisitor<GenDestructorFuncName> {
290   using Super = DestructedTypeVisitor<GenDestructorFuncName>;
291   GenDestructorFuncName(const char *Prefix, CharUnits DstAlignment,
292                         ASTContext &Ctx)
293       : GenUnaryFuncName<GenDestructorFuncName>(Prefix, DstAlignment, Ctx) {}
294   void visitWithKind(QualType::DestructionKind DK, QualType FT,
295                      const FieldDecl *FD, CharUnits CurStructOffset) {
296     if (const auto *AT = getContext().getAsArrayType(FT)) {
297       visitArray(DK, AT, FT.isVolatileQualified(), FD, CurStructOffset);
298       return;
299     }
300 
301     Super::visitWithKind(DK, FT, FD, CurStructOffset);
302   }
303 };
304 
305 // Helper function that creates CGFunctionInfo for an N-ary special function.
306 template <size_t N>
307 static const CGFunctionInfo &getFunctionInfo(CodeGenModule &CGM,
308                                              FunctionArgList &Args) {
309   ASTContext &Ctx = CGM.getContext();
310   llvm::SmallVector<ImplicitParamDecl *, N> Params;
311   QualType ParamTy = Ctx.getPointerType(Ctx.VoidPtrTy);
312 
313   for (unsigned I = 0; I < N; ++I)
314     Params.push_back(ImplicitParamDecl::Create(
315         Ctx, nullptr, SourceLocation(), &Ctx.Idents.get(ValNameStr[I]), ParamTy,
316         ImplicitParamDecl::Other));
317 
318   for (auto &P : Params)
319     Args.push_back(P);
320 
321   return CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
322 }
323 
324 template <size_t N, size_t... Ints>
325 static std::array<Address, N> getParamAddrs(std::index_sequence<Ints...> IntSeq,
326                                             std::array<CharUnits, N> Alignments,
327                                             FunctionArgList Args,
328                                             CodeGenFunction *CGF) {
329   return std::array<Address, N>{{
330       Address(CGF->Builder.CreateLoad(CGF->GetAddrOfLocalVar(Args[Ints])),
331               Alignments[Ints])...}};
332 }
333 
334 // Template classes that are used as bases for classes that emit special
335 // functions.
336 template <class Derived> struct GenFuncBase {
337   template <size_t N>
338   void visitStruct(QualType FT, const FieldDecl *FD, CharUnits CurStructOffset,
339                    std::array<Address, N> Addrs) {
340     this->asDerived().callSpecialFunction(
341         FT, CurStructOffset + asDerived().getFieldOffset(FD), Addrs);
342   }
343 
344   template <class FieldKind, size_t N>
345   void visitArray(FieldKind FK, const ArrayType *AT, bool IsVolatile,
346                   const FieldDecl *FD, CharUnits CurStructOffset,
347                   std::array<Address, N> Addrs) {
348     // Non-volatile trivial fields are copied when flushTrivialFields is called.
349     if (!FK)
350       return asDerived().visitTrivial(QualType(AT, 0), FD, CurStructOffset,
351                                       Addrs);
352 
353     asDerived().flushTrivialFields(Addrs);
354     CodeGenFunction &CGF = *this->CGF;
355     ASTContext &Ctx = CGF.getContext();
356 
357     // Compute the end address.
358     QualType BaseEltQT;
359     std::array<Address, N> StartAddrs = Addrs;
360     for (unsigned I = 0; I < N; ++I)
361       StartAddrs[I] = getAddrWithOffset(Addrs[I], CurStructOffset, FD);
362     Address DstAddr = StartAddrs[DstIdx];
363     llvm::Value *NumElts = CGF.emitArrayLength(AT, BaseEltQT, DstAddr);
364     unsigned BaseEltSize = Ctx.getTypeSizeInChars(BaseEltQT).getQuantity();
365     llvm::Value *BaseEltSizeVal =
366         llvm::ConstantInt::get(NumElts->getType(), BaseEltSize);
367     llvm::Value *SizeInBytes =
368         CGF.Builder.CreateNUWMul(BaseEltSizeVal, NumElts);
369     Address BC = CGF.Builder.CreateBitCast(DstAddr, CGF.CGM.Int8PtrTy);
370     llvm::Value *DstArrayEnd =
371         CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, BC.getPointer(), SizeInBytes);
372     DstArrayEnd = CGF.Builder.CreateBitCast(DstArrayEnd, CGF.CGM.Int8PtrPtrTy,
373                                             "dstarray.end");
374     llvm::BasicBlock *PreheaderBB = CGF.Builder.GetInsertBlock();
375 
376     // Create the header block and insert the phi instructions.
377     llvm::BasicBlock *HeaderBB = CGF.createBasicBlock("loop.header");
378     CGF.EmitBlock(HeaderBB);
379     llvm::PHINode *PHIs[N];
380 
381     for (unsigned I = 0; I < N; ++I) {
382       PHIs[I] = CGF.Builder.CreatePHI(CGF.CGM.Int8PtrPtrTy, 2, "addr.cur");
383       PHIs[I]->addIncoming(StartAddrs[I].getPointer(), PreheaderBB);
384     }
385 
386     // Create the exit and loop body blocks.
387     llvm::BasicBlock *ExitBB = CGF.createBasicBlock("loop.exit");
388     llvm::BasicBlock *LoopBB = CGF.createBasicBlock("loop.body");
389 
390     // Emit the comparison and conditional branch instruction that jumps to
391     // either the exit or the loop body.
392     llvm::Value *Done =
393         CGF.Builder.CreateICmpEQ(PHIs[DstIdx], DstArrayEnd, "done");
394     CGF.Builder.CreateCondBr(Done, ExitBB, LoopBB);
395 
396     // Visit the element of the array in the loop body.
397     CGF.EmitBlock(LoopBB);
398     QualType EltQT = AT->getElementType();
399     CharUnits EltSize = Ctx.getTypeSizeInChars(EltQT);
400     std::array<Address, N> NewAddrs = Addrs;
401 
402     for (unsigned I = 0; I < N; ++I)
403       NewAddrs[I] = Address(
404           PHIs[I], StartAddrs[I].getAlignment().alignmentAtOffset(EltSize));
405 
406     EltQT = IsVolatile ? EltQT.withVolatile() : EltQT;
407     this->asDerived().visitWithKind(FK, EltQT, nullptr, CharUnits::Zero(),
408                                     NewAddrs);
409 
410     LoopBB = CGF.Builder.GetInsertBlock();
411 
412     for (unsigned I = 0; I < N; ++I) {
413       // Instrs to update the destination and source addresses.
414       // Update phi instructions.
415       NewAddrs[I] = getAddrWithOffset(NewAddrs[I], EltSize);
416       PHIs[I]->addIncoming(NewAddrs[I].getPointer(), LoopBB);
417     }
418 
419     // Insert an unconditional branch to the header block.
420     CGF.Builder.CreateBr(HeaderBB);
421     CGF.EmitBlock(ExitBB);
422   }
423 
424   /// Return an address with the specified offset from the passed address.
425   Address getAddrWithOffset(Address Addr, CharUnits Offset) {
426     assert(Addr.isValid() && "invalid address");
427     if (Offset.getQuantity() == 0)
428       return Addr;
429     Addr = CGF->Builder.CreateBitCast(Addr, CGF->CGM.Int8PtrTy);
430     Addr = CGF->Builder.CreateConstInBoundsGEP(Addr, Offset.getQuantity());
431     return CGF->Builder.CreateBitCast(Addr, CGF->CGM.Int8PtrPtrTy);
432   }
433 
434   Address getAddrWithOffset(Address Addr, CharUnits StructFieldOffset,
435                             const FieldDecl *FD) {
436     return getAddrWithOffset(Addr, StructFieldOffset +
437                                        asDerived().getFieldOffset(FD));
438   }
439 
440   template <size_t N>
441   llvm::Function *getFunction(StringRef FuncName, QualType QT,
442                               std::array<CharUnits, N> Alignments,
443                               CodeGenModule &CGM) {
444     // If the special function already exists in the module, return it.
445     if (llvm::Function *F = CGM.getModule().getFunction(FuncName)) {
446       bool WrongType = false;
447       if (!F->getReturnType()->isVoidTy())
448         WrongType = true;
449       else {
450         for (const llvm::Argument &Arg : F->args())
451           if (Arg.getType() != CGM.Int8PtrPtrTy)
452             WrongType = true;
453       }
454 
455       if (WrongType) {
456         std::string FuncName = std::string(F->getName());
457         SourceLocation Loc = QT->castAs<RecordType>()->getDecl()->getLocation();
458         CGM.Error(Loc, "special function " + FuncName +
459                            " for non-trivial C struct has incorrect type");
460         return nullptr;
461       }
462       return F;
463     }
464 
465     ASTContext &Ctx = CGM.getContext();
466     FunctionArgList Args;
467     const CGFunctionInfo &FI = getFunctionInfo<N>(CGM, Args);
468     llvm::FunctionType *FuncTy = CGM.getTypes().GetFunctionType(FI);
469     llvm::Function *F =
470         llvm::Function::Create(FuncTy, llvm::GlobalValue::LinkOnceODRLinkage,
471                                FuncName, &CGM.getModule());
472     F->setVisibility(llvm::GlobalValue::HiddenVisibility);
473     CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F, /*IsThunk=*/false);
474     CGM.SetLLVMFunctionAttributesForDefinition(nullptr, F);
475     CodeGenFunction NewCGF(CGM);
476     setCGF(&NewCGF);
477     CGF->StartFunction(GlobalDecl(), Ctx.VoidTy, F, FI, Args);
478     auto AL = ApplyDebugLocation::CreateArtificial(*CGF);
479     std::array<Address, N> Addrs =
480         getParamAddrs<N>(std::make_index_sequence<N>{}, Alignments, Args, CGF);
481     asDerived().visitStructFields(QT, CharUnits::Zero(), Addrs);
482     CGF->FinishFunction();
483     return F;
484   }
485 
486   template <size_t N>
487   void callFunc(StringRef FuncName, QualType QT, std::array<Address, N> Addrs,
488                 CodeGenFunction &CallerCGF) {
489     std::array<CharUnits, N> Alignments;
490     llvm::Value *Ptrs[N];
491 
492     for (unsigned I = 0; I < N; ++I) {
493       Alignments[I] = Addrs[I].getAlignment();
494       Ptrs[I] =
495           CallerCGF.Builder.CreateBitCast(Addrs[I], CallerCGF.CGM.Int8PtrPtrTy)
496               .getPointer();
497     }
498 
499     if (llvm::Function *F =
500             getFunction(FuncName, QT, Alignments, CallerCGF.CGM))
501       CallerCGF.EmitNounwindRuntimeCall(F, Ptrs);
502   }
503 
504   Derived &asDerived() { return static_cast<Derived &>(*this); }
505 
506   void setCGF(CodeGenFunction *F) { CGF = F; }
507 
508   CodeGenFunction *CGF = nullptr;
509 };
510 
511 template <class Derived, bool IsMove>
512 struct GenBinaryFunc : CopyStructVisitor<Derived, IsMove>,
513                        GenFuncBase<Derived> {
514   GenBinaryFunc(ASTContext &Ctx) : CopyStructVisitor<Derived, IsMove>(Ctx) {}
515 
516   void flushTrivialFields(std::array<Address, 2> Addrs) {
517     CharUnits Size = this->End - this->Start;
518 
519     if (Size.getQuantity() == 0)
520       return;
521 
522     Address DstAddr = this->getAddrWithOffset(Addrs[DstIdx], this->Start);
523     Address SrcAddr = this->getAddrWithOffset(Addrs[SrcIdx], this->Start);
524 
525     // Emit memcpy.
526     if (Size.getQuantity() >= 16 || !llvm::isPowerOf2_32(Size.getQuantity())) {
527       llvm::Value *SizeVal =
528           llvm::ConstantInt::get(this->CGF->SizeTy, Size.getQuantity());
529       DstAddr =
530           this->CGF->Builder.CreateElementBitCast(DstAddr, this->CGF->Int8Ty);
531       SrcAddr =
532           this->CGF->Builder.CreateElementBitCast(SrcAddr, this->CGF->Int8Ty);
533       this->CGF->Builder.CreateMemCpy(DstAddr, SrcAddr, SizeVal, false);
534     } else {
535       llvm::Type *Ty = llvm::Type::getIntNTy(
536           this->CGF->getLLVMContext(),
537           Size.getQuantity() * this->CGF->getContext().getCharWidth());
538       DstAddr = this->CGF->Builder.CreateElementBitCast(DstAddr, Ty);
539       SrcAddr = this->CGF->Builder.CreateElementBitCast(SrcAddr, Ty);
540       llvm::Value *SrcVal = this->CGF->Builder.CreateLoad(SrcAddr, false);
541       this->CGF->Builder.CreateStore(SrcVal, DstAddr, false);
542     }
543 
544     this->Start = this->End = CharUnits::Zero();
545   }
546 
547   template <class... Ts>
548   void visitVolatileTrivial(QualType FT, const FieldDecl *FD, CharUnits Offset,
549                             std::array<Address, 2> Addrs) {
550     LValue DstLV, SrcLV;
551     if (FD) {
552       // No need to copy zero-length bit-fields.
553       if (FD->isZeroLengthBitField(this->CGF->getContext()))
554         return;
555 
556       QualType RT = QualType(FD->getParent()->getTypeForDecl(), 0);
557       llvm::PointerType *PtrTy = this->CGF->ConvertType(RT)->getPointerTo();
558       Address DstAddr = this->getAddrWithOffset(Addrs[DstIdx], Offset);
559       LValue DstBase = this->CGF->MakeAddrLValue(
560           this->CGF->Builder.CreateBitCast(DstAddr, PtrTy), FT);
561       DstLV = this->CGF->EmitLValueForField(DstBase, FD);
562       Address SrcAddr = this->getAddrWithOffset(Addrs[SrcIdx], Offset);
563       LValue SrcBase = this->CGF->MakeAddrLValue(
564           this->CGF->Builder.CreateBitCast(SrcAddr, PtrTy), FT);
565       SrcLV = this->CGF->EmitLValueForField(SrcBase, FD);
566     } else {
567       llvm::PointerType *Ty = this->CGF->ConvertTypeForMem(FT)->getPointerTo();
568       Address DstAddr = this->CGF->Builder.CreateBitCast(Addrs[DstIdx], Ty);
569       Address SrcAddr = this->CGF->Builder.CreateBitCast(Addrs[SrcIdx], Ty);
570       DstLV = this->CGF->MakeAddrLValue(DstAddr, FT);
571       SrcLV = this->CGF->MakeAddrLValue(SrcAddr, FT);
572     }
573     RValue SrcVal = this->CGF->EmitLoadOfLValue(SrcLV, SourceLocation());
574     this->CGF->EmitStoreThroughLValue(SrcVal, DstLV);
575   }
576 };
577 
578 // These classes that emit the special functions for a non-trivial struct.
579 struct GenDestructor : StructVisitor<GenDestructor>,
580                        GenFuncBase<GenDestructor>,
581                        DestructedTypeVisitor<GenDestructor> {
582   using Super = DestructedTypeVisitor<GenDestructor>;
583   GenDestructor(ASTContext &Ctx) : StructVisitor<GenDestructor>(Ctx) {}
584 
585   void visitWithKind(QualType::DestructionKind DK, QualType FT,
586                      const FieldDecl *FD, CharUnits CurStructOffset,
587                      std::array<Address, 1> Addrs) {
588     if (const auto *AT = getContext().getAsArrayType(FT)) {
589       visitArray(DK, AT, FT.isVolatileQualified(), FD, CurStructOffset, Addrs);
590       return;
591     }
592 
593     Super::visitWithKind(DK, FT, FD, CurStructOffset, Addrs);
594   }
595 
596   void visitARCStrong(QualType QT, const FieldDecl *FD,
597                       CharUnits CurStructOffset, std::array<Address, 1> Addrs) {
598     CGF->destroyARCStrongImprecise(
599         *CGF, getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
600   }
601 
602   void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
603                     std::array<Address, 1> Addrs) {
604     CGF->destroyARCWeak(
605         *CGF, getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
606   }
607 
608   void callSpecialFunction(QualType FT, CharUnits Offset,
609                            std::array<Address, 1> Addrs) {
610     CGF->callCStructDestructor(
611         CGF->MakeAddrLValue(getAddrWithOffset(Addrs[DstIdx], Offset), FT));
612   }
613 };
614 
615 struct GenDefaultInitialize
616     : StructVisitor<GenDefaultInitialize>,
617       GenFuncBase<GenDefaultInitialize>,
618       DefaultInitializedTypeVisitor<GenDefaultInitialize> {
619   using Super = DefaultInitializedTypeVisitor<GenDefaultInitialize>;
620   typedef GenFuncBase<GenDefaultInitialize> GenFuncBaseTy;
621 
622   GenDefaultInitialize(ASTContext &Ctx)
623       : StructVisitor<GenDefaultInitialize>(Ctx) {}
624 
625   void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT,
626                      const FieldDecl *FD, CharUnits CurStructOffset,
627                      std::array<Address, 1> Addrs) {
628     if (const auto *AT = getContext().getAsArrayType(FT)) {
629       visitArray(PDIK, AT, FT.isVolatileQualified(), FD, CurStructOffset,
630                  Addrs);
631       return;
632     }
633 
634     Super::visitWithKind(PDIK, FT, FD, CurStructOffset, Addrs);
635   }
636 
637   void visitARCStrong(QualType QT, const FieldDecl *FD,
638                       CharUnits CurStructOffset, std::array<Address, 1> Addrs) {
639     CGF->EmitNullInitialization(
640         getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
641   }
642 
643   void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
644                     std::array<Address, 1> Addrs) {
645     CGF->EmitNullInitialization(
646         getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
647   }
648 
649   template <class FieldKind, size_t... Is>
650   void visitArray(FieldKind FK, const ArrayType *AT, bool IsVolatile,
651                   const FieldDecl *FD, CharUnits CurStructOffset,
652                   std::array<Address, 1> Addrs) {
653     if (!FK)
654       return visitTrivial(QualType(AT, 0), FD, CurStructOffset, Addrs);
655 
656     ASTContext &Ctx = getContext();
657     CharUnits Size = Ctx.getTypeSizeInChars(QualType(AT, 0));
658     QualType EltTy = Ctx.getBaseElementType(QualType(AT, 0));
659 
660     if (Size < CharUnits::fromQuantity(16) || EltTy->getAs<RecordType>()) {
661       GenFuncBaseTy::visitArray(FK, AT, IsVolatile, FD, CurStructOffset, Addrs);
662       return;
663     }
664 
665     llvm::Constant *SizeVal = CGF->Builder.getInt64(Size.getQuantity());
666     Address DstAddr = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
667     Address Loc = CGF->Builder.CreateElementBitCast(DstAddr, CGF->Int8Ty);
668     CGF->Builder.CreateMemSet(Loc, CGF->Builder.getInt8(0), SizeVal,
669                               IsVolatile);
670   }
671 
672   void callSpecialFunction(QualType FT, CharUnits Offset,
673                            std::array<Address, 1> Addrs) {
674     CGF->callCStructDefaultConstructor(
675         CGF->MakeAddrLValue(getAddrWithOffset(Addrs[DstIdx], Offset), FT));
676   }
677 };
678 
679 struct GenCopyConstructor : GenBinaryFunc<GenCopyConstructor, false> {
680   GenCopyConstructor(ASTContext &Ctx)
681       : GenBinaryFunc<GenCopyConstructor, false>(Ctx) {}
682 
683   void visitARCStrong(QualType QT, const FieldDecl *FD,
684                       CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
685     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
686     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
687     llvm::Value *SrcVal = CGF->EmitLoadOfScalar(
688         Addrs[SrcIdx], QT.isVolatileQualified(), QT, SourceLocation());
689     llvm::Value *Val = CGF->EmitARCRetain(QT, SrcVal);
690     CGF->EmitStoreOfScalar(Val, CGF->MakeAddrLValue(Addrs[DstIdx], QT), true);
691   }
692 
693   void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
694                     std::array<Address, 2> Addrs) {
695     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
696     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
697     CGF->EmitARCCopyWeak(Addrs[DstIdx], Addrs[SrcIdx]);
698   }
699 
700   void callSpecialFunction(QualType FT, CharUnits Offset,
701                            std::array<Address, 2> Addrs) {
702     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
703     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
704     CGF->callCStructCopyConstructor(CGF->MakeAddrLValue(Addrs[DstIdx], FT),
705                                     CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
706   }
707 };
708 
709 struct GenMoveConstructor : GenBinaryFunc<GenMoveConstructor, true> {
710   GenMoveConstructor(ASTContext &Ctx)
711       : GenBinaryFunc<GenMoveConstructor, true>(Ctx) {}
712 
713   void visitARCStrong(QualType QT, const FieldDecl *FD,
714                       CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
715     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
716     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
717     LValue SrcLV = CGF->MakeAddrLValue(Addrs[SrcIdx], QT);
718     llvm::Value *SrcVal =
719         CGF->EmitLoadOfLValue(SrcLV, SourceLocation()).getScalarVal();
720     CGF->EmitStoreOfScalar(getNullForVariable(SrcLV.getAddress(*CGF)), SrcLV);
721     CGF->EmitStoreOfScalar(SrcVal, CGF->MakeAddrLValue(Addrs[DstIdx], QT),
722                            /* isInitialization */ true);
723   }
724 
725   void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
726                     std::array<Address, 2> Addrs) {
727     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
728     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
729     CGF->EmitARCMoveWeak(Addrs[DstIdx], Addrs[SrcIdx]);
730   }
731 
732   void callSpecialFunction(QualType FT, CharUnits Offset,
733                            std::array<Address, 2> Addrs) {
734     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
735     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
736     CGF->callCStructMoveConstructor(CGF->MakeAddrLValue(Addrs[DstIdx], FT),
737                                     CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
738   }
739 };
740 
741 struct GenCopyAssignment : GenBinaryFunc<GenCopyAssignment, false> {
742   GenCopyAssignment(ASTContext &Ctx)
743       : GenBinaryFunc<GenCopyAssignment, false>(Ctx) {}
744 
745   void visitARCStrong(QualType QT, const FieldDecl *FD,
746                       CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
747     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
748     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
749     llvm::Value *SrcVal = CGF->EmitLoadOfScalar(
750         Addrs[SrcIdx], QT.isVolatileQualified(), QT, SourceLocation());
751     CGF->EmitARCStoreStrong(CGF->MakeAddrLValue(Addrs[DstIdx], QT), SrcVal,
752                             false);
753   }
754 
755   void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
756                     std::array<Address, 2> Addrs) {
757     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
758     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
759     CGF->emitARCCopyAssignWeak(QT, Addrs[DstIdx], Addrs[SrcIdx]);
760   }
761 
762   void callSpecialFunction(QualType FT, CharUnits Offset,
763                            std::array<Address, 2> Addrs) {
764     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
765     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
766     CGF->callCStructCopyAssignmentOperator(
767         CGF->MakeAddrLValue(Addrs[DstIdx], FT),
768         CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
769   }
770 };
771 
772 struct GenMoveAssignment : GenBinaryFunc<GenMoveAssignment, true> {
773   GenMoveAssignment(ASTContext &Ctx)
774       : GenBinaryFunc<GenMoveAssignment, true>(Ctx) {}
775 
776   void visitARCStrong(QualType QT, const FieldDecl *FD,
777                       CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
778     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
779     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
780     LValue SrcLV = CGF->MakeAddrLValue(Addrs[SrcIdx], QT);
781     llvm::Value *SrcVal =
782         CGF->EmitLoadOfLValue(SrcLV, SourceLocation()).getScalarVal();
783     CGF->EmitStoreOfScalar(getNullForVariable(SrcLV.getAddress(*CGF)), SrcLV);
784     LValue DstLV = CGF->MakeAddrLValue(Addrs[DstIdx], QT);
785     llvm::Value *DstVal =
786         CGF->EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal();
787     CGF->EmitStoreOfScalar(SrcVal, DstLV);
788     CGF->EmitARCRelease(DstVal, ARCImpreciseLifetime);
789   }
790 
791   void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
792                     std::array<Address, 2> Addrs) {
793     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
794     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
795     CGF->emitARCMoveAssignWeak(QT, Addrs[DstIdx], Addrs[SrcIdx]);
796   }
797 
798   void callSpecialFunction(QualType FT, CharUnits Offset,
799                            std::array<Address, 2> Addrs) {
800     Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
801     Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
802     CGF->callCStructMoveAssignmentOperator(
803         CGF->MakeAddrLValue(Addrs[DstIdx], FT),
804         CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
805   }
806 };
807 
808 } // namespace
809 
810 void CodeGenFunction::destroyNonTrivialCStruct(CodeGenFunction &CGF,
811                                                Address Addr, QualType Type) {
812   CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Type));
813 }
814 
815 // Default-initialize a variable that is a non-trivial struct or an array of
816 // such structure.
817 void CodeGenFunction::defaultInitNonTrivialCStructVar(LValue Dst) {
818   GenDefaultInitialize Gen(getContext());
819   Address DstPtr =
820       Builder.CreateBitCast(Dst.getAddress(*this), CGM.Int8PtrPtrTy);
821   Gen.setCGF(this);
822   QualType QT = Dst.getType();
823   QT = Dst.isVolatile() ? QT.withVolatile() : QT;
824   Gen.visit(QT, nullptr, CharUnits::Zero(), std::array<Address, 1>({{DstPtr}}));
825 }
826 
827 template <class G, size_t N>
828 static void callSpecialFunction(G &&Gen, StringRef FuncName, QualType QT,
829                                 bool IsVolatile, CodeGenFunction &CGF,
830                                 std::array<Address, N> Addrs) {
831   auto SetArtificialLoc = ApplyDebugLocation::CreateArtificial(CGF);
832   for (unsigned I = 0; I < N; ++I)
833     Addrs[I] = CGF.Builder.CreateBitCast(Addrs[I], CGF.CGM.Int8PtrPtrTy);
834   QT = IsVolatile ? QT.withVolatile() : QT;
835   Gen.callFunc(FuncName, QT, Addrs, CGF);
836 }
837 
838 template <class G, size_t N>
839 static llvm::Function *
840 getSpecialFunction(G &&Gen, StringRef FuncName, QualType QT, bool IsVolatile,
841                    std::array<CharUnits, N> Alignments, CodeGenModule &CGM) {
842   QT = IsVolatile ? QT.withVolatile() : QT;
843   // The following call requires an array of addresses as arguments, but doesn't
844   // actually use them (it overwrites them with the addresses of the arguments
845   // of the created function).
846   return Gen.getFunction(FuncName, QT, Alignments, CGM);
847 }
848 
849 // Functions to emit calls to the special functions of a non-trivial C struct.
850 void CodeGenFunction::callCStructDefaultConstructor(LValue Dst) {
851   bool IsVolatile = Dst.isVolatile();
852   Address DstPtr = Dst.getAddress(*this);
853   QualType QT = Dst.getType();
854   GenDefaultInitializeFuncName GenName(DstPtr.getAlignment(), getContext());
855   std::string FuncName = GenName.getName(QT, IsVolatile);
856   callSpecialFunction(GenDefaultInitialize(getContext()), FuncName, QT,
857                       IsVolatile, *this, std::array<Address, 1>({{DstPtr}}));
858 }
859 
860 std::string CodeGenFunction::getNonTrivialCopyConstructorStr(
861     QualType QT, CharUnits Alignment, bool IsVolatile, ASTContext &Ctx) {
862   GenBinaryFuncName<false> GenName("", Alignment, Alignment, Ctx);
863   return GenName.getName(QT, IsVolatile);
864 }
865 
866 std::string CodeGenFunction::getNonTrivialDestructorStr(QualType QT,
867                                                         CharUnits Alignment,
868                                                         bool IsVolatile,
869                                                         ASTContext &Ctx) {
870   GenDestructorFuncName GenName("", Alignment, Ctx);
871   return GenName.getName(QT, IsVolatile);
872 }
873 
874 void CodeGenFunction::callCStructDestructor(LValue Dst) {
875   bool IsVolatile = Dst.isVolatile();
876   Address DstPtr = Dst.getAddress(*this);
877   QualType QT = Dst.getType();
878   GenDestructorFuncName GenName("__destructor_", DstPtr.getAlignment(),
879                                 getContext());
880   std::string FuncName = GenName.getName(QT, IsVolatile);
881   callSpecialFunction(GenDestructor(getContext()), FuncName, QT, IsVolatile,
882                       *this, std::array<Address, 1>({{DstPtr}}));
883 }
884 
885 void CodeGenFunction::callCStructCopyConstructor(LValue Dst, LValue Src) {
886   bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
887   Address DstPtr = Dst.getAddress(*this), SrcPtr = Src.getAddress(*this);
888   QualType QT = Dst.getType();
889   GenBinaryFuncName<false> GenName("__copy_constructor_", DstPtr.getAlignment(),
890                                    SrcPtr.getAlignment(), getContext());
891   std::string FuncName = GenName.getName(QT, IsVolatile);
892   callSpecialFunction(GenCopyConstructor(getContext()), FuncName, QT,
893                       IsVolatile, *this,
894                       std::array<Address, 2>({{DstPtr, SrcPtr}}));
895 }
896 
897 void CodeGenFunction::callCStructCopyAssignmentOperator(LValue Dst, LValue Src
898 
899 ) {
900   bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
901   Address DstPtr = Dst.getAddress(*this), SrcPtr = Src.getAddress(*this);
902   QualType QT = Dst.getType();
903   GenBinaryFuncName<false> GenName("__copy_assignment_", DstPtr.getAlignment(),
904                                    SrcPtr.getAlignment(), getContext());
905   std::string FuncName = GenName.getName(QT, IsVolatile);
906   callSpecialFunction(GenCopyAssignment(getContext()), FuncName, QT, IsVolatile,
907                       *this, std::array<Address, 2>({{DstPtr, SrcPtr}}));
908 }
909 
910 void CodeGenFunction::callCStructMoveConstructor(LValue Dst, LValue Src) {
911   bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
912   Address DstPtr = Dst.getAddress(*this), SrcPtr = Src.getAddress(*this);
913   QualType QT = Dst.getType();
914   GenBinaryFuncName<true> GenName("__move_constructor_", DstPtr.getAlignment(),
915                                   SrcPtr.getAlignment(), getContext());
916   std::string FuncName = GenName.getName(QT, IsVolatile);
917   callSpecialFunction(GenMoveConstructor(getContext()), FuncName, QT,
918                       IsVolatile, *this,
919                       std::array<Address, 2>({{DstPtr, SrcPtr}}));
920 }
921 
922 void CodeGenFunction::callCStructMoveAssignmentOperator(LValue Dst, LValue Src
923 
924 ) {
925   bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
926   Address DstPtr = Dst.getAddress(*this), SrcPtr = Src.getAddress(*this);
927   QualType QT = Dst.getType();
928   GenBinaryFuncName<true> GenName("__move_assignment_", DstPtr.getAlignment(),
929                                   SrcPtr.getAlignment(), getContext());
930   std::string FuncName = GenName.getName(QT, IsVolatile);
931   callSpecialFunction(GenMoveAssignment(getContext()), FuncName, QT, IsVolatile,
932                       *this, std::array<Address, 2>({{DstPtr, SrcPtr}}));
933 }
934 
935 llvm::Function *clang::CodeGen::getNonTrivialCStructDefaultConstructor(
936     CodeGenModule &CGM, CharUnits DstAlignment, bool IsVolatile, QualType QT) {
937   ASTContext &Ctx = CGM.getContext();
938   GenDefaultInitializeFuncName GenName(DstAlignment, Ctx);
939   std::string FuncName = GenName.getName(QT, IsVolatile);
940   return getSpecialFunction(GenDefaultInitialize(Ctx), FuncName, QT, IsVolatile,
941                             std::array<CharUnits, 1>({{DstAlignment}}), CGM);
942 }
943 
944 llvm::Function *clang::CodeGen::getNonTrivialCStructCopyConstructor(
945     CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
946     bool IsVolatile, QualType QT) {
947   ASTContext &Ctx = CGM.getContext();
948   GenBinaryFuncName<false> GenName("__copy_constructor_", DstAlignment,
949                                    SrcAlignment, Ctx);
950   std::string FuncName = GenName.getName(QT, IsVolatile);
951   return getSpecialFunction(
952       GenCopyConstructor(Ctx), FuncName, QT, IsVolatile,
953       std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
954 }
955 
956 llvm::Function *clang::CodeGen::getNonTrivialCStructMoveConstructor(
957     CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
958     bool IsVolatile, QualType QT) {
959   ASTContext &Ctx = CGM.getContext();
960   GenBinaryFuncName<true> GenName("__move_constructor_", DstAlignment,
961                                   SrcAlignment, Ctx);
962   std::string FuncName = GenName.getName(QT, IsVolatile);
963   return getSpecialFunction(
964       GenMoveConstructor(Ctx), FuncName, QT, IsVolatile,
965       std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
966 }
967 
968 llvm::Function *clang::CodeGen::getNonTrivialCStructCopyAssignmentOperator(
969     CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
970     bool IsVolatile, QualType QT) {
971   ASTContext &Ctx = CGM.getContext();
972   GenBinaryFuncName<false> GenName("__copy_assignment_", DstAlignment,
973                                    SrcAlignment, Ctx);
974   std::string FuncName = GenName.getName(QT, IsVolatile);
975   return getSpecialFunction(
976       GenCopyAssignment(Ctx), FuncName, QT, IsVolatile,
977       std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
978 }
979 
980 llvm::Function *clang::CodeGen::getNonTrivialCStructMoveAssignmentOperator(
981     CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
982     bool IsVolatile, QualType QT) {
983   ASTContext &Ctx = CGM.getContext();
984   GenBinaryFuncName<true> GenName("__move_assignment_", DstAlignment,
985                                   SrcAlignment, Ctx);
986   std::string FuncName = GenName.getName(QT, IsVolatile);
987   return getSpecialFunction(
988       GenMoveAssignment(Ctx), FuncName, QT, IsVolatile,
989       std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
990 }
991 
992 llvm::Function *clang::CodeGen::getNonTrivialCStructDestructor(
993     CodeGenModule &CGM, CharUnits DstAlignment, bool IsVolatile, QualType QT) {
994   ASTContext &Ctx = CGM.getContext();
995   GenDestructorFuncName GenName("__destructor_", DstAlignment, Ctx);
996   std::string FuncName = GenName.getName(QT, IsVolatile);
997   return getSpecialFunction(GenDestructor(Ctx), FuncName, QT, IsVolatile,
998                             std::array<CharUnits, 1>({{DstAlignment}}), CGM);
999 }
1000