CGExprAgg.cpp revision 251662
1193326Sed//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This contains code to emit Aggregate Expr nodes as LLVM code. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14193326Sed#include "CodeGenFunction.h" 15249423Sdim#include "CGObjCRuntime.h" 16193326Sed#include "CodeGenModule.h" 17193326Sed#include "clang/AST/ASTContext.h" 18193326Sed#include "clang/AST/DeclCXX.h" 19234353Sdim#include "clang/AST/DeclTemplate.h" 20193326Sed#include "clang/AST/StmtVisitor.h" 21249423Sdim#include "llvm/IR/Constants.h" 22249423Sdim#include "llvm/IR/Function.h" 23249423Sdim#include "llvm/IR/GlobalVariable.h" 24249423Sdim#include "llvm/IR/Intrinsics.h" 25193326Sedusing namespace clang; 26193326Sedusing namespace CodeGen; 27193326Sed 28193326Sed//===----------------------------------------------------------------------===// 29193326Sed// Aggregate Expression Emitter 30193326Sed//===----------------------------------------------------------------------===// 31193326Sed 32249423Sdimllvm::Value *AggValueSlot::getPaddedAtomicAddr() const { 33249423Sdim assert(isValueOfAtomic()); 34249423Sdim llvm::GEPOperator *op = cast<llvm::GEPOperator>(getAddr()); 35249423Sdim assert(op->getNumIndices() == 2); 36249423Sdim assert(op->hasAllZeroIndices()); 37249423Sdim return op->getPointerOperand(); 38249423Sdim} 39249423Sdim 40193326Sednamespace { 41199990Srdivackyclass AggExprEmitter : public StmtVisitor<AggExprEmitter> { 42193326Sed CodeGenFunction &CGF; 43193326Sed CGBuilderTy &Builder; 44218893Sdim AggValueSlot Dest; 45208600Srdivacky 46226633Sdim /// We want to use 'dest' as the return slot except under two 47226633Sdim /// conditions: 48226633Sdim /// - The destination slot requires garbage collection, so we 49226633Sdim /// need to use the GC API. 50226633Sdim /// - The destination slot is potentially aliased. 51226633Sdim bool shouldUseDestForReturnSlot() const { 52226633Sdim return !(Dest.requiresGCollection() || Dest.isPotentiallyAliased()); 53226633Sdim } 54226633Sdim 55208600Srdivacky ReturnValueSlot getReturnValueSlot() const { 56226633Sdim if (!shouldUseDestForReturnSlot()) 57226633Sdim return ReturnValueSlot(); 58208600Srdivacky 59218893Sdim return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile()); 60208600Srdivacky } 61208600Srdivacky 62218893Sdim AggValueSlot EnsureSlot(QualType T) { 63218893Sdim if (!Dest.isIgnored()) return Dest; 64218893Sdim return CGF.CreateAggTemp(T, "agg.tmp.ensured"); 65218893Sdim } 66239462Sdim void EnsureDest(QualType T) { 67239462Sdim if (!Dest.isIgnored()) return; 68239462Sdim Dest = CGF.CreateAggTemp(T, "agg.tmp.ensured"); 69239462Sdim } 70218893Sdim 71193326Sedpublic: 72239462Sdim AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest) 73239462Sdim : CGF(cgf), Builder(CGF.Builder), Dest(Dest) { 74193326Sed } 75193326Sed 76193326Sed //===--------------------------------------------------------------------===// 77193326Sed // Utilities 78193326Sed //===--------------------------------------------------------------------===// 79193326Sed 80193326Sed /// EmitAggLoadOfLValue - Given an expression with aggregate type that 81193326Sed /// represents a value lvalue, this method emits the address of the lvalue, 82193326Sed /// then loads the result into DestPtr. 83193326Sed void EmitAggLoadOfLValue(const Expr *E); 84193326Sed 85193326Sed /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 86239462Sdim void EmitFinalDestCopy(QualType type, const LValue &src); 87239462Sdim void EmitFinalDestCopy(QualType type, RValue src, 88239462Sdim CharUnits srcAlignment = CharUnits::Zero()); 89239462Sdim void EmitCopy(QualType type, const AggValueSlot &dest, 90239462Sdim const AggValueSlot &src); 91193326Sed 92226633Sdim void EmitMoveFromReturnSlot(const Expr *E, RValue Src); 93208600Srdivacky 94234353Sdim void EmitStdInitializerList(llvm::Value *DestPtr, InitListExpr *InitList); 95234353Sdim void EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType, 96234353Sdim QualType elementType, InitListExpr *E); 97234353Sdim 98226633Sdim AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) { 99234353Sdim if (CGF.getLangOpts().getGC() && TypeRequiresGCollection(T)) 100226633Sdim return AggValueSlot::NeedsGCBarriers; 101226633Sdim return AggValueSlot::DoesNotNeedGCBarriers; 102226633Sdim } 103226633Sdim 104208600Srdivacky bool TypeRequiresGCollection(QualType T); 105208600Srdivacky 106193326Sed //===--------------------------------------------------------------------===// 107193326Sed // Visitor Methods 108193326Sed //===--------------------------------------------------------------------===// 109198092Srdivacky 110193326Sed void VisitStmt(Stmt *S) { 111193326Sed CGF.ErrorUnsupported(S, "aggregate expression"); 112193326Sed } 113193326Sed void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } 114221345Sdim void VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 115221345Sdim Visit(GE->getResultExpr()); 116221345Sdim } 117193326Sed void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 118224145Sdim void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) { 119224145Sdim return Visit(E->getReplacement()); 120224145Sdim } 121193326Sed 122193326Sed // l-values. 123234353Sdim void VisitDeclRefExpr(DeclRefExpr *E) { 124234353Sdim // For aggregates, we should always be able to emit the variable 125234353Sdim // as an l-value unless it's a reference. This is due to the fact 126234353Sdim // that we can't actually ever see a normal l2r conversion on an 127234353Sdim // aggregate in C++, and in C there's no language standard 128234353Sdim // actively preventing us from listing variables in the captures 129234353Sdim // list of a block. 130234353Sdim if (E->getDecl()->getType()->isReferenceType()) { 131234353Sdim if (CodeGenFunction::ConstantEmission result 132234353Sdim = CGF.tryEmitAsConstant(E)) { 133239462Sdim EmitFinalDestCopy(E->getType(), result.getReferenceLValue(CGF, E)); 134234353Sdim return; 135234353Sdim } 136234353Sdim } 137234353Sdim 138234353Sdim EmitAggLoadOfLValue(E); 139234353Sdim } 140234353Sdim 141193326Sed void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 142193326Sed void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 143193326Sed void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 144224145Sdim void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 145193326Sed void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 146193326Sed EmitAggLoadOfLValue(E); 147193326Sed } 148193326Sed void VisitPredefinedExpr(const PredefinedExpr *E) { 149198092Srdivacky EmitAggLoadOfLValue(E); 150193326Sed } 151198092Srdivacky 152193326Sed // Operators. 153198092Srdivacky void VisitCastExpr(CastExpr *E); 154193326Sed void VisitCallExpr(const CallExpr *E); 155193326Sed void VisitStmtExpr(const StmtExpr *E); 156193326Sed void VisitBinaryOperator(const BinaryOperator *BO); 157198398Srdivacky void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); 158193326Sed void VisitBinAssign(const BinaryOperator *E); 159193326Sed void VisitBinComma(const BinaryOperator *E); 160193326Sed 161193326Sed void VisitObjCMessageExpr(ObjCMessageExpr *E); 162193326Sed void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 163193326Sed EmitAggLoadOfLValue(E); 164193326Sed } 165198092Srdivacky 166218893Sdim void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); 167198092Srdivacky void VisitChooseExpr(const ChooseExpr *CE); 168193326Sed void VisitInitListExpr(InitListExpr *E); 169201361Srdivacky void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); 170193326Sed void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 171193326Sed Visit(DAE->getExpr()); 172193326Sed } 173251662Sdim void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { 174251662Sdim CodeGenFunction::CXXDefaultInitExprScope Scope(CGF); 175251662Sdim Visit(DIE->getExpr()); 176251662Sdim } 177193326Sed void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 178193326Sed void VisitCXXConstructExpr(const CXXConstructExpr *E); 179234353Sdim void VisitLambdaExpr(LambdaExpr *E); 180218893Sdim void VisitExprWithCleanups(ExprWithCleanups *E); 181210299Sed void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 182199482Srdivacky void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } 183224145Sdim void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); 184218893Sdim void VisitOpaqueValueExpr(OpaqueValueExpr *E); 185218893Sdim 186234353Sdim void VisitPseudoObjectExpr(PseudoObjectExpr *E) { 187234353Sdim if (E->isGLValue()) { 188234353Sdim LValue LV = CGF.EmitPseudoObjectLValue(E); 189239462Sdim return EmitFinalDestCopy(E->getType(), LV); 190234353Sdim } 191234353Sdim 192234353Sdim CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType())); 193234353Sdim } 194234353Sdim 195193326Sed void VisitVAArgExpr(VAArgExpr *E); 196193326Sed 197224145Sdim void EmitInitializationToLValue(Expr *E, LValue Address); 198224145Sdim void EmitNullInitializationToLValue(LValue Address); 199193326Sed // case Expr::ChooseExprClass: 200200583Srdivacky void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } 201226633Sdim void VisitAtomicExpr(AtomicExpr *E) { 202226633Sdim CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr()); 203226633Sdim } 204193326Sed}; 205249423Sdim 206249423Sdim/// A helper class for emitting expressions into the value sub-object 207249423Sdim/// of a padded atomic type. 208249423Sdimclass ValueDestForAtomic { 209249423Sdim AggValueSlot Dest; 210249423Sdimpublic: 211249423Sdim ValueDestForAtomic(CodeGenFunction &CGF, AggValueSlot dest, QualType type) 212249423Sdim : Dest(dest) { 213249423Sdim assert(!Dest.isValueOfAtomic()); 214249423Sdim if (!Dest.isIgnored() && CGF.CGM.isPaddedAtomicType(type)) { 215249423Sdim llvm::Value *valueAddr = CGF.Builder.CreateStructGEP(Dest.getAddr(), 0); 216249423Sdim Dest = AggValueSlot::forAddr(valueAddr, 217249423Sdim Dest.getAlignment(), 218249423Sdim Dest.getQualifiers(), 219249423Sdim Dest.isExternallyDestructed(), 220249423Sdim Dest.requiresGCollection(), 221249423Sdim Dest.isPotentiallyAliased(), 222249423Sdim Dest.isZeroed(), 223249423Sdim AggValueSlot::IsValueOfAtomic); 224249423Sdim } 225249423Sdim } 226249423Sdim 227249423Sdim const AggValueSlot &getDest() const { return Dest; } 228249423Sdim 229249423Sdim ~ValueDestForAtomic() { 230249423Sdim // Kill the GEP if we made one and it didn't end up used. 231249423Sdim if (Dest.isValueOfAtomic()) { 232249423Sdim llvm::Instruction *addr = cast<llvm::GetElementPtrInst>(Dest.getAddr()); 233249423Sdim if (addr->use_empty()) addr->eraseFromParent(); 234249423Sdim } 235249423Sdim } 236249423Sdim}; 237193326Sed} // end anonymous namespace. 238193326Sed 239193326Sed//===----------------------------------------------------------------------===// 240193326Sed// Utilities 241193326Sed//===----------------------------------------------------------------------===// 242193326Sed 243193326Sed/// EmitAggLoadOfLValue - Given an expression with aggregate type that 244193326Sed/// represents a value lvalue, this method emits the address of the lvalue, 245193326Sed/// then loads the result into DestPtr. 246193326Sedvoid AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 247193326Sed LValue LV = CGF.EmitLValue(E); 248249423Sdim 249249423Sdim // If the type of the l-value is atomic, then do an atomic load. 250249423Sdim if (LV.getType()->isAtomicType()) { 251249423Sdim ValueDestForAtomic valueDest(CGF, Dest, LV.getType()); 252249423Sdim CGF.EmitAtomicLoad(LV, valueDest.getDest()); 253249423Sdim return; 254249423Sdim } 255249423Sdim 256239462Sdim EmitFinalDestCopy(E->getType(), LV); 257193326Sed} 258193326Sed 259208600Srdivacky/// \brief True if the given aggregate type requires special GC API calls. 260208600Srdivackybool AggExprEmitter::TypeRequiresGCollection(QualType T) { 261208600Srdivacky // Only record types have members that might require garbage collection. 262208600Srdivacky const RecordType *RecordTy = T->getAs<RecordType>(); 263208600Srdivacky if (!RecordTy) return false; 264208600Srdivacky 265208600Srdivacky // Don't mess with non-trivial C++ types. 266208600Srdivacky RecordDecl *Record = RecordTy->getDecl(); 267208600Srdivacky if (isa<CXXRecordDecl>(Record) && 268249423Sdim (cast<CXXRecordDecl>(Record)->hasNonTrivialCopyConstructor() || 269208600Srdivacky !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) 270208600Srdivacky return false; 271208600Srdivacky 272208600Srdivacky // Check whether the type has an object member. 273208600Srdivacky return Record->hasObjectMember(); 274208600Srdivacky} 275208600Srdivacky 276226633Sdim/// \brief Perform the final move to DestPtr if for some reason 277226633Sdim/// getReturnValueSlot() didn't use it directly. 278208600Srdivacky/// 279208600Srdivacky/// The idea is that you do something like this: 280208600Srdivacky/// RValue Result = EmitSomething(..., getReturnValueSlot()); 281226633Sdim/// EmitMoveFromReturnSlot(E, Result); 282226633Sdim/// 283226633Sdim/// If nothing interferes, this will cause the result to be emitted 284226633Sdim/// directly into the return value slot. Otherwise, a final move 285226633Sdim/// will be performed. 286239462Sdimvoid AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue src) { 287226633Sdim if (shouldUseDestForReturnSlot()) { 288226633Sdim // Logically, Dest.getAddr() should equal Src.getAggregateAddr(). 289226633Sdim // The possibility of undef rvalues complicates that a lot, 290226633Sdim // though, so we can't really assert. 291226633Sdim return; 292210299Sed } 293226633Sdim 294239462Sdim // Otherwise, copy from there to the destination. 295239462Sdim assert(Dest.getAddr() != src.getAggregateAddr()); 296239462Sdim std::pair<CharUnits, CharUnits> typeInfo = 297234982Sdim CGF.getContext().getTypeInfoInChars(E->getType()); 298239462Sdim EmitFinalDestCopy(E->getType(), src, typeInfo.second); 299208600Srdivacky} 300208600Srdivacky 301193326Sed/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 302239462Sdimvoid AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src, 303239462Sdim CharUnits srcAlign) { 304239462Sdim assert(src.isAggregate() && "value must be aggregate value!"); 305239462Sdim LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddr(), type, srcAlign); 306239462Sdim EmitFinalDestCopy(type, srcLV); 307239462Sdim} 308193326Sed 309239462Sdim/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 310239462Sdimvoid AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src) { 311218893Sdim // If Dest is ignored, then we're evaluating an aggregate expression 312239462Sdim // in a context that doesn't care about the result. Note that loads 313239462Sdim // from volatile l-values force the existence of a non-ignored 314239462Sdim // destination. 315239462Sdim if (Dest.isIgnored()) 316239462Sdim return; 317212904Sdim 318239462Sdim AggValueSlot srcAgg = 319239462Sdim AggValueSlot::forLValue(src, AggValueSlot::IsDestructed, 320239462Sdim needsGC(type), AggValueSlot::IsAliased); 321239462Sdim EmitCopy(type, Dest, srcAgg); 322239462Sdim} 323193326Sed 324239462Sdim/// Perform a copy from the source into the destination. 325239462Sdim/// 326239462Sdim/// \param type - the type of the aggregate being copied; qualifiers are 327239462Sdim/// ignored 328239462Sdimvoid AggExprEmitter::EmitCopy(QualType type, const AggValueSlot &dest, 329239462Sdim const AggValueSlot &src) { 330239462Sdim if (dest.requiresGCollection()) { 331239462Sdim CharUnits sz = CGF.getContext().getTypeSizeInChars(type); 332239462Sdim llvm::Value *size = llvm::ConstantInt::get(CGF.SizeTy, sz.getQuantity()); 333198092Srdivacky CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 334239462Sdim dest.getAddr(), 335239462Sdim src.getAddr(), 336239462Sdim size); 337198092Srdivacky return; 338198092Srdivacky } 339239462Sdim 340193326Sed // If the result of the assignment is used, copy the LHS there also. 341239462Sdim // It's volatile if either side is. Use the minimum alignment of 342239462Sdim // the two sides. 343239462Sdim CGF.EmitAggregateCopy(dest.getAddr(), src.getAddr(), type, 344239462Sdim dest.isVolatile() || src.isVolatile(), 345239462Sdim std::min(dest.getAlignment(), src.getAlignment())); 346193326Sed} 347193326Sed 348234353Sdimstatic QualType GetStdInitializerListElementType(QualType T) { 349234353Sdim // Just assume that this is really std::initializer_list. 350234353Sdim ClassTemplateSpecializationDecl *specialization = 351234353Sdim cast<ClassTemplateSpecializationDecl>(T->castAs<RecordType>()->getDecl()); 352234353Sdim return specialization->getTemplateArgs()[0].getAsType(); 353234353Sdim} 354234353Sdim 355234353Sdim/// \brief Prepare cleanup for the temporary array. 356234353Sdimstatic void EmitStdInitializerListCleanup(CodeGenFunction &CGF, 357234353Sdim QualType arrayType, 358234353Sdim llvm::Value *addr, 359234353Sdim const InitListExpr *initList) { 360234353Sdim QualType::DestructionKind dtorKind = arrayType.isDestructedType(); 361234353Sdim if (!dtorKind) 362234353Sdim return; // Type doesn't need destroying. 363234353Sdim if (dtorKind != QualType::DK_cxx_destructor) { 364234353Sdim CGF.ErrorUnsupported(initList, "ObjC ARC type in initializer_list"); 365234353Sdim return; 366234353Sdim } 367234353Sdim 368234353Sdim CodeGenFunction::Destroyer *destroyer = CGF.getDestroyer(dtorKind); 369234353Sdim CGF.pushDestroy(NormalAndEHCleanup, addr, arrayType, destroyer, 370234353Sdim /*EHCleanup=*/true); 371234353Sdim} 372234353Sdim 373234353Sdim/// \brief Emit the initializer for a std::initializer_list initialized with a 374234353Sdim/// real initializer list. 375234353Sdimvoid AggExprEmitter::EmitStdInitializerList(llvm::Value *destPtr, 376234353Sdim InitListExpr *initList) { 377234353Sdim // We emit an array containing the elements, then have the init list point 378234353Sdim // at the array. 379234353Sdim ASTContext &ctx = CGF.getContext(); 380234353Sdim unsigned numInits = initList->getNumInits(); 381234353Sdim QualType element = GetStdInitializerListElementType(initList->getType()); 382234353Sdim llvm::APInt size(ctx.getTypeSize(ctx.getSizeType()), numInits); 383234353Sdim QualType array = ctx.getConstantArrayType(element, size, ArrayType::Normal,0); 384234353Sdim llvm::Type *LTy = CGF.ConvertTypeForMem(array); 385234353Sdim llvm::AllocaInst *alloc = CGF.CreateTempAlloca(LTy); 386234353Sdim alloc->setAlignment(ctx.getTypeAlignInChars(array).getQuantity()); 387234353Sdim alloc->setName(".initlist."); 388234353Sdim 389234353Sdim EmitArrayInit(alloc, cast<llvm::ArrayType>(LTy), element, initList); 390234353Sdim 391234353Sdim // FIXME: The diagnostics are somewhat out of place here. 392234353Sdim RecordDecl *record = initList->getType()->castAs<RecordType>()->getDecl(); 393234353Sdim RecordDecl::field_iterator field = record->field_begin(); 394234353Sdim if (field == record->field_end()) { 395234353Sdim CGF.ErrorUnsupported(initList, "weird std::initializer_list"); 396234353Sdim return; 397234353Sdim } 398234353Sdim 399234353Sdim QualType elementPtr = ctx.getPointerType(element.withConst()); 400234353Sdim 401234353Sdim // Start pointer. 402234353Sdim if (!ctx.hasSameType(field->getType(), elementPtr)) { 403234353Sdim CGF.ErrorUnsupported(initList, "weird std::initializer_list"); 404234353Sdim return; 405234353Sdim } 406234982Sdim LValue DestLV = CGF.MakeNaturalAlignAddrLValue(destPtr, initList->getType()); 407234982Sdim LValue start = CGF.EmitLValueForFieldInitialization(DestLV, *field); 408234353Sdim llvm::Value *arrayStart = Builder.CreateStructGEP(alloc, 0, "arraystart"); 409234353Sdim CGF.EmitStoreThroughLValue(RValue::get(arrayStart), start); 410234353Sdim ++field; 411234353Sdim 412234353Sdim if (field == record->field_end()) { 413234353Sdim CGF.ErrorUnsupported(initList, "weird std::initializer_list"); 414234353Sdim return; 415234353Sdim } 416234982Sdim LValue endOrLength = CGF.EmitLValueForFieldInitialization(DestLV, *field); 417234353Sdim if (ctx.hasSameType(field->getType(), elementPtr)) { 418234353Sdim // End pointer. 419234353Sdim llvm::Value *arrayEnd = Builder.CreateStructGEP(alloc,numInits, "arrayend"); 420234353Sdim CGF.EmitStoreThroughLValue(RValue::get(arrayEnd), endOrLength); 421234353Sdim } else if(ctx.hasSameType(field->getType(), ctx.getSizeType())) { 422234353Sdim // Length. 423234353Sdim CGF.EmitStoreThroughLValue(RValue::get(Builder.getInt(size)), endOrLength); 424234353Sdim } else { 425234353Sdim CGF.ErrorUnsupported(initList, "weird std::initializer_list"); 426234353Sdim return; 427234353Sdim } 428234353Sdim 429234353Sdim if (!Dest.isExternallyDestructed()) 430234353Sdim EmitStdInitializerListCleanup(CGF, array, alloc, initList); 431234353Sdim} 432234353Sdim 433234353Sdim/// \brief Emit initialization of an array from an initializer list. 434234353Sdimvoid AggExprEmitter::EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType, 435234353Sdim QualType elementType, InitListExpr *E) { 436234353Sdim uint64_t NumInitElements = E->getNumInits(); 437234353Sdim 438234353Sdim uint64_t NumArrayElements = AType->getNumElements(); 439234353Sdim assert(NumInitElements <= NumArrayElements); 440234353Sdim 441234353Sdim // DestPtr is an array*. Construct an elementType* by drilling 442234353Sdim // down a level. 443234353Sdim llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0); 444234353Sdim llvm::Value *indices[] = { zero, zero }; 445234353Sdim llvm::Value *begin = 446234353Sdim Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin"); 447234353Sdim 448234353Sdim // Exception safety requires us to destroy all the 449234353Sdim // already-constructed members if an initializer throws. 450234353Sdim // For that, we'll need an EH cleanup. 451234353Sdim QualType::DestructionKind dtorKind = elementType.isDestructedType(); 452234353Sdim llvm::AllocaInst *endOfInit = 0; 453234353Sdim EHScopeStack::stable_iterator cleanup; 454234353Sdim llvm::Instruction *cleanupDominator = 0; 455234353Sdim if (CGF.needsEHCleanup(dtorKind)) { 456234353Sdim // In principle we could tell the cleanup where we are more 457234353Sdim // directly, but the control flow can get so varied here that it 458234353Sdim // would actually be quite complex. Therefore we go through an 459234353Sdim // alloca. 460234353Sdim endOfInit = CGF.CreateTempAlloca(begin->getType(), 461234353Sdim "arrayinit.endOfInit"); 462234353Sdim cleanupDominator = Builder.CreateStore(begin, endOfInit); 463234353Sdim CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType, 464234353Sdim CGF.getDestroyer(dtorKind)); 465234353Sdim cleanup = CGF.EHStack.stable_begin(); 466234353Sdim 467234353Sdim // Otherwise, remember that we didn't need a cleanup. 468234353Sdim } else { 469234353Sdim dtorKind = QualType::DK_none; 470234353Sdim } 471234353Sdim 472234353Sdim llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1); 473234353Sdim 474234353Sdim // The 'current element to initialize'. The invariants on this 475234353Sdim // variable are complicated. Essentially, after each iteration of 476234353Sdim // the loop, it points to the last initialized element, except 477234353Sdim // that it points to the beginning of the array before any 478234353Sdim // elements have been initialized. 479234353Sdim llvm::Value *element = begin; 480234353Sdim 481234353Sdim // Emit the explicit initializers. 482234353Sdim for (uint64_t i = 0; i != NumInitElements; ++i) { 483234353Sdim // Advance to the next element. 484234353Sdim if (i > 0) { 485234353Sdim element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element"); 486234353Sdim 487234353Sdim // Tell the cleanup that it needs to destroy up to this 488234353Sdim // element. TODO: some of these stores can be trivially 489234353Sdim // observed to be unnecessary. 490234353Sdim if (endOfInit) Builder.CreateStore(element, endOfInit); 491234353Sdim } 492234353Sdim 493234353Sdim // If these are nested std::initializer_list inits, do them directly, 494234353Sdim // because they are conceptually the same "location". 495234353Sdim InitListExpr *initList = dyn_cast<InitListExpr>(E->getInit(i)); 496234353Sdim if (initList && initList->initializesStdInitializerList()) { 497234353Sdim EmitStdInitializerList(element, initList); 498234353Sdim } else { 499234353Sdim LValue elementLV = CGF.MakeAddrLValue(element, elementType); 500234353Sdim EmitInitializationToLValue(E->getInit(i), elementLV); 501234353Sdim } 502234353Sdim } 503234353Sdim 504234353Sdim // Check whether there's a non-trivial array-fill expression. 505234353Sdim // Note that this will be a CXXConstructExpr even if the element 506234353Sdim // type is an array (or array of array, etc.) of class type. 507234353Sdim Expr *filler = E->getArrayFiller(); 508234353Sdim bool hasTrivialFiller = true; 509234353Sdim if (CXXConstructExpr *cons = dyn_cast_or_null<CXXConstructExpr>(filler)) { 510234353Sdim assert(cons->getConstructor()->isDefaultConstructor()); 511234353Sdim hasTrivialFiller = cons->getConstructor()->isTrivial(); 512234353Sdim } 513234353Sdim 514234353Sdim // Any remaining elements need to be zero-initialized, possibly 515234353Sdim // using the filler expression. We can skip this if the we're 516234353Sdim // emitting to zeroed memory. 517234353Sdim if (NumInitElements != NumArrayElements && 518234353Sdim !(Dest.isZeroed() && hasTrivialFiller && 519234353Sdim CGF.getTypes().isZeroInitializable(elementType))) { 520234353Sdim 521234353Sdim // Use an actual loop. This is basically 522234353Sdim // do { *array++ = filler; } while (array != end); 523234353Sdim 524234353Sdim // Advance to the start of the rest of the array. 525234353Sdim if (NumInitElements) { 526234353Sdim element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start"); 527234353Sdim if (endOfInit) Builder.CreateStore(element, endOfInit); 528234353Sdim } 529234353Sdim 530234353Sdim // Compute the end of the array. 531234353Sdim llvm::Value *end = Builder.CreateInBoundsGEP(begin, 532234353Sdim llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements), 533234353Sdim "arrayinit.end"); 534234353Sdim 535234353Sdim llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); 536234353Sdim llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body"); 537234353Sdim 538234353Sdim // Jump into the body. 539234353Sdim CGF.EmitBlock(bodyBB); 540234353Sdim llvm::PHINode *currentElement = 541234353Sdim Builder.CreatePHI(element->getType(), 2, "arrayinit.cur"); 542234353Sdim currentElement->addIncoming(element, entryBB); 543234353Sdim 544234353Sdim // Emit the actual filler expression. 545234353Sdim LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType); 546234353Sdim if (filler) 547234353Sdim EmitInitializationToLValue(filler, elementLV); 548234353Sdim else 549234353Sdim EmitNullInitializationToLValue(elementLV); 550234353Sdim 551234353Sdim // Move on to the next element. 552234353Sdim llvm::Value *nextElement = 553234353Sdim Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next"); 554234353Sdim 555234353Sdim // Tell the EH cleanup that we finished with the last element. 556234353Sdim if (endOfInit) Builder.CreateStore(nextElement, endOfInit); 557234353Sdim 558234353Sdim // Leave the loop if we're done. 559234353Sdim llvm::Value *done = Builder.CreateICmpEQ(nextElement, end, 560234353Sdim "arrayinit.done"); 561234353Sdim llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end"); 562234353Sdim Builder.CreateCondBr(done, endBB, bodyBB); 563234353Sdim currentElement->addIncoming(nextElement, Builder.GetInsertBlock()); 564234353Sdim 565234353Sdim CGF.EmitBlock(endBB); 566234353Sdim } 567234353Sdim 568234353Sdim // Leave the partial-array cleanup if we entered one. 569234353Sdim if (dtorKind) CGF.DeactivateCleanupBlock(cleanup, cleanupDominator); 570234353Sdim} 571234353Sdim 572193326Sed//===----------------------------------------------------------------------===// 573193326Sed// Visitor Methods 574193326Sed//===----------------------------------------------------------------------===// 575193326Sed 576224145Sdimvoid AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){ 577224145Sdim Visit(E->GetTemporaryExpr()); 578224145Sdim} 579224145Sdim 580218893Sdimvoid AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) { 581239462Sdim EmitFinalDestCopy(e->getType(), CGF.getOpaqueLValueMapping(e)); 582218893Sdim} 583218893Sdim 584224145Sdimvoid 585224145SdimAggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 586249423Sdim if (Dest.isPotentiallyAliased() && 587249423Sdim E->getType().isPODType(CGF.getContext())) { 588224145Sdim // For a POD type, just emit a load of the lvalue + a copy, because our 589224145Sdim // compound literal might alias the destination. 590224145Sdim EmitAggLoadOfLValue(E); 591224145Sdim return; 592224145Sdim } 593224145Sdim 594224145Sdim AggValueSlot Slot = EnsureSlot(E->getType()); 595224145Sdim CGF.EmitAggExpr(E->getInitializer(), Slot); 596224145Sdim} 597224145Sdim 598249423Sdim/// Attempt to look through various unimportant expressions to find a 599249423Sdim/// cast of the given kind. 600249423Sdimstatic Expr *findPeephole(Expr *op, CastKind kind) { 601249423Sdim while (true) { 602249423Sdim op = op->IgnoreParens(); 603249423Sdim if (CastExpr *castE = dyn_cast<CastExpr>(op)) { 604249423Sdim if (castE->getCastKind() == kind) 605249423Sdim return castE->getSubExpr(); 606249423Sdim if (castE->getCastKind() == CK_NoOp) 607249423Sdim continue; 608249423Sdim } 609249423Sdim return 0; 610249423Sdim } 611249423Sdim} 612224145Sdim 613198092Srdivackyvoid AggExprEmitter::VisitCastExpr(CastExpr *E) { 614198092Srdivacky switch (E->getCastKind()) { 615212904Sdim case CK_Dynamic: { 616243830Sdim // FIXME: Can this actually happen? We have no test coverage for it. 617208600Srdivacky assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?"); 618243830Sdim LValue LV = CGF.EmitCheckedLValue(E->getSubExpr(), 619243830Sdim CodeGenFunction::TCK_Load); 620208600Srdivacky // FIXME: Do we also need to handle property references here? 621208600Srdivacky if (LV.isSimple()) 622208600Srdivacky CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E)); 623208600Srdivacky else 624208600Srdivacky CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast"); 625208600Srdivacky 626218893Sdim if (!Dest.isIgnored()) 627218893Sdim CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination"); 628208600Srdivacky break; 629208600Srdivacky } 630208600Srdivacky 631212904Sdim case CK_ToUnion: { 632221345Sdim if (Dest.isIgnored()) break; 633221345Sdim 634198092Srdivacky // GCC union extension 635212904Sdim QualType Ty = E->getSubExpr()->getType(); 636212904Sdim QualType PtrTy = CGF.getContext().getPointerType(Ty); 637218893Sdim llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(), 638193401Sed CGF.ConvertType(PtrTy)); 639224145Sdim EmitInitializationToLValue(E->getSubExpr(), 640224145Sdim CGF.MakeAddrLValue(CastPtr, Ty)); 641198092Srdivacky break; 642193326Sed } 643193326Sed 644212904Sdim case CK_DerivedToBase: 645212904Sdim case CK_BaseToDerived: 646212904Sdim case CK_UncheckedDerivedToBase: { 647226633Sdim llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: " 648208600Srdivacky "should have been unpacked before we got here"); 649208600Srdivacky } 650208600Srdivacky 651249423Sdim case CK_NonAtomicToAtomic: 652249423Sdim case CK_AtomicToNonAtomic: { 653249423Sdim bool isToAtomic = (E->getCastKind() == CK_NonAtomicToAtomic); 654249423Sdim 655249423Sdim // Determine the atomic and value types. 656249423Sdim QualType atomicType = E->getSubExpr()->getType(); 657249423Sdim QualType valueType = E->getType(); 658249423Sdim if (isToAtomic) std::swap(atomicType, valueType); 659249423Sdim 660249423Sdim assert(atomicType->isAtomicType()); 661249423Sdim assert(CGF.getContext().hasSameUnqualifiedType(valueType, 662249423Sdim atomicType->castAs<AtomicType>()->getValueType())); 663249423Sdim 664249423Sdim // Just recurse normally if we're ignoring the result or the 665249423Sdim // atomic type doesn't change representation. 666249423Sdim if (Dest.isIgnored() || !CGF.CGM.isPaddedAtomicType(atomicType)) { 667249423Sdim return Visit(E->getSubExpr()); 668249423Sdim } 669249423Sdim 670249423Sdim CastKind peepholeTarget = 671249423Sdim (isToAtomic ? CK_AtomicToNonAtomic : CK_NonAtomicToAtomic); 672249423Sdim 673249423Sdim // These two cases are reverses of each other; try to peephole them. 674249423Sdim if (Expr *op = findPeephole(E->getSubExpr(), peepholeTarget)) { 675249423Sdim assert(CGF.getContext().hasSameUnqualifiedType(op->getType(), 676249423Sdim E->getType()) && 677249423Sdim "peephole significantly changed types?"); 678249423Sdim return Visit(op); 679249423Sdim } 680249423Sdim 681249423Sdim // If we're converting an r-value of non-atomic type to an r-value 682249423Sdim // of atomic type, just make an atomic temporary, emit into that, 683249423Sdim // and then copy the value out. (FIXME: do we need to 684249423Sdim // zero-initialize it first?) 685249423Sdim if (isToAtomic) { 686249423Sdim ValueDestForAtomic valueDest(CGF, Dest, atomicType); 687249423Sdim CGF.EmitAggExpr(E->getSubExpr(), valueDest.getDest()); 688249423Sdim return; 689249423Sdim } 690249423Sdim 691249423Sdim // Otherwise, we're converting an atomic type to a non-atomic type. 692249423Sdim 693249423Sdim // If the dest is a value-of-atomic subobject, drill back out. 694249423Sdim if (Dest.isValueOfAtomic()) { 695249423Sdim AggValueSlot atomicSlot = 696249423Sdim AggValueSlot::forAddr(Dest.getPaddedAtomicAddr(), 697249423Sdim Dest.getAlignment(), 698249423Sdim Dest.getQualifiers(), 699249423Sdim Dest.isExternallyDestructed(), 700249423Sdim Dest.requiresGCollection(), 701249423Sdim Dest.isPotentiallyAliased(), 702249423Sdim Dest.isZeroed(), 703249423Sdim AggValueSlot::IsNotValueOfAtomic); 704249423Sdim CGF.EmitAggExpr(E->getSubExpr(), atomicSlot); 705249423Sdim return; 706249423Sdim } 707249423Sdim 708249423Sdim // Otherwise, make an atomic temporary, emit into that, and then 709249423Sdim // copy the value out. 710249423Sdim AggValueSlot atomicSlot = 711249423Sdim CGF.CreateAggTemp(atomicType, "atomic-to-nonatomic.temp"); 712249423Sdim CGF.EmitAggExpr(E->getSubExpr(), atomicSlot); 713249423Sdim 714249423Sdim llvm::Value *valueAddr = 715249423Sdim Builder.CreateStructGEP(atomicSlot.getAddr(), 0); 716249423Sdim RValue rvalue = RValue::getAggregate(valueAddr, atomicSlot.isVolatile()); 717249423Sdim return EmitFinalDestCopy(valueType, rvalue); 718249423Sdim } 719249423Sdim 720239462Sdim case CK_LValueToRValue: 721239462Sdim // If we're loading from a volatile type, force the destination 722239462Sdim // into existence. 723239462Sdim if (E->getSubExpr()->getType().isVolatileQualified()) { 724239462Sdim EnsureDest(E->getType()); 725239462Sdim return Visit(E->getSubExpr()); 726239462Sdim } 727249423Sdim 728239462Sdim // fallthrough 729239462Sdim 730212904Sdim case CK_NoOp: 731212904Sdim case CK_UserDefinedConversion: 732212904Sdim case CK_ConstructorConversion: 733198092Srdivacky assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 734198092Srdivacky E->getType()) && 735198092Srdivacky "Implicit cast types must be compatible"); 736198092Srdivacky Visit(E->getSubExpr()); 737198092Srdivacky break; 738218893Sdim 739212904Sdim case CK_LValueBitCast: 740218893Sdim llvm_unreachable("should not be emitting lvalue bitcast as rvalue"); 741221345Sdim 742218893Sdim case CK_Dependent: 743218893Sdim case CK_BitCast: 744218893Sdim case CK_ArrayToPointerDecay: 745218893Sdim case CK_FunctionToPointerDecay: 746218893Sdim case CK_NullToPointer: 747218893Sdim case CK_NullToMemberPointer: 748218893Sdim case CK_BaseToDerivedMemberPointer: 749218893Sdim case CK_DerivedToBaseMemberPointer: 750218893Sdim case CK_MemberPointerToBoolean: 751234353Sdim case CK_ReinterpretMemberPointer: 752218893Sdim case CK_IntegralToPointer: 753218893Sdim case CK_PointerToIntegral: 754218893Sdim case CK_PointerToBoolean: 755218893Sdim case CK_ToVoid: 756218893Sdim case CK_VectorSplat: 757218893Sdim case CK_IntegralCast: 758218893Sdim case CK_IntegralToBoolean: 759218893Sdim case CK_IntegralToFloating: 760218893Sdim case CK_FloatingToIntegral: 761218893Sdim case CK_FloatingToBoolean: 762218893Sdim case CK_FloatingCast: 763226633Sdim case CK_CPointerToObjCPointerCast: 764226633Sdim case CK_BlockPointerToObjCPointerCast: 765218893Sdim case CK_AnyPointerToBlockPointerCast: 766218893Sdim case CK_ObjCObjectLValueCast: 767218893Sdim case CK_FloatingRealToComplex: 768218893Sdim case CK_FloatingComplexToReal: 769218893Sdim case CK_FloatingComplexToBoolean: 770218893Sdim case CK_FloatingComplexCast: 771218893Sdim case CK_FloatingComplexToIntegralComplex: 772218893Sdim case CK_IntegralRealToComplex: 773218893Sdim case CK_IntegralComplexToReal: 774218893Sdim case CK_IntegralComplexToBoolean: 775218893Sdim case CK_IntegralComplexCast: 776218893Sdim case CK_IntegralComplexToFloatingComplex: 777226633Sdim case CK_ARCProduceObject: 778226633Sdim case CK_ARCConsumeObject: 779226633Sdim case CK_ARCReclaimReturnedObject: 780226633Sdim case CK_ARCExtendBlockObject: 781234353Sdim case CK_CopyAndAutoreleaseBlockObject: 782243830Sdim case CK_BuiltinFnToFnPtr: 783249423Sdim case CK_ZeroToOCLEvent: 784218893Sdim llvm_unreachable("cast kind invalid for aggregate types"); 785198398Srdivacky } 786193326Sed} 787193326Sed 788193326Sedvoid AggExprEmitter::VisitCallExpr(const CallExpr *E) { 789193326Sed if (E->getCallReturnType()->isReferenceType()) { 790193326Sed EmitAggLoadOfLValue(E); 791193326Sed return; 792193326Sed } 793198092Srdivacky 794208600Srdivacky RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); 795226633Sdim EmitMoveFromReturnSlot(E, RV); 796193326Sed} 797193326Sed 798193326Sedvoid AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 799208600Srdivacky RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); 800226633Sdim EmitMoveFromReturnSlot(E, RV); 801193326Sed} 802193326Sed 803193326Sedvoid AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 804218893Sdim CGF.EmitIgnoredExpr(E->getLHS()); 805218893Sdim Visit(E->getRHS()); 806193326Sed} 807193326Sed 808193326Sedvoid AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 809218893Sdim CodeGenFunction::StmtExprEvaluation eval(CGF); 810218893Sdim CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest); 811193326Sed} 812193326Sed 813193326Sedvoid AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 814212904Sdim if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI) 815198398Srdivacky VisitPointerToDataMemberBinaryOperator(E); 816198398Srdivacky else 817198398Srdivacky CGF.ErrorUnsupported(E, "aggregate binary expression"); 818193326Sed} 819193326Sed 820198398Srdivackyvoid AggExprEmitter::VisitPointerToDataMemberBinaryOperator( 821198398Srdivacky const BinaryOperator *E) { 822198398Srdivacky LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); 823239462Sdim EmitFinalDestCopy(E->getType(), LV); 824198398Srdivacky} 825198398Srdivacky 826239462Sdim/// Is the value of the given expression possibly a reference to or 827239462Sdim/// into a __block variable? 828239462Sdimstatic bool isBlockVarRef(const Expr *E) { 829239462Sdim // Make sure we look through parens. 830239462Sdim E = E->IgnoreParens(); 831239462Sdim 832239462Sdim // Check for a direct reference to a __block variable. 833239462Sdim if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 834239462Sdim const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl()); 835239462Sdim return (var && var->hasAttr<BlocksAttr>()); 836239462Sdim } 837239462Sdim 838239462Sdim // More complicated stuff. 839239462Sdim 840239462Sdim // Binary operators. 841239462Sdim if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) { 842239462Sdim // For an assignment or pointer-to-member operation, just care 843239462Sdim // about the LHS. 844239462Sdim if (op->isAssignmentOp() || op->isPtrMemOp()) 845239462Sdim return isBlockVarRef(op->getLHS()); 846239462Sdim 847239462Sdim // For a comma, just care about the RHS. 848239462Sdim if (op->getOpcode() == BO_Comma) 849239462Sdim return isBlockVarRef(op->getRHS()); 850239462Sdim 851239462Sdim // FIXME: pointer arithmetic? 852239462Sdim return false; 853239462Sdim 854239462Sdim // Check both sides of a conditional operator. 855239462Sdim } else if (const AbstractConditionalOperator *op 856239462Sdim = dyn_cast<AbstractConditionalOperator>(E)) { 857239462Sdim return isBlockVarRef(op->getTrueExpr()) 858239462Sdim || isBlockVarRef(op->getFalseExpr()); 859239462Sdim 860239462Sdim // OVEs are required to support BinaryConditionalOperators. 861239462Sdim } else if (const OpaqueValueExpr *op 862239462Sdim = dyn_cast<OpaqueValueExpr>(E)) { 863239462Sdim if (const Expr *src = op->getSourceExpr()) 864239462Sdim return isBlockVarRef(src); 865239462Sdim 866239462Sdim // Casts are necessary to get things like (*(int*)&var) = foo(). 867239462Sdim // We don't really care about the kind of cast here, except 868239462Sdim // we don't want to look through l2r casts, because it's okay 869239462Sdim // to get the *value* in a __block variable. 870239462Sdim } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) { 871239462Sdim if (cast->getCastKind() == CK_LValueToRValue) 872239462Sdim return false; 873239462Sdim return isBlockVarRef(cast->getSubExpr()); 874239462Sdim 875239462Sdim // Handle unary operators. Again, just aggressively look through 876239462Sdim // it, ignoring the operation. 877239462Sdim } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) { 878239462Sdim return isBlockVarRef(uop->getSubExpr()); 879239462Sdim 880239462Sdim // Look into the base of a field access. 881239462Sdim } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) { 882239462Sdim return isBlockVarRef(mem->getBase()); 883239462Sdim 884239462Sdim // Look into the base of a subscript. 885239462Sdim } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) { 886239462Sdim return isBlockVarRef(sub->getBase()); 887239462Sdim } 888239462Sdim 889239462Sdim return false; 890239462Sdim} 891239462Sdim 892193326Sedvoid AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 893193326Sed // For an assignment to work, the value on the right has 894193326Sed // to be compatible with the value on the left. 895193326Sed assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 896193326Sed E->getRHS()->getType()) 897193326Sed && "Invalid assignment"); 898218893Sdim 899239462Sdim // If the LHS might be a __block variable, and the RHS can 900239462Sdim // potentially cause a block copy, we need to evaluate the RHS first 901239462Sdim // so that the assignment goes the right place. 902239462Sdim // This is pretty semantically fragile. 903239462Sdim if (isBlockVarRef(E->getLHS()) && 904239462Sdim E->getRHS()->HasSideEffects(CGF.getContext())) { 905239462Sdim // Ensure that we have a destination, and evaluate the RHS into that. 906239462Sdim EnsureDest(E->getRHS()->getType()); 907239462Sdim Visit(E->getRHS()); 908239462Sdim 909239462Sdim // Now emit the LHS and copy into it. 910243830Sdim LValue LHS = CGF.EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store); 911239462Sdim 912249423Sdim // That copy is an atomic copy if the LHS is atomic. 913249423Sdim if (LHS.getType()->isAtomicType()) { 914249423Sdim CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false); 915249423Sdim return; 916249423Sdim } 917249423Sdim 918239462Sdim EmitCopy(E->getLHS()->getType(), 919239462Sdim AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 920239462Sdim needsGC(E->getLHS()->getType()), 921239462Sdim AggValueSlot::IsAliased), 922239462Sdim Dest); 923239462Sdim return; 924239462Sdim } 925221345Sdim 926193326Sed LValue LHS = CGF.EmitLValue(E->getLHS()); 927193326Sed 928249423Sdim // If we have an atomic type, evaluate into the destination and then 929249423Sdim // do an atomic copy. 930249423Sdim if (LHS.getType()->isAtomicType()) { 931249423Sdim EnsureDest(E->getRHS()->getType()); 932249423Sdim Visit(E->getRHS()); 933249423Sdim CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false); 934249423Sdim return; 935249423Sdim } 936249423Sdim 937234353Sdim // Codegen the RHS so that it stores directly into the LHS. 938234353Sdim AggValueSlot LHSSlot = 939234353Sdim AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 940234353Sdim needsGC(E->getLHS()->getType()), 941234353Sdim AggValueSlot::IsAliased); 942249423Sdim // A non-volatile aggregate destination might have volatile member. 943249423Sdim if (!LHSSlot.isVolatile() && 944249423Sdim CGF.hasVolatileMember(E->getLHS()->getType())) 945249423Sdim LHSSlot.setVolatile(true); 946249423Sdim 947239462Sdim CGF.EmitAggExpr(E->getRHS(), LHSSlot); 948239462Sdim 949239462Sdim // Copy into the destination if the assignment isn't ignored. 950239462Sdim EmitFinalDestCopy(E->getType(), LHS); 951193326Sed} 952193326Sed 953218893Sdimvoid AggExprEmitter:: 954218893SdimVisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 955193326Sed llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 956193326Sed llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 957193326Sed llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 958198092Srdivacky 959218893Sdim // Bind the common expression if necessary. 960218893Sdim CodeGenFunction::OpaqueValueMapping binding(CGF, E); 961218893Sdim 962218893Sdim CodeGenFunction::ConditionalEvaluation eval(CGF); 963201361Srdivacky CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 964198092Srdivacky 965218893Sdim // Save whether the destination's lifetime is externally managed. 966226633Sdim bool isExternallyDestructed = Dest.isExternallyDestructed(); 967218893Sdim 968218893Sdim eval.begin(CGF); 969193326Sed CGF.EmitBlock(LHSBlock); 970218893Sdim Visit(E->getTrueExpr()); 971218893Sdim eval.end(CGF); 972198092Srdivacky 973218893Sdim assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!"); 974218893Sdim CGF.Builder.CreateBr(ContBlock); 975193326Sed 976218893Sdim // If the result of an agg expression is unused, then the emission 977218893Sdim // of the LHS might need to create a destination slot. That's fine 978218893Sdim // with us, and we can safely emit the RHS into the same slot, but 979226633Sdim // we shouldn't claim that it's already being destructed. 980226633Sdim Dest.setExternallyDestructed(isExternallyDestructed); 981198092Srdivacky 982218893Sdim eval.begin(CGF); 983193326Sed CGF.EmitBlock(RHSBlock); 984218893Sdim Visit(E->getFalseExpr()); 985218893Sdim eval.end(CGF); 986198092Srdivacky 987193326Sed CGF.EmitBlock(ContBlock); 988193326Sed} 989193326Sed 990198092Srdivackyvoid AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 991198092Srdivacky Visit(CE->getChosenSubExpr(CGF.getContext())); 992198092Srdivacky} 993198092Srdivacky 994193326Sedvoid AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 995193326Sed llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 996193326Sed llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 997193326Sed 998193326Sed if (!ArgPtr) { 999193326Sed CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 1000193326Sed return; 1001193326Sed } 1002193326Sed 1003239462Sdim EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType())); 1004193326Sed} 1005193326Sed 1006193326Sedvoid AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 1007218893Sdim // Ensure that we have a slot, but if we already do, remember 1008226633Sdim // whether it was externally destructed. 1009226633Sdim bool wasExternallyDestructed = Dest.isExternallyDestructed(); 1010239462Sdim EnsureDest(E->getType()); 1011198092Srdivacky 1012226633Sdim // We're going to push a destructor if there isn't already one. 1013226633Sdim Dest.setExternallyDestructed(); 1014226633Sdim 1015218893Sdim Visit(E->getSubExpr()); 1016193326Sed 1017226633Sdim // Push that destructor we promised. 1018226633Sdim if (!wasExternallyDestructed) 1019234353Sdim CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddr()); 1020193326Sed} 1021193326Sed 1022193326Sedvoid 1023193326SedAggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 1024218893Sdim AggValueSlot Slot = EnsureSlot(E->getType()); 1025218893Sdim CGF.EmitCXXConstructExpr(E, Slot); 1026193326Sed} 1027193326Sed 1028234353Sdimvoid 1029234353SdimAggExprEmitter::VisitLambdaExpr(LambdaExpr *E) { 1030234353Sdim AggValueSlot Slot = EnsureSlot(E->getType()); 1031234353Sdim CGF.EmitLambdaExpr(E, Slot); 1032234353Sdim} 1033234353Sdim 1034218893Sdimvoid AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) { 1035234353Sdim CGF.enterFullExpression(E); 1036234353Sdim CodeGenFunction::RunCleanupsScope cleanups(CGF); 1037234353Sdim Visit(E->getSubExpr()); 1038193326Sed} 1039193326Sed 1040210299Sedvoid AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 1041218893Sdim QualType T = E->getType(); 1042218893Sdim AggValueSlot Slot = EnsureSlot(T); 1043224145Sdim EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 1044198398Srdivacky} 1045198398Srdivacky 1046201361Srdivackyvoid AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 1047218893Sdim QualType T = E->getType(); 1048218893Sdim AggValueSlot Slot = EnsureSlot(T); 1049224145Sdim EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 1050218893Sdim} 1051201361Srdivacky 1052218893Sdim/// isSimpleZero - If emitting this value will obviously just cause a store of 1053218893Sdim/// zero to memory, return true. This can return false if uncertain, so it just 1054218893Sdim/// handles simple cases. 1055218893Sdimstatic bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { 1056221345Sdim E = E->IgnoreParens(); 1057221345Sdim 1058218893Sdim // 0 1059218893Sdim if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) 1060218893Sdim return IL->getValue() == 0; 1061218893Sdim // +0.0 1062218893Sdim if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E)) 1063218893Sdim return FL->getValue().isPosZero(); 1064218893Sdim // int() 1065218893Sdim if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) && 1066218893Sdim CGF.getTypes().isZeroInitializable(E->getType())) 1067218893Sdim return true; 1068218893Sdim // (int*)0 - Null pointer expressions. 1069218893Sdim if (const CastExpr *ICE = dyn_cast<CastExpr>(E)) 1070218893Sdim return ICE->getCastKind() == CK_NullToPointer; 1071218893Sdim // '\0' 1072218893Sdim if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) 1073218893Sdim return CL->getValue() == 0; 1074218893Sdim 1075218893Sdim // Otherwise, hard case: conservatively return false. 1076218893Sdim return false; 1077201361Srdivacky} 1078201361Srdivacky 1079218893Sdim 1080203955Srdivackyvoid 1081224145SdimAggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { 1082224145Sdim QualType type = LV.getType(); 1083193326Sed // FIXME: Ignore result? 1084193326Sed // FIXME: Are initializers affected by volatile? 1085218893Sdim if (Dest.isZeroed() && isSimpleZero(E, CGF)) { 1086218893Sdim // Storing "i32 0" to a zero'd memory location is a noop. 1087249423Sdim return; 1088249423Sdim } else if (isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) { 1089249423Sdim return EmitNullInitializationToLValue(LV); 1090224145Sdim } else if (type->isReferenceType()) { 1091210299Sed RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); 1092249423Sdim return CGF.EmitStoreThroughLValue(RV, LV); 1093249423Sdim } 1094249423Sdim 1095249423Sdim switch (CGF.getEvaluationKind(type)) { 1096249423Sdim case TEK_Complex: 1097249423Sdim CGF.EmitComplexExprIntoLValue(E, LV, /*isInit*/ true); 1098249423Sdim return; 1099249423Sdim case TEK_Aggregate: 1100226633Sdim CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV, 1101226633Sdim AggValueSlot::IsDestructed, 1102226633Sdim AggValueSlot::DoesNotNeedGCBarriers, 1103226633Sdim AggValueSlot::IsNotAliased, 1104224145Sdim Dest.isZeroed())); 1105249423Sdim return; 1106249423Sdim case TEK_Scalar: 1107249423Sdim if (LV.isSimple()) { 1108249423Sdim CGF.EmitScalarInit(E, /*D=*/0, LV, /*Captured=*/false); 1109249423Sdim } else { 1110249423Sdim CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV); 1111249423Sdim } 1112249423Sdim return; 1113193326Sed } 1114249423Sdim llvm_unreachable("bad evaluation kind"); 1115193326Sed} 1116193326Sed 1117224145Sdimvoid AggExprEmitter::EmitNullInitializationToLValue(LValue lv) { 1118224145Sdim QualType type = lv.getType(); 1119224145Sdim 1120218893Sdim // If the destination slot is already zeroed out before the aggregate is 1121218893Sdim // copied into it, we don't have to emit any zeros here. 1122224145Sdim if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type)) 1123218893Sdim return; 1124218893Sdim 1125249423Sdim if (CGF.hasScalarEvaluationKind(type)) { 1126249423Sdim // For non-aggregates, we can store the appropriate null constant. 1127249423Sdim llvm::Value *null = CGF.CGM.EmitNullConstant(type); 1128234353Sdim // Note that the following is not equivalent to 1129234353Sdim // EmitStoreThroughBitfieldLValue for ARC types. 1130234353Sdim if (lv.isBitField()) { 1131234353Sdim CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv); 1132234353Sdim } else { 1133234353Sdim assert(lv.isSimple()); 1134234353Sdim CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true); 1135234353Sdim } 1136193326Sed } else { 1137193326Sed // There's a potential optimization opportunity in combining 1138193326Sed // memsets; that would be easy for arrays, but relatively 1139193326Sed // difficult for structures with the current code. 1140224145Sdim CGF.EmitNullInitialization(lv.getAddress(), lv.getType()); 1141193326Sed } 1142193326Sed} 1143193326Sed 1144193326Sedvoid AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 1145193326Sed#if 0 1146200583Srdivacky // FIXME: Assess perf here? Figure out what cases are worth optimizing here 1147200583Srdivacky // (Length of globals? Chunks of zeroed-out space?). 1148193326Sed // 1149193326Sed // If we can, prefer a copy from a global; this is a lot less code for long 1150193326Sed // globals, and it's easier for the current optimizers to analyze. 1151200583Srdivacky if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { 1152193326Sed llvm::GlobalVariable* GV = 1153200583Srdivacky new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, 1154200583Srdivacky llvm::GlobalValue::InternalLinkage, C, ""); 1155239462Sdim EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType())); 1156193326Sed return; 1157193326Sed } 1158193326Sed#endif 1159218893Sdim if (E->hadArrayRangeDesignator()) 1160193326Sed CGF.ErrorUnsupported(E, "GNU array range designator extension"); 1161193326Sed 1162234353Sdim if (E->initializesStdInitializerList()) { 1163234353Sdim EmitStdInitializerList(Dest.getAddr(), E); 1164234353Sdim return; 1165234353Sdim } 1166218893Sdim 1167234982Sdim AggValueSlot Dest = EnsureSlot(E->getType()); 1168234982Sdim LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(), 1169234982Sdim Dest.getAlignment()); 1170234353Sdim 1171193326Sed // Handle initialization of an array. 1172193326Sed if (E->getType()->isArrayType()) { 1173234982Sdim if (E->isStringLiteralInit()) 1174234982Sdim return Visit(E->getInit(0)); 1175193326Sed 1176234353Sdim QualType elementType = 1177234353Sdim CGF.getContext().getAsArrayType(E->getType())->getElementType(); 1178193326Sed 1179234353Sdim llvm::PointerType *APType = 1180234982Sdim cast<llvm::PointerType>(Dest.getAddr()->getType()); 1181234353Sdim llvm::ArrayType *AType = 1182234353Sdim cast<llvm::ArrayType>(APType->getElementType()); 1183224145Sdim 1184234982Sdim EmitArrayInit(Dest.getAddr(), AType, elementType, E); 1185193326Sed return; 1186193326Sed } 1187198092Srdivacky 1188193326Sed assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 1189198092Srdivacky 1190193326Sed // Do struct initialization; this code just sets each individual member 1191193326Sed // to the approprate value. This makes bitfield support automatic; 1192193326Sed // the disadvantage is that the generated code is more difficult for 1193193326Sed // the optimizer, especially with bitfields. 1194193326Sed unsigned NumInitElements = E->getNumInits(); 1195224145Sdim RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl(); 1196251662Sdim 1197251662Sdim // Prepare a 'this' for CXXDefaultInitExprs. 1198251662Sdim CodeGenFunction::FieldConstructionScope FCS(CGF, Dest.getAddr()); 1199251662Sdim 1200224145Sdim if (record->isUnion()) { 1201193326Sed // Only initialize one field of a union. The field itself is 1202193326Sed // specified by the initializer list. 1203193326Sed if (!E->getInitializedFieldInUnion()) { 1204193326Sed // Empty union; we have nothing to do. 1205198092Srdivacky 1206193326Sed#ifndef NDEBUG 1207193326Sed // Make sure that it's really an empty and not a failure of 1208193326Sed // semantic analysis. 1209224145Sdim for (RecordDecl::field_iterator Field = record->field_begin(), 1210224145Sdim FieldEnd = record->field_end(); 1211193326Sed Field != FieldEnd; ++Field) 1212193326Sed assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 1213193326Sed#endif 1214193326Sed return; 1215193326Sed } 1216193326Sed 1217193326Sed // FIXME: volatility 1218193326Sed FieldDecl *Field = E->getInitializedFieldInUnion(); 1219218893Sdim 1220234982Sdim LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field); 1221193326Sed if (NumInitElements) { 1222193326Sed // Store the initializer into the field 1223224145Sdim EmitInitializationToLValue(E->getInit(0), FieldLoc); 1224193326Sed } else { 1225218893Sdim // Default-initialize to null. 1226224145Sdim EmitNullInitializationToLValue(FieldLoc); 1227193326Sed } 1228193326Sed 1229193326Sed return; 1230193326Sed } 1231198092Srdivacky 1232224145Sdim // We'll need to enter cleanup scopes in case any of the member 1233224145Sdim // initializers throw an exception. 1234226633Sdim SmallVector<EHScopeStack::stable_iterator, 16> cleanups; 1235234353Sdim llvm::Instruction *cleanupDominator = 0; 1236224145Sdim 1237193326Sed // Here we iterate over the fields; this makes it simpler to both 1238193326Sed // default-initialize fields and skip over unnamed fields. 1239224145Sdim unsigned curInitIndex = 0; 1240224145Sdim for (RecordDecl::field_iterator field = record->field_begin(), 1241224145Sdim fieldEnd = record->field_end(); 1242224145Sdim field != fieldEnd; ++field) { 1243224145Sdim // We're done once we hit the flexible array member. 1244224145Sdim if (field->getType()->isIncompleteArrayType()) 1245193326Sed break; 1246193326Sed 1247224145Sdim // Always skip anonymous bitfields. 1248224145Sdim if (field->isUnnamedBitfield()) 1249193326Sed continue; 1250193326Sed 1251224145Sdim // We're done if we reach the end of the explicit initializers, we 1252224145Sdim // have a zeroed object, and the rest of the fields are 1253224145Sdim // zero-initializable. 1254224145Sdim if (curInitIndex == NumInitElements && Dest.isZeroed() && 1255218893Sdim CGF.getTypes().isZeroInitializable(E->getType())) 1256218893Sdim break; 1257218893Sdim 1258234982Sdim 1259234982Sdim LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, *field); 1260193326Sed // We never generate write-barries for initialized fields. 1261224145Sdim LV.setNonGC(true); 1262218893Sdim 1263224145Sdim if (curInitIndex < NumInitElements) { 1264204962Srdivacky // Store the initializer into the field. 1265224145Sdim EmitInitializationToLValue(E->getInit(curInitIndex++), LV); 1266193326Sed } else { 1267193326Sed // We're out of initalizers; default-initialize to null 1268224145Sdim EmitNullInitializationToLValue(LV); 1269193326Sed } 1270224145Sdim 1271224145Sdim // Push a destructor if necessary. 1272224145Sdim // FIXME: if we have an array of structures, all explicitly 1273224145Sdim // initialized, we can end up pushing a linear number of cleanups. 1274224145Sdim bool pushedCleanup = false; 1275224145Sdim if (QualType::DestructionKind dtorKind 1276224145Sdim = field->getType().isDestructedType()) { 1277224145Sdim assert(LV.isSimple()); 1278224145Sdim if (CGF.needsEHCleanup(dtorKind)) { 1279234353Sdim if (!cleanupDominator) 1280234353Sdim cleanupDominator = CGF.Builder.CreateUnreachable(); // placeholder 1281234353Sdim 1282224145Sdim CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(), 1283224145Sdim CGF.getDestroyer(dtorKind), false); 1284224145Sdim cleanups.push_back(CGF.EHStack.stable_begin()); 1285224145Sdim pushedCleanup = true; 1286224145Sdim } 1287224145Sdim } 1288218893Sdim 1289218893Sdim // If the GEP didn't get used because of a dead zero init or something 1290218893Sdim // else, clean it up for -O0 builds and general tidiness. 1291224145Sdim if (!pushedCleanup && LV.isSimple()) 1292218893Sdim if (llvm::GetElementPtrInst *GEP = 1293224145Sdim dyn_cast<llvm::GetElementPtrInst>(LV.getAddress())) 1294218893Sdim if (GEP->use_empty()) 1295218893Sdim GEP->eraseFromParent(); 1296193326Sed } 1297224145Sdim 1298224145Sdim // Deactivate all the partial cleanups in reverse order, which 1299224145Sdim // generally means popping them. 1300224145Sdim for (unsigned i = cleanups.size(); i != 0; --i) 1301234353Sdim CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator); 1302234353Sdim 1303234353Sdim // Destroy the placeholder if we made one. 1304234353Sdim if (cleanupDominator) 1305234353Sdim cleanupDominator->eraseFromParent(); 1306193326Sed} 1307193326Sed 1308193326Sed//===----------------------------------------------------------------------===// 1309193326Sed// Entry Points into this File 1310193326Sed//===----------------------------------------------------------------------===// 1311193326Sed 1312218893Sdim/// GetNumNonZeroBytesInInit - Get an approximate count of the number of 1313218893Sdim/// non-zero bytes that will be stored when outputting the initializer for the 1314218893Sdim/// specified initializer expression. 1315221345Sdimstatic CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) { 1316221345Sdim E = E->IgnoreParens(); 1317218893Sdim 1318218893Sdim // 0 and 0.0 won't require any non-zero stores! 1319221345Sdim if (isSimpleZero(E, CGF)) return CharUnits::Zero(); 1320218893Sdim 1321218893Sdim // If this is an initlist expr, sum up the size of sizes of the (present) 1322218893Sdim // elements. If this is something weird, assume the whole thing is non-zero. 1323218893Sdim const InitListExpr *ILE = dyn_cast<InitListExpr>(E); 1324218893Sdim if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType())) 1325221345Sdim return CGF.getContext().getTypeSizeInChars(E->getType()); 1326218893Sdim 1327218893Sdim // InitListExprs for structs have to be handled carefully. If there are 1328218893Sdim // reference members, we need to consider the size of the reference, not the 1329218893Sdim // referencee. InitListExprs for unions and arrays can't have references. 1330218893Sdim if (const RecordType *RT = E->getType()->getAs<RecordType>()) { 1331218893Sdim if (!RT->isUnionType()) { 1332218893Sdim RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 1333221345Sdim CharUnits NumNonZeroBytes = CharUnits::Zero(); 1334218893Sdim 1335218893Sdim unsigned ILEElement = 0; 1336218893Sdim for (RecordDecl::field_iterator Field = SD->field_begin(), 1337218893Sdim FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) { 1338218893Sdim // We're done once we hit the flexible array member or run out of 1339218893Sdim // InitListExpr elements. 1340218893Sdim if (Field->getType()->isIncompleteArrayType() || 1341218893Sdim ILEElement == ILE->getNumInits()) 1342218893Sdim break; 1343218893Sdim if (Field->isUnnamedBitfield()) 1344218893Sdim continue; 1345218893Sdim 1346218893Sdim const Expr *E = ILE->getInit(ILEElement++); 1347218893Sdim 1348218893Sdim // Reference values are always non-null and have the width of a pointer. 1349218893Sdim if (Field->getType()->isReferenceType()) 1350221345Sdim NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits( 1351251662Sdim CGF.getTarget().getPointerWidth(0)); 1352218893Sdim else 1353218893Sdim NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF); 1354218893Sdim } 1355218893Sdim 1356218893Sdim return NumNonZeroBytes; 1357218893Sdim } 1358218893Sdim } 1359218893Sdim 1360218893Sdim 1361221345Sdim CharUnits NumNonZeroBytes = CharUnits::Zero(); 1362218893Sdim for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) 1363218893Sdim NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF); 1364218893Sdim return NumNonZeroBytes; 1365218893Sdim} 1366218893Sdim 1367218893Sdim/// CheckAggExprForMemSetUse - If the initializer is large and has a lot of 1368218893Sdim/// zeros in it, emit a memset and avoid storing the individual zeros. 1369218893Sdim/// 1370218893Sdimstatic void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E, 1371218893Sdim CodeGenFunction &CGF) { 1372218893Sdim // If the slot is already known to be zeroed, nothing to do. Don't mess with 1373218893Sdim // volatile stores. 1374218893Sdim if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return; 1375221345Sdim 1376221345Sdim // C++ objects with a user-declared constructor don't need zero'ing. 1377243830Sdim if (CGF.getLangOpts().CPlusPlus) 1378221345Sdim if (const RecordType *RT = CGF.getContext() 1379221345Sdim .getBaseElementType(E->getType())->getAs<RecordType>()) { 1380221345Sdim const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1381221345Sdim if (RD->hasUserDeclaredConstructor()) 1382221345Sdim return; 1383221345Sdim } 1384221345Sdim 1385218893Sdim // If the type is 16-bytes or smaller, prefer individual stores over memset. 1386221345Sdim std::pair<CharUnits, CharUnits> TypeInfo = 1387221345Sdim CGF.getContext().getTypeInfoInChars(E->getType()); 1388221345Sdim if (TypeInfo.first <= CharUnits::fromQuantity(16)) 1389218893Sdim return; 1390218893Sdim 1391218893Sdim // Check to see if over 3/4 of the initializer are known to be zero. If so, 1392218893Sdim // we prefer to emit memset + individual stores for the rest. 1393221345Sdim CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF); 1394221345Sdim if (NumNonZeroBytes*4 > TypeInfo.first) 1395218893Sdim return; 1396218893Sdim 1397218893Sdim // Okay, it seems like a good idea to use an initial memset, emit the call. 1398221345Sdim llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity()); 1399221345Sdim CharUnits Align = TypeInfo.second; 1400218893Sdim 1401218893Sdim llvm::Value *Loc = Slot.getAddr(); 1402218893Sdim 1403234353Sdim Loc = CGF.Builder.CreateBitCast(Loc, CGF.Int8PtrTy); 1404221345Sdim CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, 1405221345Sdim Align.getQuantity(), false); 1406218893Sdim 1407218893Sdim // Tell the AggExprEmitter that the slot is known zero. 1408218893Sdim Slot.setZeroed(); 1409218893Sdim} 1410218893Sdim 1411218893Sdim 1412218893Sdim 1413218893Sdim 1414193326Sed/// EmitAggExpr - Emit the computation of the specified expression of aggregate 1415193326Sed/// type. The result is computed into DestPtr. Note that if DestPtr is null, 1416193326Sed/// the value of the aggregate expression is not needed. If VolatileDest is 1417193326Sed/// true, DestPtr cannot be 0. 1418239462Sdimvoid CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) { 1419249423Sdim assert(E && hasAggregateEvaluationKind(E->getType()) && 1420193326Sed "Invalid aggregate expression to emit"); 1421218893Sdim assert((Slot.getAddr() != 0 || Slot.isIgnored()) && 1422218893Sdim "slot has bits but no address"); 1423198092Srdivacky 1424218893Sdim // Optimize the slot if possible. 1425218893Sdim CheckAggExprForMemSetUse(Slot, E, *this); 1426218893Sdim 1427239462Sdim AggExprEmitter(*this, Slot).Visit(const_cast<Expr*>(E)); 1428193326Sed} 1429193326Sed 1430203955SrdivackyLValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { 1431249423Sdim assert(hasAggregateEvaluationKind(E->getType()) && "Invalid argument!"); 1432203955Srdivacky llvm::Value *Temp = CreateMemTemp(E->getType()); 1433212904Sdim LValue LV = MakeAddrLValue(Temp, E->getType()); 1434226633Sdim EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed, 1435226633Sdim AggValueSlot::DoesNotNeedGCBarriers, 1436226633Sdim AggValueSlot::IsNotAliased)); 1437212904Sdim return LV; 1438203955Srdivacky} 1439203955Srdivacky 1440193326Sedvoid CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 1441193326Sed llvm::Value *SrcPtr, QualType Ty, 1442239462Sdim bool isVolatile, 1443243830Sdim CharUnits alignment, 1444243830Sdim bool isAssignment) { 1445193326Sed assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 1446198092Srdivacky 1447243830Sdim if (getLangOpts().CPlusPlus) { 1448207619Srdivacky if (const RecordType *RT = Ty->getAs<RecordType>()) { 1449208600Srdivacky CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 1450208600Srdivacky assert((Record->hasTrivialCopyConstructor() || 1451226633Sdim Record->hasTrivialCopyAssignment() || 1452226633Sdim Record->hasTrivialMoveConstructor() || 1453226633Sdim Record->hasTrivialMoveAssignment()) && 1454249423Sdim "Trying to aggregate-copy a type without a trivial copy/move " 1455208600Srdivacky "constructor or assignment operator"); 1456208600Srdivacky // Ignore empty classes in C++. 1457208600Srdivacky if (Record->isEmpty()) 1458207619Srdivacky return; 1459207619Srdivacky } 1460207619Srdivacky } 1461207619Srdivacky 1462193326Sed // Aggregate assignment turns into llvm.memcpy. This is almost valid per 1463193326Sed // C99 6.5.16.1p3, which states "If the value being stored in an object is 1464193326Sed // read from another object that overlaps in anyway the storage of the first 1465193326Sed // object, then the overlap shall be exact and the two objects shall have 1466193326Sed // qualified or unqualified versions of a compatible type." 1467193326Sed // 1468193326Sed // memcpy is not defined if the source and destination pointers are exactly 1469193326Sed // equal, but other compilers do this optimization, and almost every memcpy 1470193326Sed // implementation handles this case safely. If there is a libc that does not 1471193326Sed // safely handle this, we can add a target hook. 1472198092Srdivacky 1473243830Sdim // Get data size and alignment info for this aggregate. If this is an 1474243830Sdim // assignment don't copy the tail padding. Otherwise copying it is fine. 1475243830Sdim std::pair<CharUnits, CharUnits> TypeInfo; 1476243830Sdim if (isAssignment) 1477243830Sdim TypeInfo = getContext().getTypeInfoDataSizeInChars(Ty); 1478243830Sdim else 1479243830Sdim TypeInfo = getContext().getTypeInfoInChars(Ty); 1480198092Srdivacky 1481239462Sdim if (alignment.isZero()) 1482239462Sdim alignment = TypeInfo.second; 1483234353Sdim 1484193326Sed // FIXME: Handle variable sized types. 1485198092Srdivacky 1486193326Sed // FIXME: If we have a volatile struct, the optimizer can remove what might 1487193326Sed // appear to be `extra' memory ops: 1488193326Sed // 1489193326Sed // volatile struct { int i; } a, b; 1490193326Sed // 1491193326Sed // int main() { 1492193326Sed // a = b; 1493193326Sed // a = b; 1494193326Sed // } 1495193326Sed // 1496206275Srdivacky // we need to use a different call here. We use isVolatile to indicate when 1497193326Sed // either the source or the destination is volatile. 1498206275Srdivacky 1499226633Sdim llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); 1500226633Sdim llvm::Type *DBP = 1501218893Sdim llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace()); 1502226633Sdim DestPtr = Builder.CreateBitCast(DestPtr, DBP); 1503206275Srdivacky 1504226633Sdim llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); 1505226633Sdim llvm::Type *SBP = 1506218893Sdim llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace()); 1507226633Sdim SrcPtr = Builder.CreateBitCast(SrcPtr, SBP); 1508206275Srdivacky 1509224145Sdim // Don't do any of the memmove_collectable tests if GC isn't set. 1510234353Sdim if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { 1511224145Sdim // fall through 1512224145Sdim } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { 1513210299Sed RecordDecl *Record = RecordTy->getDecl(); 1514210299Sed if (Record->hasObjectMember()) { 1515221345Sdim CharUnits size = TypeInfo.first; 1516226633Sdim llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1517221345Sdim llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1518210299Sed CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1519210299Sed SizeVal); 1520210299Sed return; 1521210299Sed } 1522224145Sdim } else if (Ty->isArrayType()) { 1523210299Sed QualType BaseType = getContext().getBaseElementType(Ty); 1524210299Sed if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { 1525210299Sed if (RecordTy->getDecl()->hasObjectMember()) { 1526221345Sdim CharUnits size = TypeInfo.first; 1527226633Sdim llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1528221345Sdim llvm::Value *SizeVal = 1529221345Sdim llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1530210299Sed CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1531210299Sed SizeVal); 1532210299Sed return; 1533210299Sed } 1534210299Sed } 1535210299Sed } 1536243830Sdim 1537243830Sdim // Determine the metadata to describe the position of any padding in this 1538243830Sdim // memcpy, as well as the TBAA tags for the members of the struct, in case 1539243830Sdim // the optimizer wishes to expand it in to scalar memory operations. 1540243830Sdim llvm::MDNode *TBAAStructTag = CGM.getTBAAStructInfo(Ty); 1541210299Sed 1542218893Sdim Builder.CreateMemCpy(DestPtr, SrcPtr, 1543221345Sdim llvm::ConstantInt::get(IntPtrTy, 1544221345Sdim TypeInfo.first.getQuantity()), 1545243830Sdim alignment.getQuantity(), isVolatile, 1546243830Sdim /*TBAATag=*/0, TBAAStructTag); 1547193326Sed} 1548234353Sdim 1549234353Sdimvoid CodeGenFunction::MaybeEmitStdInitializerListCleanup(llvm::Value *loc, 1550234353Sdim const Expr *init) { 1551234353Sdim const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(init); 1552234353Sdim if (cleanups) 1553234353Sdim init = cleanups->getSubExpr(); 1554234353Sdim 1555234353Sdim if (isa<InitListExpr>(init) && 1556234353Sdim cast<InitListExpr>(init)->initializesStdInitializerList()) { 1557234353Sdim // We initialized this std::initializer_list with an initializer list. 1558234353Sdim // A backing array was created. Push a cleanup for it. 1559234353Sdim EmitStdInitializerListCleanup(loc, cast<InitListExpr>(init)); 1560234353Sdim } 1561234353Sdim} 1562234353Sdim 1563234353Sdimstatic void EmitRecursiveStdInitializerListCleanup(CodeGenFunction &CGF, 1564234353Sdim llvm::Value *arrayStart, 1565234353Sdim const InitListExpr *init) { 1566234353Sdim // Check if there are any recursive cleanups to do, i.e. if we have 1567234353Sdim // std::initializer_list<std::initializer_list<obj>> list = {{obj()}}; 1568234353Sdim // then we need to destroy the inner array as well. 1569234353Sdim for (unsigned i = 0, e = init->getNumInits(); i != e; ++i) { 1570234353Sdim const InitListExpr *subInit = dyn_cast<InitListExpr>(init->getInit(i)); 1571234353Sdim if (!subInit || !subInit->initializesStdInitializerList()) 1572234353Sdim continue; 1573234353Sdim 1574234353Sdim // This one needs to be destroyed. Get the address of the std::init_list. 1575234353Sdim llvm::Value *offset = llvm::ConstantInt::get(CGF.SizeTy, i); 1576234353Sdim llvm::Value *loc = CGF.Builder.CreateInBoundsGEP(arrayStart, offset, 1577234353Sdim "std.initlist"); 1578234353Sdim CGF.EmitStdInitializerListCleanup(loc, subInit); 1579234353Sdim } 1580234353Sdim} 1581234353Sdim 1582234353Sdimvoid CodeGenFunction::EmitStdInitializerListCleanup(llvm::Value *loc, 1583234353Sdim const InitListExpr *init) { 1584234353Sdim ASTContext &ctx = getContext(); 1585234353Sdim QualType element = GetStdInitializerListElementType(init->getType()); 1586234353Sdim unsigned numInits = init->getNumInits(); 1587234353Sdim llvm::APInt size(ctx.getTypeSize(ctx.getSizeType()), numInits); 1588234353Sdim QualType array =ctx.getConstantArrayType(element, size, ArrayType::Normal, 0); 1589234353Sdim QualType arrayPtr = ctx.getPointerType(array); 1590234353Sdim llvm::Type *arrayPtrType = ConvertType(arrayPtr); 1591234353Sdim 1592234353Sdim // lvalue is the location of a std::initializer_list, which as its first 1593234353Sdim // element has a pointer to the array we want to destroy. 1594234353Sdim llvm::Value *startPointer = Builder.CreateStructGEP(loc, 0, "startPointer"); 1595234353Sdim llvm::Value *startAddress = Builder.CreateLoad(startPointer, "startAddress"); 1596234353Sdim 1597234353Sdim ::EmitRecursiveStdInitializerListCleanup(*this, startAddress, init); 1598234353Sdim 1599234353Sdim llvm::Value *arrayAddress = 1600234353Sdim Builder.CreateBitCast(startAddress, arrayPtrType, "arrayAddress"); 1601234353Sdim ::EmitStdInitializerListCleanup(*this, array, arrayAddress, init); 1602234353Sdim} 1603