Chris Lattner | 566b6ce | 2007-08-24 02:22:53 +0000 | [diff] [blame] | 1 | //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===// |
Chris Lattner | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Aggregate Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 15 | #include "CGObjCRuntime.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/Function.h" |
| 23 | #include "llvm/IR/GlobalVariable.h" |
| 24 | #include "llvm/IR/Intrinsics.h" |
Chris Lattner | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // Aggregate Expression Emitter |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 33 | class AggExprEmitter : public StmtVisitor<AggExprEmitter> { |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 34 | CodeGenFunction &CGF; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 35 | CGBuilderTy &Builder; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 36 | AggValueSlot Dest; |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 37 | |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 38 | /// 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 McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 47 | ReturnValueSlot getReturnValueSlot() const { |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 48 | if (!shouldUseDestForReturnSlot()) |
| 49 | return ReturnValueSlot(); |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 50 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 51 | 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 McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 57 | } |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 58 | void EnsureDest(QualType T) { |
| 59 | if (!Dest.isIgnored()) return; |
| 60 | Dest = CGF.CreateAggTemp(T, "agg.tmp.ensured"); |
| 61 | } |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 63 | public: |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 64 | AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest) |
| 65 | : CGF(cgf), Builder(CGF.Builder), Dest(Dest) { |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 68 | //===--------------------------------------------------------------------===// |
| 69 | // Utilities |
| 70 | //===--------------------------------------------------------------------===// |
| 71 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 72 | /// 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 Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 76 | |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 77 | /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 78 | 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 Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 83 | |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 84 | void EmitMoveFromReturnSlot(const Expr *E, RValue Src); |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 85 | |
Sebastian Redl | af130fd | 2012-02-19 12:28:02 +0000 | [diff] [blame] | 86 | void EmitStdInitializerList(llvm::Value *DestPtr, InitListExpr *InitList); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 87 | void EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType, |
| 88 | QualType elementType, InitListExpr *E); |
| 89 | |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 90 | AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 91 | if (CGF.getLangOpts().getGC() && TypeRequiresGCollection(T)) |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 92 | return AggValueSlot::NeedsGCBarriers; |
| 93 | return AggValueSlot::DoesNotNeedGCBarriers; |
| 94 | } |
| 95 | |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 96 | bool TypeRequiresGCollection(QualType T); |
| 97 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 98 | //===--------------------------------------------------------------------===// |
| 99 | // Visitor Methods |
| 100 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 102 | void VisitStmt(Stmt *S) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 103 | CGF.ErrorUnsupported(S, "aggregate expression"); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 104 | } |
| 105 | void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 106 | void VisitGenericSelectionExpr(GenericSelectionExpr *GE) { |
| 107 | Visit(GE->getResultExpr()); |
| 108 | } |
Eli Friedman | 12444a2 | 2009-01-27 09:03:41 +0000 | [diff] [blame] | 109 | void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 110 | void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) { |
| 111 | return Visit(E->getReplacement()); |
| 112 | } |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 113 | |
| 114 | // l-values. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 115 | void VisitDeclRefExpr(DeclRefExpr *E) { |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 116 | // For aggregates, we should always be able to emit the variable |
| 117 | // as an l-value unless it's a reference. This is due to the fact |
| 118 | // that we can't actually ever see a normal l2r conversion on an |
| 119 | // aggregate in C++, and in C there's no language standard |
| 120 | // actively preventing us from listing variables in the captures |
| 121 | // list of a block. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 122 | if (E->getDecl()->getType()->isReferenceType()) { |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 123 | if (CodeGenFunction::ConstantEmission result |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 124 | = CGF.tryEmitAsConstant(E)) { |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 125 | EmitFinalDestCopy(E->getType(), result.getReferenceLValue(CGF, E)); |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 130 | EmitAggLoadOfLValue(E); |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 133 | void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } |
| 134 | void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } |
Daniel Dunbar | 5be028f | 2010-01-04 18:47:06 +0000 | [diff] [blame] | 135 | void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 136 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 137 | void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 138 | EmitAggLoadOfLValue(E); |
| 139 | } |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 140 | void VisitPredefinedExpr(const PredefinedExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | EmitAggLoadOfLValue(E); |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 144 | // Operators. |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 145 | void VisitCastExpr(CastExpr *E); |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 146 | void VisitCallExpr(const CallExpr *E); |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 147 | void VisitStmtExpr(const StmtExpr *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 148 | void VisitBinaryOperator(const BinaryOperator *BO); |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 149 | void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 150 | void VisitBinAssign(const BinaryOperator *E); |
Eli Friedman | 07fa52a | 2008-05-20 07:56:31 +0000 | [diff] [blame] | 151 | void VisitBinComma(const BinaryOperator *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 153 | void VisitObjCMessageExpr(ObjCMessageExpr *E); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 154 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 155 | EmitAggLoadOfLValue(E); |
| 156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 158 | void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); |
Anders Carlsson | a294ca8 | 2009-07-08 18:33:14 +0000 | [diff] [blame] | 159 | void VisitChooseExpr(const ChooseExpr *CE); |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 160 | void VisitInitListExpr(InitListExpr *E); |
Anders Carlsson | 30311fa | 2009-12-16 06:57:54 +0000 | [diff] [blame] | 161 | void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 162 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 163 | Visit(DAE->getExpr()); |
| 164 | } |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 165 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 166 | void VisitCXXConstructExpr(const CXXConstructExpr *E); |
Eli Friedman | 4c5d8af | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 167 | void VisitLambdaExpr(LambdaExpr *E); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 168 | void VisitExprWithCleanups(ExprWithCleanups *E); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 169 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Mike Stump | 2710c41 | 2009-11-18 00:40:12 +0000 | [diff] [blame] | 170 | void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 171 | void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 172 | void VisitOpaqueValueExpr(OpaqueValueExpr *E); |
| 173 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 174 | void VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 175 | if (E->isGLValue()) { |
| 176 | LValue LV = CGF.EmitPseudoObjectLValue(E); |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 177 | return EmitFinalDestCopy(E->getType(), LV); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType())); |
| 181 | } |
| 182 | |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 183 | void VisitVAArgExpr(VAArgExpr *E); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 184 | |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 185 | void EmitInitializationToLValue(Expr *E, LValue Address); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 186 | void EmitNullInitializationToLValue(LValue Address); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 187 | // case Expr::ChooseExprClass: |
Mike Stump | 39406b1 | 2009-12-09 19:24:08 +0000 | [diff] [blame] | 188 | void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 189 | void VisitAtomicExpr(AtomicExpr *E) { |
| 190 | CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr()); |
| 191 | } |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 192 | }; |
| 193 | } // end anonymous namespace. |
| 194 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 195 | //===----------------------------------------------------------------------===// |
| 196 | // Utilities |
| 197 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 199 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 200 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 201 | /// then loads the result into DestPtr. |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 202 | void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { |
| 203 | LValue LV = CGF.EmitLValue(E); |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 204 | EmitFinalDestCopy(E->getType(), LV); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 205 | } |
| 206 | |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 207 | /// \brief True if the given aggregate type requires special GC API calls. |
| 208 | bool AggExprEmitter::TypeRequiresGCollection(QualType T) { |
| 209 | // Only record types have members that might require garbage collection. |
| 210 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 211 | if (!RecordTy) return false; |
| 212 | |
| 213 | // Don't mess with non-trivial C++ types. |
| 214 | RecordDecl *Record = RecordTy->getDecl(); |
| 215 | if (isa<CXXRecordDecl>(Record) && |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 216 | (cast<CXXRecordDecl>(Record)->hasNonTrivialCopyConstructor() || |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 217 | !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) |
| 218 | return false; |
| 219 | |
| 220 | // Check whether the type has an object member. |
| 221 | return Record->hasObjectMember(); |
| 222 | } |
| 223 | |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 224 | /// \brief Perform the final move to DestPtr if for some reason |
| 225 | /// getReturnValueSlot() didn't use it directly. |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 226 | /// |
| 227 | /// The idea is that you do something like this: |
| 228 | /// RValue Result = EmitSomething(..., getReturnValueSlot()); |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 229 | /// EmitMoveFromReturnSlot(E, Result); |
| 230 | /// |
| 231 | /// If nothing interferes, this will cause the result to be emitted |
| 232 | /// directly into the return value slot. Otherwise, a final move |
| 233 | /// will be performed. |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 234 | void AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue src) { |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 235 | if (shouldUseDestForReturnSlot()) { |
| 236 | // Logically, Dest.getAddr() should equal Src.getAggregateAddr(). |
| 237 | // The possibility of undef rvalues complicates that a lot, |
| 238 | // though, so we can't really assert. |
| 239 | return; |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 240 | } |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 241 | |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 242 | // Otherwise, copy from there to the destination. |
| 243 | assert(Dest.getAddr() != src.getAggregateAddr()); |
| 244 | std::pair<CharUnits, CharUnits> typeInfo = |
Chad Rosier | 26397ed | 2012-04-17 01:14:29 +0000 | [diff] [blame] | 245 | CGF.getContext().getTypeInfoInChars(E->getType()); |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 246 | EmitFinalDestCopy(E->getType(), src, typeInfo.second); |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 249 | /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 250 | void AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src, |
| 251 | CharUnits srcAlign) { |
| 252 | assert(src.isAggregate() && "value must be aggregate value!"); |
| 253 | LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddr(), type, srcAlign); |
| 254 | EmitFinalDestCopy(type, srcLV); |
| 255 | } |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 256 | |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 257 | /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. |
| 258 | void AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src) { |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 259 | // If Dest is ignored, then we're evaluating an aggregate expression |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 260 | // in a context that doesn't care about the result. Note that loads |
| 261 | // from volatile l-values force the existence of a non-ignored |
| 262 | // destination. |
| 263 | if (Dest.isIgnored()) |
| 264 | return; |
Fariborz Jahanian | 8a97005 | 2010-10-22 22:05:03 +0000 | [diff] [blame] | 265 | |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 266 | AggValueSlot srcAgg = |
| 267 | AggValueSlot::forLValue(src, AggValueSlot::IsDestructed, |
| 268 | needsGC(type), AggValueSlot::IsAliased); |
| 269 | EmitCopy(type, Dest, srcAgg); |
| 270 | } |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 271 | |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 272 | /// Perform a copy from the source into the destination. |
| 273 | /// |
| 274 | /// \param type - the type of the aggregate being copied; qualifiers are |
| 275 | /// ignored |
| 276 | void AggExprEmitter::EmitCopy(QualType type, const AggValueSlot &dest, |
| 277 | const AggValueSlot &src) { |
| 278 | if (dest.requiresGCollection()) { |
| 279 | CharUnits sz = CGF.getContext().getTypeSizeInChars(type); |
| 280 | llvm::Value *size = llvm::ConstantInt::get(CGF.SizeTy, sz.getQuantity()); |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 281 | CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 282 | dest.getAddr(), |
| 283 | src.getAddr(), |
| 284 | size); |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 285 | return; |
| 286 | } |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 287 | |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 288 | // If the result of the assignment is used, copy the LHS there also. |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 289 | // It's volatile if either side is. Use the minimum alignment of |
| 290 | // the two sides. |
| 291 | CGF.EmitAggregateCopy(dest.getAddr(), src.getAddr(), type, |
| 292 | dest.isVolatile() || src.isVolatile(), |
| 293 | std::min(dest.getAlignment(), src.getAlignment())); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 296 | static QualType GetStdInitializerListElementType(QualType T) { |
| 297 | // Just assume that this is really std::initializer_list. |
| 298 | ClassTemplateSpecializationDecl *specialization = |
| 299 | cast<ClassTemplateSpecializationDecl>(T->castAs<RecordType>()->getDecl()); |
| 300 | return specialization->getTemplateArgs()[0].getAsType(); |
| 301 | } |
| 302 | |
| 303 | /// \brief Prepare cleanup for the temporary array. |
| 304 | static void EmitStdInitializerListCleanup(CodeGenFunction &CGF, |
| 305 | QualType arrayType, |
| 306 | llvm::Value *addr, |
| 307 | const InitListExpr *initList) { |
| 308 | QualType::DestructionKind dtorKind = arrayType.isDestructedType(); |
| 309 | if (!dtorKind) |
| 310 | return; // Type doesn't need destroying. |
| 311 | if (dtorKind != QualType::DK_cxx_destructor) { |
| 312 | CGF.ErrorUnsupported(initList, "ObjC ARC type in initializer_list"); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | CodeGenFunction::Destroyer *destroyer = CGF.getDestroyer(dtorKind); |
| 317 | CGF.pushDestroy(NormalAndEHCleanup, addr, arrayType, destroyer, |
| 318 | /*EHCleanup=*/true); |
| 319 | } |
| 320 | |
| 321 | /// \brief Emit the initializer for a std::initializer_list initialized with a |
| 322 | /// real initializer list. |
Sebastian Redl | af130fd | 2012-02-19 12:28:02 +0000 | [diff] [blame] | 323 | void AggExprEmitter::EmitStdInitializerList(llvm::Value *destPtr, |
| 324 | InitListExpr *initList) { |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 325 | // We emit an array containing the elements, then have the init list point |
| 326 | // at the array. |
| 327 | ASTContext &ctx = CGF.getContext(); |
| 328 | unsigned numInits = initList->getNumInits(); |
| 329 | QualType element = GetStdInitializerListElementType(initList->getType()); |
| 330 | llvm::APInt size(ctx.getTypeSize(ctx.getSizeType()), numInits); |
| 331 | QualType array = ctx.getConstantArrayType(element, size, ArrayType::Normal,0); |
| 332 | llvm::Type *LTy = CGF.ConvertTypeForMem(array); |
| 333 | llvm::AllocaInst *alloc = CGF.CreateTempAlloca(LTy); |
| 334 | alloc->setAlignment(ctx.getTypeAlignInChars(array).getQuantity()); |
| 335 | alloc->setName(".initlist."); |
| 336 | |
| 337 | EmitArrayInit(alloc, cast<llvm::ArrayType>(LTy), element, initList); |
| 338 | |
| 339 | // FIXME: The diagnostics are somewhat out of place here. |
| 340 | RecordDecl *record = initList->getType()->castAs<RecordType>()->getDecl(); |
| 341 | RecordDecl::field_iterator field = record->field_begin(); |
| 342 | if (field == record->field_end()) { |
| 343 | CGF.ErrorUnsupported(initList, "weird std::initializer_list"); |
Sebastian Redl | babcf9d | 2012-02-25 20:51:13 +0000 | [diff] [blame] | 344 | return; |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | QualType elementPtr = ctx.getPointerType(element.withConst()); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 348 | |
| 349 | // Start pointer. |
| 350 | if (!ctx.hasSameType(field->getType(), elementPtr)) { |
| 351 | CGF.ErrorUnsupported(initList, "weird std::initializer_list"); |
Sebastian Redl | babcf9d | 2012-02-25 20:51:13 +0000 | [diff] [blame] | 352 | return; |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 353 | } |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 354 | LValue DestLV = CGF.MakeNaturalAlignAddrLValue(destPtr, initList->getType()); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 355 | LValue start = CGF.EmitLValueForFieldInitialization(DestLV, *field); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 356 | llvm::Value *arrayStart = Builder.CreateStructGEP(alloc, 0, "arraystart"); |
| 357 | CGF.EmitStoreThroughLValue(RValue::get(arrayStart), start); |
| 358 | ++field; |
| 359 | |
| 360 | if (field == record->field_end()) { |
| 361 | CGF.ErrorUnsupported(initList, "weird std::initializer_list"); |
Sebastian Redl | babcf9d | 2012-02-25 20:51:13 +0000 | [diff] [blame] | 362 | return; |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 363 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 364 | LValue endOrLength = CGF.EmitLValueForFieldInitialization(DestLV, *field); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 365 | if (ctx.hasSameType(field->getType(), elementPtr)) { |
| 366 | // End pointer. |
| 367 | llvm::Value *arrayEnd = Builder.CreateStructGEP(alloc,numInits, "arrayend"); |
| 368 | CGF.EmitStoreThroughLValue(RValue::get(arrayEnd), endOrLength); |
| 369 | } else if(ctx.hasSameType(field->getType(), ctx.getSizeType())) { |
| 370 | // Length. |
| 371 | CGF.EmitStoreThroughLValue(RValue::get(Builder.getInt(size)), endOrLength); |
| 372 | } else { |
| 373 | CGF.ErrorUnsupported(initList, "weird std::initializer_list"); |
Sebastian Redl | babcf9d | 2012-02-25 20:51:13 +0000 | [diff] [blame] | 374 | return; |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | if (!Dest.isExternallyDestructed()) |
| 378 | EmitStdInitializerListCleanup(CGF, array, alloc, initList); |
| 379 | } |
| 380 | |
| 381 | /// \brief Emit initialization of an array from an initializer list. |
| 382 | void AggExprEmitter::EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType, |
| 383 | QualType elementType, InitListExpr *E) { |
| 384 | uint64_t NumInitElements = E->getNumInits(); |
| 385 | |
| 386 | uint64_t NumArrayElements = AType->getNumElements(); |
| 387 | assert(NumInitElements <= NumArrayElements); |
| 388 | |
| 389 | // DestPtr is an array*. Construct an elementType* by drilling |
| 390 | // down a level. |
| 391 | llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0); |
| 392 | llvm::Value *indices[] = { zero, zero }; |
| 393 | llvm::Value *begin = |
| 394 | Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin"); |
| 395 | |
| 396 | // Exception safety requires us to destroy all the |
| 397 | // already-constructed members if an initializer throws. |
| 398 | // For that, we'll need an EH cleanup. |
| 399 | QualType::DestructionKind dtorKind = elementType.isDestructedType(); |
| 400 | llvm::AllocaInst *endOfInit = 0; |
| 401 | EHScopeStack::stable_iterator cleanup; |
| 402 | llvm::Instruction *cleanupDominator = 0; |
| 403 | if (CGF.needsEHCleanup(dtorKind)) { |
| 404 | // In principle we could tell the cleanup where we are more |
| 405 | // directly, but the control flow can get so varied here that it |
| 406 | // would actually be quite complex. Therefore we go through an |
| 407 | // alloca. |
| 408 | endOfInit = CGF.CreateTempAlloca(begin->getType(), |
| 409 | "arrayinit.endOfInit"); |
| 410 | cleanupDominator = Builder.CreateStore(begin, endOfInit); |
| 411 | CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType, |
| 412 | CGF.getDestroyer(dtorKind)); |
| 413 | cleanup = CGF.EHStack.stable_begin(); |
| 414 | |
| 415 | // Otherwise, remember that we didn't need a cleanup. |
| 416 | } else { |
| 417 | dtorKind = QualType::DK_none; |
| 418 | } |
| 419 | |
| 420 | llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1); |
| 421 | |
| 422 | // The 'current element to initialize'. The invariants on this |
| 423 | // variable are complicated. Essentially, after each iteration of |
| 424 | // the loop, it points to the last initialized element, except |
| 425 | // that it points to the beginning of the array before any |
| 426 | // elements have been initialized. |
| 427 | llvm::Value *element = begin; |
| 428 | |
| 429 | // Emit the explicit initializers. |
| 430 | for (uint64_t i = 0; i != NumInitElements; ++i) { |
| 431 | // Advance to the next element. |
| 432 | if (i > 0) { |
| 433 | element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element"); |
| 434 | |
| 435 | // Tell the cleanup that it needs to destroy up to this |
| 436 | // element. TODO: some of these stores can be trivially |
| 437 | // observed to be unnecessary. |
| 438 | if (endOfInit) Builder.CreateStore(element, endOfInit); |
| 439 | } |
| 440 | |
Sebastian Redl | af130fd | 2012-02-19 12:28:02 +0000 | [diff] [blame] | 441 | // If these are nested std::initializer_list inits, do them directly, |
| 442 | // because they are conceptually the same "location". |
| 443 | InitListExpr *initList = dyn_cast<InitListExpr>(E->getInit(i)); |
| 444 | if (initList && initList->initializesStdInitializerList()) { |
| 445 | EmitStdInitializerList(element, initList); |
| 446 | } else { |
| 447 | LValue elementLV = CGF.MakeAddrLValue(element, elementType); |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 448 | EmitInitializationToLValue(E->getInit(i), elementLV); |
Sebastian Redl | af130fd | 2012-02-19 12:28:02 +0000 | [diff] [blame] | 449 | } |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // Check whether there's a non-trivial array-fill expression. |
| 453 | // Note that this will be a CXXConstructExpr even if the element |
| 454 | // type is an array (or array of array, etc.) of class type. |
| 455 | Expr *filler = E->getArrayFiller(); |
| 456 | bool hasTrivialFiller = true; |
| 457 | if (CXXConstructExpr *cons = dyn_cast_or_null<CXXConstructExpr>(filler)) { |
| 458 | assert(cons->getConstructor()->isDefaultConstructor()); |
| 459 | hasTrivialFiller = cons->getConstructor()->isTrivial(); |
| 460 | } |
| 461 | |
| 462 | // Any remaining elements need to be zero-initialized, possibly |
| 463 | // using the filler expression. We can skip this if the we're |
| 464 | // emitting to zeroed memory. |
| 465 | if (NumInitElements != NumArrayElements && |
| 466 | !(Dest.isZeroed() && hasTrivialFiller && |
| 467 | CGF.getTypes().isZeroInitializable(elementType))) { |
| 468 | |
| 469 | // Use an actual loop. This is basically |
| 470 | // do { *array++ = filler; } while (array != end); |
| 471 | |
| 472 | // Advance to the start of the rest of the array. |
| 473 | if (NumInitElements) { |
| 474 | element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start"); |
| 475 | if (endOfInit) Builder.CreateStore(element, endOfInit); |
| 476 | } |
| 477 | |
| 478 | // Compute the end of the array. |
| 479 | llvm::Value *end = Builder.CreateInBoundsGEP(begin, |
| 480 | llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements), |
| 481 | "arrayinit.end"); |
| 482 | |
| 483 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); |
| 484 | llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body"); |
| 485 | |
| 486 | // Jump into the body. |
| 487 | CGF.EmitBlock(bodyBB); |
| 488 | llvm::PHINode *currentElement = |
| 489 | Builder.CreatePHI(element->getType(), 2, "arrayinit.cur"); |
| 490 | currentElement->addIncoming(element, entryBB); |
| 491 | |
| 492 | // Emit the actual filler expression. |
| 493 | LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType); |
| 494 | if (filler) |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 495 | EmitInitializationToLValue(filler, elementLV); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 496 | else |
| 497 | EmitNullInitializationToLValue(elementLV); |
| 498 | |
| 499 | // Move on to the next element. |
| 500 | llvm::Value *nextElement = |
| 501 | Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next"); |
| 502 | |
| 503 | // Tell the EH cleanup that we finished with the last element. |
| 504 | if (endOfInit) Builder.CreateStore(nextElement, endOfInit); |
| 505 | |
| 506 | // Leave the loop if we're done. |
| 507 | llvm::Value *done = Builder.CreateICmpEQ(nextElement, end, |
| 508 | "arrayinit.done"); |
| 509 | llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end"); |
| 510 | Builder.CreateCondBr(done, endBB, bodyBB); |
| 511 | currentElement->addIncoming(nextElement, Builder.GetInsertBlock()); |
| 512 | |
| 513 | CGF.EmitBlock(endBB); |
| 514 | } |
| 515 | |
| 516 | // Leave the partial-array cleanup if we entered one. |
| 517 | if (dtorKind) CGF.DeactivateCleanupBlock(cleanup, cleanupDominator); |
| 518 | } |
| 519 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 520 | //===----------------------------------------------------------------------===// |
| 521 | // Visitor Methods |
| 522 | //===----------------------------------------------------------------------===// |
| 523 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 524 | void AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){ |
| 525 | Visit(E->GetTemporaryExpr()); |
| 526 | } |
| 527 | |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 528 | void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) { |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 529 | EmitFinalDestCopy(e->getType(), CGF.getOpaqueLValueMapping(e)); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 532 | void |
| 533 | AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
Douglas Gregor | 673e98b | 2011-06-17 16:37:20 +0000 | [diff] [blame] | 534 | if (E->getType().isPODType(CGF.getContext())) { |
| 535 | // For a POD type, just emit a load of the lvalue + a copy, because our |
| 536 | // compound literal might alias the destination. |
| 537 | // FIXME: This is a band-aid; the real problem appears to be in our handling |
| 538 | // of assignments, where we store directly into the LHS without checking |
| 539 | // whether anything in the RHS aliases. |
| 540 | EmitAggLoadOfLValue(E); |
| 541 | return; |
| 542 | } |
| 543 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 544 | AggValueSlot Slot = EnsureSlot(E->getType()); |
| 545 | CGF.EmitAggExpr(E->getInitializer(), Slot); |
| 546 | } |
| 547 | |
| 548 | |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 549 | void AggExprEmitter::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame] | 550 | switch (E->getCastKind()) { |
Anders Carlsson | 575b374 | 2011-04-11 02:03:26 +0000 | [diff] [blame] | 551 | case CK_Dynamic: { |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 552 | // FIXME: Can this actually happen? We have no test coverage for it. |
Douglas Gregor | 69cfeb1 | 2010-05-14 21:31:02 +0000 | [diff] [blame] | 553 | assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?"); |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 554 | LValue LV = CGF.EmitCheckedLValue(E->getSubExpr(), |
Richard Smith | 7ac9ef1 | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 555 | CodeGenFunction::TCK_Load); |
Douglas Gregor | 69cfeb1 | 2010-05-14 21:31:02 +0000 | [diff] [blame] | 556 | // FIXME: Do we also need to handle property references here? |
| 557 | if (LV.isSimple()) |
| 558 | CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E)); |
| 559 | else |
| 560 | CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast"); |
| 561 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 562 | if (!Dest.isIgnored()) |
| 563 | CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination"); |
Douglas Gregor | 69cfeb1 | 2010-05-14 21:31:02 +0000 | [diff] [blame] | 564 | break; |
| 565 | } |
| 566 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 567 | case CK_ToUnion: { |
John McCall | 6591271 | 2011-04-12 22:02:02 +0000 | [diff] [blame] | 568 | if (Dest.isIgnored()) break; |
| 569 | |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 570 | // GCC union extension |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 571 | QualType Ty = E->getSubExpr()->getType(); |
| 572 | QualType PtrTy = CGF.getContext().getPointerType(Ty); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 573 | llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(), |
Eli Friedman | 34ebf4d | 2009-06-03 20:45:06 +0000 | [diff] [blame] | 574 | CGF.ConvertType(PtrTy)); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 575 | EmitInitializationToLValue(E->getSubExpr(), |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 576 | CGF.MakeAddrLValue(CastPtr, Ty)); |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame] | 577 | break; |
Nuno Lopes | 7e91627 | 2009-01-15 20:14:33 +0000 | [diff] [blame] | 578 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 580 | case CK_DerivedToBase: |
| 581 | case CK_BaseToDerived: |
| 582 | case CK_UncheckedDerivedToBase: { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 583 | llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: " |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 584 | "should have been unpacked before we got here"); |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 585 | } |
| 586 | |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 587 | case CK_LValueToRValue: |
| 588 | // If we're loading from a volatile type, force the destination |
| 589 | // into existence. |
| 590 | if (E->getSubExpr()->getType().isVolatileQualified()) { |
| 591 | EnsureDest(E->getType()); |
| 592 | return Visit(E->getSubExpr()); |
| 593 | } |
| 594 | // fallthrough |
| 595 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 596 | case CK_NoOp: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 597 | case CK_AtomicToNonAtomic: |
| 598 | case CK_NonAtomicToAtomic: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 599 | case CK_UserDefinedConversion: |
| 600 | case CK_ConstructorConversion: |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame] | 601 | assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), |
| 602 | E->getType()) && |
| 603 | "Implicit cast types must be compatible"); |
| 604 | Visit(E->getSubExpr()); |
| 605 | break; |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 606 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 607 | case CK_LValueBitCast: |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 608 | llvm_unreachable("should not be emitting lvalue bitcast as rvalue"); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 609 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 610 | case CK_Dependent: |
| 611 | case CK_BitCast: |
| 612 | case CK_ArrayToPointerDecay: |
| 613 | case CK_FunctionToPointerDecay: |
| 614 | case CK_NullToPointer: |
| 615 | case CK_NullToMemberPointer: |
| 616 | case CK_BaseToDerivedMemberPointer: |
| 617 | case CK_DerivedToBaseMemberPointer: |
| 618 | case CK_MemberPointerToBoolean: |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 619 | case CK_ReinterpretMemberPointer: |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 620 | case CK_IntegralToPointer: |
| 621 | case CK_PointerToIntegral: |
| 622 | case CK_PointerToBoolean: |
| 623 | case CK_ToVoid: |
| 624 | case CK_VectorSplat: |
| 625 | case CK_IntegralCast: |
| 626 | case CK_IntegralToBoolean: |
| 627 | case CK_IntegralToFloating: |
| 628 | case CK_FloatingToIntegral: |
| 629 | case CK_FloatingToBoolean: |
| 630 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 631 | case CK_CPointerToObjCPointerCast: |
| 632 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 633 | case CK_AnyPointerToBlockPointerCast: |
| 634 | case CK_ObjCObjectLValueCast: |
| 635 | case CK_FloatingRealToComplex: |
| 636 | case CK_FloatingComplexToReal: |
| 637 | case CK_FloatingComplexToBoolean: |
| 638 | case CK_FloatingComplexCast: |
| 639 | case CK_FloatingComplexToIntegralComplex: |
| 640 | case CK_IntegralRealToComplex: |
| 641 | case CK_IntegralComplexToReal: |
| 642 | case CK_IntegralComplexToBoolean: |
| 643 | case CK_IntegralComplexCast: |
| 644 | case CK_IntegralComplexToFloatingComplex: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 645 | case CK_ARCProduceObject: |
| 646 | case CK_ARCConsumeObject: |
| 647 | case CK_ARCReclaimReturnedObject: |
| 648 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 649 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 650 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame^] | 651 | case CK_ZeroToOCLEvent: |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 652 | llvm_unreachable("cast kind invalid for aggregate types"); |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame] | 653 | } |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 656 | void AggExprEmitter::VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | e70e8f7 | 2009-05-27 16:45:02 +0000 | [diff] [blame] | 657 | if (E->getCallReturnType()->isReferenceType()) { |
| 658 | EmitAggLoadOfLValue(E); |
| 659 | return; |
| 660 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 662 | RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 663 | EmitMoveFromReturnSlot(E, RV); |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 664 | } |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 665 | |
| 666 | void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 667 | RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 668 | EmitMoveFromReturnSlot(E, RV); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 669 | } |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 671 | void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 672 | CGF.EmitIgnoredExpr(E->getLHS()); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 673 | Visit(E->getRHS()); |
Eli Friedman | 07fa52a | 2008-05-20 07:56:31 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 676 | void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 677 | CodeGenFunction::StmtExprEvaluation eval(CGF); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 678 | CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest); |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 681 | void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 682 | if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI) |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 683 | VisitPointerToDataMemberBinaryOperator(E); |
| 684 | else |
| 685 | CGF.ErrorUnsupported(E, "aggregate binary expression"); |
| 686 | } |
| 687 | |
| 688 | void AggExprEmitter::VisitPointerToDataMemberBinaryOperator( |
| 689 | const BinaryOperator *E) { |
| 690 | LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 691 | EmitFinalDestCopy(E->getType(), LV); |
| 692 | } |
| 693 | |
| 694 | /// Is the value of the given expression possibly a reference to or |
| 695 | /// into a __block variable? |
| 696 | static bool isBlockVarRef(const Expr *E) { |
| 697 | // Make sure we look through parens. |
| 698 | E = E->IgnoreParens(); |
| 699 | |
| 700 | // Check for a direct reference to a __block variable. |
| 701 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 702 | const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl()); |
| 703 | return (var && var->hasAttr<BlocksAttr>()); |
| 704 | } |
| 705 | |
| 706 | // More complicated stuff. |
| 707 | |
| 708 | // Binary operators. |
| 709 | if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) { |
| 710 | // For an assignment or pointer-to-member operation, just care |
| 711 | // about the LHS. |
| 712 | if (op->isAssignmentOp() || op->isPtrMemOp()) |
| 713 | return isBlockVarRef(op->getLHS()); |
| 714 | |
| 715 | // For a comma, just care about the RHS. |
| 716 | if (op->getOpcode() == BO_Comma) |
| 717 | return isBlockVarRef(op->getRHS()); |
| 718 | |
| 719 | // FIXME: pointer arithmetic? |
| 720 | return false; |
| 721 | |
| 722 | // Check both sides of a conditional operator. |
| 723 | } else if (const AbstractConditionalOperator *op |
| 724 | = dyn_cast<AbstractConditionalOperator>(E)) { |
| 725 | return isBlockVarRef(op->getTrueExpr()) |
| 726 | || isBlockVarRef(op->getFalseExpr()); |
| 727 | |
| 728 | // OVEs are required to support BinaryConditionalOperators. |
| 729 | } else if (const OpaqueValueExpr *op |
| 730 | = dyn_cast<OpaqueValueExpr>(E)) { |
| 731 | if (const Expr *src = op->getSourceExpr()) |
| 732 | return isBlockVarRef(src); |
| 733 | |
| 734 | // Casts are necessary to get things like (*(int*)&var) = foo(). |
| 735 | // We don't really care about the kind of cast here, except |
| 736 | // we don't want to look through l2r casts, because it's okay |
| 737 | // to get the *value* in a __block variable. |
| 738 | } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) { |
| 739 | if (cast->getCastKind() == CK_LValueToRValue) |
| 740 | return false; |
| 741 | return isBlockVarRef(cast->getSubExpr()); |
| 742 | |
| 743 | // Handle unary operators. Again, just aggressively look through |
| 744 | // it, ignoring the operation. |
| 745 | } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) { |
| 746 | return isBlockVarRef(uop->getSubExpr()); |
| 747 | |
| 748 | // Look into the base of a field access. |
| 749 | } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) { |
| 750 | return isBlockVarRef(mem->getBase()); |
| 751 | |
| 752 | // Look into the base of a subscript. |
| 753 | } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) { |
| 754 | return isBlockVarRef(sub->getBase()); |
| 755 | } |
| 756 | |
| 757 | return false; |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 760 | void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 761 | // For an assignment to work, the value on the right has |
| 762 | // to be compatible with the value on the left. |
Eli Friedman | 2dce5f8 | 2009-05-28 23:04:00 +0000 | [diff] [blame] | 763 | assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), |
| 764 | E->getRHS()->getType()) |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 765 | && "Invalid assignment"); |
John McCall | cd940a1 | 2010-12-06 06:10:02 +0000 | [diff] [blame] | 766 | |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 767 | // If the LHS might be a __block variable, and the RHS can |
| 768 | // potentially cause a block copy, we need to evaluate the RHS first |
| 769 | // so that the assignment goes the right place. |
| 770 | // This is pretty semantically fragile. |
| 771 | if (isBlockVarRef(E->getLHS()) && |
| 772 | E->getRHS()->HasSideEffects(CGF.getContext())) { |
| 773 | // Ensure that we have a destination, and evaluate the RHS into that. |
| 774 | EnsureDest(E->getRHS()->getType()); |
| 775 | Visit(E->getRHS()); |
| 776 | |
| 777 | // Now emit the LHS and copy into it. |
Richard Smith | 4def70d | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 778 | LValue LHS = CGF.EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store); |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 779 | |
| 780 | EmitCopy(E->getLHS()->getType(), |
| 781 | AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, |
| 782 | needsGC(E->getLHS()->getType()), |
| 783 | AggValueSlot::IsAliased), |
| 784 | Dest); |
| 785 | return; |
| 786 | } |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 787 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 788 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 789 | |
John McCall | db45806 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 790 | // Codegen the RHS so that it stores directly into the LHS. |
| 791 | AggValueSlot LHSSlot = |
| 792 | AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, |
| 793 | needsGC(E->getLHS()->getType()), |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 794 | AggValueSlot::IsAliased); |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 795 | CGF.EmitAggExpr(E->getRHS(), LHSSlot); |
| 796 | |
| 797 | // Copy into the destination if the assignment isn't ignored. |
| 798 | EmitFinalDestCopy(E->getType(), LHS); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 799 | } |
| 800 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 801 | void AggExprEmitter:: |
| 802 | VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 803 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 804 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
| 805 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 807 | // Bind the common expression if necessary. |
Eli Friedman | d97927d | 2012-01-06 20:42:20 +0000 | [diff] [blame] | 808 | CodeGenFunction::OpaqueValueMapping binding(CGF, E); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 809 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 810 | CodeGenFunction::ConditionalEvaluation eval(CGF); |
Eli Friedman | 8e274bd | 2009-12-25 06:17:05 +0000 | [diff] [blame] | 811 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | |
John McCall | 74fb0ed | 2010-11-17 00:07:33 +0000 | [diff] [blame] | 813 | // Save whether the destination's lifetime is externally managed. |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 814 | bool isExternallyDestructed = Dest.isExternallyDestructed(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 815 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 816 | eval.begin(CGF); |
| 817 | CGF.EmitBlock(LHSBlock); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 818 | Visit(E->getTrueExpr()); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 819 | eval.end(CGF); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 821 | assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!"); |
| 822 | CGF.Builder.CreateBr(ContBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | |
John McCall | 74fb0ed | 2010-11-17 00:07:33 +0000 | [diff] [blame] | 824 | // If the result of an agg expression is unused, then the emission |
| 825 | // of the LHS might need to create a destination slot. That's fine |
| 826 | // with us, and we can safely emit the RHS into the same slot, but |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 827 | // we shouldn't claim that it's already being destructed. |
| 828 | Dest.setExternallyDestructed(isExternallyDestructed); |
John McCall | 74fb0ed | 2010-11-17 00:07:33 +0000 | [diff] [blame] | 829 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 830 | eval.begin(CGF); |
| 831 | CGF.EmitBlock(RHSBlock); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 832 | Visit(E->getFalseExpr()); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 833 | eval.end(CGF); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 835 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 836 | } |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 837 | |
Anders Carlsson | a294ca8 | 2009-07-08 18:33:14 +0000 | [diff] [blame] | 838 | void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { |
| 839 | Visit(CE->getChosenSubExpr(CGF.getContext())); |
| 840 | } |
| 841 | |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 842 | void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 843 | llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 844 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); |
| 845 | |
Sebastian Redl | 0262f02 | 2009-01-09 21:09:38 +0000 | [diff] [blame] | 846 | if (!ArgPtr) { |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 847 | CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); |
Sebastian Redl | 0262f02 | 2009-01-09 21:09:38 +0000 | [diff] [blame] | 848 | return; |
| 849 | } |
| 850 | |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 851 | EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType())); |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 854 | void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 855 | // Ensure that we have a slot, but if we already do, remember |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 856 | // whether it was externally destructed. |
| 857 | bool wasExternallyDestructed = Dest.isExternallyDestructed(); |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 858 | EnsureDest(E->getType()); |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 859 | |
| 860 | // We're going to push a destructor if there isn't already one. |
| 861 | Dest.setExternallyDestructed(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 862 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 863 | Visit(E->getSubExpr()); |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 864 | |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 865 | // Push that destructor we promised. |
| 866 | if (!wasExternallyDestructed) |
Peter Collingbourne | 8681160 | 2011-11-27 22:09:22 +0000 | [diff] [blame] | 867 | CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddr()); |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 870 | void |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 871 | AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 872 | AggValueSlot Slot = EnsureSlot(E->getType()); |
| 873 | CGF.EmitCXXConstructExpr(E, Slot); |
Anders Carlsson | 7f6ad15 | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Eli Friedman | 4c5d8af | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 876 | void |
| 877 | AggExprEmitter::VisitLambdaExpr(LambdaExpr *E) { |
| 878 | AggValueSlot Slot = EnsureSlot(E->getType()); |
| 879 | CGF.EmitLambdaExpr(E, Slot); |
| 880 | } |
| 881 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 882 | void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) { |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 883 | CGF.enterFullExpression(E); |
| 884 | CodeGenFunction::RunCleanupsScope cleanups(CGF); |
| 885 | Visit(E->getSubExpr()); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 888 | void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 889 | QualType T = E->getType(); |
| 890 | AggValueSlot Slot = EnsureSlot(T); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 891 | EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); |
Anders Carlsson | 30311fa | 2009-12-16 06:57:54 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 895 | QualType T = E->getType(); |
| 896 | AggValueSlot Slot = EnsureSlot(T); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 897 | EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); |
Nuno Lopes | 329763b | 2009-10-18 15:18:11 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 900 | /// isSimpleZero - If emitting this value will obviously just cause a store of |
| 901 | /// zero to memory, return true. This can return false if uncertain, so it just |
| 902 | /// handles simple cases. |
| 903 | static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 904 | E = E->IgnoreParens(); |
| 905 | |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 906 | // 0 |
| 907 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) |
| 908 | return IL->getValue() == 0; |
| 909 | // +0.0 |
| 910 | if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E)) |
| 911 | return FL->getValue().isPosZero(); |
| 912 | // int() |
| 913 | if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) && |
| 914 | CGF.getTypes().isZeroInitializable(E->getType())) |
| 915 | return true; |
| 916 | // (int*)0 - Null pointer expressions. |
| 917 | if (const CastExpr *ICE = dyn_cast<CastExpr>(E)) |
| 918 | return ICE->getCastKind() == CK_NullToPointer; |
| 919 | // '\0' |
| 920 | if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) |
| 921 | return CL->getValue() == 0; |
| 922 | |
| 923 | // Otherwise, hard case: conservatively return false. |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | |
Anders Carlsson | 78e83f8 | 2010-02-03 17:33:16 +0000 | [diff] [blame] | 928 | void |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 929 | AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 930 | QualType type = LV.getType(); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 931 | // FIXME: Ignore result? |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 932 | // FIXME: Are initializers affected by volatile? |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 933 | if (Dest.isZeroed() && isSimpleZero(E, CGF)) { |
| 934 | // Storing "i32 0" to a zero'd memory location is a noop. |
Richard Smith | 0dbe2fb | 2012-12-21 03:17:28 +0000 | [diff] [blame] | 935 | } else if (isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) { |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 936 | EmitNullInitializationToLValue(LV); |
| 937 | } else if (type->isReferenceType()) { |
Anders Carlsson | 32f36ba | 2010-06-26 16:35:32 +0000 | [diff] [blame] | 938 | RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 939 | CGF.EmitStoreThroughLValue(RV, LV); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 940 | } else if (type->isAnyComplexType()) { |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 941 | CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 942 | } else if (CGF.hasAggregateLLVMType(type)) { |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 943 | CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV, |
| 944 | AggValueSlot::IsDestructed, |
| 945 | AggValueSlot::DoesNotNeedGCBarriers, |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 946 | AggValueSlot::IsNotAliased, |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 947 | Dest.isZeroed())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 948 | } else if (LV.isSimple()) { |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 949 | CGF.EmitScalarInit(E, /*D=*/0, LV, /*Captured=*/false); |
Eli Friedman | c8ba961 | 2008-05-12 15:06:05 +0000 | [diff] [blame] | 950 | } else { |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 951 | CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 952 | } |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 953 | } |
| 954 | |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 955 | void AggExprEmitter::EmitNullInitializationToLValue(LValue lv) { |
| 956 | QualType type = lv.getType(); |
| 957 | |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 958 | // If the destination slot is already zeroed out before the aggregate is |
| 959 | // copied into it, we don't have to emit any zeros here. |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 960 | if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type)) |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 961 | return; |
| 962 | |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 963 | if (!CGF.hasAggregateLLVMType(type)) { |
Richard Smith | 0dbe2fb | 2012-12-21 03:17:28 +0000 | [diff] [blame] | 964 | // For non-aggregates, we can store the appropriate null constant. |
| 965 | llvm::Value *null = CGF.CGM.EmitNullConstant(type); |
Eli Friedman | b1e3f32 | 2012-02-22 05:38:59 +0000 | [diff] [blame] | 966 | // Note that the following is not equivalent to |
| 967 | // EmitStoreThroughBitfieldLValue for ARC types. |
Eli Friedman | 5a13d4d | 2012-02-24 23:53:49 +0000 | [diff] [blame] | 968 | if (lv.isBitField()) { |
Eli Friedman | b1e3f32 | 2012-02-22 05:38:59 +0000 | [diff] [blame] | 969 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv); |
Eli Friedman | 5a13d4d | 2012-02-24 23:53:49 +0000 | [diff] [blame] | 970 | } else { |
| 971 | assert(lv.isSimple()); |
| 972 | CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true); |
| 973 | } |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 974 | } else { |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 975 | // There's a potential optimization opportunity in combining |
| 976 | // memsets; that would be easy for arrays, but relatively |
| 977 | // difficult for structures with the current code. |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 978 | CGF.EmitNullInitialization(lv.getAddress(), lv.getType()); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 982 | void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { |
Eli Friedman | a385b3c | 2008-12-02 01:17:45 +0000 | [diff] [blame] | 983 | #if 0 |
Eli Friedman | 13a5be1 | 2009-12-04 01:30:56 +0000 | [diff] [blame] | 984 | // FIXME: Assess perf here? Figure out what cases are worth optimizing here |
| 985 | // (Length of globals? Chunks of zeroed-out space?). |
Eli Friedman | a385b3c | 2008-12-02 01:17:45 +0000 | [diff] [blame] | 986 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 987 | // If we can, prefer a copy from a global; this is a lot less code for long |
| 988 | // globals, and it's easier for the current optimizers to analyze. |
Eli Friedman | 13a5be1 | 2009-12-04 01:30:56 +0000 | [diff] [blame] | 989 | if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { |
Eli Friedman | 994ffef | 2008-11-30 02:11:09 +0000 | [diff] [blame] | 990 | llvm::GlobalVariable* GV = |
Eli Friedman | 13a5be1 | 2009-12-04 01:30:56 +0000 | [diff] [blame] | 991 | new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, |
| 992 | llvm::GlobalValue::InternalLinkage, C, ""); |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 993 | EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType())); |
Eli Friedman | 994ffef | 2008-11-30 02:11:09 +0000 | [diff] [blame] | 994 | return; |
| 995 | } |
Eli Friedman | a385b3c | 2008-12-02 01:17:45 +0000 | [diff] [blame] | 996 | #endif |
Chris Lattner | d0db03a | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 997 | if (E->hadArrayRangeDesignator()) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 998 | CGF.ErrorUnsupported(E, "GNU array range designator extension"); |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 999 | |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1000 | if (E->initializesStdInitializerList()) { |
Sebastian Redl | af130fd | 2012-02-19 12:28:02 +0000 | [diff] [blame] | 1001 | EmitStdInitializerList(Dest.getAddr(), E); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1002 | return; |
| 1003 | } |
| 1004 | |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1005 | AggValueSlot Dest = EnsureSlot(E->getType()); |
| 1006 | LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(), |
| 1007 | Dest.getAlignment()); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 1009 | // Handle initialization of an array. |
| 1010 | if (E->getType()->isArrayType()) { |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1011 | if (E->isStringLiteralInit()) |
| 1012 | return Visit(E->getInit(0)); |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 1013 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 1014 | QualType elementType = |
| 1015 | CGF.getContext().getAsArrayType(E->getType())->getElementType(); |
Argyrios Kyrtzidis | 3b4d490 | 2011-04-28 18:53:58 +0000 | [diff] [blame] | 1016 | |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1017 | llvm::PointerType *APType = |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1018 | cast<llvm::PointerType>(Dest.getAddr()->getType()); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1019 | llvm::ArrayType *AType = |
| 1020 | cast<llvm::ArrayType>(APType->getElementType()); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1021 | |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1022 | EmitArrayInit(Dest.getAddr(), AType, elementType, E); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 1023 | return; |
| 1024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 1026 | assert(E->getType()->isRecordType() && "Only support structs/unions here!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 1028 | // Do struct initialization; this code just sets each individual member |
| 1029 | // to the approprate value. This makes bitfield support automatic; |
| 1030 | // the disadvantage is that the generated code is more difficult for |
| 1031 | // the optimizer, especially with bitfields. |
| 1032 | unsigned NumInitElements = E->getNumInits(); |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1033 | RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl(); |
Chris Lattner | bd7de38 | 2010-09-06 00:13:11 +0000 | [diff] [blame] | 1034 | |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1035 | if (record->isUnion()) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1036 | // Only initialize one field of a union. The field itself is |
| 1037 | // specified by the initializer list. |
| 1038 | if (!E->getInitializedFieldInUnion()) { |
| 1039 | // Empty union; we have nothing to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1041 | #ifndef NDEBUG |
| 1042 | // Make sure that it's really an empty and not a failure of |
| 1043 | // semantic analysis. |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1044 | for (RecordDecl::field_iterator Field = record->field_begin(), |
| 1045 | FieldEnd = record->field_end(); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1046 | Field != FieldEnd; ++Field) |
| 1047 | assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); |
| 1048 | #endif |
| 1049 | return; |
| 1050 | } |
| 1051 | |
| 1052 | // FIXME: volatility |
| 1053 | FieldDecl *Field = E->getInitializedFieldInUnion(); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1054 | |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1055 | LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1056 | if (NumInitElements) { |
| 1057 | // Store the initializer into the field |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1058 | EmitInitializationToLValue(E->getInit(0), FieldLoc); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1059 | } else { |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1060 | // Default-initialize to null. |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1061 | EmitNullInitializationToLValue(FieldLoc); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | return; |
| 1065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1067 | // We'll need to enter cleanup scopes in case any of the member |
| 1068 | // initializers throw an exception. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1069 | SmallVector<EHScopeStack::stable_iterator, 16> cleanups; |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1070 | llvm::Instruction *cleanupDominator = 0; |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 1072 | // Here we iterate over the fields; this makes it simpler to both |
| 1073 | // default-initialize fields and skip over unnamed fields. |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1074 | unsigned curInitIndex = 0; |
| 1075 | for (RecordDecl::field_iterator field = record->field_begin(), |
| 1076 | fieldEnd = record->field_end(); |
| 1077 | field != fieldEnd; ++field) { |
| 1078 | // We're done once we hit the flexible array member. |
| 1079 | if (field->getType()->isIncompleteArrayType()) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1082 | // Always skip anonymous bitfields. |
| 1083 | if (field->isUnnamedBitfield()) |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 1084 | continue; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1085 | |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1086 | // We're done if we reach the end of the explicit initializers, we |
| 1087 | // have a zeroed object, and the rest of the fields are |
| 1088 | // zero-initializable. |
| 1089 | if (curInitIndex == NumInitElements && Dest.isZeroed() && |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1090 | CGF.getTypes().isZeroInitializable(E->getType())) |
| 1091 | break; |
| 1092 | |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1093 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1094 | LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, *field); |
Fariborz Jahanian | 14674ff | 2009-05-27 19:54:11 +0000 | [diff] [blame] | 1095 | // We never generate write-barries for initialized fields. |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1096 | LV.setNonGC(true); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1097 | |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1098 | if (curInitIndex < NumInitElements) { |
Chris Lattner | b35baae | 2010-03-08 21:08:07 +0000 | [diff] [blame] | 1099 | // Store the initializer into the field. |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1100 | EmitInitializationToLValue(E->getInit(curInitIndex++), LV); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 1101 | } else { |
| 1102 | // We're out of initalizers; default-initialize to null |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1103 | EmitNullInitializationToLValue(LV); |
| 1104 | } |
| 1105 | |
| 1106 | // Push a destructor if necessary. |
| 1107 | // FIXME: if we have an array of structures, all explicitly |
| 1108 | // initialized, we can end up pushing a linear number of cleanups. |
| 1109 | bool pushedCleanup = false; |
| 1110 | if (QualType::DestructionKind dtorKind |
| 1111 | = field->getType().isDestructedType()) { |
| 1112 | assert(LV.isSimple()); |
| 1113 | if (CGF.needsEHCleanup(dtorKind)) { |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1114 | if (!cleanupDominator) |
| 1115 | cleanupDominator = CGF.Builder.CreateUnreachable(); // placeholder |
| 1116 | |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1117 | CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(), |
| 1118 | CGF.getDestroyer(dtorKind), false); |
| 1119 | cleanups.push_back(CGF.EHStack.stable_begin()); |
| 1120 | pushedCleanup = true; |
| 1121 | } |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 1122 | } |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1123 | |
| 1124 | // If the GEP didn't get used because of a dead zero init or something |
| 1125 | // else, clean it up for -O0 builds and general tidiness. |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1126 | if (!pushedCleanup && LV.isSimple()) |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1127 | if (llvm::GetElementPtrInst *GEP = |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1128 | dyn_cast<llvm::GetElementPtrInst>(LV.getAddress())) |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1129 | if (GEP->use_empty()) |
| 1130 | GEP->eraseFromParent(); |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 1131 | } |
John McCall | 2b30dcf | 2011-07-11 19:35:02 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Deactivate all the partial cleanups in reverse order, which |
| 1134 | // generally means popping them. |
| 1135 | for (unsigned i = cleanups.size(); i != 0; --i) |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1136 | CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator); |
| 1137 | |
| 1138 | // Destroy the placeholder if we made one. |
| 1139 | if (cleanupDominator) |
| 1140 | cleanupDominator->eraseFromParent(); |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 1143 | //===----------------------------------------------------------------------===// |
| 1144 | // Entry Points into this File |
| 1145 | //===----------------------------------------------------------------------===// |
| 1146 | |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1147 | /// GetNumNonZeroBytesInInit - Get an approximate count of the number of |
| 1148 | /// non-zero bytes that will be stored when outputting the initializer for the |
| 1149 | /// specified initializer expression. |
Ken Dyck | 02c4533 | 2011-04-24 17:17:56 +0000 | [diff] [blame] | 1150 | static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1151 | E = E->IgnoreParens(); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1152 | |
| 1153 | // 0 and 0.0 won't require any non-zero stores! |
Ken Dyck | 02c4533 | 2011-04-24 17:17:56 +0000 | [diff] [blame] | 1154 | if (isSimpleZero(E, CGF)) return CharUnits::Zero(); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1155 | |
| 1156 | // If this is an initlist expr, sum up the size of sizes of the (present) |
| 1157 | // elements. If this is something weird, assume the whole thing is non-zero. |
| 1158 | const InitListExpr *ILE = dyn_cast<InitListExpr>(E); |
| 1159 | if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType())) |
Ken Dyck | 02c4533 | 2011-04-24 17:17:56 +0000 | [diff] [blame] | 1160 | return CGF.getContext().getTypeSizeInChars(E->getType()); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1161 | |
Chris Lattner | d1d56df | 2010-12-02 18:29:00 +0000 | [diff] [blame] | 1162 | // InitListExprs for structs have to be handled carefully. If there are |
| 1163 | // reference members, we need to consider the size of the reference, not the |
| 1164 | // referencee. InitListExprs for unions and arrays can't have references. |
Chris Lattner | 8c00ad1 | 2010-12-02 22:52:04 +0000 | [diff] [blame] | 1165 | if (const RecordType *RT = E->getType()->getAs<RecordType>()) { |
| 1166 | if (!RT->isUnionType()) { |
| 1167 | RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); |
Ken Dyck | 02c4533 | 2011-04-24 17:17:56 +0000 | [diff] [blame] | 1168 | CharUnits NumNonZeroBytes = CharUnits::Zero(); |
Chris Lattner | 8c00ad1 | 2010-12-02 22:52:04 +0000 | [diff] [blame] | 1169 | |
| 1170 | unsigned ILEElement = 0; |
| 1171 | for (RecordDecl::field_iterator Field = SD->field_begin(), |
| 1172 | FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) { |
| 1173 | // We're done once we hit the flexible array member or run out of |
| 1174 | // InitListExpr elements. |
| 1175 | if (Field->getType()->isIncompleteArrayType() || |
| 1176 | ILEElement == ILE->getNumInits()) |
| 1177 | break; |
| 1178 | if (Field->isUnnamedBitfield()) |
| 1179 | continue; |
Chris Lattner | d1d56df | 2010-12-02 18:29:00 +0000 | [diff] [blame] | 1180 | |
Chris Lattner | 8c00ad1 | 2010-12-02 22:52:04 +0000 | [diff] [blame] | 1181 | const Expr *E = ILE->getInit(ILEElement++); |
| 1182 | |
| 1183 | // Reference values are always non-null and have the width of a pointer. |
| 1184 | if (Field->getType()->isReferenceType()) |
Ken Dyck | 02c4533 | 2011-04-24 17:17:56 +0000 | [diff] [blame] | 1185 | NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits( |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1186 | CGF.getContext().getTargetInfo().getPointerWidth(0)); |
Chris Lattner | 8c00ad1 | 2010-12-02 22:52:04 +0000 | [diff] [blame] | 1187 | else |
| 1188 | NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF); |
| 1189 | } |
Chris Lattner | d1d56df | 2010-12-02 18:29:00 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | 8c00ad1 | 2010-12-02 22:52:04 +0000 | [diff] [blame] | 1191 | return NumNonZeroBytes; |
Chris Lattner | d1d56df | 2010-12-02 18:29:00 +0000 | [diff] [blame] | 1192 | } |
Chris Lattner | d1d56df | 2010-12-02 18:29:00 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | |
Ken Dyck | 02c4533 | 2011-04-24 17:17:56 +0000 | [diff] [blame] | 1196 | CharUnits NumNonZeroBytes = CharUnits::Zero(); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1197 | for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) |
| 1198 | NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF); |
| 1199 | return NumNonZeroBytes; |
| 1200 | } |
| 1201 | |
| 1202 | /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of |
| 1203 | /// zeros in it, emit a memset and avoid storing the individual zeros. |
| 1204 | /// |
| 1205 | static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E, |
| 1206 | CodeGenFunction &CGF) { |
| 1207 | // If the slot is already known to be zeroed, nothing to do. Don't mess with |
| 1208 | // volatile stores. |
| 1209 | if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return; |
Argyrios Kyrtzidis | 657baf1 | 2011-04-28 22:57:55 +0000 | [diff] [blame] | 1210 | |
| 1211 | // C++ objects with a user-declared constructor don't need zero'ing. |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 1212 | if (CGF.getLangOpts().CPlusPlus) |
Argyrios Kyrtzidis | 657baf1 | 2011-04-28 22:57:55 +0000 | [diff] [blame] | 1213 | if (const RecordType *RT = CGF.getContext() |
| 1214 | .getBaseElementType(E->getType())->getAs<RecordType>()) { |
| 1215 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1216 | if (RD->hasUserDeclaredConstructor()) |
| 1217 | return; |
| 1218 | } |
| 1219 | |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1220 | // If the type is 16-bytes or smaller, prefer individual stores over memset. |
Ken Dyck | 5ff1a35 | 2011-04-24 17:25:32 +0000 | [diff] [blame] | 1221 | std::pair<CharUnits, CharUnits> TypeInfo = |
| 1222 | CGF.getContext().getTypeInfoInChars(E->getType()); |
| 1223 | if (TypeInfo.first <= CharUnits::fromQuantity(16)) |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1224 | return; |
| 1225 | |
| 1226 | // Check to see if over 3/4 of the initializer are known to be zero. If so, |
| 1227 | // we prefer to emit memset + individual stores for the rest. |
Ken Dyck | 5ff1a35 | 2011-04-24 17:25:32 +0000 | [diff] [blame] | 1228 | CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF); |
| 1229 | if (NumNonZeroBytes*4 > TypeInfo.first) |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1230 | return; |
| 1231 | |
| 1232 | // Okay, it seems like a good idea to use an initial memset, emit the call. |
Ken Dyck | 5ff1a35 | 2011-04-24 17:25:32 +0000 | [diff] [blame] | 1233 | llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity()); |
| 1234 | CharUnits Align = TypeInfo.second; |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1235 | |
| 1236 | llvm::Value *Loc = Slot.getAddr(); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1238 | Loc = CGF.Builder.CreateBitCast(Loc, CGF.Int8PtrTy); |
Ken Dyck | 5ff1a35 | 2011-04-24 17:25:32 +0000 | [diff] [blame] | 1239 | CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, |
| 1240 | Align.getQuantity(), false); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1241 | |
| 1242 | // Tell the AggExprEmitter that the slot is known zero. |
| 1243 | Slot.setZeroed(); |
| 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
Mike Stump | e1129a9 | 2009-05-26 18:57:45 +0000 | [diff] [blame] | 1249 | /// EmitAggExpr - Emit the computation of the specified expression of aggregate |
| 1250 | /// type. The result is computed into DestPtr. Note that if DestPtr is null, |
| 1251 | /// the value of the aggregate expression is not needed. If VolatileDest is |
| 1252 | /// true, DestPtr cannot be 0. |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 1253 | void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) { |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 1254 | assert(E && hasAggregateLLVMType(E->getType()) && |
| 1255 | "Invalid aggregate expression to emit"); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1256 | assert((Slot.getAddr() != 0 || Slot.isIgnored()) && |
| 1257 | "slot has bits but no address"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 1259 | // Optimize the slot if possible. |
| 1260 | CheckAggExprForMemSetUse(Slot, E, *this); |
| 1261 | |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 1262 | AggExprEmitter(*this, Slot).Visit(const_cast<Expr*>(E)); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 1263 | } |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 1264 | |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 1265 | LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { |
| 1266 | assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!"); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1267 | llvm::Value *Temp = CreateMemTemp(E->getType()); |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 1268 | LValue LV = MakeAddrLValue(Temp, E->getType()); |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 1269 | EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed, |
John McCall | 4418439 | 2011-08-26 07:31:35 +0000 | [diff] [blame] | 1270 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1271 | AggValueSlot::IsNotAliased)); |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 1272 | return LV; |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1275 | void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, |
| 1276 | llvm::Value *SrcPtr, QualType Ty, |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 1277 | bool isVolatile, |
Benjamin Kramer | 6cacae8 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 1278 | CharUnits alignment, |
| 1279 | bool isAssignment) { |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1280 | assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 1282 | if (getLangOpts().CPlusPlus) { |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1283 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1284 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); |
| 1285 | assert((Record->hasTrivialCopyConstructor() || |
| 1286 | Record->hasTrivialCopyAssignment() || |
| 1287 | Record->hasTrivialMoveConstructor() || |
| 1288 | Record->hasTrivialMoveAssignment()) && |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 1289 | "Trying to aggregate-copy a type without a trivial copy/move " |
Douglas Gregor | e997948 | 2010-05-20 15:39:01 +0000 | [diff] [blame] | 1290 | "constructor or assignment operator"); |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1291 | // Ignore empty classes in C++. |
| 1292 | if (Record->isEmpty()) |
Anders Carlsson | 0d7c583 | 2010-05-03 01:20:20 +0000 | [diff] [blame] | 1293 | return; |
| 1294 | } |
| 1295 | } |
| 1296 | |
Chris Lattner | 83c9629 | 2009-02-28 18:31:01 +0000 | [diff] [blame] | 1297 | // Aggregate assignment turns into llvm.memcpy. This is almost valid per |
Chris Lattner | ca4fc2c | 2009-02-28 18:18:58 +0000 | [diff] [blame] | 1298 | // C99 6.5.16.1p3, which states "If the value being stored in an object is |
| 1299 | // read from another object that overlaps in anyway the storage of the first |
| 1300 | // object, then the overlap shall be exact and the two objects shall have |
| 1301 | // qualified or unqualified versions of a compatible type." |
| 1302 | // |
Chris Lattner | 83c9629 | 2009-02-28 18:31:01 +0000 | [diff] [blame] | 1303 | // memcpy is not defined if the source and destination pointers are exactly |
Chris Lattner | ca4fc2c | 2009-02-28 18:18:58 +0000 | [diff] [blame] | 1304 | // equal, but other compilers do this optimization, and almost every memcpy |
| 1305 | // implementation handles this case safely. If there is a libc that does not |
| 1306 | // safely handle this, we can add a target hook. |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1307 | |
Benjamin Kramer | 6cacae8 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 1308 | // Get data size and alignment info for this aggregate. If this is an |
| 1309 | // assignment don't copy the tail padding. Otherwise copying it is fine. |
| 1310 | std::pair<CharUnits, CharUnits> TypeInfo; |
| 1311 | if (isAssignment) |
| 1312 | TypeInfo = getContext().getTypeInfoDataSizeInChars(Ty); |
| 1313 | else |
| 1314 | TypeInfo = getContext().getTypeInfoInChars(Ty); |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1315 | |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 1316 | if (alignment.isZero()) |
| 1317 | alignment = TypeInfo.second; |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1318 | |
| 1319 | // FIXME: Handle variable sized types. |
| 1320 | |
| 1321 | // FIXME: If we have a volatile struct, the optimizer can remove what might |
| 1322 | // appear to be `extra' memory ops: |
| 1323 | // |
| 1324 | // volatile struct { int i; } a, b; |
| 1325 | // |
| 1326 | // int main() { |
| 1327 | // a = b; |
| 1328 | // a = b; |
| 1329 | // } |
| 1330 | // |
| 1331 | // we need to use a different call here. We use isVolatile to indicate when |
| 1332 | // either the source or the destination is volatile. |
| 1333 | |
| 1334 | llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); |
| 1335 | llvm::Type *DBP = |
| 1336 | llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace()); |
| 1337 | DestPtr = Builder.CreateBitCast(DestPtr, DBP); |
| 1338 | |
| 1339 | llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); |
| 1340 | llvm::Type *SBP = |
| 1341 | llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace()); |
| 1342 | SrcPtr = Builder.CreateBitCast(SrcPtr, SBP); |
| 1343 | |
| 1344 | // Don't do any of the memmove_collectable tests if GC isn't set. |
| 1345 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
| 1346 | // fall through |
| 1347 | } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { |
| 1348 | RecordDecl *Record = RecordTy->getDecl(); |
| 1349 | if (Record->hasObjectMember()) { |
| 1350 | CharUnits size = TypeInfo.first; |
| 1351 | llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1352 | llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); |
| 1353 | CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, |
| 1354 | SizeVal); |
| 1355 | return; |
| 1356 | } |
| 1357 | } else if (Ty->isArrayType()) { |
| 1358 | QualType BaseType = getContext().getBaseElementType(Ty); |
| 1359 | if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { |
| 1360 | if (RecordTy->getDecl()->hasObjectMember()) { |
| 1361 | CharUnits size = TypeInfo.first; |
| 1362 | llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1363 | llvm::Value *SizeVal = |
| 1364 | llvm::ConstantInt::get(SizeTy, size.getQuantity()); |
| 1365 | CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, |
| 1366 | SizeVal); |
| 1367 | return; |
| 1368 | } |
| 1369 | } |
| 1370 | } |
Dan Gohman | b22c7dc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 1371 | |
| 1372 | // Determine the metadata to describe the position of any padding in this |
| 1373 | // memcpy, as well as the TBAA tags for the members of the struct, in case |
| 1374 | // the optimizer wishes to expand it in to scalar memory operations. |
| 1375 | llvm::MDNode *TBAAStructTag = CGM.getTBAAStructInfo(Ty); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1376 | |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1377 | Builder.CreateMemCpy(DestPtr, SrcPtr, |
| 1378 | llvm::ConstantInt::get(IntPtrTy, |
| 1379 | TypeInfo.first.getQuantity()), |
Dan Gohman | b22c7dc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 1380 | alignment.getQuantity(), isVolatile, |
| 1381 | /*TBAATag=*/0, TBAAStructTag); |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 1382 | } |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1383 | |
Sebastian Redl | 972edf0 | 2012-02-19 16:03:09 +0000 | [diff] [blame] | 1384 | void CodeGenFunction::MaybeEmitStdInitializerListCleanup(llvm::Value *loc, |
| 1385 | const Expr *init) { |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1386 | const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(init); |
Sebastian Redl | 972edf0 | 2012-02-19 16:03:09 +0000 | [diff] [blame] | 1387 | if (cleanups) |
| 1388 | init = cleanups->getSubExpr(); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (isa<InitListExpr>(init) && |
| 1391 | cast<InitListExpr>(init)->initializesStdInitializerList()) { |
| 1392 | // We initialized this std::initializer_list with an initializer list. |
| 1393 | // A backing array was created. Push a cleanup for it. |
Sebastian Redl | 972edf0 | 2012-02-19 16:03:09 +0000 | [diff] [blame] | 1394 | EmitStdInitializerListCleanup(loc, cast<InitListExpr>(init)); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | |
Sebastian Redl | af130fd | 2012-02-19 12:28:02 +0000 | [diff] [blame] | 1398 | static void EmitRecursiveStdInitializerListCleanup(CodeGenFunction &CGF, |
| 1399 | llvm::Value *arrayStart, |
| 1400 | const InitListExpr *init) { |
| 1401 | // Check if there are any recursive cleanups to do, i.e. if we have |
| 1402 | // std::initializer_list<std::initializer_list<obj>> list = {{obj()}}; |
| 1403 | // then we need to destroy the inner array as well. |
| 1404 | for (unsigned i = 0, e = init->getNumInits(); i != e; ++i) { |
| 1405 | const InitListExpr *subInit = dyn_cast<InitListExpr>(init->getInit(i)); |
| 1406 | if (!subInit || !subInit->initializesStdInitializerList()) |
| 1407 | continue; |
| 1408 | |
| 1409 | // This one needs to be destroyed. Get the address of the std::init_list. |
| 1410 | llvm::Value *offset = llvm::ConstantInt::get(CGF.SizeTy, i); |
| 1411 | llvm::Value *loc = CGF.Builder.CreateInBoundsGEP(arrayStart, offset, |
| 1412 | "std.initlist"); |
| 1413 | CGF.EmitStdInitializerListCleanup(loc, subInit); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | void CodeGenFunction::EmitStdInitializerListCleanup(llvm::Value *loc, |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1418 | const InitListExpr *init) { |
| 1419 | ASTContext &ctx = getContext(); |
| 1420 | QualType element = GetStdInitializerListElementType(init->getType()); |
| 1421 | unsigned numInits = init->getNumInits(); |
| 1422 | llvm::APInt size(ctx.getTypeSize(ctx.getSizeType()), numInits); |
| 1423 | QualType array =ctx.getConstantArrayType(element, size, ArrayType::Normal, 0); |
| 1424 | QualType arrayPtr = ctx.getPointerType(array); |
| 1425 | llvm::Type *arrayPtrType = ConvertType(arrayPtr); |
| 1426 | |
| 1427 | // lvalue is the location of a std::initializer_list, which as its first |
| 1428 | // element has a pointer to the array we want to destroy. |
Sebastian Redl | af130fd | 2012-02-19 12:28:02 +0000 | [diff] [blame] | 1429 | llvm::Value *startPointer = Builder.CreateStructGEP(loc, 0, "startPointer"); |
| 1430 | llvm::Value *startAddress = Builder.CreateLoad(startPointer, "startAddress"); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1431 | |
Sebastian Redl | af130fd | 2012-02-19 12:28:02 +0000 | [diff] [blame] | 1432 | ::EmitRecursiveStdInitializerListCleanup(*this, startAddress, init); |
| 1433 | |
| 1434 | llvm::Value *arrayAddress = |
| 1435 | Builder.CreateBitCast(startAddress, arrayPtrType, "arrayAddress"); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1436 | ::EmitStdInitializerListCleanup(*this, array, arrayAddress, init); |
| 1437 | } |