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/Support/Compiler.h" 24 #include "llvm/Intrinsics.h" 25 using namespace clang; 26 using namespace CodeGen; 27 28 //===----------------------------------------------------------------------===// 29 // Aggregate Expression Emitter 30 //===----------------------------------------------------------------------===// 31 32 namespace { 33 class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> { 34 CodeGenFunction &CGF; 35 CGBuilderTy &Builder; 36 llvm::Value *DestPtr; 37 bool VolatileDest; 38 bool IgnoreResult; 39 bool IsInitializer; 40 bool RequiresGCollection; 41 public: 42 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v, 43 bool ignore, bool isinit, bool requiresGCollection) 44 : CGF(cgf), Builder(CGF.Builder), 45 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore), 46 IsInitializer(isinit), RequiresGCollection(requiresGCollection) { 47 } 48 49 //===--------------------------------------------------------------------===// 50 // Utilities 51 //===--------------------------------------------------------------------===// 52 53 /// EmitAggLoadOfLValue - Given an expression with aggregate type that 54 /// represents a value lvalue, this method emits the address of the lvalue, 55 /// then loads the result into DestPtr. 56 void EmitAggLoadOfLValue(const Expr *E); 57 58 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 59 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false); 60 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false); 61 62 //===--------------------------------------------------------------------===// 63 // Visitor Methods 64 //===--------------------------------------------------------------------===// 65 66 void VisitStmt(Stmt *S) { 67 CGF.ErrorUnsupported(S, "aggregate expression"); 68 } 69 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } 70 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 71 72 // l-values. 73 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } 74 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 75 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 76 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 77 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 78 EmitAggLoadOfLValue(E); 79 } 80 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 81 EmitAggLoadOfLValue(E); 82 } 83 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 84 EmitAggLoadOfLValue(E); 85 } 86 void VisitPredefinedExpr(const PredefinedExpr *E) { 87 EmitAggLoadOfLValue(E); 88 } 89 90 // Operators. 91 void VisitCastExpr(CastExpr *E); 92 void VisitCallExpr(const CallExpr *E); 93 void VisitStmtExpr(const StmtExpr *E); 94 void VisitBinaryOperator(const BinaryOperator *BO); 95 void VisitBinAssign(const BinaryOperator *E); 96 void VisitBinComma(const BinaryOperator *E); 97 98 void VisitObjCMessageExpr(ObjCMessageExpr *E); 99 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 100 EmitAggLoadOfLValue(E); 101 } 102 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); 103 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E); 104 105 void VisitConditionalOperator(const ConditionalOperator *CO); 106 void VisitChooseExpr(const ChooseExpr *CE); 107 void VisitInitListExpr(InitListExpr *E); 108 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 109 Visit(DAE->getExpr()); 110 } 111 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 112 void VisitCXXConstructExpr(const CXXConstructExpr *E); 113 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E); 114 115 void VisitVAArgExpr(VAArgExpr *E); 116 117 void EmitInitializationToLValue(Expr *E, LValue Address); 118 void EmitNullInitializationToLValue(LValue Address, QualType T); 119 // case Expr::ChooseExprClass: 120 121 }; 122 } // end anonymous namespace. 123 124 //===----------------------------------------------------------------------===// 125 // Utilities 126 //===----------------------------------------------------------------------===// 127 128 /// EmitAggLoadOfLValue - Given an expression with aggregate type that 129 /// represents a value lvalue, this method emits the address of the lvalue, 130 /// then loads the result into DestPtr. 131 void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 132 LValue LV = CGF.EmitLValue(E); 133 EmitFinalDestCopy(E, LV); 134 } 135 136 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 137 void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) { 138 assert(Src.isAggregate() && "value must be aggregate value!"); 139 140 // If the result is ignored, don't copy from the value. 141 if (DestPtr == 0) { 142 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore)) 143 return; 144 // If the source is volatile, we must read from it; to do that, we need 145 // some place to put it. 146 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp"); 147 } 148 149 if (RequiresGCollection) { 150 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 151 DestPtr, Src.getAggregateAddr(), 152 E->getType()); 153 return; 154 } 155 // If the result of the assignment is used, copy the LHS there also. 156 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile 157 // from the source as well, as we can't eliminate it if either operand 158 // is volatile, unless copy has volatile for both source and destination.. 159 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(), 160 VolatileDest|Src.isVolatileQualified()); 161 } 162 163 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 164 void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { 165 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); 166 167 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(), 168 Src.isVolatileQualified()), 169 Ignore); 170 } 171 172 //===----------------------------------------------------------------------===// 173 // Visitor Methods 174 //===----------------------------------------------------------------------===// 175 176 void AggExprEmitter::VisitCastExpr(CastExpr *E) { 177 switch (E->getCastKind()) { 178 default: assert(0 && "Unhandled cast kind!"); 179 180 case CastExpr::CK_ToUnion: { 181 // GCC union extension 182 QualType PtrTy = 183 CGF.getContext().getPointerType(E->getSubExpr()->getType()); 184 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr, 185 CGF.ConvertType(PtrTy)); 186 EmitInitializationToLValue(E->getSubExpr(), 187 LValue::MakeAddr(CastPtr, Qualifiers())); 188 break; 189 } 190 191 // FIXME: Remove the CK_Unknown check here. 192 case CastExpr::CK_Unknown: 193 case CastExpr::CK_NoOp: 194 case CastExpr::CK_UserDefinedConversion: 195 case CastExpr::CK_ConstructorConversion: 196 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 197 E->getType()) && 198 "Implicit cast types must be compatible"); 199 Visit(E->getSubExpr()); 200 break; 201 } 202 } 203 204 void AggExprEmitter::VisitCallExpr(const CallExpr *E) { 205 if (E->getCallReturnType()->isReferenceType()) { 206 EmitAggLoadOfLValue(E); 207 return; 208 } 209 210 RValue RV = CGF.EmitCallExpr(E); 211 EmitFinalDestCopy(E, RV); 212 } 213 214 void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 215 RValue RV = CGF.EmitObjCMessageExpr(E); 216 EmitFinalDestCopy(E, RV); 217 } 218 219 void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 220 RValue RV = CGF.EmitObjCPropertyGet(E); 221 EmitFinalDestCopy(E, RV); 222 } 223 224 void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr( 225 ObjCImplicitSetterGetterRefExpr *E) { 226 RValue RV = CGF.EmitObjCPropertyGet(E); 227 EmitFinalDestCopy(E, RV); 228 } 229 230 void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 231 CGF.EmitAnyExpr(E->getLHS(), 0, false, true); 232 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest, 233 /*IgnoreResult=*/false, IsInitializer); 234 } 235 236 void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 237 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest); 238 } 239 240 void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 241 CGF.ErrorUnsupported(E, "aggregate binary expression"); 242 } 243 244 void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 245 // For an assignment to work, the value on the right has 246 // to be compatible with the value on the left. 247 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 248 E->getRHS()->getType()) 249 && "Invalid assignment"); 250 LValue LHS = CGF.EmitLValue(E->getLHS()); 251 252 // We have to special case property setters, otherwise we must have 253 // a simple lvalue (no aggregates inside vectors, bitfields). 254 if (LHS.isPropertyRef()) { 255 llvm::Value *AggLoc = DestPtr; 256 if (!AggLoc) 257 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType())); 258 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); 259 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), 260 RValue::getAggregate(AggLoc, VolatileDest)); 261 } else if (LHS.isKVCRef()) { 262 llvm::Value *AggLoc = DestPtr; 263 if (!AggLoc) 264 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType())); 265 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); 266 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), 267 RValue::getAggregate(AggLoc, VolatileDest)); 268 } else { 269 bool RequiresGCollection = false; 270 if (CGF.getContext().getLangOptions().NeXTRuntime) { 271 QualType LHSTy = E->getLHS()->getType(); 272 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>()) 273 RequiresGCollection = FDTTy->getDecl()->hasObjectMember(); 274 } 275 // Codegen the RHS so that it stores directly into the LHS. 276 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(), 277 false, false, RequiresGCollection); 278 EmitFinalDestCopy(E, LHS, true); 279 } 280 } 281 282 void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { 283 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 284 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 285 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 286 287 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); 288 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); 289 290 CGF.PushConditionalTempDestruction(); 291 CGF.EmitBlock(LHSBlock); 292 293 // Handle the GNU extension for missing LHS. 294 assert(E->getLHS() && "Must have LHS for aggregate value"); 295 296 Visit(E->getLHS()); 297 CGF.PopConditionalTempDestruction(); 298 CGF.EmitBranch(ContBlock); 299 300 CGF.PushConditionalTempDestruction(); 301 CGF.EmitBlock(RHSBlock); 302 303 Visit(E->getRHS()); 304 CGF.PopConditionalTempDestruction(); 305 CGF.EmitBranch(ContBlock); 306 307 CGF.EmitBlock(ContBlock); 308 } 309 310 void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 311 Visit(CE->getChosenSubExpr(CGF.getContext())); 312 } 313 314 void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 315 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 316 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 317 318 if (!ArgPtr) { 319 CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 320 return; 321 } 322 323 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers())); 324 } 325 326 void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 327 llvm::Value *Val = DestPtr; 328 329 if (!Val) { 330 // Create a temporary variable. 331 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp"); 332 333 // FIXME: volatile 334 CGF.EmitAggExpr(E->getSubExpr(), Val, false); 335 } else 336 Visit(E->getSubExpr()); 337 338 // Don't make this a live temporary if we're emitting an initializer expr. 339 if (!IsInitializer) 340 CGF.PushCXXTemporary(E->getTemporary(), Val); 341 } 342 343 void 344 AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 345 llvm::Value *Val = DestPtr; 346 347 if (!Val) { 348 // Create a temporary variable. 349 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp"); 350 } 351 352 CGF.EmitCXXConstructExpr(Val, E); 353 } 354 355 void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { 356 CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest, IsInitializer); 357 } 358 359 void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { 360 // FIXME: Ignore result? 361 // FIXME: Are initializers affected by volatile? 362 if (isa<ImplicitValueInitExpr>(E)) { 363 EmitNullInitializationToLValue(LV, E->getType()); 364 } else if (E->getType()->isComplexType()) { 365 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); 366 } else if (CGF.hasAggregateLLVMType(E->getType())) { 367 CGF.EmitAnyExpr(E, LV.getAddress(), false); 368 } else { 369 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType()); 370 } 371 } 372 373 void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { 374 if (!CGF.hasAggregateLLVMType(T)) { 375 // For non-aggregates, we can store zero 376 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T)); 377 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T); 378 } else { 379 // Otherwise, just memset the whole thing to zero. This is legal 380 // because in LLVM, all default initializers are guaranteed to have a 381 // bit pattern of all zeros. 382 // FIXME: That isn't true for member pointers! 383 // There's a potential optimization opportunity in combining 384 // memsets; that would be easy for arrays, but relatively 385 // difficult for structures with the current code. 386 CGF.EmitMemSetToZero(LV.getAddress(), T); 387 } 388 } 389 390 void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 391 #if 0 392 // FIXME: Disabled while we figure out what to do about 393 // test/CodeGen/bitfield.c 394 // 395 // If we can, prefer a copy from a global; this is a lot less code for long 396 // globals, and it's easier for the current optimizers to analyze. 397 // FIXME: Should we really be doing this? Should we try to avoid cases where 398 // we emit a global with a lot of zeros? Should we try to avoid short 399 // globals? 400 if (E->isConstantInitializer(CGF.getContext(), 0)) { 401 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF); 402 llvm::GlobalVariable* GV = 403 new llvm::GlobalVariable(C->getType(), true, 404 llvm::GlobalValue::InternalLinkage, 405 C, "", &CGF.CGM.getModule(), 0); 406 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0)); 407 return; 408 } 409 #endif 410 if (E->hadArrayRangeDesignator()) { 411 CGF.ErrorUnsupported(E, "GNU array range designator extension"); 412 } 413 414 // Handle initialization of an array. 415 if (E->getType()->isArrayType()) { 416 const llvm::PointerType *APType = 417 cast<llvm::PointerType>(DestPtr->getType()); 418 const llvm::ArrayType *AType = 419 cast<llvm::ArrayType>(APType->getElementType()); 420 421 uint64_t NumInitElements = E->getNumInits(); 422 423 if (E->getNumInits() > 0) { 424 QualType T1 = E->getType(); 425 QualType T2 = E->getInit(0)->getType(); 426 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { 427 EmitAggLoadOfLValue(E->getInit(0)); 428 return; 429 } 430 } 431 432 uint64_t NumArrayElements = AType->getNumElements(); 433 QualType ElementType = CGF.getContext().getCanonicalType(E->getType()); 434 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType(); 435 436 // FIXME: were we intentionally ignoring address spaces and GC attributes? 437 Qualifiers Quals = CGF.MakeQualifiers(ElementType); 438 439 for (uint64_t i = 0; i != NumArrayElements; ++i) { 440 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); 441 if (i < NumInitElements) 442 EmitInitializationToLValue(E->getInit(i), 443 LValue::MakeAddr(NextVal, Quals)); 444 else 445 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals), 446 ElementType); 447 } 448 return; 449 } 450 451 assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 452 453 // Do struct initialization; this code just sets each individual member 454 // to the approprate value. This makes bitfield support automatic; 455 // the disadvantage is that the generated code is more difficult for 456 // the optimizer, especially with bitfields. 457 unsigned NumInitElements = E->getNumInits(); 458 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 459 unsigned CurInitVal = 0; 460 461 if (E->getType()->isUnionType()) { 462 // Only initialize one field of a union. The field itself is 463 // specified by the initializer list. 464 if (!E->getInitializedFieldInUnion()) { 465 // Empty union; we have nothing to do. 466 467 #ifndef NDEBUG 468 // Make sure that it's really an empty and not a failure of 469 // semantic analysis. 470 for (RecordDecl::field_iterator Field = SD->field_begin(), 471 FieldEnd = SD->field_end(); 472 Field != FieldEnd; ++Field) 473 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 474 #endif 475 return; 476 } 477 478 // FIXME: volatility 479 FieldDecl *Field = E->getInitializedFieldInUnion(); 480 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0); 481 482 if (NumInitElements) { 483 // Store the initializer into the field 484 EmitInitializationToLValue(E->getInit(0), FieldLoc); 485 } else { 486 // Default-initialize to null 487 EmitNullInitializationToLValue(FieldLoc, Field->getType()); 488 } 489 490 return; 491 } 492 493 // Here we iterate over the fields; this makes it simpler to both 494 // default-initialize fields and skip over unnamed fields. 495 for (RecordDecl::field_iterator Field = SD->field_begin(), 496 FieldEnd = SD->field_end(); 497 Field != FieldEnd; ++Field) { 498 // We're done once we hit the flexible array member 499 if (Field->getType()->isIncompleteArrayType()) 500 break; 501 502 if (Field->isUnnamedBitfield()) 503 continue; 504 505 // FIXME: volatility 506 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0); 507 // We never generate write-barries for initialized fields. 508 LValue::SetObjCNonGC(FieldLoc, true); 509 if (CurInitVal < NumInitElements) { 510 // Store the initializer into the field 511 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc); 512 } else { 513 // We're out of initalizers; default-initialize to null 514 EmitNullInitializationToLValue(FieldLoc, Field->getType()); 515 } 516 } 517 } 518 519 //===----------------------------------------------------------------------===// 520 // Entry Points into this File 521 //===----------------------------------------------------------------------===// 522 523 /// EmitAggExpr - Emit the computation of the specified expression of aggregate 524 /// type. The result is computed into DestPtr. Note that if DestPtr is null, 525 /// the value of the aggregate expression is not needed. If VolatileDest is 526 /// true, DestPtr cannot be 0. 527 void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr, 528 bool VolatileDest, bool IgnoreResult, 529 bool IsInitializer, 530 bool RequiresGCollection) { 531 assert(E && hasAggregateLLVMType(E->getType()) && 532 "Invalid aggregate expression to emit"); 533 assert ((DestPtr != 0 || VolatileDest == false) 534 && "volatile aggregate can't be 0"); 535 536 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer, 537 RequiresGCollection) 538 .Visit(const_cast<Expr*>(E)); 539 } 540 541 void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) { 542 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 543 544 EmitMemSetToZero(DestPtr, Ty); 545 } 546 547 void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 548 llvm::Value *SrcPtr, QualType Ty, 549 bool isVolatile) { 550 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 551 552 // Aggregate assignment turns into llvm.memcpy. This is almost valid per 553 // C99 6.5.16.1p3, which states "If the value being stored in an object is 554 // read from another object that overlaps in anyway the storage of the first 555 // object, then the overlap shall be exact and the two objects shall have 556 // qualified or unqualified versions of a compatible type." 557 // 558 // memcpy is not defined if the source and destination pointers are exactly 559 // equal, but other compilers do this optimization, and almost every memcpy 560 // implementation handles this case safely. If there is a libc that does not 561 // safely handle this, we can add a target hook. 562 const llvm::Type *BP = 563 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); 564 if (DestPtr->getType() != BP) 565 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); 566 if (SrcPtr->getType() != BP) 567 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); 568 569 // Get size and alignment info for this aggregate. 570 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 571 572 // FIXME: Handle variable sized types. 573 const llvm::Type *IntPtr = 574 llvm::IntegerType::get(VMContext, LLVMPointerWidth); 575 576 // FIXME: If we have a volatile struct, the optimizer can remove what might 577 // appear to be `extra' memory ops: 578 // 579 // volatile struct { int i; } a, b; 580 // 581 // int main() { 582 // a = b; 583 // a = b; 584 // } 585 // 586 // we need to use a differnt call here. We use isVolatile to indicate when 587 // either the source or the destination is volatile. 588 Builder.CreateCall4(CGM.getMemCpyFn(), 589 DestPtr, SrcPtr, 590 // TypeInfo.first describes size in bits. 591 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), 592 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 593 TypeInfo.second/8)); 594 } 595