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 VisitObjCKVCRefExpr(ObjCKVCRefExpr *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::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) { 212 RValue RV = CGF.EmitObjCPropertyGet(E); 213 EmitFinalDestCopy(E, RV); 214 } 215 216 void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 217 CGF.EmitAnyExpr(E->getLHS(), 0, false, true); 218 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest, 219 /*IgnoreResult=*/false, IsInitializer); 220 } 221 222 void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 223 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest); 224 } 225 226 void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 227 CGF.ErrorUnsupported(E, "aggregate binary expression"); 228 } 229 230 void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 231 // For an assignment to work, the value on the right has 232 // to be compatible with the value on the left. 233 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 234 E->getRHS()->getType()) 235 && "Invalid assignment"); 236 LValue LHS = CGF.EmitLValue(E->getLHS()); 237 238 // We have to special case property setters, otherwise we must have 239 // a simple lvalue (no aggregates inside vectors, bitfields). 240 if (LHS.isPropertyRef()) { 241 llvm::Value *AggLoc = DestPtr; 242 if (!AggLoc) 243 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType())); 244 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); 245 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), 246 RValue::getAggregate(AggLoc, VolatileDest)); 247 } else if (LHS.isKVCRef()) { 248 llvm::Value *AggLoc = DestPtr; 249 if (!AggLoc) 250 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType())); 251 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); 252 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), 253 RValue::getAggregate(AggLoc, VolatileDest)); 254 } else { 255 if (CGF.getContext().getLangOptions().NeXTRuntime) { 256 QualType LHSTy = E->getLHS()->getType(); 257 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>()) 258 if (FDTTy->getDecl()->hasObjectMember()) { 259 LValue RHS = CGF.EmitLValue(E->getRHS()); 260 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, LHS.getAddress(), 261 RHS.getAddress(), 262 CGF.getContext().getTypeSize(LHSTy) / 8); 263 return; 264 } 265 } 266 // Codegen the RHS so that it stores directly into the LHS. 267 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified()); 268 EmitFinalDestCopy(E, LHS, true); 269 } 270 } 271 272 void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { 273 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 274 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 275 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 276 277 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); 278 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); 279 280 CGF.PushConditionalTempDestruction(); 281 CGF.EmitBlock(LHSBlock); 282 283 // Handle the GNU extension for missing LHS. 284 assert(E->getLHS() && "Must have LHS for aggregate value"); 285 286 Visit(E->getLHS()); 287 CGF.PopConditionalTempDestruction(); 288 CGF.EmitBranch(ContBlock); 289 290 CGF.PushConditionalTempDestruction(); 291 CGF.EmitBlock(RHSBlock); 292 293 Visit(E->getRHS()); 294 CGF.PopConditionalTempDestruction(); 295 CGF.EmitBranch(ContBlock); 296 297 CGF.EmitBlock(ContBlock); 298 } 299 300 void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 301 Visit(CE->getChosenSubExpr(CGF.getContext())); 302 } 303 304 void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 305 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 306 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 307 308 if (!ArgPtr) { 309 CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 310 return; 311 } 312 313 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0)); 314 } 315 316 void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 317 llvm::Value *Val = DestPtr; 318 319 if (!Val) { 320 // Create a temporary variable. 321 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp"); 322 323 // FIXME: volatile 324 CGF.EmitAggExpr(E->getSubExpr(), Val, false); 325 } else 326 Visit(E->getSubExpr()); 327 328 // Don't make this a live temporary if we're emitting an initializer expr. 329 if (!IsInitializer) 330 CGF.PushCXXTemporary(E->getTemporary(), Val); 331 } 332 333 void 334 AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 335 llvm::Value *Val = DestPtr; 336 337 if (!Val) { 338 // Create a temporary variable. 339 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp"); 340 } 341 342 CGF.EmitCXXConstructExpr(Val, E); 343 } 344 345 void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { 346 CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest, IsInitializer); 347 } 348 349 void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { 350 // FIXME: Ignore result? 351 // FIXME: Are initializers affected by volatile? 352 if (isa<ImplicitValueInitExpr>(E)) { 353 EmitNullInitializationToLValue(LV, E->getType()); 354 } else if (E->getType()->isComplexType()) { 355 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); 356 } else if (CGF.hasAggregateLLVMType(E->getType())) { 357 CGF.EmitAnyExpr(E, LV.getAddress(), false); 358 } else { 359 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType()); 360 } 361 } 362 363 void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { 364 if (!CGF.hasAggregateLLVMType(T)) { 365 // For non-aggregates, we can store zero 366 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T)); 367 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T); 368 } else { 369 // Otherwise, just memset the whole thing to zero. This is legal 370 // because in LLVM, all default initializers are guaranteed to have a 371 // bit pattern of all zeros. 372 // FIXME: That isn't true for member pointers! 373 // There's a potential optimization opportunity in combining 374 // memsets; that would be easy for arrays, but relatively 375 // difficult for structures with the current code. 376 CGF.EmitMemSetToZero(LV.getAddress(), T); 377 } 378 } 379 380 void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 381 #if 0 382 // FIXME: Disabled while we figure out what to do about 383 // test/CodeGen/bitfield.c 384 // 385 // If we can, prefer a copy from a global; this is a lot less code for long 386 // globals, and it's easier for the current optimizers to analyze. 387 // FIXME: Should we really be doing this? Should we try to avoid cases where 388 // we emit a global with a lot of zeros? Should we try to avoid short 389 // globals? 390 if (E->isConstantInitializer(CGF.getContext(), 0)) { 391 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF); 392 llvm::GlobalVariable* GV = 393 new llvm::GlobalVariable(C->getType(), true, 394 llvm::GlobalValue::InternalLinkage, 395 C, "", &CGF.CGM.getModule(), 0); 396 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0)); 397 return; 398 } 399 #endif 400 if (E->hadArrayRangeDesignator()) { 401 CGF.ErrorUnsupported(E, "GNU array range designator extension"); 402 } 403 404 // Handle initialization of an array. 405 if (E->getType()->isArrayType()) { 406 const llvm::PointerType *APType = 407 cast<llvm::PointerType>(DestPtr->getType()); 408 const llvm::ArrayType *AType = 409 cast<llvm::ArrayType>(APType->getElementType()); 410 411 uint64_t NumInitElements = E->getNumInits(); 412 413 if (E->getNumInits() > 0) { 414 QualType T1 = E->getType(); 415 QualType T2 = E->getInit(0)->getType(); 416 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { 417 EmitAggLoadOfLValue(E->getInit(0)); 418 return; 419 } 420 } 421 422 uint64_t NumArrayElements = AType->getNumElements(); 423 QualType ElementType = CGF.getContext().getCanonicalType(E->getType()); 424 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType(); 425 426 unsigned CVRqualifier = ElementType.getCVRQualifiers(); 427 428 for (uint64_t i = 0; i != NumArrayElements; ++i) { 429 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); 430 if (i < NumInitElements) 431 EmitInitializationToLValue(E->getInit(i), 432 LValue::MakeAddr(NextVal, CVRqualifier)); 433 else 434 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier), 435 ElementType); 436 } 437 return; 438 } 439 440 assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 441 442 // Do struct initialization; this code just sets each individual member 443 // to the approprate value. This makes bitfield support automatic; 444 // the disadvantage is that the generated code is more difficult for 445 // the optimizer, especially with bitfields. 446 unsigned NumInitElements = E->getNumInits(); 447 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 448 unsigned CurInitVal = 0; 449 450 if (E->getType()->isUnionType()) { 451 // Only initialize one field of a union. The field itself is 452 // specified by the initializer list. 453 if (!E->getInitializedFieldInUnion()) { 454 // Empty union; we have nothing to do. 455 456 #ifndef NDEBUG 457 // Make sure that it's really an empty and not a failure of 458 // semantic analysis. 459 for (RecordDecl::field_iterator Field = SD->field_begin(), 460 FieldEnd = SD->field_end(); 461 Field != FieldEnd; ++Field) 462 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 463 #endif 464 return; 465 } 466 467 // FIXME: volatility 468 FieldDecl *Field = E->getInitializedFieldInUnion(); 469 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0); 470 471 if (NumInitElements) { 472 // Store the initializer into the field 473 EmitInitializationToLValue(E->getInit(0), FieldLoc); 474 } else { 475 // Default-initialize to null 476 EmitNullInitializationToLValue(FieldLoc, Field->getType()); 477 } 478 479 return; 480 } 481 482 // Here we iterate over the fields; this makes it simpler to both 483 // default-initialize fields and skip over unnamed fields. 484 for (RecordDecl::field_iterator Field = SD->field_begin(), 485 FieldEnd = SD->field_end(); 486 Field != FieldEnd; ++Field) { 487 // We're done once we hit the flexible array member 488 if (Field->getType()->isIncompleteArrayType()) 489 break; 490 491 if (Field->isUnnamedBitfield()) 492 continue; 493 494 // FIXME: volatility 495 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0); 496 // We never generate write-barries for initialized fields. 497 LValue::SetObjCNonGC(FieldLoc, true); 498 if (CurInitVal < NumInitElements) { 499 // Store the initializer into the field 500 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc); 501 } else { 502 // We're out of initalizers; default-initialize to null 503 EmitNullInitializationToLValue(FieldLoc, Field->getType()); 504 } 505 } 506 } 507 508 //===----------------------------------------------------------------------===// 509 // Entry Points into this File 510 //===----------------------------------------------------------------------===// 511 512 /// EmitAggExpr - Emit the computation of the specified expression of aggregate 513 /// type. The result is computed into DestPtr. Note that if DestPtr is null, 514 /// the value of the aggregate expression is not needed. If VolatileDest is 515 /// true, DestPtr cannot be 0. 516 void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr, 517 bool VolatileDest, bool IgnoreResult, 518 bool IsInitializer) { 519 assert(E && hasAggregateLLVMType(E->getType()) && 520 "Invalid aggregate expression to emit"); 521 assert ((DestPtr != 0 || VolatileDest == false) 522 && "volatile aggregate can't be 0"); 523 524 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer) 525 .Visit(const_cast<Expr*>(E)); 526 } 527 528 void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) { 529 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 530 531 EmitMemSetToZero(DestPtr, Ty); 532 } 533 534 void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 535 llvm::Value *SrcPtr, QualType Ty, 536 bool isVolatile) { 537 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 538 539 // Aggregate assignment turns into llvm.memcpy. This is almost valid per 540 // C99 6.5.16.1p3, which states "If the value being stored in an object is 541 // read from another object that overlaps in anyway the storage of the first 542 // object, then the overlap shall be exact and the two objects shall have 543 // qualified or unqualified versions of a compatible type." 544 // 545 // memcpy is not defined if the source and destination pointers are exactly 546 // equal, but other compilers do this optimization, and almost every memcpy 547 // implementation handles this case safely. If there is a libc that does not 548 // safely handle this, we can add a target hook. 549 const llvm::Type *BP = 550 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); 551 if (DestPtr->getType() != BP) 552 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); 553 if (SrcPtr->getType() != BP) 554 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); 555 556 // Get size and alignment info for this aggregate. 557 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 558 559 // FIXME: Handle variable sized types. 560 const llvm::Type *IntPtr = 561 llvm::IntegerType::get(VMContext, LLVMPointerWidth); 562 563 // FIXME: If we have a volatile struct, the optimizer can remove what might 564 // appear to be `extra' memory ops: 565 // 566 // volatile struct { int i; } a, b; 567 // 568 // int main() { 569 // a = b; 570 // a = b; 571 // } 572 // 573 // we need to use a differnt call here. We use isVolatile to indicate when 574 // either the source or the destination is volatile. 575 Builder.CreateCall4(CGM.getMemCpyFn(), 576 DestPtr, SrcPtr, 577 // TypeInfo.first describes size in bits. 578 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), 579 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 580 TypeInfo.second/8)); 581 } 582