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