1 //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This contains code to emit Aggregate Expr nodes as LLVM code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenFunction.h" 15 #include "CodeGenModule.h" 16 #include "CGObjCRuntime.h" 17 #include "clang/AST/ASTContext.h" 18 #include "clang/AST/DeclCXX.h" 19 #include "clang/AST/StmtVisitor.h" 20 #include "llvm/Constants.h" 21 #include "llvm/Function.h" 22 #include "llvm/GlobalVariable.h" 23 #include "llvm/Intrinsics.h" 24 using namespace clang; 25 using namespace CodeGen; 26 27 //===----------------------------------------------------------------------===// 28 // Aggregate Expression Emitter 29 //===----------------------------------------------------------------------===// 30 31 namespace { 32 class AggExprEmitter : public StmtVisitor<AggExprEmitter> { 33 CodeGenFunction &CGF; 34 CGBuilderTy &Builder; 35 AggValueSlot Dest; 36 bool IgnoreResult; 37 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 47 ReturnValueSlot getReturnValueSlot() const { 48 if (!shouldUseDestForReturnSlot()) 49 return ReturnValueSlot(); 50 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"); 57 } 58 59 public: 60 AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest, 61 bool ignore) 62 : CGF(cgf), Builder(CGF.Builder), Dest(Dest), 63 IgnoreResult(ignore) { 64 } 65 66 //===--------------------------------------------------------------------===// 67 // Utilities 68 //===--------------------------------------------------------------------===// 69 70 /// EmitAggLoadOfLValue - Given an expression with aggregate type that 71 /// represents a value lvalue, this method emits the address of the lvalue, 72 /// then loads the result into DestPtr. 73 void EmitAggLoadOfLValue(const Expr *E); 74 75 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 76 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false); 77 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false); 78 79 void EmitMoveFromReturnSlot(const Expr *E, RValue Src); 80 81 AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) { 82 if (CGF.getLangOptions().getGC() && TypeRequiresGCollection(T)) 83 return AggValueSlot::NeedsGCBarriers; 84 return AggValueSlot::DoesNotNeedGCBarriers; 85 } 86 87 bool TypeRequiresGCollection(QualType T); 88 89 //===--------------------------------------------------------------------===// 90 // Visitor Methods 91 //===--------------------------------------------------------------------===// 92 93 void VisitStmt(Stmt *S) { 94 CGF.ErrorUnsupported(S, "aggregate expression"); 95 } 96 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } 97 void VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 98 Visit(GE->getResultExpr()); 99 } 100 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 101 void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) { 102 return Visit(E->getReplacement()); 103 } 104 105 // l-values. 106 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } 107 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 108 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 109 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 110 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 111 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 112 EmitAggLoadOfLValue(E); 113 } 114 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 115 EmitAggLoadOfLValue(E); 116 } 117 void VisitPredefinedExpr(const PredefinedExpr *E) { 118 EmitAggLoadOfLValue(E); 119 } 120 121 // Operators. 122 void VisitCastExpr(CastExpr *E); 123 void VisitCallExpr(const CallExpr *E); 124 void VisitStmtExpr(const StmtExpr *E); 125 void VisitBinaryOperator(const BinaryOperator *BO); 126 void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); 127 void VisitBinAssign(const BinaryOperator *E); 128 void VisitBinComma(const BinaryOperator *E); 129 130 void VisitObjCMessageExpr(ObjCMessageExpr *E); 131 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 132 EmitAggLoadOfLValue(E); 133 } 134 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); 135 136 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); 137 void VisitChooseExpr(const ChooseExpr *CE); 138 void VisitInitListExpr(InitListExpr *E); 139 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); 140 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 141 Visit(DAE->getExpr()); 142 } 143 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 144 void VisitCXXConstructExpr(const CXXConstructExpr *E); 145 void VisitExprWithCleanups(ExprWithCleanups *E); 146 void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 147 void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } 148 void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); 149 void VisitOpaqueValueExpr(OpaqueValueExpr *E); 150 151 void VisitVAArgExpr(VAArgExpr *E); 152 153 void EmitInitializationToLValue(Expr *E, LValue Address); 154 void EmitNullInitializationToLValue(LValue Address); 155 // case Expr::ChooseExprClass: 156 void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } 157 void VisitAtomicExpr(AtomicExpr *E) { 158 CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr()); 159 } 160 }; 161 } // end anonymous namespace. 162 163 //===----------------------------------------------------------------------===// 164 // Utilities 165 //===----------------------------------------------------------------------===// 166 167 /// EmitAggLoadOfLValue - Given an expression with aggregate type that 168 /// represents a value lvalue, this method emits the address of the lvalue, 169 /// then loads the result into DestPtr. 170 void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 171 LValue LV = CGF.EmitLValue(E); 172 EmitFinalDestCopy(E, LV); 173 } 174 175 /// \brief True if the given aggregate type requires special GC API calls. 176 bool AggExprEmitter::TypeRequiresGCollection(QualType T) { 177 // Only record types have members that might require garbage collection. 178 const RecordType *RecordTy = T->getAs<RecordType>(); 179 if (!RecordTy) return false; 180 181 // Don't mess with non-trivial C++ types. 182 RecordDecl *Record = RecordTy->getDecl(); 183 if (isa<CXXRecordDecl>(Record) && 184 (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() || 185 !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) 186 return false; 187 188 // Check whether the type has an object member. 189 return Record->hasObjectMember(); 190 } 191 192 /// \brief Perform the final move to DestPtr if for some reason 193 /// getReturnValueSlot() didn't use it directly. 194 /// 195 /// The idea is that you do something like this: 196 /// RValue Result = EmitSomething(..., getReturnValueSlot()); 197 /// EmitMoveFromReturnSlot(E, Result); 198 /// 199 /// If nothing interferes, this will cause the result to be emitted 200 /// directly into the return value slot. Otherwise, a final move 201 /// will be performed. 202 void AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue Src) { 203 if (shouldUseDestForReturnSlot()) { 204 // Logically, Dest.getAddr() should equal Src.getAggregateAddr(). 205 // The possibility of undef rvalues complicates that a lot, 206 // though, so we can't really assert. 207 return; 208 } 209 210 // Otherwise, do a final copy, 211 assert(Dest.getAddr() != Src.getAggregateAddr()); 212 EmitFinalDestCopy(E, Src, /*Ignore*/ true); 213 } 214 215 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 216 void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) { 217 assert(Src.isAggregate() && "value must be aggregate value!"); 218 219 // If Dest is ignored, then we're evaluating an aggregate expression 220 // in a context (like an expression statement) that doesn't care 221 // about the result. C says that an lvalue-to-rvalue conversion is 222 // performed in these cases; C++ says that it is not. In either 223 // case, we don't actually need to do anything unless the value is 224 // volatile. 225 if (Dest.isIgnored()) { 226 if (!Src.isVolatileQualified() || 227 CGF.CGM.getLangOptions().CPlusPlus || 228 (IgnoreResult && Ignore)) 229 return; 230 231 // If the source is volatile, we must read from it; to do that, we need 232 // some place to put it. 233 Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp"); 234 } 235 236 if (Dest.requiresGCollection()) { 237 CharUnits size = CGF.getContext().getTypeSizeInChars(E->getType()); 238 llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 239 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 240 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 241 Dest.getAddr(), 242 Src.getAggregateAddr(), 243 SizeVal); 244 return; 245 } 246 // If the result of the assignment is used, copy the LHS there also. 247 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile 248 // from the source as well, as we can't eliminate it if either operand 249 // is volatile, unless copy has volatile for both source and destination.. 250 CGF.EmitAggregateCopy(Dest.getAddr(), Src.getAggregateAddr(), E->getType(), 251 Dest.isVolatile()|Src.isVolatileQualified()); 252 } 253 254 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 255 void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { 256 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); 257 258 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(), 259 Src.isVolatileQualified()), 260 Ignore); 261 } 262 263 //===----------------------------------------------------------------------===// 264 // Visitor Methods 265 //===----------------------------------------------------------------------===// 266 267 void AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){ 268 Visit(E->GetTemporaryExpr()); 269 } 270 271 void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) { 272 EmitFinalDestCopy(e, CGF.getOpaqueLValueMapping(e)); 273 } 274 275 void 276 AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 277 if (E->getType().isPODType(CGF.getContext())) { 278 // For a POD type, just emit a load of the lvalue + a copy, because our 279 // compound literal might alias the destination. 280 // FIXME: This is a band-aid; the real problem appears to be in our handling 281 // of assignments, where we store directly into the LHS without checking 282 // whether anything in the RHS aliases. 283 EmitAggLoadOfLValue(E); 284 return; 285 } 286 287 AggValueSlot Slot = EnsureSlot(E->getType()); 288 CGF.EmitAggExpr(E->getInitializer(), Slot); 289 } 290 291 292 void AggExprEmitter::VisitCastExpr(CastExpr *E) { 293 switch (E->getCastKind()) { 294 case CK_Dynamic: { 295 assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?"); 296 LValue LV = CGF.EmitCheckedLValue(E->getSubExpr()); 297 // FIXME: Do we also need to handle property references here? 298 if (LV.isSimple()) 299 CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E)); 300 else 301 CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast"); 302 303 if (!Dest.isIgnored()) 304 CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination"); 305 break; 306 } 307 308 case CK_ToUnion: { 309 if (Dest.isIgnored()) break; 310 311 // GCC union extension 312 QualType Ty = E->getSubExpr()->getType(); 313 QualType PtrTy = CGF.getContext().getPointerType(Ty); 314 llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(), 315 CGF.ConvertType(PtrTy)); 316 EmitInitializationToLValue(E->getSubExpr(), 317 CGF.MakeAddrLValue(CastPtr, Ty)); 318 break; 319 } 320 321 case CK_DerivedToBase: 322 case CK_BaseToDerived: 323 case CK_UncheckedDerivedToBase: { 324 llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: " 325 "should have been unpacked before we got here"); 326 } 327 328 case CK_GetObjCProperty: { 329 LValue LV = CGF.EmitLValue(E->getSubExpr()); 330 assert(LV.isPropertyRef()); 331 RValue RV = CGF.EmitLoadOfPropertyRefLValue(LV, getReturnValueSlot()); 332 EmitMoveFromReturnSlot(E, RV); 333 break; 334 } 335 336 case CK_LValueToRValue: // hope for downstream optimization 337 case CK_NoOp: 338 case CK_UserDefinedConversion: 339 case CK_ConstructorConversion: 340 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 341 E->getType()) && 342 "Implicit cast types must be compatible"); 343 Visit(E->getSubExpr()); 344 break; 345 346 case CK_LValueBitCast: 347 llvm_unreachable("should not be emitting lvalue bitcast as rvalue"); 348 break; 349 350 case CK_Dependent: 351 case CK_BitCast: 352 case CK_ArrayToPointerDecay: 353 case CK_FunctionToPointerDecay: 354 case CK_NullToPointer: 355 case CK_NullToMemberPointer: 356 case CK_BaseToDerivedMemberPointer: 357 case CK_DerivedToBaseMemberPointer: 358 case CK_MemberPointerToBoolean: 359 case CK_IntegralToPointer: 360 case CK_PointerToIntegral: 361 case CK_PointerToBoolean: 362 case CK_ToVoid: 363 case CK_VectorSplat: 364 case CK_IntegralCast: 365 case CK_IntegralToBoolean: 366 case CK_IntegralToFloating: 367 case CK_FloatingToIntegral: 368 case CK_FloatingToBoolean: 369 case CK_FloatingCast: 370 case CK_CPointerToObjCPointerCast: 371 case CK_BlockPointerToObjCPointerCast: 372 case CK_AnyPointerToBlockPointerCast: 373 case CK_ObjCObjectLValueCast: 374 case CK_FloatingRealToComplex: 375 case CK_FloatingComplexToReal: 376 case CK_FloatingComplexToBoolean: 377 case CK_FloatingComplexCast: 378 case CK_FloatingComplexToIntegralComplex: 379 case CK_IntegralRealToComplex: 380 case CK_IntegralComplexToReal: 381 case CK_IntegralComplexToBoolean: 382 case CK_IntegralComplexCast: 383 case CK_IntegralComplexToFloatingComplex: 384 case CK_ARCProduceObject: 385 case CK_ARCConsumeObject: 386 case CK_ARCReclaimReturnedObject: 387 case CK_ARCExtendBlockObject: 388 llvm_unreachable("cast kind invalid for aggregate types"); 389 } 390 } 391 392 void AggExprEmitter::VisitCallExpr(const CallExpr *E) { 393 if (E->getCallReturnType()->isReferenceType()) { 394 EmitAggLoadOfLValue(E); 395 return; 396 } 397 398 RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); 399 EmitMoveFromReturnSlot(E, RV); 400 } 401 402 void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 403 RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); 404 EmitMoveFromReturnSlot(E, RV); 405 } 406 407 void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 408 llvm_unreachable("direct property access not surrounded by " 409 "lvalue-to-rvalue cast"); 410 } 411 412 void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 413 CGF.EmitIgnoredExpr(E->getLHS()); 414 Visit(E->getRHS()); 415 } 416 417 void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 418 CodeGenFunction::StmtExprEvaluation eval(CGF); 419 CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest); 420 } 421 422 void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 423 if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI) 424 VisitPointerToDataMemberBinaryOperator(E); 425 else 426 CGF.ErrorUnsupported(E, "aggregate binary expression"); 427 } 428 429 void AggExprEmitter::VisitPointerToDataMemberBinaryOperator( 430 const BinaryOperator *E) { 431 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); 432 EmitFinalDestCopy(E, LV); 433 } 434 435 void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 436 // For an assignment to work, the value on the right has 437 // to be compatible with the value on the left. 438 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 439 E->getRHS()->getType()) 440 && "Invalid assignment"); 441 442 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->getLHS())) 443 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) 444 if (VD->hasAttr<BlocksAttr>() && 445 E->getRHS()->HasSideEffects(CGF.getContext())) { 446 // When __block variable on LHS, the RHS must be evaluated first 447 // as it may change the 'forwarding' field via call to Block_copy. 448 LValue RHS = CGF.EmitLValue(E->getRHS()); 449 LValue LHS = CGF.EmitLValue(E->getLHS()); 450 Dest = AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 451 needsGC(E->getLHS()->getType()), 452 AggValueSlot::IsAliased); 453 EmitFinalDestCopy(E, RHS, true); 454 return; 455 } 456 457 LValue LHS = CGF.EmitLValue(E->getLHS()); 458 459 // We have to special case property setters, otherwise we must have 460 // a simple lvalue (no aggregates inside vectors, bitfields). 461 if (LHS.isPropertyRef()) { 462 const ObjCPropertyRefExpr *RE = LHS.getPropertyRefExpr(); 463 QualType ArgType = RE->getSetterArgType(); 464 RValue Src; 465 if (ArgType->isReferenceType()) 466 Src = CGF.EmitReferenceBindingToExpr(E->getRHS(), 0); 467 else { 468 AggValueSlot Slot = EnsureSlot(E->getRHS()->getType()); 469 CGF.EmitAggExpr(E->getRHS(), Slot); 470 Src = Slot.asRValue(); 471 } 472 CGF.EmitStoreThroughPropertyRefLValue(Src, LHS); 473 } else { 474 // Codegen the RHS so that it stores directly into the LHS. 475 AggValueSlot LHSSlot = 476 AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 477 needsGC(E->getLHS()->getType()), 478 AggValueSlot::IsAliased); 479 CGF.EmitAggExpr(E->getRHS(), LHSSlot, false); 480 EmitFinalDestCopy(E, LHS, true); 481 } 482 } 483 484 void AggExprEmitter:: 485 VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 486 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 487 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 488 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 489 490 // Bind the common expression if necessary. 491 CodeGenFunction::OpaqueValueMapping binding(CGF, E); 492 493 CodeGenFunction::ConditionalEvaluation eval(CGF); 494 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 495 496 // Save whether the destination's lifetime is externally managed. 497 bool isExternallyDestructed = Dest.isExternallyDestructed(); 498 499 eval.begin(CGF); 500 CGF.EmitBlock(LHSBlock); 501 Visit(E->getTrueExpr()); 502 eval.end(CGF); 503 504 assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!"); 505 CGF.Builder.CreateBr(ContBlock); 506 507 // If the result of an agg expression is unused, then the emission 508 // of the LHS might need to create a destination slot. That's fine 509 // with us, and we can safely emit the RHS into the same slot, but 510 // we shouldn't claim that it's already being destructed. 511 Dest.setExternallyDestructed(isExternallyDestructed); 512 513 eval.begin(CGF); 514 CGF.EmitBlock(RHSBlock); 515 Visit(E->getFalseExpr()); 516 eval.end(CGF); 517 518 CGF.EmitBlock(ContBlock); 519 } 520 521 void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 522 Visit(CE->getChosenSubExpr(CGF.getContext())); 523 } 524 525 void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 526 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 527 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 528 529 if (!ArgPtr) { 530 CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 531 return; 532 } 533 534 EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType())); 535 } 536 537 void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 538 // Ensure that we have a slot, but if we already do, remember 539 // whether it was externally destructed. 540 bool wasExternallyDestructed = Dest.isExternallyDestructed(); 541 Dest = EnsureSlot(E->getType()); 542 543 // We're going to push a destructor if there isn't already one. 544 Dest.setExternallyDestructed(); 545 546 Visit(E->getSubExpr()); 547 548 // Push that destructor we promised. 549 if (!wasExternallyDestructed) 550 CGF.EmitCXXTemporary(E->getTemporary(), Dest.getAddr()); 551 } 552 553 void 554 AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 555 AggValueSlot Slot = EnsureSlot(E->getType()); 556 CGF.EmitCXXConstructExpr(E, Slot); 557 } 558 559 void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) { 560 CGF.EmitExprWithCleanups(E, Dest); 561 } 562 563 void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 564 QualType T = E->getType(); 565 AggValueSlot Slot = EnsureSlot(T); 566 EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 567 } 568 569 void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 570 QualType T = E->getType(); 571 AggValueSlot Slot = EnsureSlot(T); 572 EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 573 } 574 575 /// isSimpleZero - If emitting this value will obviously just cause a store of 576 /// zero to memory, return true. This can return false if uncertain, so it just 577 /// handles simple cases. 578 static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { 579 E = E->IgnoreParens(); 580 581 // 0 582 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) 583 return IL->getValue() == 0; 584 // +0.0 585 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E)) 586 return FL->getValue().isPosZero(); 587 // int() 588 if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) && 589 CGF.getTypes().isZeroInitializable(E->getType())) 590 return true; 591 // (int*)0 - Null pointer expressions. 592 if (const CastExpr *ICE = dyn_cast<CastExpr>(E)) 593 return ICE->getCastKind() == CK_NullToPointer; 594 // '\0' 595 if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) 596 return CL->getValue() == 0; 597 598 // Otherwise, hard case: conservatively return false. 599 return false; 600 } 601 602 603 void 604 AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { 605 QualType type = LV.getType(); 606 // FIXME: Ignore result? 607 // FIXME: Are initializers affected by volatile? 608 if (Dest.isZeroed() && isSimpleZero(E, CGF)) { 609 // Storing "i32 0" to a zero'd memory location is a noop. 610 } else if (isa<ImplicitValueInitExpr>(E)) { 611 EmitNullInitializationToLValue(LV); 612 } else if (type->isReferenceType()) { 613 RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); 614 CGF.EmitStoreThroughLValue(RV, LV); 615 } else if (type->isAnyComplexType()) { 616 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); 617 } else if (CGF.hasAggregateLLVMType(type)) { 618 CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV, 619 AggValueSlot::IsDestructed, 620 AggValueSlot::DoesNotNeedGCBarriers, 621 AggValueSlot::IsNotAliased, 622 Dest.isZeroed())); 623 } else if (LV.isSimple()) { 624 CGF.EmitScalarInit(E, /*D=*/0, LV, /*Captured=*/false); 625 } else { 626 CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV); 627 } 628 } 629 630 void AggExprEmitter::EmitNullInitializationToLValue(LValue lv) { 631 QualType type = lv.getType(); 632 633 // If the destination slot is already zeroed out before the aggregate is 634 // copied into it, we don't have to emit any zeros here. 635 if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type)) 636 return; 637 638 if (!CGF.hasAggregateLLVMType(type)) { 639 // For non-aggregates, we can store zero 640 llvm::Value *null = llvm::Constant::getNullValue(CGF.ConvertType(type)); 641 CGF.EmitStoreThroughLValue(RValue::get(null), lv); 642 } else { 643 // There's a potential optimization opportunity in combining 644 // memsets; that would be easy for arrays, but relatively 645 // difficult for structures with the current code. 646 CGF.EmitNullInitialization(lv.getAddress(), lv.getType()); 647 } 648 } 649 650 void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 651 #if 0 652 // FIXME: Assess perf here? Figure out what cases are worth optimizing here 653 // (Length of globals? Chunks of zeroed-out space?). 654 // 655 // If we can, prefer a copy from a global; this is a lot less code for long 656 // globals, and it's easier for the current optimizers to analyze. 657 if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { 658 llvm::GlobalVariable* GV = 659 new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, 660 llvm::GlobalValue::InternalLinkage, C, ""); 661 EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType())); 662 return; 663 } 664 #endif 665 if (E->hadArrayRangeDesignator()) 666 CGF.ErrorUnsupported(E, "GNU array range designator extension"); 667 668 llvm::Value *DestPtr = Dest.getAddr(); 669 670 // Handle initialization of an array. 671 if (E->getType()->isArrayType()) { 672 llvm::PointerType *APType = 673 cast<llvm::PointerType>(DestPtr->getType()); 674 llvm::ArrayType *AType = 675 cast<llvm::ArrayType>(APType->getElementType()); 676 677 uint64_t NumInitElements = E->getNumInits(); 678 679 if (E->getNumInits() > 0) { 680 QualType T1 = E->getType(); 681 QualType T2 = E->getInit(0)->getType(); 682 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { 683 EmitAggLoadOfLValue(E->getInit(0)); 684 return; 685 } 686 } 687 688 uint64_t NumArrayElements = AType->getNumElements(); 689 assert(NumInitElements <= NumArrayElements); 690 691 QualType elementType = E->getType().getCanonicalType(); 692 elementType = CGF.getContext().getQualifiedType( 693 cast<ArrayType>(elementType)->getElementType(), 694 elementType.getQualifiers() + Dest.getQualifiers()); 695 696 // DestPtr is an array*. Construct an elementType* by drilling 697 // down a level. 698 llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0); 699 llvm::Value *indices[] = { zero, zero }; 700 llvm::Value *begin = 701 Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin"); 702 703 // Exception safety requires us to destroy all the 704 // already-constructed members if an initializer throws. 705 // For that, we'll need an EH cleanup. 706 QualType::DestructionKind dtorKind = elementType.isDestructedType(); 707 llvm::AllocaInst *endOfInit = 0; 708 EHScopeStack::stable_iterator cleanup; 709 if (CGF.needsEHCleanup(dtorKind)) { 710 // In principle we could tell the cleanup where we are more 711 // directly, but the control flow can get so varied here that it 712 // would actually be quite complex. Therefore we go through an 713 // alloca. 714 endOfInit = CGF.CreateTempAlloca(begin->getType(), 715 "arrayinit.endOfInit"); 716 Builder.CreateStore(begin, endOfInit); 717 CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType, 718 CGF.getDestroyer(dtorKind)); 719 cleanup = CGF.EHStack.stable_begin(); 720 721 // Otherwise, remember that we didn't need a cleanup. 722 } else { 723 dtorKind = QualType::DK_none; 724 } 725 726 llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1); 727 728 // The 'current element to initialize'. The invariants on this 729 // variable are complicated. Essentially, after each iteration of 730 // the loop, it points to the last initialized element, except 731 // that it points to the beginning of the array before any 732 // elements have been initialized. 733 llvm::Value *element = begin; 734 735 // Emit the explicit initializers. 736 for (uint64_t i = 0; i != NumInitElements; ++i) { 737 // Advance to the next element. 738 if (i > 0) { 739 element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element"); 740 741 // Tell the cleanup that it needs to destroy up to this 742 // element. TODO: some of these stores can be trivially 743 // observed to be unnecessary. 744 if (endOfInit) Builder.CreateStore(element, endOfInit); 745 } 746 747 LValue elementLV = CGF.MakeAddrLValue(element, elementType); 748 EmitInitializationToLValue(E->getInit(i), elementLV); 749 } 750 751 // Check whether there's a non-trivial array-fill expression. 752 // Note that this will be a CXXConstructExpr even if the element 753 // type is an array (or array of array, etc.) of class type. 754 Expr *filler = E->getArrayFiller(); 755 bool hasTrivialFiller = true; 756 if (CXXConstructExpr *cons = dyn_cast_or_null<CXXConstructExpr>(filler)) { 757 assert(cons->getConstructor()->isDefaultConstructor()); 758 hasTrivialFiller = cons->getConstructor()->isTrivial(); 759 } 760 761 // Any remaining elements need to be zero-initialized, possibly 762 // using the filler expression. We can skip this if the we're 763 // emitting to zeroed memory. 764 if (NumInitElements != NumArrayElements && 765 !(Dest.isZeroed() && hasTrivialFiller && 766 CGF.getTypes().isZeroInitializable(elementType))) { 767 768 // Use an actual loop. This is basically 769 // do { *array++ = filler; } while (array != end); 770 771 // Advance to the start of the rest of the array. 772 if (NumInitElements) { 773 element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start"); 774 if (endOfInit) Builder.CreateStore(element, endOfInit); 775 } 776 777 // Compute the end of the array. 778 llvm::Value *end = Builder.CreateInBoundsGEP(begin, 779 llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements), 780 "arrayinit.end"); 781 782 llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); 783 llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body"); 784 785 // Jump into the body. 786 CGF.EmitBlock(bodyBB); 787 llvm::PHINode *currentElement = 788 Builder.CreatePHI(element->getType(), 2, "arrayinit.cur"); 789 currentElement->addIncoming(element, entryBB); 790 791 // Emit the actual filler expression. 792 LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType); 793 if (filler) 794 EmitInitializationToLValue(filler, elementLV); 795 else 796 EmitNullInitializationToLValue(elementLV); 797 798 // Move on to the next element. 799 llvm::Value *nextElement = 800 Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next"); 801 802 // Tell the EH cleanup that we finished with the last element. 803 if (endOfInit) Builder.CreateStore(nextElement, endOfInit); 804 805 // Leave the loop if we're done. 806 llvm::Value *done = Builder.CreateICmpEQ(nextElement, end, 807 "arrayinit.done"); 808 llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end"); 809 Builder.CreateCondBr(done, endBB, bodyBB); 810 currentElement->addIncoming(nextElement, Builder.GetInsertBlock()); 811 812 CGF.EmitBlock(endBB); 813 } 814 815 // Leave the partial-array cleanup if we entered one. 816 if (dtorKind) CGF.DeactivateCleanupBlock(cleanup); 817 818 return; 819 } 820 821 assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 822 823 // Do struct initialization; this code just sets each individual member 824 // to the approprate value. This makes bitfield support automatic; 825 // the disadvantage is that the generated code is more difficult for 826 // the optimizer, especially with bitfields. 827 unsigned NumInitElements = E->getNumInits(); 828 RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl(); 829 830 if (record->isUnion()) { 831 // Only initialize one field of a union. The field itself is 832 // specified by the initializer list. 833 if (!E->getInitializedFieldInUnion()) { 834 // Empty union; we have nothing to do. 835 836 #ifndef NDEBUG 837 // Make sure that it's really an empty and not a failure of 838 // semantic analysis. 839 for (RecordDecl::field_iterator Field = record->field_begin(), 840 FieldEnd = record->field_end(); 841 Field != FieldEnd; ++Field) 842 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 843 #endif 844 return; 845 } 846 847 // FIXME: volatility 848 FieldDecl *Field = E->getInitializedFieldInUnion(); 849 850 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0); 851 if (NumInitElements) { 852 // Store the initializer into the field 853 EmitInitializationToLValue(E->getInit(0), FieldLoc); 854 } else { 855 // Default-initialize to null. 856 EmitNullInitializationToLValue(FieldLoc); 857 } 858 859 return; 860 } 861 862 // We'll need to enter cleanup scopes in case any of the member 863 // initializers throw an exception. 864 SmallVector<EHScopeStack::stable_iterator, 16> cleanups; 865 866 // Here we iterate over the fields; this makes it simpler to both 867 // default-initialize fields and skip over unnamed fields. 868 unsigned curInitIndex = 0; 869 for (RecordDecl::field_iterator field = record->field_begin(), 870 fieldEnd = record->field_end(); 871 field != fieldEnd; ++field) { 872 // We're done once we hit the flexible array member. 873 if (field->getType()->isIncompleteArrayType()) 874 break; 875 876 // Always skip anonymous bitfields. 877 if (field->isUnnamedBitfield()) 878 continue; 879 880 // We're done if we reach the end of the explicit initializers, we 881 // have a zeroed object, and the rest of the fields are 882 // zero-initializable. 883 if (curInitIndex == NumInitElements && Dest.isZeroed() && 884 CGF.getTypes().isZeroInitializable(E->getType())) 885 break; 886 887 // FIXME: volatility 888 LValue LV = CGF.EmitLValueForFieldInitialization(DestPtr, *field, 0); 889 // We never generate write-barries for initialized fields. 890 LV.setNonGC(true); 891 892 if (curInitIndex < NumInitElements) { 893 // Store the initializer into the field. 894 EmitInitializationToLValue(E->getInit(curInitIndex++), LV); 895 } else { 896 // We're out of initalizers; default-initialize to null 897 EmitNullInitializationToLValue(LV); 898 } 899 900 // Push a destructor if necessary. 901 // FIXME: if we have an array of structures, all explicitly 902 // initialized, we can end up pushing a linear number of cleanups. 903 bool pushedCleanup = false; 904 if (QualType::DestructionKind dtorKind 905 = field->getType().isDestructedType()) { 906 assert(LV.isSimple()); 907 if (CGF.needsEHCleanup(dtorKind)) { 908 CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(), 909 CGF.getDestroyer(dtorKind), false); 910 cleanups.push_back(CGF.EHStack.stable_begin()); 911 pushedCleanup = true; 912 } 913 } 914 915 // If the GEP didn't get used because of a dead zero init or something 916 // else, clean it up for -O0 builds and general tidiness. 917 if (!pushedCleanup && LV.isSimple()) 918 if (llvm::GetElementPtrInst *GEP = 919 dyn_cast<llvm::GetElementPtrInst>(LV.getAddress())) 920 if (GEP->use_empty()) 921 GEP->eraseFromParent(); 922 } 923 924 // Deactivate all the partial cleanups in reverse order, which 925 // generally means popping them. 926 for (unsigned i = cleanups.size(); i != 0; --i) 927 CGF.DeactivateCleanupBlock(cleanups[i-1]); 928 } 929 930 //===----------------------------------------------------------------------===// 931 // Entry Points into this File 932 //===----------------------------------------------------------------------===// 933 934 /// GetNumNonZeroBytesInInit - Get an approximate count of the number of 935 /// non-zero bytes that will be stored when outputting the initializer for the 936 /// specified initializer expression. 937 static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) { 938 E = E->IgnoreParens(); 939 940 // 0 and 0.0 won't require any non-zero stores! 941 if (isSimpleZero(E, CGF)) return CharUnits::Zero(); 942 943 // If this is an initlist expr, sum up the size of sizes of the (present) 944 // elements. If this is something weird, assume the whole thing is non-zero. 945 const InitListExpr *ILE = dyn_cast<InitListExpr>(E); 946 if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType())) 947 return CGF.getContext().getTypeSizeInChars(E->getType()); 948 949 // InitListExprs for structs have to be handled carefully. If there are 950 // reference members, we need to consider the size of the reference, not the 951 // referencee. InitListExprs for unions and arrays can't have references. 952 if (const RecordType *RT = E->getType()->getAs<RecordType>()) { 953 if (!RT->isUnionType()) { 954 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 955 CharUnits NumNonZeroBytes = CharUnits::Zero(); 956 957 unsigned ILEElement = 0; 958 for (RecordDecl::field_iterator Field = SD->field_begin(), 959 FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) { 960 // We're done once we hit the flexible array member or run out of 961 // InitListExpr elements. 962 if (Field->getType()->isIncompleteArrayType() || 963 ILEElement == ILE->getNumInits()) 964 break; 965 if (Field->isUnnamedBitfield()) 966 continue; 967 968 const Expr *E = ILE->getInit(ILEElement++); 969 970 // Reference values are always non-null and have the width of a pointer. 971 if (Field->getType()->isReferenceType()) 972 NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits( 973 CGF.getContext().getTargetInfo().getPointerWidth(0)); 974 else 975 NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF); 976 } 977 978 return NumNonZeroBytes; 979 } 980 } 981 982 983 CharUnits NumNonZeroBytes = CharUnits::Zero(); 984 for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) 985 NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF); 986 return NumNonZeroBytes; 987 } 988 989 /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of 990 /// zeros in it, emit a memset and avoid storing the individual zeros. 991 /// 992 static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E, 993 CodeGenFunction &CGF) { 994 // If the slot is already known to be zeroed, nothing to do. Don't mess with 995 // volatile stores. 996 if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return; 997 998 // C++ objects with a user-declared constructor don't need zero'ing. 999 if (CGF.getContext().getLangOptions().CPlusPlus) 1000 if (const RecordType *RT = CGF.getContext() 1001 .getBaseElementType(E->getType())->getAs<RecordType>()) { 1002 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1003 if (RD->hasUserDeclaredConstructor()) 1004 return; 1005 } 1006 1007 // If the type is 16-bytes or smaller, prefer individual stores over memset. 1008 std::pair<CharUnits, CharUnits> TypeInfo = 1009 CGF.getContext().getTypeInfoInChars(E->getType()); 1010 if (TypeInfo.first <= CharUnits::fromQuantity(16)) 1011 return; 1012 1013 // Check to see if over 3/4 of the initializer are known to be zero. If so, 1014 // we prefer to emit memset + individual stores for the rest. 1015 CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF); 1016 if (NumNonZeroBytes*4 > TypeInfo.first) 1017 return; 1018 1019 // Okay, it seems like a good idea to use an initial memset, emit the call. 1020 llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity()); 1021 CharUnits Align = TypeInfo.second; 1022 1023 llvm::Value *Loc = Slot.getAddr(); 1024 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 1025 1026 Loc = CGF.Builder.CreateBitCast(Loc, BP); 1027 CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, 1028 Align.getQuantity(), false); 1029 1030 // Tell the AggExprEmitter that the slot is known zero. 1031 Slot.setZeroed(); 1032 } 1033 1034 1035 1036 1037 /// EmitAggExpr - Emit the computation of the specified expression of aggregate 1038 /// type. The result is computed into DestPtr. Note that if DestPtr is null, 1039 /// the value of the aggregate expression is not needed. If VolatileDest is 1040 /// true, DestPtr cannot be 0. 1041 /// 1042 /// \param IsInitializer - true if this evaluation is initializing an 1043 /// object whose lifetime is already being managed. 1044 void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot, 1045 bool IgnoreResult) { 1046 assert(E && hasAggregateLLVMType(E->getType()) && 1047 "Invalid aggregate expression to emit"); 1048 assert((Slot.getAddr() != 0 || Slot.isIgnored()) && 1049 "slot has bits but no address"); 1050 1051 // Optimize the slot if possible. 1052 CheckAggExprForMemSetUse(Slot, E, *this); 1053 1054 AggExprEmitter(*this, Slot, IgnoreResult).Visit(const_cast<Expr*>(E)); 1055 } 1056 1057 LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { 1058 assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!"); 1059 llvm::Value *Temp = CreateMemTemp(E->getType()); 1060 LValue LV = MakeAddrLValue(Temp, E->getType()); 1061 EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed, 1062 AggValueSlot::DoesNotNeedGCBarriers, 1063 AggValueSlot::IsNotAliased)); 1064 return LV; 1065 } 1066 1067 void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 1068 llvm::Value *SrcPtr, QualType Ty, 1069 bool isVolatile) { 1070 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 1071 1072 if (getContext().getLangOptions().CPlusPlus) { 1073 if (const RecordType *RT = Ty->getAs<RecordType>()) { 1074 CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 1075 assert((Record->hasTrivialCopyConstructor() || 1076 Record->hasTrivialCopyAssignment() || 1077 Record->hasTrivialMoveConstructor() || 1078 Record->hasTrivialMoveAssignment()) && 1079 "Trying to aggregate-copy a type without a trivial copy " 1080 "constructor or assignment operator"); 1081 // Ignore empty classes in C++. 1082 if (Record->isEmpty()) 1083 return; 1084 } 1085 } 1086 1087 // Aggregate assignment turns into llvm.memcpy. This is almost valid per 1088 // C99 6.5.16.1p3, which states "If the value being stored in an object is 1089 // read from another object that overlaps in anyway the storage of the first 1090 // object, then the overlap shall be exact and the two objects shall have 1091 // qualified or unqualified versions of a compatible type." 1092 // 1093 // memcpy is not defined if the source and destination pointers are exactly 1094 // equal, but other compilers do this optimization, and almost every memcpy 1095 // implementation handles this case safely. If there is a libc that does not 1096 // safely handle this, we can add a target hook. 1097 1098 // Get size and alignment info for this aggregate. 1099 std::pair<CharUnits, CharUnits> TypeInfo = 1100 getContext().getTypeInfoInChars(Ty); 1101 1102 // FIXME: Handle variable sized types. 1103 1104 // FIXME: If we have a volatile struct, the optimizer can remove what might 1105 // appear to be `extra' memory ops: 1106 // 1107 // volatile struct { int i; } a, b; 1108 // 1109 // int main() { 1110 // a = b; 1111 // a = b; 1112 // } 1113 // 1114 // we need to use a different call here. We use isVolatile to indicate when 1115 // either the source or the destination is volatile. 1116 1117 llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); 1118 llvm::Type *DBP = 1119 llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace()); 1120 DestPtr = Builder.CreateBitCast(DestPtr, DBP); 1121 1122 llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); 1123 llvm::Type *SBP = 1124 llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace()); 1125 SrcPtr = Builder.CreateBitCast(SrcPtr, SBP); 1126 1127 // Don't do any of the memmove_collectable tests if GC isn't set. 1128 if (CGM.getLangOptions().getGC() == LangOptions::NonGC) { 1129 // fall through 1130 } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { 1131 RecordDecl *Record = RecordTy->getDecl(); 1132 if (Record->hasObjectMember()) { 1133 CharUnits size = TypeInfo.first; 1134 llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1135 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1136 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1137 SizeVal); 1138 return; 1139 } 1140 } else if (Ty->isArrayType()) { 1141 QualType BaseType = getContext().getBaseElementType(Ty); 1142 if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { 1143 if (RecordTy->getDecl()->hasObjectMember()) { 1144 CharUnits size = TypeInfo.first; 1145 llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1146 llvm::Value *SizeVal = 1147 llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1148 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1149 SizeVal); 1150 return; 1151 } 1152 } 1153 } 1154 1155 Builder.CreateMemCpy(DestPtr, SrcPtr, 1156 llvm::ConstantInt::get(IntPtrTy, 1157 TypeInfo.first.getQuantity()), 1158 TypeInfo.second.getQuantity(), isVolatile); 1159 } 1160