blob: a67f6593f33d796ec35af411f1370b154931151f [file] [log] [blame]
Chris Lattner566b6ce2007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattneraf6f5282007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattneraf6f5282007-08-10 20:13:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Aggregate Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Fariborz Jahanian082b02e2009-07-08 01:18:33 +000015#include "CGObjCRuntime.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "CodeGenModule.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Anders Carlssonb14095a2009-04-17 00:06:03 +000018#include "clang/AST/DeclCXX.h"
Sebastian Redl32cf1f22012-02-17 08:42:25 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000020#include "clang/AST/StmtVisitor.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000025using namespace clang;
26using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000027
Chris Lattner9c033562007-08-21 04:25:47 +000028//===----------------------------------------------------------------------===//
29// Aggregate Expression Emitter
30//===----------------------------------------------------------------------===//
31
32namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000033class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
Chris Lattner9c033562007-08-21 04:25:47 +000034 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000035 CGBuilderTy &Builder;
John McCall558d2ab2010-09-15 10:14:12 +000036 AggValueSlot Dest;
John McCallef072fd2010-05-22 01:48:05 +000037
John McCall410ffb22011-08-25 23:04:34 +000038 /// We want to use 'dest' as the return slot except under two
39 /// conditions:
40 /// - The destination slot requires garbage collection, so we
41 /// need to use the GC API.
42 /// - The destination slot is potentially aliased.
43 bool shouldUseDestForReturnSlot() const {
44 return !(Dest.requiresGCollection() || Dest.isPotentiallyAliased());
45 }
46
John McCallef072fd2010-05-22 01:48:05 +000047 ReturnValueSlot getReturnValueSlot() const {
John McCall410ffb22011-08-25 23:04:34 +000048 if (!shouldUseDestForReturnSlot())
49 return ReturnValueSlot();
John McCallfa037bd2010-05-22 22:13:32 +000050
John McCall558d2ab2010-09-15 10:14:12 +000051 return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile());
52 }
53
54 AggValueSlot EnsureSlot(QualType T) {
55 if (!Dest.isIgnored()) return Dest;
56 return CGF.CreateAggTemp(T, "agg.tmp.ensured");
John McCallef072fd2010-05-22 01:48:05 +000057 }
John McCalle0c11682012-07-02 23:58:38 +000058 void EnsureDest(QualType T) {
59 if (!Dest.isIgnored()) return;
60 Dest = CGF.CreateAggTemp(T, "agg.tmp.ensured");
61 }
John McCallfa037bd2010-05-22 22:13:32 +000062
Chris Lattner9c033562007-08-21 04:25:47 +000063public:
John McCalle0c11682012-07-02 23:58:38 +000064 AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest)
65 : CGF(cgf), Builder(CGF.Builder), Dest(Dest) {
Chris Lattner9c033562007-08-21 04:25:47 +000066 }
67
Chris Lattneree755f92007-08-21 04:59:27 +000068 //===--------------------------------------------------------------------===//
69 // Utilities
70 //===--------------------------------------------------------------------===//
71
Chris Lattner9c033562007-08-21 04:25:47 +000072 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
73 /// represents a value lvalue, this method emits the address of the lvalue,
74 /// then loads the result into DestPtr.
75 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000076
Mike Stump4ac20dd2009-05-23 20:28:01 +000077 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
John McCalle0c11682012-07-02 23:58:38 +000078 void EmitFinalDestCopy(QualType type, const LValue &src);
79 void EmitFinalDestCopy(QualType type, RValue src,
80 CharUnits srcAlignment = CharUnits::Zero());
81 void EmitCopy(QualType type, const AggValueSlot &dest,
82 const AggValueSlot &src);
Mike Stump4ac20dd2009-05-23 20:28:01 +000083
John McCall410ffb22011-08-25 23:04:34 +000084 void EmitMoveFromReturnSlot(const Expr *E, RValue Src);
John McCallfa037bd2010-05-22 22:13:32 +000085
Sebastian Redl32cf1f22012-02-17 08:42:25 +000086 void EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType,
87 QualType elementType, InitListExpr *E);
88
John McCall7c2349b2011-08-25 20:40:09 +000089 AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) {
David Blaikie4e4d0842012-03-11 07:00:24 +000090 if (CGF.getLangOpts().getGC() && TypeRequiresGCollection(T))
John McCall7c2349b2011-08-25 20:40:09 +000091 return AggValueSlot::NeedsGCBarriers;
92 return AggValueSlot::DoesNotNeedGCBarriers;
93 }
94
John McCallfa037bd2010-05-22 22:13:32 +000095 bool TypeRequiresGCollection(QualType T);
96
Chris Lattneree755f92007-08-21 04:59:27 +000097 //===--------------------------------------------------------------------===//
98 // Visitor Methods
99 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattner9c033562007-08-21 04:25:47 +0000101 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000102 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +0000103 }
104 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Peter Collingbournef111d932011-04-15 00:35:48 +0000105 void VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
106 Visit(GE->getResultExpr());
107 }
Eli Friedman12444a22009-01-27 09:03:41 +0000108 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
John McCall91a57552011-07-15 05:09:51 +0000109 void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
110 return Visit(E->getReplacement());
111 }
Chris Lattner9c033562007-08-21 04:25:47 +0000112
113 // l-values.
John McCallf4b88a42012-03-10 09:33:50 +0000114 void VisitDeclRefExpr(DeclRefExpr *E) {
John McCalldd2ecee2012-03-10 03:05:10 +0000115 // For aggregates, we should always be able to emit the variable
116 // as an l-value unless it's a reference. This is due to the fact
117 // that we can't actually ever see a normal l2r conversion on an
118 // aggregate in C++, and in C there's no language standard
119 // actively preventing us from listing variables in the captures
120 // list of a block.
John McCallf4b88a42012-03-10 09:33:50 +0000121 if (E->getDecl()->getType()->isReferenceType()) {
John McCalldd2ecee2012-03-10 03:05:10 +0000122 if (CodeGenFunction::ConstantEmission result
John McCallf4b88a42012-03-10 09:33:50 +0000123 = CGF.tryEmitAsConstant(E)) {
John McCalle0c11682012-07-02 23:58:38 +0000124 EmitFinalDestCopy(E->getType(), result.getReferenceLValue(CGF, E));
John McCalldd2ecee2012-03-10 03:05:10 +0000125 return;
126 }
127 }
128
John McCallf4b88a42012-03-10 09:33:50 +0000129 EmitAggLoadOfLValue(E);
John McCalldd2ecee2012-03-10 03:05:10 +0000130 }
131
Seo Sanghyeon9b73b392007-12-14 02:04:12 +0000132 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
133 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Daniel Dunbar5be028f2010-01-04 18:47:06 +0000134 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Douglas Gregor751ec9b2011-06-17 04:59:12 +0000135 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Seo Sanghyeon9b73b392007-12-14 02:04:12 +0000136 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
137 EmitAggLoadOfLValue(E);
138 }
Chris Lattnerf0a990c2009-04-21 23:00:09 +0000139 void VisitPredefinedExpr(const PredefinedExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000140 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +0000141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattner9c033562007-08-21 04:25:47 +0000143 // Operators.
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000144 void VisitCastExpr(CastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +0000145 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000146 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +0000147 void VisitBinaryOperator(const BinaryOperator *BO);
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +0000148 void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +0000149 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000150 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +0000151
Chris Lattner8fdf3282008-06-24 17:04:18 +0000152 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000153 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
154 EmitAggLoadOfLValue(E);
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
John McCall56ca35d2011-02-17 10:25:35 +0000157 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
Anders Carlssona294ca82009-07-08 18:33:14 +0000158 void VisitChooseExpr(const ChooseExpr *CE);
Devang Patel636c3d02007-10-26 17:44:44 +0000159 void VisitInitListExpr(InitListExpr *E);
Anders Carlsson30311fa2009-12-16 06:57:54 +0000160 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +0000161 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
162 Visit(DAE->getExpr());
163 }
Richard Smithc3bf52c2013-04-20 22:23:05 +0000164 void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
165 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
166 Visit(DIE->getExpr());
167 }
Anders Carlssonb58d0172009-05-30 23:23:33 +0000168 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
Anders Carlsson31ccf372009-05-03 17:47:16 +0000169 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Eli Friedman4c5d8af2012-02-09 03:32:31 +0000170 void VisitLambdaExpr(LambdaExpr *E);
Richard Smith7c3e6152013-06-12 22:31:48 +0000171 void VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
John McCall4765fa02010-12-06 08:20:24 +0000172 void VisitExprWithCleanups(ExprWithCleanups *E);
Douglas Gregored8abf12010-07-08 06:14:04 +0000173 void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
Mike Stump2710c412009-11-18 00:40:12 +0000174 void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
Douglas Gregor03e80032011-06-21 17:03:29 +0000175 void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
John McCalle996ffd2011-02-16 08:02:54 +0000176 void VisitOpaqueValueExpr(OpaqueValueExpr *E);
177
John McCall4b9c2d22011-11-06 09:01:30 +0000178 void VisitPseudoObjectExpr(PseudoObjectExpr *E) {
179 if (E->isGLValue()) {
180 LValue LV = CGF.EmitPseudoObjectLValue(E);
John McCalle0c11682012-07-02 23:58:38 +0000181 return EmitFinalDestCopy(E->getType(), LV);
John McCall4b9c2d22011-11-06 09:01:30 +0000182 }
183
184 CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType()));
185 }
186
Eli Friedmanb1851242008-05-27 15:51:49 +0000187 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000188
Chad Rosier649b4a12012-03-29 17:37:10 +0000189 void EmitInitializationToLValue(Expr *E, LValue Address);
John McCalla07398e2011-06-16 04:16:24 +0000190 void EmitNullInitializationToLValue(LValue Address);
Chris Lattner9c033562007-08-21 04:25:47 +0000191 // case Expr::ChooseExprClass:
Mike Stump39406b12009-12-09 19:24:08 +0000192 void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
Eli Friedman276b0612011-10-11 02:20:01 +0000193 void VisitAtomicExpr(AtomicExpr *E) {
194 CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr());
195 }
Chris Lattner9c033562007-08-21 04:25:47 +0000196};
197} // end anonymous namespace.
198
Chris Lattneree755f92007-08-21 04:59:27 +0000199//===----------------------------------------------------------------------===//
200// Utilities
201//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000202
Chris Lattner883f6a72007-08-11 00:04:45 +0000203/// EmitAggLoadOfLValue - Given an expression with aggregate type that
204/// represents a value lvalue, this method emits the address of the lvalue,
205/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000206void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
207 LValue LV = CGF.EmitLValue(E);
John McCall9eda3ab2013-03-07 21:37:17 +0000208
209 // If the type of the l-value is atomic, then do an atomic load.
210 if (LV.getType()->isAtomicType()) {
Eli Friedman336d9df2013-07-11 01:32:21 +0000211 CGF.EmitAtomicLoad(LV, Dest);
John McCall9eda3ab2013-03-07 21:37:17 +0000212 return;
213 }
214
John McCalle0c11682012-07-02 23:58:38 +0000215 EmitFinalDestCopy(E->getType(), LV);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000216}
217
John McCallfa037bd2010-05-22 22:13:32 +0000218/// \brief True if the given aggregate type requires special GC API calls.
219bool AggExprEmitter::TypeRequiresGCollection(QualType T) {
220 // Only record types have members that might require garbage collection.
221 const RecordType *RecordTy = T->getAs<RecordType>();
222 if (!RecordTy) return false;
223
224 // Don't mess with non-trivial C++ types.
225 RecordDecl *Record = RecordTy->getDecl();
226 if (isa<CXXRecordDecl>(Record) &&
Richard Smith426391c2012-11-16 00:53:38 +0000227 (cast<CXXRecordDecl>(Record)->hasNonTrivialCopyConstructor() ||
John McCallfa037bd2010-05-22 22:13:32 +0000228 !cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
229 return false;
230
231 // Check whether the type has an object member.
232 return Record->hasObjectMember();
233}
234
John McCall410ffb22011-08-25 23:04:34 +0000235/// \brief Perform the final move to DestPtr if for some reason
236/// getReturnValueSlot() didn't use it directly.
John McCallfa037bd2010-05-22 22:13:32 +0000237///
238/// The idea is that you do something like this:
239/// RValue Result = EmitSomething(..., getReturnValueSlot());
John McCall410ffb22011-08-25 23:04:34 +0000240/// EmitMoveFromReturnSlot(E, Result);
241///
242/// If nothing interferes, this will cause the result to be emitted
243/// directly into the return value slot. Otherwise, a final move
244/// will be performed.
John McCalle0c11682012-07-02 23:58:38 +0000245void AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue src) {
John McCall410ffb22011-08-25 23:04:34 +0000246 if (shouldUseDestForReturnSlot()) {
247 // Logically, Dest.getAddr() should equal Src.getAggregateAddr().
248 // The possibility of undef rvalues complicates that a lot,
249 // though, so we can't really assert.
250 return;
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000251 }
John McCall410ffb22011-08-25 23:04:34 +0000252
John McCalle0c11682012-07-02 23:58:38 +0000253 // Otherwise, copy from there to the destination.
254 assert(Dest.getAddr() != src.getAggregateAddr());
255 std::pair<CharUnits, CharUnits> typeInfo =
Chad Rosier26397ed2012-04-17 01:14:29 +0000256 CGF.getContext().getTypeInfoInChars(E->getType());
John McCalle0c11682012-07-02 23:58:38 +0000257 EmitFinalDestCopy(E->getType(), src, typeInfo.second);
John McCallfa037bd2010-05-22 22:13:32 +0000258}
259
Mike Stump4ac20dd2009-05-23 20:28:01 +0000260/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
John McCalle0c11682012-07-02 23:58:38 +0000261void AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src,
262 CharUnits srcAlign) {
263 assert(src.isAggregate() && "value must be aggregate value!");
264 LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddr(), type, srcAlign);
265 EmitFinalDestCopy(type, srcLV);
266}
Mike Stump4ac20dd2009-05-23 20:28:01 +0000267
John McCalle0c11682012-07-02 23:58:38 +0000268/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
269void AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src) {
John McCall558d2ab2010-09-15 10:14:12 +0000270 // If Dest is ignored, then we're evaluating an aggregate expression
John McCalle0c11682012-07-02 23:58:38 +0000271 // in a context that doesn't care about the result. Note that loads
272 // from volatile l-values force the existence of a non-ignored
273 // destination.
274 if (Dest.isIgnored())
275 return;
Fariborz Jahanian8a970052010-10-22 22:05:03 +0000276
John McCalle0c11682012-07-02 23:58:38 +0000277 AggValueSlot srcAgg =
278 AggValueSlot::forLValue(src, AggValueSlot::IsDestructed,
279 needsGC(type), AggValueSlot::IsAliased);
280 EmitCopy(type, Dest, srcAgg);
281}
Chris Lattner883f6a72007-08-11 00:04:45 +0000282
John McCalle0c11682012-07-02 23:58:38 +0000283/// Perform a copy from the source into the destination.
284///
285/// \param type - the type of the aggregate being copied; qualifiers are
286/// ignored
287void AggExprEmitter::EmitCopy(QualType type, const AggValueSlot &dest,
288 const AggValueSlot &src) {
289 if (dest.requiresGCollection()) {
290 CharUnits sz = CGF.getContext().getTypeSizeInChars(type);
291 llvm::Value *size = llvm::ConstantInt::get(CGF.SizeTy, sz.getQuantity());
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000292 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
John McCalle0c11682012-07-02 23:58:38 +0000293 dest.getAddr(),
294 src.getAddr(),
295 size);
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000296 return;
297 }
John McCalle0c11682012-07-02 23:58:38 +0000298
Mike Stump4ac20dd2009-05-23 20:28:01 +0000299 // If the result of the assignment is used, copy the LHS there also.
John McCalle0c11682012-07-02 23:58:38 +0000300 // It's volatile if either side is. Use the minimum alignment of
301 // the two sides.
302 CGF.EmitAggregateCopy(dest.getAddr(), src.getAddr(), type,
303 dest.isVolatile() || src.isVolatile(),
304 std::min(dest.getAlignment(), src.getAlignment()));
Chris Lattner883f6a72007-08-11 00:04:45 +0000305}
306
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000307/// \brief Emit the initializer for a std::initializer_list initialized with a
308/// real initializer list.
Richard Smith7c3e6152013-06-12 22:31:48 +0000309void
310AggExprEmitter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
311 // Emit an array containing the elements. The array is externally destructed
312 // if the std::initializer_list object is.
313 ASTContext &Ctx = CGF.getContext();
314 LValue Array = CGF.EmitLValue(E->getSubExpr());
315 assert(Array.isSimple() && "initializer_list array not a simple lvalue");
316 llvm::Value *ArrayPtr = Array.getAddress();
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000317
Richard Smith7c3e6152013-06-12 22:31:48 +0000318 const ConstantArrayType *ArrayType =
319 Ctx.getAsConstantArrayType(E->getSubExpr()->getType());
320 assert(ArrayType && "std::initializer_list constructed from non-array");
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000321
Richard Smith7c3e6152013-06-12 22:31:48 +0000322 // FIXME: Perform the checks on the field types in SemaInit.
323 RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl();
324 RecordDecl::field_iterator Field = Record->field_begin();
325 if (Field == Record->field_end()) {
326 CGF.ErrorUnsupported(E, "weird std::initializer_list");
Sebastian Redlbabcf9d2012-02-25 20:51:13 +0000327 return;
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000328 }
329
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000330 // Start pointer.
Richard Smith7c3e6152013-06-12 22:31:48 +0000331 if (!Field->getType()->isPointerType() ||
332 !Ctx.hasSameType(Field->getType()->getPointeeType(),
333 ArrayType->getElementType())) {
334 CGF.ErrorUnsupported(E, "weird std::initializer_list");
Sebastian Redlbabcf9d2012-02-25 20:51:13 +0000335 return;
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000336 }
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000337
Richard Smith7c3e6152013-06-12 22:31:48 +0000338 AggValueSlot Dest = EnsureSlot(E->getType());
339 LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
340 Dest.getAlignment());
341 LValue Start = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
342 llvm::Value *Zero = llvm::ConstantInt::get(CGF.PtrDiffTy, 0);
343 llvm::Value *IdxStart[] = { Zero, Zero };
344 llvm::Value *ArrayStart =
345 Builder.CreateInBoundsGEP(ArrayPtr, IdxStart, "arraystart");
346 CGF.EmitStoreThroughLValue(RValue::get(ArrayStart), Start);
347 ++Field;
348
349 if (Field == Record->field_end()) {
350 CGF.ErrorUnsupported(E, "weird std::initializer_list");
Sebastian Redlbabcf9d2012-02-25 20:51:13 +0000351 return;
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000352 }
Richard Smith7c3e6152013-06-12 22:31:48 +0000353
354 llvm::Value *Size = Builder.getInt(ArrayType->getSize());
355 LValue EndOrLength = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
356 if (Field->getType()->isPointerType() &&
357 Ctx.hasSameType(Field->getType()->getPointeeType(),
358 ArrayType->getElementType())) {
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000359 // End pointer.
Richard Smith7c3e6152013-06-12 22:31:48 +0000360 llvm::Value *IdxEnd[] = { Zero, Size };
361 llvm::Value *ArrayEnd =
362 Builder.CreateInBoundsGEP(ArrayPtr, IdxEnd, "arrayend");
363 CGF.EmitStoreThroughLValue(RValue::get(ArrayEnd), EndOrLength);
364 } else if (Ctx.hasSameType(Field->getType(), Ctx.getSizeType())) {
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000365 // Length.
Richard Smith7c3e6152013-06-12 22:31:48 +0000366 CGF.EmitStoreThroughLValue(RValue::get(Size), EndOrLength);
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000367 } else {
Richard Smith7c3e6152013-06-12 22:31:48 +0000368 CGF.ErrorUnsupported(E, "weird std::initializer_list");
Sebastian Redlbabcf9d2012-02-25 20:51:13 +0000369 return;
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000370 }
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000371}
372
373/// \brief Emit initialization of an array from an initializer list.
374void AggExprEmitter::EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType,
375 QualType elementType, InitListExpr *E) {
376 uint64_t NumInitElements = E->getNumInits();
377
378 uint64_t NumArrayElements = AType->getNumElements();
379 assert(NumInitElements <= NumArrayElements);
380
381 // DestPtr is an array*. Construct an elementType* by drilling
382 // down a level.
383 llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
384 llvm::Value *indices[] = { zero, zero };
385 llvm::Value *begin =
386 Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin");
387
388 // Exception safety requires us to destroy all the
389 // already-constructed members if an initializer throws.
390 // For that, we'll need an EH cleanup.
391 QualType::DestructionKind dtorKind = elementType.isDestructedType();
392 llvm::AllocaInst *endOfInit = 0;
393 EHScopeStack::stable_iterator cleanup;
394 llvm::Instruction *cleanupDominator = 0;
395 if (CGF.needsEHCleanup(dtorKind)) {
396 // In principle we could tell the cleanup where we are more
397 // directly, but the control flow can get so varied here that it
398 // would actually be quite complex. Therefore we go through an
399 // alloca.
400 endOfInit = CGF.CreateTempAlloca(begin->getType(),
401 "arrayinit.endOfInit");
402 cleanupDominator = Builder.CreateStore(begin, endOfInit);
403 CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType,
404 CGF.getDestroyer(dtorKind));
405 cleanup = CGF.EHStack.stable_begin();
406
407 // Otherwise, remember that we didn't need a cleanup.
408 } else {
409 dtorKind = QualType::DK_none;
410 }
411
412 llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1);
413
414 // The 'current element to initialize'. The invariants on this
415 // variable are complicated. Essentially, after each iteration of
416 // the loop, it points to the last initialized element, except
417 // that it points to the beginning of the array before any
418 // elements have been initialized.
419 llvm::Value *element = begin;
420
421 // Emit the explicit initializers.
422 for (uint64_t i = 0; i != NumInitElements; ++i) {
423 // Advance to the next element.
424 if (i > 0) {
425 element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element");
426
427 // Tell the cleanup that it needs to destroy up to this
428 // element. TODO: some of these stores can be trivially
429 // observed to be unnecessary.
430 if (endOfInit) Builder.CreateStore(element, endOfInit);
431 }
432
Richard Smith7c3e6152013-06-12 22:31:48 +0000433 LValue elementLV = CGF.MakeAddrLValue(element, elementType);
434 EmitInitializationToLValue(E->getInit(i), elementLV);
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000435 }
436
437 // Check whether there's a non-trivial array-fill expression.
438 // Note that this will be a CXXConstructExpr even if the element
439 // type is an array (or array of array, etc.) of class type.
440 Expr *filler = E->getArrayFiller();
441 bool hasTrivialFiller = true;
442 if (CXXConstructExpr *cons = dyn_cast_or_null<CXXConstructExpr>(filler)) {
443 assert(cons->getConstructor()->isDefaultConstructor());
444 hasTrivialFiller = cons->getConstructor()->isTrivial();
445 }
446
447 // Any remaining elements need to be zero-initialized, possibly
448 // using the filler expression. We can skip this if the we're
449 // emitting to zeroed memory.
450 if (NumInitElements != NumArrayElements &&
451 !(Dest.isZeroed() && hasTrivialFiller &&
452 CGF.getTypes().isZeroInitializable(elementType))) {
453
454 // Use an actual loop. This is basically
455 // do { *array++ = filler; } while (array != end);
456
457 // Advance to the start of the rest of the array.
458 if (NumInitElements) {
459 element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start");
460 if (endOfInit) Builder.CreateStore(element, endOfInit);
461 }
462
463 // Compute the end of the array.
464 llvm::Value *end = Builder.CreateInBoundsGEP(begin,
465 llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements),
466 "arrayinit.end");
467
468 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
469 llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body");
470
471 // Jump into the body.
472 CGF.EmitBlock(bodyBB);
473 llvm::PHINode *currentElement =
474 Builder.CreatePHI(element->getType(), 2, "arrayinit.cur");
475 currentElement->addIncoming(element, entryBB);
476
477 // Emit the actual filler expression.
478 LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType);
479 if (filler)
Chad Rosier649b4a12012-03-29 17:37:10 +0000480 EmitInitializationToLValue(filler, elementLV);
Sebastian Redl32cf1f22012-02-17 08:42:25 +0000481 else
482 EmitNullInitializationToLValue(elementLV);
483
484 // Move on to the next element.
485 llvm::Value *nextElement =
486 Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next");
487
488 // Tell the EH cleanup that we finished with the last element.
489 if (endOfInit) Builder.CreateStore(nextElement, endOfInit);
490
491 // Leave the loop if we're done.
492 llvm::Value *done = Builder.CreateICmpEQ(nextElement, end,
493 "arrayinit.done");
494 llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end");
495 Builder.CreateCondBr(done, endBB, bodyBB);
496 currentElement->addIncoming(nextElement, Builder.GetInsertBlock());
497
498 CGF.EmitBlock(endBB);
499 }
500
501 // Leave the partial-array cleanup if we entered one.
502 if (dtorKind) CGF.DeactivateCleanupBlock(cleanup, cleanupDominator);
503}
504
Chris Lattneree755f92007-08-21 04:59:27 +0000505//===----------------------------------------------------------------------===//
506// Visitor Methods
507//===----------------------------------------------------------------------===//
508
Douglas Gregor03e80032011-06-21 17:03:29 +0000509void AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){
510 Visit(E->GetTemporaryExpr());
511}
512
John McCalle996ffd2011-02-16 08:02:54 +0000513void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) {
John McCalle0c11682012-07-02 23:58:38 +0000514 EmitFinalDestCopy(e->getType(), CGF.getOpaqueLValueMapping(e));
John McCalle996ffd2011-02-16 08:02:54 +0000515}
516
Douglas Gregor751ec9b2011-06-17 04:59:12 +0000517void
518AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
John McCall1723f632013-03-07 21:36:54 +0000519 if (Dest.isPotentiallyAliased() &&
520 E->getType().isPODType(CGF.getContext())) {
Douglas Gregor673e98b2011-06-17 16:37:20 +0000521 // For a POD type, just emit a load of the lvalue + a copy, because our
522 // compound literal might alias the destination.
Douglas Gregor673e98b2011-06-17 16:37:20 +0000523 EmitAggLoadOfLValue(E);
524 return;
525 }
526
Douglas Gregor751ec9b2011-06-17 04:59:12 +0000527 AggValueSlot Slot = EnsureSlot(E->getType());
528 CGF.EmitAggExpr(E->getInitializer(), Slot);
529}
530
John McCall9eda3ab2013-03-07 21:37:17 +0000531/// Attempt to look through various unimportant expressions to find a
532/// cast of the given kind.
533static Expr *findPeephole(Expr *op, CastKind kind) {
534 while (true) {
535 op = op->IgnoreParens();
536 if (CastExpr *castE = dyn_cast<CastExpr>(op)) {
537 if (castE->getCastKind() == kind)
538 return castE->getSubExpr();
539 if (castE->getCastKind() == CK_NoOp)
540 continue;
541 }
542 return 0;
543 }
544}
Douglas Gregor751ec9b2011-06-17 04:59:12 +0000545
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000546void AggExprEmitter::VisitCastExpr(CastExpr *E) {
Anders Carlsson30168422009-09-29 01:23:39 +0000547 switch (E->getCastKind()) {
Anders Carlsson575b3742011-04-11 02:03:26 +0000548 case CK_Dynamic: {
Richard Smith2c9f87c2012-08-24 00:54:33 +0000549 // FIXME: Can this actually happen? We have no test coverage for it.
Douglas Gregor69cfeb12010-05-14 21:31:02 +0000550 assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
Richard Smith2c9f87c2012-08-24 00:54:33 +0000551 LValue LV = CGF.EmitCheckedLValue(E->getSubExpr(),
Richard Smith7ac9ef12012-09-08 02:08:36 +0000552 CodeGenFunction::TCK_Load);
Douglas Gregor69cfeb12010-05-14 21:31:02 +0000553 // FIXME: Do we also need to handle property references here?
554 if (LV.isSimple())
555 CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
556 else
557 CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
558
John McCall558d2ab2010-09-15 10:14:12 +0000559 if (!Dest.isIgnored())
560 CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
Douglas Gregor69cfeb12010-05-14 21:31:02 +0000561 break;
562 }
563
John McCall2de56d12010-08-25 11:45:40 +0000564 case CK_ToUnion: {
John McCall65912712011-04-12 22:02:02 +0000565 if (Dest.isIgnored()) break;
566
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000567 // GCC union extension
Daniel Dunbar79c39282010-08-21 03:15:20 +0000568 QualType Ty = E->getSubExpr()->getType();
569 QualType PtrTy = CGF.getContext().getPointerType(Ty);
John McCall558d2ab2010-09-15 10:14:12 +0000570 llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(),
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000571 CGF.ConvertType(PtrTy));
John McCalla07398e2011-06-16 04:16:24 +0000572 EmitInitializationToLValue(E->getSubExpr(),
Chad Rosier649b4a12012-03-29 17:37:10 +0000573 CGF.MakeAddrLValue(CastPtr, Ty));
Anders Carlsson30168422009-09-29 01:23:39 +0000574 break;
Nuno Lopes7e916272009-01-15 20:14:33 +0000575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
John McCall2de56d12010-08-25 11:45:40 +0000577 case CK_DerivedToBase:
578 case CK_BaseToDerived:
579 case CK_UncheckedDerivedToBase: {
David Blaikieb219cfc2011-09-23 05:06:16 +0000580 llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: "
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000581 "should have been unpacked before we got here");
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000582 }
583
John McCall9eda3ab2013-03-07 21:37:17 +0000584 case CK_NonAtomicToAtomic:
585 case CK_AtomicToNonAtomic: {
586 bool isToAtomic = (E->getCastKind() == CK_NonAtomicToAtomic);
587
588 // Determine the atomic and value types.
589 QualType atomicType = E->getSubExpr()->getType();
590 QualType valueType = E->getType();
591 if (isToAtomic) std::swap(atomicType, valueType);
592
593 assert(atomicType->isAtomicType());
594 assert(CGF.getContext().hasSameUnqualifiedType(valueType,
595 atomicType->castAs<AtomicType>()->getValueType()));
596
597 // Just recurse normally if we're ignoring the result or the
598 // atomic type doesn't change representation.
599 if (Dest.isIgnored() || !CGF.CGM.isPaddedAtomicType(atomicType)) {
600 return Visit(E->getSubExpr());
601 }
602
603 CastKind peepholeTarget =
604 (isToAtomic ? CK_AtomicToNonAtomic : CK_NonAtomicToAtomic);
605
606 // These two cases are reverses of each other; try to peephole them.
607 if (Expr *op = findPeephole(E->getSubExpr(), peepholeTarget)) {
608 assert(CGF.getContext().hasSameUnqualifiedType(op->getType(),
609 E->getType()) &&
610 "peephole significantly changed types?");
611 return Visit(op);
612 }
613
614 // If we're converting an r-value of non-atomic type to an r-value
Eli Friedman336d9df2013-07-11 01:32:21 +0000615 // of atomic type, just emit directly into the relevant sub-object.
John McCall9eda3ab2013-03-07 21:37:17 +0000616 if (isToAtomic) {
Eli Friedman336d9df2013-07-11 01:32:21 +0000617 AggValueSlot valueDest = Dest;
618 if (!valueDest.isIgnored() && CGF.CGM.isPaddedAtomicType(atomicType)) {
619 // Zero-initialize. (Strictly speaking, we only need to intialize
620 // the padding at the end, but this is simpler.)
621 if (!Dest.isZeroed())
622 CGF.EmitNullInitialization(Dest.getAddr(), type);
623
624 // Build a GEP to refer to the subobject.
625 llvm::Value *valueAddr =
626 CGF.Builder.CreateStructGEP(valueDest.getAddr(), 0);
627 valueDest = AggValueSlot::forAddr(valueAddr,
628 valueDest.getAlignment(),
629 valueDest.getQualifiers(),
630 valueDest.isExternallyDestructed(),
631 valueDest.requiresGCollection(),
632 valueDest.isPotentiallyAliased(),
633 AggValueSlot::IsZeroed);
634 }
635
John McCall9eda3ab2013-03-07 21:37:17 +0000636 CGF.EmitAggExpr(E->getSubExpr(), valueDest.getDest());
637 return;
638 }
639
640 // Otherwise, we're converting an atomic type to a non-atomic type.
Eli Friedman336d9df2013-07-11 01:32:21 +0000641 // Make an atomic temporary, emit into that, and then copy the value out.
John McCall9eda3ab2013-03-07 21:37:17 +0000642 AggValueSlot atomicSlot =
643 CGF.CreateAggTemp(atomicType, "atomic-to-nonatomic.temp");
644 CGF.EmitAggExpr(E->getSubExpr(), atomicSlot);
645
646 llvm::Value *valueAddr =
647 Builder.CreateStructGEP(atomicSlot.getAddr(), 0);
648 RValue rvalue = RValue::getAggregate(valueAddr, atomicSlot.isVolatile());
649 return EmitFinalDestCopy(valueType, rvalue);
650 }
651
John McCalle0c11682012-07-02 23:58:38 +0000652 case CK_LValueToRValue:
653 // If we're loading from a volatile type, force the destination
654 // into existence.
655 if (E->getSubExpr()->getType().isVolatileQualified()) {
656 EnsureDest(E->getType());
657 return Visit(E->getSubExpr());
658 }
John McCall9eda3ab2013-03-07 21:37:17 +0000659
John McCalle0c11682012-07-02 23:58:38 +0000660 // fallthrough
661
John McCall2de56d12010-08-25 11:45:40 +0000662 case CK_NoOp:
663 case CK_UserDefinedConversion:
664 case CK_ConstructorConversion:
Anders Carlsson30168422009-09-29 01:23:39 +0000665 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
666 E->getType()) &&
667 "Implicit cast types must be compatible");
668 Visit(E->getSubExpr());
669 break;
John McCall0ae287a2010-12-01 04:43:34 +0000670
John McCall2de56d12010-08-25 11:45:40 +0000671 case CK_LValueBitCast:
John McCall0ae287a2010-12-01 04:43:34 +0000672 llvm_unreachable("should not be emitting lvalue bitcast as rvalue");
John McCall1de4d4e2011-04-07 08:22:57 +0000673
John McCall0ae287a2010-12-01 04:43:34 +0000674 case CK_Dependent:
675 case CK_BitCast:
676 case CK_ArrayToPointerDecay:
677 case CK_FunctionToPointerDecay:
678 case CK_NullToPointer:
679 case CK_NullToMemberPointer:
680 case CK_BaseToDerivedMemberPointer:
681 case CK_DerivedToBaseMemberPointer:
682 case CK_MemberPointerToBoolean:
John McCall4d4e5c12012-02-15 01:22:51 +0000683 case CK_ReinterpretMemberPointer:
John McCall0ae287a2010-12-01 04:43:34 +0000684 case CK_IntegralToPointer:
685 case CK_PointerToIntegral:
686 case CK_PointerToBoolean:
687 case CK_ToVoid:
688 case CK_VectorSplat:
689 case CK_IntegralCast:
690 case CK_IntegralToBoolean:
691 case CK_IntegralToFloating:
692 case CK_FloatingToIntegral:
693 case CK_FloatingToBoolean:
694 case CK_FloatingCast:
John McCall1d9b3b22011-09-09 05:25:32 +0000695 case CK_CPointerToObjCPointerCast:
696 case CK_BlockPointerToObjCPointerCast:
John McCall0ae287a2010-12-01 04:43:34 +0000697 case CK_AnyPointerToBlockPointerCast:
698 case CK_ObjCObjectLValueCast:
699 case CK_FloatingRealToComplex:
700 case CK_FloatingComplexToReal:
701 case CK_FloatingComplexToBoolean:
702 case CK_FloatingComplexCast:
703 case CK_FloatingComplexToIntegralComplex:
704 case CK_IntegralRealToComplex:
705 case CK_IntegralComplexToReal:
706 case CK_IntegralComplexToBoolean:
707 case CK_IntegralComplexCast:
708 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +0000709 case CK_ARCProduceObject:
710 case CK_ARCConsumeObject:
711 case CK_ARCReclaimReturnedObject:
712 case CK_ARCExtendBlockObject:
Douglas Gregorac1303e2012-02-22 05:02:47 +0000713 case CK_CopyAndAutoreleaseBlockObject:
Eli Friedmana6c66ce2012-08-31 00:14:07 +0000714 case CK_BuiltinFnToFnPtr:
Guy Benyeie6b9d802013-01-20 12:31:11 +0000715 case CK_ZeroToOCLEvent:
John McCall0ae287a2010-12-01 04:43:34 +0000716 llvm_unreachable("cast kind invalid for aggregate types");
Anders Carlsson30168422009-09-29 01:23:39 +0000717 }
Anders Carlssone4707ff2008-01-14 06:28:57 +0000718}
719
Chris Lattner96196622008-07-26 22:37:01 +0000720void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone70e8f72009-05-27 16:45:02 +0000721 if (E->getCallReturnType()->isReferenceType()) {
722 EmitAggLoadOfLValue(E);
723 return;
724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
John McCallfa037bd2010-05-22 22:13:32 +0000726 RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
John McCall410ffb22011-08-25 23:04:34 +0000727 EmitMoveFromReturnSlot(E, RV);
Anders Carlsson148fe672007-10-31 22:04:46 +0000728}
Chris Lattner96196622008-07-26 22:37:01 +0000729
730void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
John McCallfa037bd2010-05-22 22:13:32 +0000731 RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
John McCall410ffb22011-08-25 23:04:34 +0000732 EmitMoveFromReturnSlot(E, RV);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000733}
Anders Carlsson148fe672007-10-31 22:04:46 +0000734
Chris Lattner96196622008-07-26 22:37:01 +0000735void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
John McCall2a416372010-12-05 02:00:02 +0000736 CGF.EmitIgnoredExpr(E->getLHS());
John McCall558d2ab2010-09-15 10:14:12 +0000737 Visit(E->getRHS());
Eli Friedman07fa52a2008-05-20 07:56:31 +0000738}
739
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000740void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
John McCall150b4622011-01-26 04:00:11 +0000741 CodeGenFunction::StmtExprEvaluation eval(CGF);
John McCall558d2ab2010-09-15 10:14:12 +0000742 CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000743}
744
Chris Lattner9c033562007-08-21 04:25:47 +0000745void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +0000746 if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +0000747 VisitPointerToDataMemberBinaryOperator(E);
748 else
749 CGF.ErrorUnsupported(E, "aggregate binary expression");
750}
751
752void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
753 const BinaryOperator *E) {
754 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
John McCalle0c11682012-07-02 23:58:38 +0000755 EmitFinalDestCopy(E->getType(), LV);
756}
757
758/// Is the value of the given expression possibly a reference to or
759/// into a __block variable?
760static bool isBlockVarRef(const Expr *E) {
761 // Make sure we look through parens.
762 E = E->IgnoreParens();
763
764 // Check for a direct reference to a __block variable.
765 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
766 const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
767 return (var && var->hasAttr<BlocksAttr>());
768 }
769
770 // More complicated stuff.
771
772 // Binary operators.
773 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) {
774 // For an assignment or pointer-to-member operation, just care
775 // about the LHS.
776 if (op->isAssignmentOp() || op->isPtrMemOp())
777 return isBlockVarRef(op->getLHS());
778
779 // For a comma, just care about the RHS.
780 if (op->getOpcode() == BO_Comma)
781 return isBlockVarRef(op->getRHS());
782
783 // FIXME: pointer arithmetic?
784 return false;
785
786 // Check both sides of a conditional operator.
787 } else if (const AbstractConditionalOperator *op
788 = dyn_cast<AbstractConditionalOperator>(E)) {
789 return isBlockVarRef(op->getTrueExpr())
790 || isBlockVarRef(op->getFalseExpr());
791
792 // OVEs are required to support BinaryConditionalOperators.
793 } else if (const OpaqueValueExpr *op
794 = dyn_cast<OpaqueValueExpr>(E)) {
795 if (const Expr *src = op->getSourceExpr())
796 return isBlockVarRef(src);
797
798 // Casts are necessary to get things like (*(int*)&var) = foo().
799 // We don't really care about the kind of cast here, except
800 // we don't want to look through l2r casts, because it's okay
801 // to get the *value* in a __block variable.
802 } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) {
803 if (cast->getCastKind() == CK_LValueToRValue)
804 return false;
805 return isBlockVarRef(cast->getSubExpr());
806
807 // Handle unary operators. Again, just aggressively look through
808 // it, ignoring the operation.
809 } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) {
810 return isBlockVarRef(uop->getSubExpr());
811
812 // Look into the base of a field access.
813 } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) {
814 return isBlockVarRef(mem->getBase());
815
816 // Look into the base of a subscript.
817 } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) {
818 return isBlockVarRef(sub->getBase());
819 }
820
821 return false;
Chris Lattneree755f92007-08-21 04:59:27 +0000822}
823
Chris Lattner03d6fb92007-08-21 04:43:17 +0000824void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000825 // For an assignment to work, the value on the right has
826 // to be compatible with the value on the left.
Eli Friedman2dce5f82009-05-28 23:04:00 +0000827 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
828 E->getRHS()->getType())
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000829 && "Invalid assignment");
John McCallcd940a12010-12-06 06:10:02 +0000830
John McCalle0c11682012-07-02 23:58:38 +0000831 // If the LHS might be a __block variable, and the RHS can
832 // potentially cause a block copy, we need to evaluate the RHS first
833 // so that the assignment goes the right place.
834 // This is pretty semantically fragile.
835 if (isBlockVarRef(E->getLHS()) &&
836 E->getRHS()->HasSideEffects(CGF.getContext())) {
837 // Ensure that we have a destination, and evaluate the RHS into that.
838 EnsureDest(E->getRHS()->getType());
839 Visit(E->getRHS());
840
841 // Now emit the LHS and copy into it.
Richard Smith4def70d2012-10-09 19:52:38 +0000842 LValue LHS = CGF.EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
John McCalle0c11682012-07-02 23:58:38 +0000843
John McCall9eda3ab2013-03-07 21:37:17 +0000844 // That copy is an atomic copy if the LHS is atomic.
845 if (LHS.getType()->isAtomicType()) {
846 CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
847 return;
848 }
849
John McCalle0c11682012-07-02 23:58:38 +0000850 EmitCopy(E->getLHS()->getType(),
851 AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
852 needsGC(E->getLHS()->getType()),
853 AggValueSlot::IsAliased),
854 Dest);
855 return;
856 }
Chad Rosier649b4a12012-03-29 17:37:10 +0000857
Chris Lattner9c033562007-08-21 04:25:47 +0000858 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000859
John McCall9eda3ab2013-03-07 21:37:17 +0000860 // If we have an atomic type, evaluate into the destination and then
861 // do an atomic copy.
862 if (LHS.getType()->isAtomicType()) {
863 EnsureDest(E->getRHS()->getType());
864 Visit(E->getRHS());
865 CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
866 return;
867 }
868
John McCalldb458062011-11-07 03:59:57 +0000869 // Codegen the RHS so that it stores directly into the LHS.
870 AggValueSlot LHSSlot =
871 AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
872 needsGC(E->getLHS()->getType()),
Chad Rosier649b4a12012-03-29 17:37:10 +0000873 AggValueSlot::IsAliased);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000874 // A non-volatile aggregate destination might have volatile member.
875 if (!LHSSlot.isVolatile() &&
876 CGF.hasVolatileMember(E->getLHS()->getType()))
877 LHSSlot.setVolatile(true);
878
John McCalle0c11682012-07-02 23:58:38 +0000879 CGF.EmitAggExpr(E->getRHS(), LHSSlot);
880
881 // Copy into the destination if the assignment isn't ignored.
882 EmitFinalDestCopy(E->getType(), LHS);
Chris Lattner883f6a72007-08-11 00:04:45 +0000883}
884
John McCall56ca35d2011-02-17 10:25:35 +0000885void AggExprEmitter::
886VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000887 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
888 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
889 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000890
John McCall56ca35d2011-02-17 10:25:35 +0000891 // Bind the common expression if necessary.
Eli Friedmand97927d2012-01-06 20:42:20 +0000892 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
John McCall56ca35d2011-02-17 10:25:35 +0000893
John McCall150b4622011-01-26 04:00:11 +0000894 CodeGenFunction::ConditionalEvaluation eval(CGF);
Eli Friedman8e274bd2009-12-25 06:17:05 +0000895 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000896
John McCall74fb0ed2010-11-17 00:07:33 +0000897 // Save whether the destination's lifetime is externally managed.
John McCallfd71fb82011-08-26 08:02:37 +0000898 bool isExternallyDestructed = Dest.isExternallyDestructed();
Chris Lattner883f6a72007-08-11 00:04:45 +0000899
John McCall150b4622011-01-26 04:00:11 +0000900 eval.begin(CGF);
901 CGF.EmitBlock(LHSBlock);
John McCall56ca35d2011-02-17 10:25:35 +0000902 Visit(E->getTrueExpr());
John McCall150b4622011-01-26 04:00:11 +0000903 eval.end(CGF);
Mike Stump1eb44332009-09-09 15:08:12 +0000904
John McCall150b4622011-01-26 04:00:11 +0000905 assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!");
906 CGF.Builder.CreateBr(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
John McCall74fb0ed2010-11-17 00:07:33 +0000908 // If the result of an agg expression is unused, then the emission
909 // of the LHS might need to create a destination slot. That's fine
910 // with us, and we can safely emit the RHS into the same slot, but
John McCallfd71fb82011-08-26 08:02:37 +0000911 // we shouldn't claim that it's already being destructed.
912 Dest.setExternallyDestructed(isExternallyDestructed);
John McCall74fb0ed2010-11-17 00:07:33 +0000913
John McCall150b4622011-01-26 04:00:11 +0000914 eval.begin(CGF);
915 CGF.EmitBlock(RHSBlock);
John McCall56ca35d2011-02-17 10:25:35 +0000916 Visit(E->getFalseExpr());
John McCall150b4622011-01-26 04:00:11 +0000917 eval.end(CGF);
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Chris Lattner9c033562007-08-21 04:25:47 +0000919 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000920}
Chris Lattneree755f92007-08-21 04:59:27 +0000921
Anders Carlssona294ca82009-07-08 18:33:14 +0000922void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
923 Visit(CE->getChosenSubExpr(CGF.getContext()));
924}
925
Eli Friedmanb1851242008-05-27 15:51:49 +0000926void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000927 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000928 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
929
Sebastian Redl0262f022009-01-09 21:09:38 +0000930 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000931 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000932 return;
933 }
934
John McCalle0c11682012-07-02 23:58:38 +0000935 EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType()));
Eli Friedmanb1851242008-05-27 15:51:49 +0000936}
937
Anders Carlssonb58d0172009-05-30 23:23:33 +0000938void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000939 // Ensure that we have a slot, but if we already do, remember
John McCallfd71fb82011-08-26 08:02:37 +0000940 // whether it was externally destructed.
941 bool wasExternallyDestructed = Dest.isExternallyDestructed();
John McCalle0c11682012-07-02 23:58:38 +0000942 EnsureDest(E->getType());
John McCallfd71fb82011-08-26 08:02:37 +0000943
944 // We're going to push a destructor if there isn't already one.
945 Dest.setExternallyDestructed();
Mike Stump1eb44332009-09-09 15:08:12 +0000946
John McCall558d2ab2010-09-15 10:14:12 +0000947 Visit(E->getSubExpr());
Anders Carlssonb58d0172009-05-30 23:23:33 +0000948
John McCallfd71fb82011-08-26 08:02:37 +0000949 // Push that destructor we promised.
950 if (!wasExternallyDestructed)
Peter Collingbourne86811602011-11-27 22:09:22 +0000951 CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddr());
Anders Carlssonb58d0172009-05-30 23:23:33 +0000952}
953
Anders Carlssonb14095a2009-04-17 00:06:03 +0000954void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000955AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000956 AggValueSlot Slot = EnsureSlot(E->getType());
957 CGF.EmitCXXConstructExpr(E, Slot);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000958}
959
Eli Friedman4c5d8af2012-02-09 03:32:31 +0000960void
961AggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {
962 AggValueSlot Slot = EnsureSlot(E->getType());
963 CGF.EmitLambdaExpr(E, Slot);
964}
965
John McCall4765fa02010-12-06 08:20:24 +0000966void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {
John McCall1a343eb2011-11-10 08:15:53 +0000967 CGF.enterFullExpression(E);
968 CodeGenFunction::RunCleanupsScope cleanups(CGF);
969 Visit(E->getSubExpr());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000970}
971
Douglas Gregored8abf12010-07-08 06:14:04 +0000972void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000973 QualType T = E->getType();
974 AggValueSlot Slot = EnsureSlot(T);
John McCalla07398e2011-06-16 04:16:24 +0000975 EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
Anders Carlsson30311fa2009-12-16 06:57:54 +0000976}
977
978void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000979 QualType T = E->getType();
980 AggValueSlot Slot = EnsureSlot(T);
John McCalla07398e2011-06-16 04:16:24 +0000981 EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
Nuno Lopes329763b2009-10-18 15:18:11 +0000982}
983
Chris Lattner1b726772010-12-02 07:07:26 +0000984/// isSimpleZero - If emitting this value will obviously just cause a store of
985/// zero to memory, return true. This can return false if uncertain, so it just
986/// handles simple cases.
987static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) {
Peter Collingbournef111d932011-04-15 00:35:48 +0000988 E = E->IgnoreParens();
989
Chris Lattner1b726772010-12-02 07:07:26 +0000990 // 0
991 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E))
992 return IL->getValue() == 0;
993 // +0.0
994 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E))
995 return FL->getValue().isPosZero();
996 // int()
997 if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) &&
998 CGF.getTypes().isZeroInitializable(E->getType()))
999 return true;
1000 // (int*)0 - Null pointer expressions.
1001 if (const CastExpr *ICE = dyn_cast<CastExpr>(E))
1002 return ICE->getCastKind() == CK_NullToPointer;
1003 // '\0'
1004 if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E))
1005 return CL->getValue() == 0;
1006
1007 // Otherwise, hard case: conservatively return false.
1008 return false;
1009}
1010
1011
Anders Carlsson78e83f82010-02-03 17:33:16 +00001012void
Chad Rosier649b4a12012-03-29 17:37:10 +00001013AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
John McCalla07398e2011-06-16 04:16:24 +00001014 QualType type = LV.getType();
Mike Stump7f79f9b2009-05-29 15:46:01 +00001015 // FIXME: Ignore result?
Chris Lattnerf81557c2008-04-04 18:42:16 +00001016 // FIXME: Are initializers affected by volatile?
Chris Lattner1b726772010-12-02 07:07:26 +00001017 if (Dest.isZeroed() && isSimpleZero(E, CGF)) {
1018 // Storing "i32 0" to a zero'd memory location is a noop.
John McCall9d232c82013-03-07 21:37:08 +00001019 return;
Richard Smith0dbe2fb2012-12-21 03:17:28 +00001020 } else if (isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) {
John McCall9d232c82013-03-07 21:37:08 +00001021 return EmitNullInitializationToLValue(LV);
John McCalla07398e2011-06-16 04:16:24 +00001022 } else if (type->isReferenceType()) {
Richard Smithd4ec5622013-06-12 23:38:09 +00001023 RValue RV = CGF.EmitReferenceBindingToExpr(E);
John McCall9d232c82013-03-07 21:37:08 +00001024 return CGF.EmitStoreThroughLValue(RV, LV);
1025 }
1026
1027 switch (CGF.getEvaluationKind(type)) {
1028 case TEK_Complex:
1029 CGF.EmitComplexExprIntoLValue(E, LV, /*isInit*/ true);
1030 return;
1031 case TEK_Aggregate:
John McCall7c2349b2011-08-25 20:40:09 +00001032 CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV,
1033 AggValueSlot::IsDestructed,
1034 AggValueSlot::DoesNotNeedGCBarriers,
John McCall410ffb22011-08-25 23:04:34 +00001035 AggValueSlot::IsNotAliased,
John McCalla07398e2011-06-16 04:16:24 +00001036 Dest.isZeroed()));
John McCall9d232c82013-03-07 21:37:08 +00001037 return;
1038 case TEK_Scalar:
1039 if (LV.isSimple()) {
1040 CGF.EmitScalarInit(E, /*D=*/0, LV, /*Captured=*/false);
1041 } else {
1042 CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV);
1043 }
1044 return;
Chris Lattnerf81557c2008-04-04 18:42:16 +00001045 }
John McCall9d232c82013-03-07 21:37:08 +00001046 llvm_unreachable("bad evaluation kind");
Chris Lattnerf81557c2008-04-04 18:42:16 +00001047}
1048
John McCalla07398e2011-06-16 04:16:24 +00001049void AggExprEmitter::EmitNullInitializationToLValue(LValue lv) {
1050 QualType type = lv.getType();
1051
Chris Lattner1b726772010-12-02 07:07:26 +00001052 // If the destination slot is already zeroed out before the aggregate is
1053 // copied into it, we don't have to emit any zeros here.
John McCalla07398e2011-06-16 04:16:24 +00001054 if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type))
Chris Lattner1b726772010-12-02 07:07:26 +00001055 return;
1056
John McCall9d232c82013-03-07 21:37:08 +00001057 if (CGF.hasScalarEvaluationKind(type)) {
Richard Smith0dbe2fb2012-12-21 03:17:28 +00001058 // For non-aggregates, we can store the appropriate null constant.
1059 llvm::Value *null = CGF.CGM.EmitNullConstant(type);
Eli Friedmanb1e3f322012-02-22 05:38:59 +00001060 // Note that the following is not equivalent to
1061 // EmitStoreThroughBitfieldLValue for ARC types.
Eli Friedman5a13d4d2012-02-24 23:53:49 +00001062 if (lv.isBitField()) {
Eli Friedmanb1e3f322012-02-22 05:38:59 +00001063 CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv);
Eli Friedman5a13d4d2012-02-24 23:53:49 +00001064 } else {
1065 assert(lv.isSimple());
1066 CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true);
1067 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +00001068 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +00001069 // There's a potential optimization opportunity in combining
1070 // memsets; that would be easy for arrays, but relatively
1071 // difficult for structures with the current code.
John McCalla07398e2011-06-16 04:16:24 +00001072 CGF.EmitNullInitialization(lv.getAddress(), lv.getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +00001073 }
1074}
1075
Chris Lattnerf81557c2008-04-04 18:42:16 +00001076void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +00001077#if 0
Eli Friedman13a5be12009-12-04 01:30:56 +00001078 // FIXME: Assess perf here? Figure out what cases are worth optimizing here
1079 // (Length of globals? Chunks of zeroed-out space?).
Eli Friedmana385b3c2008-12-02 01:17:45 +00001080 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00001081 // If we can, prefer a copy from a global; this is a lot less code for long
1082 // globals, and it's easier for the current optimizers to analyze.
Eli Friedman13a5be12009-12-04 01:30:56 +00001083 if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
Eli Friedman994ffef2008-11-30 02:11:09 +00001084 llvm::GlobalVariable* GV =
Eli Friedman13a5be12009-12-04 01:30:56 +00001085 new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
1086 llvm::GlobalValue::InternalLinkage, C, "");
John McCalle0c11682012-07-02 23:58:38 +00001087 EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType()));
Eli Friedman994ffef2008-11-30 02:11:09 +00001088 return;
1089 }
Eli Friedmana385b3c2008-12-02 01:17:45 +00001090#endif
Chris Lattnerd0db03a2010-09-06 00:11:41 +00001091 if (E->hadArrayRangeDesignator())
Douglas Gregora9c87802009-01-29 19:42:23 +00001092 CGF.ErrorUnsupported(E, "GNU array range designator extension");
Douglas Gregora9c87802009-01-29 19:42:23 +00001093
Richard Smithe69fb202013-05-23 21:54:14 +00001094 AggValueSlot Dest = EnsureSlot(E->getType());
1095
Eli Friedman377ecc72012-04-16 03:54:45 +00001096 LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
1097 Dest.getAlignment());
John McCall558d2ab2010-09-15 10:14:12 +00001098
Chris Lattnerf81557c2008-04-04 18:42:16 +00001099 // Handle initialization of an array.
1100 if (E->getType()->isArrayType()) {
Richard Smithfe587202012-04-15 02:50:59 +00001101 if (E->isStringLiteralInit())
1102 return Visit(E->getInit(0));
Eli Friedman922696f2008-05-19 17:51:16 +00001103
Eli Friedman5c89c392012-02-23 02:25:10 +00001104 QualType elementType =
1105 CGF.getContext().getAsArrayType(E->getType())->getElementType();
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +00001106
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001107 llvm::PointerType *APType =
Eli Friedman377ecc72012-04-16 03:54:45 +00001108 cast<llvm::PointerType>(Dest.getAddr()->getType());
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001109 llvm::ArrayType *AType =
1110 cast<llvm::ArrayType>(APType->getElementType());
Chris Lattner1b726772010-12-02 07:07:26 +00001111
Eli Friedman377ecc72012-04-16 03:54:45 +00001112 EmitArrayInit(Dest.getAddr(), AType, elementType, E);
Chris Lattnerf81557c2008-04-04 18:42:16 +00001113 return;
1114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Chris Lattnerf81557c2008-04-04 18:42:16 +00001116 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Chris Lattnerf81557c2008-04-04 18:42:16 +00001118 // Do struct initialization; this code just sets each individual member
1119 // to the approprate value. This makes bitfield support automatic;
1120 // the disadvantage is that the generated code is more difficult for
1121 // the optimizer, especially with bitfields.
1122 unsigned NumInitElements = E->getNumInits();
John McCall2b30dcf2011-07-11 19:35:02 +00001123 RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl();
Richard Smithc3bf52c2013-04-20 22:23:05 +00001124
1125 // Prepare a 'this' for CXXDefaultInitExprs.
1126 CodeGenFunction::FieldConstructionScope FCS(CGF, Dest.getAddr());
1127
John McCall2b30dcf2011-07-11 19:35:02 +00001128 if (record->isUnion()) {
Douglas Gregor0bb76892009-01-29 16:53:55 +00001129 // Only initialize one field of a union. The field itself is
1130 // specified by the initializer list.
1131 if (!E->getInitializedFieldInUnion()) {
1132 // Empty union; we have nothing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Douglas Gregor0bb76892009-01-29 16:53:55 +00001134#ifndef NDEBUG
1135 // Make sure that it's really an empty and not a failure of
1136 // semantic analysis.
John McCall2b30dcf2011-07-11 19:35:02 +00001137 for (RecordDecl::field_iterator Field = record->field_begin(),
1138 FieldEnd = record->field_end();
Douglas Gregor0bb76892009-01-29 16:53:55 +00001139 Field != FieldEnd; ++Field)
1140 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
1141#endif
1142 return;
1143 }
1144
1145 // FIXME: volatility
1146 FieldDecl *Field = E->getInitializedFieldInUnion();
Douglas Gregor0bb76892009-01-29 16:53:55 +00001147
Eli Friedman377ecc72012-04-16 03:54:45 +00001148 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field);
Douglas Gregor0bb76892009-01-29 16:53:55 +00001149 if (NumInitElements) {
1150 // Store the initializer into the field
Chad Rosier649b4a12012-03-29 17:37:10 +00001151 EmitInitializationToLValue(E->getInit(0), FieldLoc);
Douglas Gregor0bb76892009-01-29 16:53:55 +00001152 } else {
Chris Lattner1b726772010-12-02 07:07:26 +00001153 // Default-initialize to null.
John McCalla07398e2011-06-16 04:16:24 +00001154 EmitNullInitializationToLValue(FieldLoc);
Douglas Gregor0bb76892009-01-29 16:53:55 +00001155 }
1156
1157 return;
1158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
John McCall2b30dcf2011-07-11 19:35:02 +00001160 // We'll need to enter cleanup scopes in case any of the member
1161 // initializers throw an exception.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001162 SmallVector<EHScopeStack::stable_iterator, 16> cleanups;
John McCall6f103ba2011-11-10 10:43:54 +00001163 llvm::Instruction *cleanupDominator = 0;
John McCall2b30dcf2011-07-11 19:35:02 +00001164
Chris Lattnerf81557c2008-04-04 18:42:16 +00001165 // Here we iterate over the fields; this makes it simpler to both
1166 // default-initialize fields and skip over unnamed fields.
John McCall2b30dcf2011-07-11 19:35:02 +00001167 unsigned curInitIndex = 0;
1168 for (RecordDecl::field_iterator field = record->field_begin(),
1169 fieldEnd = record->field_end();
1170 field != fieldEnd; ++field) {
1171 // We're done once we hit the flexible array member.
1172 if (field->getType()->isIncompleteArrayType())
Douglas Gregor44b43212008-12-11 16:49:14 +00001173 break;
1174
John McCall2b30dcf2011-07-11 19:35:02 +00001175 // Always skip anonymous bitfields.
1176 if (field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +00001177 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +00001178
John McCall2b30dcf2011-07-11 19:35:02 +00001179 // We're done if we reach the end of the explicit initializers, we
1180 // have a zeroed object, and the rest of the fields are
1181 // zero-initializable.
1182 if (curInitIndex == NumInitElements && Dest.isZeroed() &&
Chris Lattner1b726772010-12-02 07:07:26 +00001183 CGF.getTypes().isZeroInitializable(E->getType()))
1184 break;
1185
Eli Friedman377ecc72012-04-16 03:54:45 +00001186
David Blaikie581deb32012-06-06 20:45:41 +00001187 LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, *field);
Fariborz Jahanian14674ff2009-05-27 19:54:11 +00001188 // We never generate write-barries for initialized fields.
John McCall2b30dcf2011-07-11 19:35:02 +00001189 LV.setNonGC(true);
Chris Lattner1b726772010-12-02 07:07:26 +00001190
John McCall2b30dcf2011-07-11 19:35:02 +00001191 if (curInitIndex < NumInitElements) {
Chris Lattnerb35baae2010-03-08 21:08:07 +00001192 // Store the initializer into the field.
Chad Rosier649b4a12012-03-29 17:37:10 +00001193 EmitInitializationToLValue(E->getInit(curInitIndex++), LV);
Chris Lattnerf81557c2008-04-04 18:42:16 +00001194 } else {
1195 // We're out of initalizers; default-initialize to null
John McCall2b30dcf2011-07-11 19:35:02 +00001196 EmitNullInitializationToLValue(LV);
1197 }
1198
1199 // Push a destructor if necessary.
1200 // FIXME: if we have an array of structures, all explicitly
1201 // initialized, we can end up pushing a linear number of cleanups.
1202 bool pushedCleanup = false;
1203 if (QualType::DestructionKind dtorKind
1204 = field->getType().isDestructedType()) {
1205 assert(LV.isSimple());
1206 if (CGF.needsEHCleanup(dtorKind)) {
John McCall6f103ba2011-11-10 10:43:54 +00001207 if (!cleanupDominator)
1208 cleanupDominator = CGF.Builder.CreateUnreachable(); // placeholder
1209
John McCall2b30dcf2011-07-11 19:35:02 +00001210 CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(),
1211 CGF.getDestroyer(dtorKind), false);
1212 cleanups.push_back(CGF.EHStack.stable_begin());
1213 pushedCleanup = true;
1214 }
Chris Lattnerf81557c2008-04-04 18:42:16 +00001215 }
Chris Lattner1b726772010-12-02 07:07:26 +00001216
1217 // If the GEP didn't get used because of a dead zero init or something
1218 // else, clean it up for -O0 builds and general tidiness.
John McCall2b30dcf2011-07-11 19:35:02 +00001219 if (!pushedCleanup && LV.isSimple())
Chris Lattner1b726772010-12-02 07:07:26 +00001220 if (llvm::GetElementPtrInst *GEP =
John McCall2b30dcf2011-07-11 19:35:02 +00001221 dyn_cast<llvm::GetElementPtrInst>(LV.getAddress()))
Chris Lattner1b726772010-12-02 07:07:26 +00001222 if (GEP->use_empty())
1223 GEP->eraseFromParent();
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +00001224 }
John McCall2b30dcf2011-07-11 19:35:02 +00001225
1226 // Deactivate all the partial cleanups in reverse order, which
1227 // generally means popping them.
1228 for (unsigned i = cleanups.size(); i != 0; --i)
John McCall6f103ba2011-11-10 10:43:54 +00001229 CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator);
1230
1231 // Destroy the placeholder if we made one.
1232 if (cleanupDominator)
1233 cleanupDominator->eraseFromParent();
Devang Patel636c3d02007-10-26 17:44:44 +00001234}
1235
Chris Lattneree755f92007-08-21 04:59:27 +00001236//===----------------------------------------------------------------------===//
1237// Entry Points into this File
1238//===----------------------------------------------------------------------===//
1239
Chris Lattner1b726772010-12-02 07:07:26 +00001240/// GetNumNonZeroBytesInInit - Get an approximate count of the number of
1241/// non-zero bytes that will be stored when outputting the initializer for the
1242/// specified initializer expression.
Ken Dyck02c45332011-04-24 17:17:56 +00001243static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
Peter Collingbournef111d932011-04-15 00:35:48 +00001244 E = E->IgnoreParens();
Chris Lattner1b726772010-12-02 07:07:26 +00001245
1246 // 0 and 0.0 won't require any non-zero stores!
Ken Dyck02c45332011-04-24 17:17:56 +00001247 if (isSimpleZero(E, CGF)) return CharUnits::Zero();
Chris Lattner1b726772010-12-02 07:07:26 +00001248
1249 // If this is an initlist expr, sum up the size of sizes of the (present)
1250 // elements. If this is something weird, assume the whole thing is non-zero.
1251 const InitListExpr *ILE = dyn_cast<InitListExpr>(E);
1252 if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType()))
Ken Dyck02c45332011-04-24 17:17:56 +00001253 return CGF.getContext().getTypeSizeInChars(E->getType());
Chris Lattner1b726772010-12-02 07:07:26 +00001254
Chris Lattnerd1d56df2010-12-02 18:29:00 +00001255 // InitListExprs for structs have to be handled carefully. If there are
1256 // reference members, we need to consider the size of the reference, not the
1257 // referencee. InitListExprs for unions and arrays can't have references.
Chris Lattner8c00ad12010-12-02 22:52:04 +00001258 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
1259 if (!RT->isUnionType()) {
1260 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
Ken Dyck02c45332011-04-24 17:17:56 +00001261 CharUnits NumNonZeroBytes = CharUnits::Zero();
Chris Lattner8c00ad12010-12-02 22:52:04 +00001262
1263 unsigned ILEElement = 0;
1264 for (RecordDecl::field_iterator Field = SD->field_begin(),
1265 FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) {
1266 // We're done once we hit the flexible array member or run out of
1267 // InitListExpr elements.
1268 if (Field->getType()->isIncompleteArrayType() ||
1269 ILEElement == ILE->getNumInits())
1270 break;
1271 if (Field->isUnnamedBitfield())
1272 continue;
Chris Lattnerd1d56df2010-12-02 18:29:00 +00001273
Chris Lattner8c00ad12010-12-02 22:52:04 +00001274 const Expr *E = ILE->getInit(ILEElement++);
1275
1276 // Reference values are always non-null and have the width of a pointer.
1277 if (Field->getType()->isReferenceType())
Ken Dyck02c45332011-04-24 17:17:56 +00001278 NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits(
John McCall64aa4b32013-04-16 22:48:15 +00001279 CGF.getTarget().getPointerWidth(0));
Chris Lattner8c00ad12010-12-02 22:52:04 +00001280 else
1281 NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
1282 }
Chris Lattnerd1d56df2010-12-02 18:29:00 +00001283
Chris Lattner8c00ad12010-12-02 22:52:04 +00001284 return NumNonZeroBytes;
Chris Lattnerd1d56df2010-12-02 18:29:00 +00001285 }
Chris Lattnerd1d56df2010-12-02 18:29:00 +00001286 }
1287
1288
Ken Dyck02c45332011-04-24 17:17:56 +00001289 CharUnits NumNonZeroBytes = CharUnits::Zero();
Chris Lattner1b726772010-12-02 07:07:26 +00001290 for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
1291 NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);
1292 return NumNonZeroBytes;
1293}
1294
1295/// CheckAggExprForMemSetUse - If the initializer is large and has a lot of
1296/// zeros in it, emit a memset and avoid storing the individual zeros.
1297///
1298static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
1299 CodeGenFunction &CGF) {
1300 // If the slot is already known to be zeroed, nothing to do. Don't mess with
1301 // volatile stores.
1302 if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return;
Argyrios Kyrtzidis657baf12011-04-28 22:57:55 +00001303
1304 // C++ objects with a user-declared constructor don't need zero'ing.
Richard Smith7edf9e32012-11-01 22:30:59 +00001305 if (CGF.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis657baf12011-04-28 22:57:55 +00001306 if (const RecordType *RT = CGF.getContext()
1307 .getBaseElementType(E->getType())->getAs<RecordType>()) {
1308 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1309 if (RD->hasUserDeclaredConstructor())
1310 return;
1311 }
1312
Chris Lattner1b726772010-12-02 07:07:26 +00001313 // If the type is 16-bytes or smaller, prefer individual stores over memset.
Ken Dyck5ff1a352011-04-24 17:25:32 +00001314 std::pair<CharUnits, CharUnits> TypeInfo =
1315 CGF.getContext().getTypeInfoInChars(E->getType());
1316 if (TypeInfo.first <= CharUnits::fromQuantity(16))
Chris Lattner1b726772010-12-02 07:07:26 +00001317 return;
1318
1319 // Check to see if over 3/4 of the initializer are known to be zero. If so,
1320 // we prefer to emit memset + individual stores for the rest.
Ken Dyck5ff1a352011-04-24 17:25:32 +00001321 CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF);
1322 if (NumNonZeroBytes*4 > TypeInfo.first)
Chris Lattner1b726772010-12-02 07:07:26 +00001323 return;
1324
1325 // Okay, it seems like a good idea to use an initial memset, emit the call.
Ken Dyck5ff1a352011-04-24 17:25:32 +00001326 llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity());
1327 CharUnits Align = TypeInfo.second;
Chris Lattner1b726772010-12-02 07:07:26 +00001328
1329 llvm::Value *Loc = Slot.getAddr();
Chris Lattner1b726772010-12-02 07:07:26 +00001330
Chris Lattner8b418682012-02-07 00:39:47 +00001331 Loc = CGF.Builder.CreateBitCast(Loc, CGF.Int8PtrTy);
Ken Dyck5ff1a352011-04-24 17:25:32 +00001332 CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal,
1333 Align.getQuantity(), false);
Chris Lattner1b726772010-12-02 07:07:26 +00001334
1335 // Tell the AggExprEmitter that the slot is known zero.
1336 Slot.setZeroed();
1337}
1338
1339
1340
1341
Mike Stumpe1129a92009-05-26 18:57:45 +00001342/// EmitAggExpr - Emit the computation of the specified expression of aggregate
1343/// type. The result is computed into DestPtr. Note that if DestPtr is null,
1344/// the value of the aggregate expression is not needed. If VolatileDest is
1345/// true, DestPtr cannot be 0.
John McCalle0c11682012-07-02 23:58:38 +00001346void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) {
John McCall9d232c82013-03-07 21:37:08 +00001347 assert(E && hasAggregateEvaluationKind(E->getType()) &&
Chris Lattneree755f92007-08-21 04:59:27 +00001348 "Invalid aggregate expression to emit");
Chris Lattner1b726772010-12-02 07:07:26 +00001349 assert((Slot.getAddr() != 0 || Slot.isIgnored()) &&
1350 "slot has bits but no address");
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Chris Lattner1b726772010-12-02 07:07:26 +00001352 // Optimize the slot if possible.
1353 CheckAggExprForMemSetUse(Slot, E, *this);
1354
John McCalle0c11682012-07-02 23:58:38 +00001355 AggExprEmitter(*this, Slot).Visit(const_cast<Expr*>(E));
Chris Lattneree755f92007-08-21 04:59:27 +00001356}
Daniel Dunbar7482d122008-09-09 20:49:46 +00001357
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001358LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
John McCall9d232c82013-03-07 21:37:08 +00001359 assert(hasAggregateEvaluationKind(E->getType()) && "Invalid argument!");
Daniel Dunbar195337d2010-02-09 02:48:28 +00001360 llvm::Value *Temp = CreateMemTemp(E->getType());
Daniel Dunbar79c39282010-08-21 03:15:20 +00001361 LValue LV = MakeAddrLValue(Temp, E->getType());
John McCall7c2349b2011-08-25 20:40:09 +00001362 EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed,
John McCall44184392011-08-26 07:31:35 +00001363 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier649b4a12012-03-29 17:37:10 +00001364 AggValueSlot::IsNotAliased));
Daniel Dunbar79c39282010-08-21 03:15:20 +00001365 return LV;
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001366}
1367
Chad Rosier649b4a12012-03-29 17:37:10 +00001368void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
1369 llvm::Value *SrcPtr, QualType Ty,
John McCalle0c11682012-07-02 23:58:38 +00001370 bool isVolatile,
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001371 CharUnits alignment,
1372 bool isAssignment) {
Chad Rosier649b4a12012-03-29 17:37:10 +00001373 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Richard Smith7edf9e32012-11-01 22:30:59 +00001375 if (getLangOpts().CPlusPlus) {
Chad Rosier649b4a12012-03-29 17:37:10 +00001376 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1377 CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
1378 assert((Record->hasTrivialCopyConstructor() ||
1379 Record->hasTrivialCopyAssignment() ||
1380 Record->hasTrivialMoveConstructor() ||
1381 Record->hasTrivialMoveAssignment()) &&
Richard Smith426391c2012-11-16 00:53:38 +00001382 "Trying to aggregate-copy a type without a trivial copy/move "
Douglas Gregore9979482010-05-20 15:39:01 +00001383 "constructor or assignment operator");
Chad Rosier649b4a12012-03-29 17:37:10 +00001384 // Ignore empty classes in C++.
1385 if (Record->isEmpty())
Anders Carlsson0d7c5832010-05-03 01:20:20 +00001386 return;
1387 }
1388 }
1389
Chris Lattner83c96292009-02-28 18:31:01 +00001390 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +00001391 // C99 6.5.16.1p3, which states "If the value being stored in an object is
1392 // read from another object that overlaps in anyway the storage of the first
1393 // object, then the overlap shall be exact and the two objects shall have
1394 // qualified or unqualified versions of a compatible type."
1395 //
Chris Lattner83c96292009-02-28 18:31:01 +00001396 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +00001397 // equal, but other compilers do this optimization, and almost every memcpy
1398 // implementation handles this case safely. If there is a libc that does not
1399 // safely handle this, we can add a target hook.
Chad Rosier649b4a12012-03-29 17:37:10 +00001400
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001401 // Get data size and alignment info for this aggregate. If this is an
1402 // assignment don't copy the tail padding. Otherwise copying it is fine.
1403 std::pair<CharUnits, CharUnits> TypeInfo;
1404 if (isAssignment)
1405 TypeInfo = getContext().getTypeInfoDataSizeInChars(Ty);
1406 else
1407 TypeInfo = getContext().getTypeInfoInChars(Ty);
Chad Rosier649b4a12012-03-29 17:37:10 +00001408
John McCalle0c11682012-07-02 23:58:38 +00001409 if (alignment.isZero())
1410 alignment = TypeInfo.second;
Chad Rosier649b4a12012-03-29 17:37:10 +00001411
1412 // FIXME: Handle variable sized types.
1413
1414 // FIXME: If we have a volatile struct, the optimizer can remove what might
1415 // appear to be `extra' memory ops:
1416 //
1417 // volatile struct { int i; } a, b;
1418 //
1419 // int main() {
1420 // a = b;
1421 // a = b;
1422 // }
1423 //
1424 // we need to use a different call here. We use isVolatile to indicate when
1425 // either the source or the destination is volatile.
1426
1427 llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
1428 llvm::Type *DBP =
1429 llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace());
1430 DestPtr = Builder.CreateBitCast(DestPtr, DBP);
1431
1432 llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
1433 llvm::Type *SBP =
1434 llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace());
1435 SrcPtr = Builder.CreateBitCast(SrcPtr, SBP);
1436
1437 // Don't do any of the memmove_collectable tests if GC isn't set.
1438 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
1439 // fall through
1440 } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1441 RecordDecl *Record = RecordTy->getDecl();
1442 if (Record->hasObjectMember()) {
1443 CharUnits size = TypeInfo.first;
1444 llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
1445 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity());
1446 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
1447 SizeVal);
1448 return;
1449 }
1450 } else if (Ty->isArrayType()) {
1451 QualType BaseType = getContext().getBaseElementType(Ty);
1452 if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
1453 if (RecordTy->getDecl()->hasObjectMember()) {
1454 CharUnits size = TypeInfo.first;
1455 llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
1456 llvm::Value *SizeVal =
1457 llvm::ConstantInt::get(SizeTy, size.getQuantity());
1458 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
1459 SizeVal);
1460 return;
1461 }
1462 }
1463 }
Dan Gohmanb22c7dc2012-09-28 21:58:29 +00001464
1465 // Determine the metadata to describe the position of any padding in this
1466 // memcpy, as well as the TBAA tags for the members of the struct, in case
1467 // the optimizer wishes to expand it in to scalar memory operations.
1468 llvm::MDNode *TBAAStructTag = CGM.getTBAAStructInfo(Ty);
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001469
Chad Rosier649b4a12012-03-29 17:37:10 +00001470 Builder.CreateMemCpy(DestPtr, SrcPtr,
1471 llvm::ConstantInt::get(IntPtrTy,
1472 TypeInfo.first.getQuantity()),
Dan Gohmanb22c7dc2012-09-28 21:58:29 +00001473 alignment.getQuantity(), isVolatile,
1474 /*TBAATag=*/0, TBAAStructTag);
Daniel Dunbar7482d122008-09-09 20:49:46 +00001475}