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