1 //===--- CGExpr.cpp - Emit LLVM Code from 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 Expr nodes as LLVM code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenFunction.h" 15 #include "CodeGenModule.h" 16 #include "CGCall.h" 17 #include "CGCXXABI.h" 18 #include "CGDebugInfo.h" 19 #include "CGRecordLayout.h" 20 #include "CGObjCRuntime.h" 21 #include "TargetInfo.h" 22 #include "clang/AST/ASTContext.h" 23 #include "clang/AST/DeclObjC.h" 24 #include "clang/Frontend/CodeGenOptions.h" 25 #include "llvm/Intrinsics.h" 26 #include "llvm/LLVMContext.h" 27 #include "llvm/Support/MDBuilder.h" 28 #include "llvm/Target/TargetData.h" 29 using namespace clang; 30 using namespace CodeGen; 31 32 //===--------------------------------------------------------------------===// 33 // Miscellaneous Helper Methods 34 //===--------------------------------------------------------------------===// 35 36 llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) { 37 unsigned addressSpace = 38 cast<llvm::PointerType>(value->getType())->getAddressSpace(); 39 40 llvm::PointerType *destType = Int8PtrTy; 41 if (addressSpace) 42 destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace); 43 44 if (value->getType() == destType) return value; 45 return Builder.CreateBitCast(value, destType); 46 } 47 48 /// CreateTempAlloca - This creates a alloca and inserts it into the entry 49 /// block. 50 llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty, 51 const Twine &Name) { 52 if (!Builder.isNamePreserving()) 53 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt); 54 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt); 55 } 56 57 void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var, 58 llvm::Value *Init) { 59 llvm::StoreInst *Store = new llvm::StoreInst(Init, Var); 60 llvm::BasicBlock *Block = AllocaInsertPt->getParent(); 61 Block->getInstList().insertAfter(&*AllocaInsertPt, Store); 62 } 63 64 llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty, 65 const Twine &Name) { 66 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name); 67 // FIXME: Should we prefer the preferred type alignment here? 68 CharUnits Align = getContext().getTypeAlignInChars(Ty); 69 Alloc->setAlignment(Align.getQuantity()); 70 return Alloc; 71 } 72 73 llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty, 74 const Twine &Name) { 75 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name); 76 // FIXME: Should we prefer the preferred type alignment here? 77 CharUnits Align = getContext().getTypeAlignInChars(Ty); 78 Alloc->setAlignment(Align.getQuantity()); 79 return Alloc; 80 } 81 82 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified 83 /// expression and compare the result against zero, returning an Int1Ty value. 84 llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { 85 if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) { 86 llvm::Value *MemPtr = EmitScalarExpr(E); 87 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT); 88 } 89 90 QualType BoolTy = getContext().BoolTy; 91 if (!E->getType()->isAnyComplexType()) 92 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy); 93 94 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy); 95 } 96 97 /// EmitIgnoredExpr - Emit code to compute the specified expression, 98 /// ignoring the result. 99 void CodeGenFunction::EmitIgnoredExpr(const Expr *E) { 100 if (E->isRValue()) 101 return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true); 102 103 // Just emit it as an l-value and drop the result. 104 EmitLValue(E); 105 } 106 107 /// EmitAnyExpr - Emit code to compute the specified expression which 108 /// can have any type. The result is returned as an RValue struct. 109 /// If this is an aggregate expression, AggSlot indicates where the 110 /// result should be returned. 111 RValue CodeGenFunction::EmitAnyExpr(const Expr *E, AggValueSlot AggSlot, 112 bool IgnoreResult) { 113 if (!hasAggregateLLVMType(E->getType())) 114 return RValue::get(EmitScalarExpr(E, IgnoreResult)); 115 else if (E->getType()->isAnyComplexType()) 116 return RValue::getComplex(EmitComplexExpr(E, IgnoreResult, IgnoreResult)); 117 118 EmitAggExpr(E, AggSlot, IgnoreResult); 119 return AggSlot.asRValue(); 120 } 121 122 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will 123 /// always be accessible even if no aggregate location is provided. 124 RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) { 125 AggValueSlot AggSlot = AggValueSlot::ignored(); 126 127 if (hasAggregateLLVMType(E->getType()) && 128 !E->getType()->isAnyComplexType()) 129 AggSlot = CreateAggTemp(E->getType(), "agg.tmp"); 130 return EmitAnyExpr(E, AggSlot); 131 } 132 133 /// EmitAnyExprToMem - Evaluate an expression into a given memory 134 /// location. 135 void CodeGenFunction::EmitAnyExprToMem(const Expr *E, 136 llvm::Value *Location, 137 Qualifiers Quals, 138 bool IsInit) { 139 // FIXME: This function should take an LValue as an argument. 140 if (E->getType()->isAnyComplexType()) { 141 EmitComplexExprIntoAddr(E, Location, Quals.hasVolatile()); 142 } else if (hasAggregateLLVMType(E->getType())) { 143 CharUnits Alignment = getContext().getTypeAlignInChars(E->getType()); 144 EmitAggExpr(E, AggValueSlot::forAddr(Location, Alignment, Quals, 145 AggValueSlot::IsDestructed_t(IsInit), 146 AggValueSlot::DoesNotNeedGCBarriers, 147 AggValueSlot::IsAliased_t(!IsInit))); 148 } else { 149 RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false)); 150 LValue LV = MakeAddrLValue(Location, E->getType()); 151 EmitStoreThroughLValue(RV, LV); 152 } 153 } 154 155 namespace { 156 /// \brief An adjustment to be made to the temporary created when emitting a 157 /// reference binding, which accesses a particular subobject of that temporary. 158 struct SubobjectAdjustment { 159 enum { DerivedToBaseAdjustment, FieldAdjustment } Kind; 160 161 union { 162 struct { 163 const CastExpr *BasePath; 164 const CXXRecordDecl *DerivedClass; 165 } DerivedToBase; 166 167 FieldDecl *Field; 168 }; 169 170 SubobjectAdjustment(const CastExpr *BasePath, 171 const CXXRecordDecl *DerivedClass) 172 : Kind(DerivedToBaseAdjustment) { 173 DerivedToBase.BasePath = BasePath; 174 DerivedToBase.DerivedClass = DerivedClass; 175 } 176 177 SubobjectAdjustment(FieldDecl *Field) 178 : Kind(FieldAdjustment) { 179 this->Field = Field; 180 } 181 }; 182 } 183 184 static llvm::Value * 185 CreateReferenceTemporary(CodeGenFunction &CGF, QualType Type, 186 const NamedDecl *InitializedDecl) { 187 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) { 188 if (VD->hasGlobalStorage()) { 189 SmallString<256> Name; 190 llvm::raw_svector_ostream Out(Name); 191 CGF.CGM.getCXXABI().getMangleContext().mangleReferenceTemporary(VD, Out); 192 Out.flush(); 193 194 llvm::Type *RefTempTy = CGF.ConvertTypeForMem(Type); 195 196 // Create the reference temporary. 197 llvm::GlobalValue *RefTemp = 198 new llvm::GlobalVariable(CGF.CGM.getModule(), 199 RefTempTy, /*isConstant=*/false, 200 llvm::GlobalValue::InternalLinkage, 201 llvm::Constant::getNullValue(RefTempTy), 202 Name.str()); 203 return RefTemp; 204 } 205 } 206 207 return CGF.CreateMemTemp(Type, "ref.tmp"); 208 } 209 210 static llvm::Value * 211 EmitExprForReferenceBinding(CodeGenFunction &CGF, const Expr *E, 212 llvm::Value *&ReferenceTemporary, 213 const CXXDestructorDecl *&ReferenceTemporaryDtor, 214 QualType &ObjCARCReferenceLifetimeType, 215 const NamedDecl *InitializedDecl) { 216 // Look through single-element init lists that claim to be lvalues. They're 217 // just syntactic wrappers in this case. 218 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) { 219 if (ILE->getNumInits() == 1 && ILE->isGLValue()) 220 E = ILE->getInit(0); 221 } 222 223 // Look through expressions for materialized temporaries (for now). 224 if (const MaterializeTemporaryExpr *M 225 = dyn_cast<MaterializeTemporaryExpr>(E)) { 226 // Objective-C++ ARC: 227 // If we are binding a reference to a temporary that has ownership, we 228 // need to perform retain/release operations on the temporary. 229 if (CGF.getContext().getLangOpts().ObjCAutoRefCount && 230 E->getType()->isObjCLifetimeType() && 231 (E->getType().getObjCLifetime() == Qualifiers::OCL_Strong || 232 E->getType().getObjCLifetime() == Qualifiers::OCL_Weak || 233 E->getType().getObjCLifetime() == Qualifiers::OCL_Autoreleasing)) 234 ObjCARCReferenceLifetimeType = E->getType(); 235 236 E = M->GetTemporaryExpr(); 237 } 238 239 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E)) 240 E = DAE->getExpr(); 241 242 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(E)) { 243 CGF.enterFullExpression(EWC); 244 CodeGenFunction::RunCleanupsScope Scope(CGF); 245 246 return EmitExprForReferenceBinding(CGF, EWC->getSubExpr(), 247 ReferenceTemporary, 248 ReferenceTemporaryDtor, 249 ObjCARCReferenceLifetimeType, 250 InitializedDecl); 251 } 252 253 RValue RV; 254 if (E->isGLValue()) { 255 // Emit the expression as an lvalue. 256 LValue LV = CGF.EmitLValue(E); 257 258 if (LV.isSimple()) 259 return LV.getAddress(); 260 261 // We have to load the lvalue. 262 RV = CGF.EmitLoadOfLValue(LV); 263 } else { 264 if (!ObjCARCReferenceLifetimeType.isNull()) { 265 ReferenceTemporary = CreateReferenceTemporary(CGF, 266 ObjCARCReferenceLifetimeType, 267 InitializedDecl); 268 269 270 LValue RefTempDst = CGF.MakeAddrLValue(ReferenceTemporary, 271 ObjCARCReferenceLifetimeType); 272 273 CGF.EmitScalarInit(E, dyn_cast_or_null<ValueDecl>(InitializedDecl), 274 RefTempDst, false); 275 276 bool ExtendsLifeOfTemporary = false; 277 if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(InitializedDecl)) { 278 if (Var->extendsLifetimeOfTemporary()) 279 ExtendsLifeOfTemporary = true; 280 } else if (InitializedDecl && isa<FieldDecl>(InitializedDecl)) { 281 ExtendsLifeOfTemporary = true; 282 } 283 284 if (!ExtendsLifeOfTemporary) { 285 // Since the lifetime of this temporary isn't going to be extended, 286 // we need to clean it up ourselves at the end of the full expression. 287 switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) { 288 case Qualifiers::OCL_None: 289 case Qualifiers::OCL_ExplicitNone: 290 case Qualifiers::OCL_Autoreleasing: 291 break; 292 293 case Qualifiers::OCL_Strong: { 294 assert(!ObjCARCReferenceLifetimeType->isArrayType()); 295 CleanupKind cleanupKind = CGF.getARCCleanupKind(); 296 CGF.pushDestroy(cleanupKind, 297 ReferenceTemporary, 298 ObjCARCReferenceLifetimeType, 299 CodeGenFunction::destroyARCStrongImprecise, 300 cleanupKind & EHCleanup); 301 break; 302 } 303 304 case Qualifiers::OCL_Weak: 305 assert(!ObjCARCReferenceLifetimeType->isArrayType()); 306 CGF.pushDestroy(NormalAndEHCleanup, 307 ReferenceTemporary, 308 ObjCARCReferenceLifetimeType, 309 CodeGenFunction::destroyARCWeak, 310 /*useEHCleanupForArray*/ true); 311 break; 312 } 313 314 ObjCARCReferenceLifetimeType = QualType(); 315 } 316 317 return ReferenceTemporary; 318 } 319 320 SmallVector<SubobjectAdjustment, 2> Adjustments; 321 while (true) { 322 E = E->IgnoreParens(); 323 324 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { 325 if ((CE->getCastKind() == CK_DerivedToBase || 326 CE->getCastKind() == CK_UncheckedDerivedToBase) && 327 E->getType()->isRecordType()) { 328 E = CE->getSubExpr(); 329 CXXRecordDecl *Derived 330 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl()); 331 Adjustments.push_back(SubobjectAdjustment(CE, Derived)); 332 continue; 333 } 334 335 if (CE->getCastKind() == CK_NoOp) { 336 E = CE->getSubExpr(); 337 continue; 338 } 339 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 340 if (!ME->isArrow() && ME->getBase()->isRValue()) { 341 assert(ME->getBase()->getType()->isRecordType()); 342 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) { 343 E = ME->getBase(); 344 Adjustments.push_back(SubobjectAdjustment(Field)); 345 continue; 346 } 347 } 348 } 349 350 if (const OpaqueValueExpr *opaque = dyn_cast<OpaqueValueExpr>(E)) 351 if (opaque->getType()->isRecordType()) 352 return CGF.EmitOpaqueValueLValue(opaque).getAddress(); 353 354 // Nothing changed. 355 break; 356 } 357 358 // Create a reference temporary if necessary. 359 AggValueSlot AggSlot = AggValueSlot::ignored(); 360 if (CGF.hasAggregateLLVMType(E->getType()) && 361 !E->getType()->isAnyComplexType()) { 362 ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(), 363 InitializedDecl); 364 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(E->getType()); 365 AggValueSlot::IsDestructed_t isDestructed 366 = AggValueSlot::IsDestructed_t(InitializedDecl != 0); 367 AggSlot = AggValueSlot::forAddr(ReferenceTemporary, Alignment, 368 Qualifiers(), isDestructed, 369 AggValueSlot::DoesNotNeedGCBarriers, 370 AggValueSlot::IsNotAliased); 371 } 372 373 if (InitializedDecl) { 374 // Get the destructor for the reference temporary. 375 if (const RecordType *RT = E->getType()->getAs<RecordType>()) { 376 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 377 if (!ClassDecl->hasTrivialDestructor()) 378 ReferenceTemporaryDtor = ClassDecl->getDestructor(); 379 } 380 } 381 382 RV = CGF.EmitAnyExpr(E, AggSlot); 383 384 // Check if need to perform derived-to-base casts and/or field accesses, to 385 // get from the temporary object we created (and, potentially, for which we 386 // extended the lifetime) to the subobject we're binding the reference to. 387 if (!Adjustments.empty()) { 388 llvm::Value *Object = RV.getAggregateAddr(); 389 for (unsigned I = Adjustments.size(); I != 0; --I) { 390 SubobjectAdjustment &Adjustment = Adjustments[I-1]; 391 switch (Adjustment.Kind) { 392 case SubobjectAdjustment::DerivedToBaseAdjustment: 393 Object = 394 CGF.GetAddressOfBaseClass(Object, 395 Adjustment.DerivedToBase.DerivedClass, 396 Adjustment.DerivedToBase.BasePath->path_begin(), 397 Adjustment.DerivedToBase.BasePath->path_end(), 398 /*NullCheckValue=*/false); 399 break; 400 401 case SubobjectAdjustment::FieldAdjustment: { 402 LValue LV = 403 CGF.EmitLValueForField(Object, Adjustment.Field, 0); 404 if (LV.isSimple()) { 405 Object = LV.getAddress(); 406 break; 407 } 408 409 // For non-simple lvalues, we actually have to create a copy of 410 // the object we're binding to. 411 QualType T = Adjustment.Field->getType().getNonReferenceType() 412 .getUnqualifiedType(); 413 Object = CreateReferenceTemporary(CGF, T, InitializedDecl); 414 LValue TempLV = CGF.MakeAddrLValue(Object, 415 Adjustment.Field->getType()); 416 CGF.EmitStoreThroughLValue(CGF.EmitLoadOfLValue(LV), TempLV); 417 break; 418 } 419 420 } 421 } 422 423 return Object; 424 } 425 } 426 427 if (RV.isAggregate()) 428 return RV.getAggregateAddr(); 429 430 // Create a temporary variable that we can bind the reference to. 431 ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(), 432 InitializedDecl); 433 434 435 unsigned Alignment = 436 CGF.getContext().getTypeAlignInChars(E->getType()).getQuantity(); 437 if (RV.isScalar()) 438 CGF.EmitStoreOfScalar(RV.getScalarVal(), ReferenceTemporary, 439 /*Volatile=*/false, Alignment, E->getType()); 440 else 441 CGF.StoreComplexToAddr(RV.getComplexVal(), ReferenceTemporary, 442 /*Volatile=*/false); 443 return ReferenceTemporary; 444 } 445 446 RValue 447 CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E, 448 const NamedDecl *InitializedDecl) { 449 llvm::Value *ReferenceTemporary = 0; 450 const CXXDestructorDecl *ReferenceTemporaryDtor = 0; 451 QualType ObjCARCReferenceLifetimeType; 452 llvm::Value *Value = EmitExprForReferenceBinding(*this, E, ReferenceTemporary, 453 ReferenceTemporaryDtor, 454 ObjCARCReferenceLifetimeType, 455 InitializedDecl); 456 if (!ReferenceTemporaryDtor && ObjCARCReferenceLifetimeType.isNull()) 457 return RValue::get(Value); 458 459 // Make sure to call the destructor for the reference temporary. 460 const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl); 461 if (VD && VD->hasGlobalStorage()) { 462 if (ReferenceTemporaryDtor) { 463 llvm::Constant *DtorFn = 464 CGM.GetAddrOfCXXDestructor(ReferenceTemporaryDtor, Dtor_Complete); 465 EmitCXXGlobalDtorRegistration(DtorFn, 466 cast<llvm::Constant>(ReferenceTemporary)); 467 } else { 468 assert(!ObjCARCReferenceLifetimeType.isNull()); 469 // Note: We intentionally do not register a global "destructor" to 470 // release the object. 471 } 472 473 return RValue::get(Value); 474 } 475 476 if (ReferenceTemporaryDtor) 477 PushDestructorCleanup(ReferenceTemporaryDtor, ReferenceTemporary); 478 else { 479 switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) { 480 case Qualifiers::OCL_None: 481 llvm_unreachable( 482 "Not a reference temporary that needs to be deallocated"); 483 case Qualifiers::OCL_ExplicitNone: 484 case Qualifiers::OCL_Autoreleasing: 485 // Nothing to do. 486 break; 487 488 case Qualifiers::OCL_Strong: { 489 bool precise = VD && VD->hasAttr<ObjCPreciseLifetimeAttr>(); 490 CleanupKind cleanupKind = getARCCleanupKind(); 491 pushDestroy(cleanupKind, ReferenceTemporary, ObjCARCReferenceLifetimeType, 492 precise ? destroyARCStrongPrecise : destroyARCStrongImprecise, 493 cleanupKind & EHCleanup); 494 break; 495 } 496 497 case Qualifiers::OCL_Weak: { 498 // __weak objects always get EH cleanups; otherwise, exceptions 499 // could cause really nasty crashes instead of mere leaks. 500 pushDestroy(NormalAndEHCleanup, ReferenceTemporary, 501 ObjCARCReferenceLifetimeType, destroyARCWeak, true); 502 break; 503 } 504 } 505 } 506 507 return RValue::get(Value); 508 } 509 510 511 /// getAccessedFieldNo - Given an encoded value and a result number, return the 512 /// input field number being accessed. 513 unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx, 514 const llvm::Constant *Elts) { 515 return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx)) 516 ->getZExtValue(); 517 } 518 519 void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) { 520 if (!CatchUndefined) 521 return; 522 523 // This needs to be to the standard address space. 524 Address = Builder.CreateBitCast(Address, Int8PtrTy); 525 526 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, IntPtrTy); 527 528 // In time, people may want to control this and use a 1 here. 529 llvm::Value *Arg = Builder.getFalse(); 530 llvm::Value *C = Builder.CreateCall2(F, Address, Arg); 531 llvm::BasicBlock *Cont = createBasicBlock(); 532 llvm::BasicBlock *Check = createBasicBlock(); 533 llvm::Value *NegativeOne = llvm::ConstantInt::get(IntPtrTy, -1ULL); 534 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check); 535 536 EmitBlock(Check); 537 Builder.CreateCondBr(Builder.CreateICmpUGE(C, 538 llvm::ConstantInt::get(IntPtrTy, Size)), 539 Cont, getTrapBB()); 540 EmitBlock(Cont); 541 } 542 543 544 CodeGenFunction::ComplexPairTy CodeGenFunction:: 545 EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, 546 bool isInc, bool isPre) { 547 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(), 548 LV.isVolatileQualified()); 549 550 llvm::Value *NextVal; 551 if (isa<llvm::IntegerType>(InVal.first->getType())) { 552 uint64_t AmountVal = isInc ? 1 : -1; 553 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true); 554 555 // Add the inc/dec to the real part. 556 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); 557 } else { 558 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); 559 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1); 560 if (!isInc) 561 FVal.changeSign(); 562 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal); 563 564 // Add the inc/dec to the real part. 565 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); 566 } 567 568 ComplexPairTy IncVal(NextVal, InVal.second); 569 570 // Store the updated result through the lvalue. 571 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified()); 572 573 // If this is a postinc, return the value read from memory, otherwise use the 574 // updated value. 575 return isPre ? IncVal : InVal; 576 } 577 578 579 //===----------------------------------------------------------------------===// 580 // LValue Expression Emission 581 //===----------------------------------------------------------------------===// 582 583 RValue CodeGenFunction::GetUndefRValue(QualType Ty) { 584 if (Ty->isVoidType()) 585 return RValue::get(0); 586 587 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 588 llvm::Type *EltTy = ConvertType(CTy->getElementType()); 589 llvm::Value *U = llvm::UndefValue::get(EltTy); 590 return RValue::getComplex(std::make_pair(U, U)); 591 } 592 593 // If this is a use of an undefined aggregate type, the aggregate must have an 594 // identifiable address. Just because the contents of the value are undefined 595 // doesn't mean that the address can't be taken and compared. 596 if (hasAggregateLLVMType(Ty)) { 597 llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp"); 598 return RValue::getAggregate(DestPtr); 599 } 600 601 return RValue::get(llvm::UndefValue::get(ConvertType(Ty))); 602 } 603 604 RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E, 605 const char *Name) { 606 ErrorUnsupported(E, Name); 607 return GetUndefRValue(E->getType()); 608 } 609 610 LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E, 611 const char *Name) { 612 ErrorUnsupported(E, Name); 613 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); 614 return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType()); 615 } 616 617 LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) { 618 LValue LV = EmitLValue(E); 619 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple()) 620 EmitCheck(LV.getAddress(), 621 getContext().getTypeSizeInChars(E->getType()).getQuantity()); 622 return LV; 623 } 624 625 /// EmitLValue - Emit code to compute a designator that specifies the location 626 /// of the expression. 627 /// 628 /// This can return one of two things: a simple address or a bitfield reference. 629 /// In either case, the LLVM Value* in the LValue structure is guaranteed to be 630 /// an LLVM pointer type. 631 /// 632 /// If this returns a bitfield reference, nothing about the pointee type of the 633 /// LLVM value is known: For example, it may not be a pointer to an integer. 634 /// 635 /// If this returns a normal address, and if the lvalue's C type is fixed size, 636 /// this method guarantees that the returned pointer type will point to an LLVM 637 /// type of the same size of the lvalue's type. If the lvalue has a variable 638 /// length type, this is not possible. 639 /// 640 LValue CodeGenFunction::EmitLValue(const Expr *E) { 641 switch (E->getStmtClass()) { 642 default: return EmitUnsupportedLValue(E, "l-value expression"); 643 644 case Expr::ObjCPropertyRefExprClass: 645 llvm_unreachable("cannot emit a property reference directly"); 646 647 case Expr::ObjCSelectorExprClass: 648 return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E)); 649 case Expr::ObjCIsaExprClass: 650 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E)); 651 case Expr::BinaryOperatorClass: 652 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E)); 653 case Expr::CompoundAssignOperatorClass: 654 if (!E->getType()->isAnyComplexType()) 655 return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); 656 return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); 657 case Expr::CallExprClass: 658 case Expr::CXXMemberCallExprClass: 659 case Expr::CXXOperatorCallExprClass: 660 case Expr::UserDefinedLiteralClass: 661 return EmitCallExprLValue(cast<CallExpr>(E)); 662 case Expr::VAArgExprClass: 663 return EmitVAArgExprLValue(cast<VAArgExpr>(E)); 664 case Expr::DeclRefExprClass: 665 return EmitDeclRefLValue(cast<DeclRefExpr>(E)); 666 case Expr::ParenExprClass: 667 return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); 668 case Expr::GenericSelectionExprClass: 669 return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr()); 670 case Expr::PredefinedExprClass: 671 return EmitPredefinedLValue(cast<PredefinedExpr>(E)); 672 case Expr::StringLiteralClass: 673 return EmitStringLiteralLValue(cast<StringLiteral>(E)); 674 case Expr::ObjCEncodeExprClass: 675 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E)); 676 case Expr::PseudoObjectExprClass: 677 return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E)); 678 case Expr::InitListExprClass: 679 assert(cast<InitListExpr>(E)->getNumInits() == 1 && 680 "Only single-element init list can be lvalue."); 681 return EmitLValue(cast<InitListExpr>(E)->getInit(0)); 682 683 case Expr::CXXTemporaryObjectExprClass: 684 case Expr::CXXConstructExprClass: 685 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E)); 686 case Expr::CXXBindTemporaryExprClass: 687 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E)); 688 case Expr::LambdaExprClass: 689 return EmitLambdaLValue(cast<LambdaExpr>(E)); 690 691 case Expr::ExprWithCleanupsClass: { 692 const ExprWithCleanups *cleanups = cast<ExprWithCleanups>(E); 693 enterFullExpression(cleanups); 694 RunCleanupsScope Scope(*this); 695 return EmitLValue(cleanups->getSubExpr()); 696 } 697 698 case Expr::CXXScalarValueInitExprClass: 699 return EmitNullInitializationLValue(cast<CXXScalarValueInitExpr>(E)); 700 case Expr::CXXDefaultArgExprClass: 701 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr()); 702 case Expr::CXXTypeidExprClass: 703 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E)); 704 705 case Expr::ObjCMessageExprClass: 706 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E)); 707 case Expr::ObjCIvarRefExprClass: 708 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E)); 709 case Expr::StmtExprClass: 710 return EmitStmtExprLValue(cast<StmtExpr>(E)); 711 case Expr::UnaryOperatorClass: 712 return EmitUnaryOpLValue(cast<UnaryOperator>(E)); 713 case Expr::ArraySubscriptExprClass: 714 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); 715 case Expr::ExtVectorElementExprClass: 716 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E)); 717 case Expr::MemberExprClass: 718 return EmitMemberExpr(cast<MemberExpr>(E)); 719 case Expr::CompoundLiteralExprClass: 720 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E)); 721 case Expr::ConditionalOperatorClass: 722 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E)); 723 case Expr::BinaryConditionalOperatorClass: 724 return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E)); 725 case Expr::ChooseExprClass: 726 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext())); 727 case Expr::OpaqueValueExprClass: 728 return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E)); 729 case Expr::SubstNonTypeTemplateParmExprClass: 730 return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); 731 case Expr::ImplicitCastExprClass: 732 case Expr::CStyleCastExprClass: 733 case Expr::CXXFunctionalCastExprClass: 734 case Expr::CXXStaticCastExprClass: 735 case Expr::CXXDynamicCastExprClass: 736 case Expr::CXXReinterpretCastExprClass: 737 case Expr::CXXConstCastExprClass: 738 case Expr::ObjCBridgedCastExprClass: 739 return EmitCastLValue(cast<CastExpr>(E)); 740 741 case Expr::MaterializeTemporaryExprClass: 742 return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E)); 743 } 744 } 745 746 /// Given an object of the given canonical type, can we safely copy a 747 /// value out of it based on its initializer? 748 static bool isConstantEmittableObjectType(QualType type) { 749 assert(type.isCanonical()); 750 assert(!type->isReferenceType()); 751 752 // Must be const-qualified but non-volatile. 753 Qualifiers qs = type.getLocalQualifiers(); 754 if (!qs.hasConst() || qs.hasVolatile()) return false; 755 756 // Otherwise, all object types satisfy this except C++ classes with 757 // mutable subobjects or non-trivial copy/destroy behavior. 758 if (const RecordType *RT = dyn_cast<RecordType>(type)) 759 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) 760 if (RD->hasMutableFields() || !RD->isTrivial()) 761 return false; 762 763 return true; 764 } 765 766 /// Can we constant-emit a load of a reference to a variable of the 767 /// given type? This is different from predicates like 768 /// Decl::isUsableInConstantExpressions because we do want it to apply 769 /// in situations that don't necessarily satisfy the language's rules 770 /// for this (e.g. C++'s ODR-use rules). For example, we want to able 771 /// to do this with const float variables even if those variables 772 /// aren't marked 'constexpr'. 773 enum ConstantEmissionKind { 774 CEK_None, 775 CEK_AsReferenceOnly, 776 CEK_AsValueOrReference, 777 CEK_AsValueOnly 778 }; 779 static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) { 780 type = type.getCanonicalType(); 781 if (const ReferenceType *ref = dyn_cast<ReferenceType>(type)) { 782 if (isConstantEmittableObjectType(ref->getPointeeType())) 783 return CEK_AsValueOrReference; 784 return CEK_AsReferenceOnly; 785 } 786 if (isConstantEmittableObjectType(type)) 787 return CEK_AsValueOnly; 788 return CEK_None; 789 } 790 791 /// Try to emit a reference to the given value without producing it as 792 /// an l-value. This is actually more than an optimization: we can't 793 /// produce an l-value for variables that we never actually captured 794 /// in a block or lambda, which means const int variables or constexpr 795 /// literals or similar. 796 CodeGenFunction::ConstantEmission 797 CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) { 798 ValueDecl *value = refExpr->getDecl(); 799 800 // The value needs to be an enum constant or a constant variable. 801 ConstantEmissionKind CEK; 802 if (isa<ParmVarDecl>(value)) { 803 CEK = CEK_None; 804 } else if (VarDecl *var = dyn_cast<VarDecl>(value)) { 805 CEK = checkVarTypeForConstantEmission(var->getType()); 806 } else if (isa<EnumConstantDecl>(value)) { 807 CEK = CEK_AsValueOnly; 808 } else { 809 CEK = CEK_None; 810 } 811 if (CEK == CEK_None) return ConstantEmission(); 812 813 Expr::EvalResult result; 814 bool resultIsReference; 815 QualType resultType; 816 817 // It's best to evaluate all the way as an r-value if that's permitted. 818 if (CEK != CEK_AsReferenceOnly && 819 refExpr->EvaluateAsRValue(result, getContext())) { 820 resultIsReference = false; 821 resultType = refExpr->getType(); 822 823 // Otherwise, try to evaluate as an l-value. 824 } else if (CEK != CEK_AsValueOnly && 825 refExpr->EvaluateAsLValue(result, getContext())) { 826 resultIsReference = true; 827 resultType = value->getType(); 828 829 // Failure. 830 } else { 831 return ConstantEmission(); 832 } 833 834 // In any case, if the initializer has side-effects, abandon ship. 835 if (result.HasSideEffects) 836 return ConstantEmission(); 837 838 // Emit as a constant. 839 llvm::Constant *C = CGM.EmitConstantValue(result.Val, resultType, this); 840 841 // Make sure we emit a debug reference to the global variable. 842 // This should probably fire even for 843 if (isa<VarDecl>(value)) { 844 if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value))) 845 EmitDeclRefExprDbgValue(refExpr, C); 846 } else { 847 assert(isa<EnumConstantDecl>(value)); 848 EmitDeclRefExprDbgValue(refExpr, C); 849 } 850 851 // If we emitted a reference constant, we need to dereference that. 852 if (resultIsReference) 853 return ConstantEmission::forReference(C); 854 855 return ConstantEmission::forValue(C); 856 } 857 858 llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue) { 859 return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(), 860 lvalue.getAlignment().getQuantity(), 861 lvalue.getType(), lvalue.getTBAAInfo()); 862 } 863 864 static bool hasBooleanRepresentation(QualType Ty) { 865 if (Ty->isBooleanType()) 866 return true; 867 868 if (const EnumType *ET = Ty->getAs<EnumType>()) 869 return ET->getDecl()->getIntegerType()->isBooleanType(); 870 871 if (const AtomicType *AT = Ty->getAs<AtomicType>()) 872 return hasBooleanRepresentation(AT->getValueType()); 873 874 return false; 875 } 876 877 llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) { 878 const EnumType *ET = Ty->getAs<EnumType>(); 879 bool IsRegularCPlusPlusEnum = (getLangOpts().CPlusPlus && ET && 880 CGM.getCodeGenOpts().StrictEnums && 881 !ET->getDecl()->isFixed()); 882 bool IsBool = hasBooleanRepresentation(Ty); 883 llvm::Type *LTy; 884 if (!IsBool && !IsRegularCPlusPlusEnum) 885 return NULL; 886 887 llvm::APInt Min; 888 llvm::APInt End; 889 if (IsBool) { 890 Min = llvm::APInt(8, 0); 891 End = llvm::APInt(8, 2); 892 LTy = Int8Ty; 893 } else { 894 const EnumDecl *ED = ET->getDecl(); 895 LTy = ConvertTypeForMem(ED->getIntegerType()); 896 unsigned Bitwidth = LTy->getScalarSizeInBits(); 897 unsigned NumNegativeBits = ED->getNumNegativeBits(); 898 unsigned NumPositiveBits = ED->getNumPositiveBits(); 899 900 if (NumNegativeBits) { 901 unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1); 902 assert(NumBits <= Bitwidth); 903 End = llvm::APInt(Bitwidth, 1) << (NumBits - 1); 904 Min = -End; 905 } else { 906 assert(NumPositiveBits <= Bitwidth); 907 End = llvm::APInt(Bitwidth, 1) << NumPositiveBits; 908 Min = llvm::APInt(Bitwidth, 0); 909 } 910 } 911 912 llvm::MDBuilder MDHelper(getLLVMContext()); 913 return MDHelper.CreateRange(Min, End); 914 } 915 916 llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, 917 unsigned Alignment, QualType Ty, 918 llvm::MDNode *TBAAInfo) { 919 llvm::LoadInst *Load = Builder.CreateLoad(Addr); 920 if (Volatile) 921 Load->setVolatile(true); 922 if (Alignment) 923 Load->setAlignment(Alignment); 924 if (TBAAInfo) 925 CGM.DecorateInstruction(Load, TBAAInfo); 926 // If this is an atomic type, all normal reads must be atomic 927 if (Ty->isAtomicType()) 928 Load->setAtomic(llvm::SequentiallyConsistent); 929 930 if (CGM.getCodeGenOpts().OptimizationLevel > 0) 931 if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty)) 932 Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo); 933 934 return EmitFromMemory(Load, Ty); 935 } 936 937 llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) { 938 // Bool has a different representation in memory than in registers. 939 if (hasBooleanRepresentation(Ty)) { 940 // This should really always be an i1, but sometimes it's already 941 // an i8, and it's awkward to track those cases down. 942 if (Value->getType()->isIntegerTy(1)) 943 return Builder.CreateZExt(Value, Builder.getInt8Ty(), "frombool"); 944 assert(Value->getType()->isIntegerTy(8) && "value rep of bool not i1/i8"); 945 } 946 947 return Value; 948 } 949 950 llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) { 951 // Bool has a different representation in memory than in registers. 952 if (hasBooleanRepresentation(Ty)) { 953 assert(Value->getType()->isIntegerTy(8) && "memory rep of bool not i8"); 954 return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool"); 955 } 956 957 return Value; 958 } 959 960 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, 961 bool Volatile, unsigned Alignment, 962 QualType Ty, 963 llvm::MDNode *TBAAInfo, 964 bool isInit) { 965 Value = EmitToMemory(Value, Ty); 966 967 llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile); 968 if (Alignment) 969 Store->setAlignment(Alignment); 970 if (TBAAInfo) 971 CGM.DecorateInstruction(Store, TBAAInfo); 972 if (!isInit && Ty->isAtomicType()) 973 Store->setAtomic(llvm::SequentiallyConsistent); 974 } 975 976 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue, 977 bool isInit) { 978 EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(), 979 lvalue.getAlignment().getQuantity(), lvalue.getType(), 980 lvalue.getTBAAInfo(), isInit); 981 } 982 983 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this 984 /// method emits the address of the lvalue, then loads the result as an rvalue, 985 /// returning the rvalue. 986 RValue CodeGenFunction::EmitLoadOfLValue(LValue LV) { 987 if (LV.isObjCWeak()) { 988 // load of a __weak object. 989 llvm::Value *AddrWeakObj = LV.getAddress(); 990 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this, 991 AddrWeakObj)); 992 } 993 if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) 994 return RValue::get(EmitARCLoadWeak(LV.getAddress())); 995 996 if (LV.isSimple()) { 997 assert(!LV.getType()->isFunctionType()); 998 999 // Everything needs a load. 1000 return RValue::get(EmitLoadOfScalar(LV)); 1001 } 1002 1003 if (LV.isVectorElt()) { 1004 llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddr(), 1005 LV.isVolatileQualified()); 1006 Load->setAlignment(LV.getAlignment().getQuantity()); 1007 return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(), 1008 "vecext")); 1009 } 1010 1011 // If this is a reference to a subset of the elements of a vector, either 1012 // shuffle the input or extract/insert them as appropriate. 1013 if (LV.isExtVectorElt()) 1014 return EmitLoadOfExtVectorElementLValue(LV); 1015 1016 assert(LV.isBitField() && "Unknown LValue type!"); 1017 return EmitLoadOfBitfieldLValue(LV); 1018 } 1019 1020 RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) { 1021 const CGBitFieldInfo &Info = LV.getBitFieldInfo(); 1022 1023 // Get the output type. 1024 llvm::Type *ResLTy = ConvertType(LV.getType()); 1025 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy); 1026 1027 // Compute the result as an OR of all of the individual component accesses. 1028 llvm::Value *Res = 0; 1029 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) { 1030 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i); 1031 1032 // Get the field pointer. 1033 llvm::Value *Ptr = LV.getBitFieldBaseAddr(); 1034 1035 // Only offset by the field index if used, so that incoming values are not 1036 // required to be structures. 1037 if (AI.FieldIndex) 1038 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field"); 1039 1040 // Offset by the byte offset, if used. 1041 if (!AI.FieldByteOffset.isZero()) { 1042 Ptr = EmitCastToVoidPtr(Ptr); 1043 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(), 1044 "bf.field.offs"); 1045 } 1046 1047 // Cast to the access type. 1048 llvm::Type *PTy = llvm::Type::getIntNPtrTy(getLLVMContext(), AI.AccessWidth, 1049 CGM.getContext().getTargetAddressSpace(LV.getType())); 1050 Ptr = Builder.CreateBitCast(Ptr, PTy); 1051 1052 // Perform the load. 1053 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified()); 1054 if (!AI.AccessAlignment.isZero()) 1055 Load->setAlignment(AI.AccessAlignment.getQuantity()); 1056 1057 // Shift out unused low bits and mask out unused high bits. 1058 llvm::Value *Val = Load; 1059 if (AI.FieldBitStart) 1060 Val = Builder.CreateLShr(Load, AI.FieldBitStart); 1061 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth, 1062 AI.TargetBitWidth), 1063 "bf.clear"); 1064 1065 // Extend or truncate to the target size. 1066 if (AI.AccessWidth < ResSizeInBits) 1067 Val = Builder.CreateZExt(Val, ResLTy); 1068 else if (AI.AccessWidth > ResSizeInBits) 1069 Val = Builder.CreateTrunc(Val, ResLTy); 1070 1071 // Shift into place, and OR into the result. 1072 if (AI.TargetBitOffset) 1073 Val = Builder.CreateShl(Val, AI.TargetBitOffset); 1074 Res = Res ? Builder.CreateOr(Res, Val) : Val; 1075 } 1076 1077 // If the bit-field is signed, perform the sign-extension. 1078 // 1079 // FIXME: This can easily be folded into the load of the high bits, which 1080 // could also eliminate the mask of high bits in some situations. 1081 if (Info.isSigned()) { 1082 unsigned ExtraBits = ResSizeInBits - Info.getSize(); 1083 if (ExtraBits) 1084 Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits), 1085 ExtraBits, "bf.val.sext"); 1086 } 1087 1088 return RValue::get(Res); 1089 } 1090 1091 // If this is a reference to a subset of the elements of a vector, create an 1092 // appropriate shufflevector. 1093 RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) { 1094 llvm::LoadInst *Load = Builder.CreateLoad(LV.getExtVectorAddr(), 1095 LV.isVolatileQualified()); 1096 Load->setAlignment(LV.getAlignment().getQuantity()); 1097 llvm::Value *Vec = Load; 1098 1099 const llvm::Constant *Elts = LV.getExtVectorElts(); 1100 1101 // If the result of the expression is a non-vector type, we must be extracting 1102 // a single element. Just codegen as an extractelement. 1103 const VectorType *ExprVT = LV.getType()->getAs<VectorType>(); 1104 if (!ExprVT) { 1105 unsigned InIdx = getAccessedFieldNo(0, Elts); 1106 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx); 1107 return RValue::get(Builder.CreateExtractElement(Vec, Elt)); 1108 } 1109 1110 // Always use shuffle vector to try to retain the original program structure 1111 unsigned NumResultElts = ExprVT->getNumElements(); 1112 1113 SmallVector<llvm::Constant*, 4> Mask; 1114 for (unsigned i = 0; i != NumResultElts; ++i) 1115 Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts))); 1116 1117 llvm::Value *MaskV = llvm::ConstantVector::get(Mask); 1118 Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()), 1119 MaskV); 1120 return RValue::get(Vec); 1121 } 1122 1123 1124 1125 /// EmitStoreThroughLValue - Store the specified rvalue into the specified 1126 /// lvalue, where both are guaranteed to the have the same type, and that type 1127 /// is 'Ty'. 1128 void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit) { 1129 if (!Dst.isSimple()) { 1130 if (Dst.isVectorElt()) { 1131 // Read/modify/write the vector, inserting the new element. 1132 llvm::LoadInst *Load = Builder.CreateLoad(Dst.getVectorAddr(), 1133 Dst.isVolatileQualified()); 1134 Load->setAlignment(Dst.getAlignment().getQuantity()); 1135 llvm::Value *Vec = Load; 1136 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), 1137 Dst.getVectorIdx(), "vecins"); 1138 llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getVectorAddr(), 1139 Dst.isVolatileQualified()); 1140 Store->setAlignment(Dst.getAlignment().getQuantity()); 1141 return; 1142 } 1143 1144 // If this is an update of extended vector elements, insert them as 1145 // appropriate. 1146 if (Dst.isExtVectorElt()) 1147 return EmitStoreThroughExtVectorComponentLValue(Src, Dst); 1148 1149 assert(Dst.isBitField() && "Unknown LValue type"); 1150 return EmitStoreThroughBitfieldLValue(Src, Dst); 1151 } 1152 1153 // There's special magic for assigning into an ARC-qualified l-value. 1154 if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) { 1155 switch (Lifetime) { 1156 case Qualifiers::OCL_None: 1157 llvm_unreachable("present but none"); 1158 1159 case Qualifiers::OCL_ExplicitNone: 1160 // nothing special 1161 break; 1162 1163 case Qualifiers::OCL_Strong: 1164 EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true); 1165 return; 1166 1167 case Qualifiers::OCL_Weak: 1168 EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true); 1169 return; 1170 1171 case Qualifiers::OCL_Autoreleasing: 1172 Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(), 1173 Src.getScalarVal())); 1174 // fall into the normal path 1175 break; 1176 } 1177 } 1178 1179 if (Dst.isObjCWeak() && !Dst.isNonGC()) { 1180 // load of a __weak object. 1181 llvm::Value *LvalueDst = Dst.getAddress(); 1182 llvm::Value *src = Src.getScalarVal(); 1183 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst); 1184 return; 1185 } 1186 1187 if (Dst.isObjCStrong() && !Dst.isNonGC()) { 1188 // load of a __strong object. 1189 llvm::Value *LvalueDst = Dst.getAddress(); 1190 llvm::Value *src = Src.getScalarVal(); 1191 if (Dst.isObjCIvar()) { 1192 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL"); 1193 llvm::Type *ResultType = ConvertType(getContext().LongTy); 1194 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp()); 1195 llvm::Value *dst = RHS; 1196 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); 1197 llvm::Value *LHS = 1198 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast"); 1199 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset"); 1200 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst, 1201 BytesBetween); 1202 } else if (Dst.isGlobalObjCRef()) { 1203 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst, 1204 Dst.isThreadLocalRef()); 1205 } 1206 else 1207 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst); 1208 return; 1209 } 1210 1211 assert(Src.isScalar() && "Can't emit an agg store with this method"); 1212 EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit); 1213 } 1214 1215 void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, 1216 llvm::Value **Result) { 1217 const CGBitFieldInfo &Info = Dst.getBitFieldInfo(); 1218 1219 // Get the output type. 1220 llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType()); 1221 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy); 1222 1223 // Get the source value, truncated to the width of the bit-field. 1224 llvm::Value *SrcVal = Src.getScalarVal(); 1225 1226 if (hasBooleanRepresentation(Dst.getType())) 1227 SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false); 1228 1229 SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits, 1230 Info.getSize()), 1231 "bf.value"); 1232 1233 // Return the new value of the bit-field, if requested. 1234 if (Result) { 1235 // Cast back to the proper type for result. 1236 llvm::Type *SrcTy = Src.getScalarVal()->getType(); 1237 llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false, 1238 "bf.reload.val"); 1239 1240 // Sign extend if necessary. 1241 if (Info.isSigned()) { 1242 unsigned ExtraBits = ResSizeInBits - Info.getSize(); 1243 if (ExtraBits) 1244 ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits), 1245 ExtraBits, "bf.reload.sext"); 1246 } 1247 1248 *Result = ReloadVal; 1249 } 1250 1251 // Iterate over the components, writing each piece to memory. 1252 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) { 1253 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i); 1254 1255 // Get the field pointer. 1256 llvm::Value *Ptr = Dst.getBitFieldBaseAddr(); 1257 unsigned addressSpace = 1258 cast<llvm::PointerType>(Ptr->getType())->getAddressSpace(); 1259 1260 // Only offset by the field index if used, so that incoming values are not 1261 // required to be structures. 1262 if (AI.FieldIndex) 1263 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field"); 1264 1265 // Offset by the byte offset, if used. 1266 if (!AI.FieldByteOffset.isZero()) { 1267 Ptr = EmitCastToVoidPtr(Ptr); 1268 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(), 1269 "bf.field.offs"); 1270 } 1271 1272 // Cast to the access type. 1273 llvm::Type *AccessLTy = 1274 llvm::Type::getIntNTy(getLLVMContext(), AI.AccessWidth); 1275 1276 llvm::Type *PTy = AccessLTy->getPointerTo(addressSpace); 1277 Ptr = Builder.CreateBitCast(Ptr, PTy); 1278 1279 // Extract the piece of the bit-field value to write in this access, limited 1280 // to the values that are part of this access. 1281 llvm::Value *Val = SrcVal; 1282 if (AI.TargetBitOffset) 1283 Val = Builder.CreateLShr(Val, AI.TargetBitOffset); 1284 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits, 1285 AI.TargetBitWidth)); 1286 1287 // Extend or truncate to the access size. 1288 if (ResSizeInBits < AI.AccessWidth) 1289 Val = Builder.CreateZExt(Val, AccessLTy); 1290 else if (ResSizeInBits > AI.AccessWidth) 1291 Val = Builder.CreateTrunc(Val, AccessLTy); 1292 1293 // Shift into the position in memory. 1294 if (AI.FieldBitStart) 1295 Val = Builder.CreateShl(Val, AI.FieldBitStart); 1296 1297 // If necessary, load and OR in bits that are outside of the bit-field. 1298 if (AI.TargetBitWidth != AI.AccessWidth) { 1299 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified()); 1300 if (!AI.AccessAlignment.isZero()) 1301 Load->setAlignment(AI.AccessAlignment.getQuantity()); 1302 1303 // Compute the mask for zeroing the bits that are part of the bit-field. 1304 llvm::APInt InvMask = 1305 ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart, 1306 AI.FieldBitStart + AI.TargetBitWidth); 1307 1308 // Apply the mask and OR in to the value to write. 1309 Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val); 1310 } 1311 1312 // Write the value. 1313 llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr, 1314 Dst.isVolatileQualified()); 1315 if (!AI.AccessAlignment.isZero()) 1316 Store->setAlignment(AI.AccessAlignment.getQuantity()); 1317 } 1318 } 1319 1320 void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, 1321 LValue Dst) { 1322 // This access turns into a read/modify/write of the vector. Load the input 1323 // value now. 1324 llvm::LoadInst *Load = Builder.CreateLoad(Dst.getExtVectorAddr(), 1325 Dst.isVolatileQualified()); 1326 Load->setAlignment(Dst.getAlignment().getQuantity()); 1327 llvm::Value *Vec = Load; 1328 const llvm::Constant *Elts = Dst.getExtVectorElts(); 1329 1330 llvm::Value *SrcVal = Src.getScalarVal(); 1331 1332 if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) { 1333 unsigned NumSrcElts = VTy->getNumElements(); 1334 unsigned NumDstElts = 1335 cast<llvm::VectorType>(Vec->getType())->getNumElements(); 1336 if (NumDstElts == NumSrcElts) { 1337 // Use shuffle vector is the src and destination are the same number of 1338 // elements and restore the vector mask since it is on the side it will be 1339 // stored. 1340 SmallVector<llvm::Constant*, 4> Mask(NumDstElts); 1341 for (unsigned i = 0; i != NumSrcElts; ++i) 1342 Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i); 1343 1344 llvm::Value *MaskV = llvm::ConstantVector::get(Mask); 1345 Vec = Builder.CreateShuffleVector(SrcVal, 1346 llvm::UndefValue::get(Vec->getType()), 1347 MaskV); 1348 } else if (NumDstElts > NumSrcElts) { 1349 // Extended the source vector to the same length and then shuffle it 1350 // into the destination. 1351 // FIXME: since we're shuffling with undef, can we just use the indices 1352 // into that? This could be simpler. 1353 SmallVector<llvm::Constant*, 4> ExtMask; 1354 for (unsigned i = 0; i != NumSrcElts; ++i) 1355 ExtMask.push_back(Builder.getInt32(i)); 1356 ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty)); 1357 llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask); 1358 llvm::Value *ExtSrcVal = 1359 Builder.CreateShuffleVector(SrcVal, 1360 llvm::UndefValue::get(SrcVal->getType()), 1361 ExtMaskV); 1362 // build identity 1363 SmallVector<llvm::Constant*, 4> Mask; 1364 for (unsigned i = 0; i != NumDstElts; ++i) 1365 Mask.push_back(Builder.getInt32(i)); 1366 1367 // modify when what gets shuffled in 1368 for (unsigned i = 0; i != NumSrcElts; ++i) 1369 Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts); 1370 llvm::Value *MaskV = llvm::ConstantVector::get(Mask); 1371 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV); 1372 } else { 1373 // We should never shorten the vector 1374 llvm_unreachable("unexpected shorten vector length"); 1375 } 1376 } else { 1377 // If the Src is a scalar (not a vector) it must be updating one element. 1378 unsigned InIdx = getAccessedFieldNo(0, Elts); 1379 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx); 1380 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt); 1381 } 1382 1383 llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getExtVectorAddr(), 1384 Dst.isVolatileQualified()); 1385 Store->setAlignment(Dst.getAlignment().getQuantity()); 1386 } 1387 1388 // setObjCGCLValueClass - sets class of he lvalue for the purpose of 1389 // generating write-barries API. It is currently a global, ivar, 1390 // or neither. 1391 static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, 1392 LValue &LV, 1393 bool IsMemberAccess=false) { 1394 if (Ctx.getLangOpts().getGC() == LangOptions::NonGC) 1395 return; 1396 1397 if (isa<ObjCIvarRefExpr>(E)) { 1398 QualType ExpTy = E->getType(); 1399 if (IsMemberAccess && ExpTy->isPointerType()) { 1400 // If ivar is a structure pointer, assigning to field of 1401 // this struct follows gcc's behavior and makes it a non-ivar 1402 // writer-barrier conservatively. 1403 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); 1404 if (ExpTy->isRecordType()) { 1405 LV.setObjCIvar(false); 1406 return; 1407 } 1408 } 1409 LV.setObjCIvar(true); 1410 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E)); 1411 LV.setBaseIvarExp(Exp->getBase()); 1412 LV.setObjCArray(E->getType()->isArrayType()); 1413 return; 1414 } 1415 1416 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) { 1417 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) { 1418 if (VD->hasGlobalStorage()) { 1419 LV.setGlobalObjCRef(true); 1420 LV.setThreadLocalRef(VD->isThreadSpecified()); 1421 } 1422 } 1423 LV.setObjCArray(E->getType()->isArrayType()); 1424 return; 1425 } 1426 1427 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) { 1428 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 1429 return; 1430 } 1431 1432 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) { 1433 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 1434 if (LV.isObjCIvar()) { 1435 // If cast is to a structure pointer, follow gcc's behavior and make it 1436 // a non-ivar write-barrier. 1437 QualType ExpTy = E->getType(); 1438 if (ExpTy->isPointerType()) 1439 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); 1440 if (ExpTy->isRecordType()) 1441 LV.setObjCIvar(false); 1442 } 1443 return; 1444 } 1445 1446 if (const GenericSelectionExpr *Exp = dyn_cast<GenericSelectionExpr>(E)) { 1447 setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV); 1448 return; 1449 } 1450 1451 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) { 1452 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 1453 return; 1454 } 1455 1456 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) { 1457 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 1458 return; 1459 } 1460 1461 if (const ObjCBridgedCastExpr *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) { 1462 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 1463 return; 1464 } 1465 1466 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) { 1467 setObjCGCLValueClass(Ctx, Exp->getBase(), LV); 1468 if (LV.isObjCIvar() && !LV.isObjCArray()) 1469 // Using array syntax to assigning to what an ivar points to is not 1470 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0; 1471 LV.setObjCIvar(false); 1472 else if (LV.isGlobalObjCRef() && !LV.isObjCArray()) 1473 // Using array syntax to assigning to what global points to is not 1474 // same as assigning to the global itself. {id *G;} G[i] = 0; 1475 LV.setGlobalObjCRef(false); 1476 return; 1477 } 1478 1479 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) { 1480 setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true); 1481 // We don't know if member is an 'ivar', but this flag is looked at 1482 // only in the context of LV.isObjCIvar(). 1483 LV.setObjCArray(E->getType()->isArrayType()); 1484 return; 1485 } 1486 } 1487 1488 static llvm::Value * 1489 EmitBitCastOfLValueToProperType(CodeGenFunction &CGF, 1490 llvm::Value *V, llvm::Type *IRType, 1491 StringRef Name = StringRef()) { 1492 unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace(); 1493 return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name); 1494 } 1495 1496 static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, 1497 const Expr *E, const VarDecl *VD) { 1498 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) && 1499 "Var decl must have external storage or be a file var decl!"); 1500 1501 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD); 1502 llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType()); 1503 V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy); 1504 CharUnits Alignment = CGF.getContext().getDeclAlign(VD); 1505 QualType T = E->getType(); 1506 LValue LV; 1507 if (VD->getType()->isReferenceType()) { 1508 llvm::LoadInst *LI = CGF.Builder.CreateLoad(V); 1509 LI->setAlignment(Alignment.getQuantity()); 1510 V = LI; 1511 LV = CGF.MakeNaturalAlignAddrLValue(V, T); 1512 } else { 1513 LV = CGF.MakeAddrLValue(V, E->getType(), Alignment); 1514 } 1515 setObjCGCLValueClass(CGF.getContext(), E, LV); 1516 return LV; 1517 } 1518 1519 static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, 1520 const Expr *E, const FunctionDecl *FD) { 1521 llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD); 1522 if (!FD->hasPrototype()) { 1523 if (const FunctionProtoType *Proto = 1524 FD->getType()->getAs<FunctionProtoType>()) { 1525 // Ugly case: for a K&R-style definition, the type of the definition 1526 // isn't the same as the type of a use. Correct for this with a 1527 // bitcast. 1528 QualType NoProtoType = 1529 CGF.getContext().getFunctionNoProtoType(Proto->getResultType()); 1530 NoProtoType = CGF.getContext().getPointerType(NoProtoType); 1531 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType)); 1532 } 1533 } 1534 CharUnits Alignment = CGF.getContext().getDeclAlign(FD); 1535 return CGF.MakeAddrLValue(V, E->getType(), Alignment); 1536 } 1537 1538 LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { 1539 const NamedDecl *ND = E->getDecl(); 1540 CharUnits Alignment = getContext().getDeclAlign(ND); 1541 QualType T = E->getType(); 1542 1543 // FIXME: We should be able to assert this for FunctionDecls as well! 1544 // FIXME: We should be able to assert this for all DeclRefExprs, not just 1545 // those with a valid source location. 1546 assert((ND->isUsed(false) || !isa<VarDecl>(ND) || 1547 !E->getLocation().isValid()) && 1548 "Should not use decl without marking it used!"); 1549 1550 if (ND->hasAttr<WeakRefAttr>()) { 1551 const ValueDecl *VD = cast<ValueDecl>(ND); 1552 llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD); 1553 return MakeAddrLValue(Aliasee, E->getType(), Alignment); 1554 } 1555 1556 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { 1557 // Check if this is a global variable. 1558 if (VD->hasExternalStorage() || VD->isFileVarDecl()) 1559 return EmitGlobalVarDeclLValue(*this, E, VD); 1560 1561 bool isBlockVariable = VD->hasAttr<BlocksAttr>(); 1562 1563 bool NonGCable = VD->hasLocalStorage() && 1564 !VD->getType()->isReferenceType() && 1565 !isBlockVariable; 1566 1567 llvm::Value *V = LocalDeclMap[VD]; 1568 if (!V && VD->isStaticLocal()) 1569 V = CGM.getStaticLocalDeclAddress(VD); 1570 1571 // Use special handling for lambdas. 1572 if (!V) { 1573 if (FieldDecl *FD = LambdaCaptureFields.lookup(VD)) 1574 return EmitLValueForField(CXXABIThisValue, FD, 0); 1575 1576 assert(isa<BlockDecl>(CurCodeDecl) && E->refersToEnclosingLocal()); 1577 CharUnits alignment = getContext().getDeclAlign(VD); 1578 return MakeAddrLValue(GetAddrOfBlockDecl(VD, isBlockVariable), 1579 E->getType(), alignment); 1580 } 1581 1582 assert(V && "DeclRefExpr not entered in LocalDeclMap?"); 1583 1584 if (isBlockVariable) 1585 V = BuildBlockByrefAddress(V, VD); 1586 1587 LValue LV; 1588 if (VD->getType()->isReferenceType()) { 1589 llvm::LoadInst *LI = Builder.CreateLoad(V); 1590 LI->setAlignment(Alignment.getQuantity()); 1591 V = LI; 1592 LV = MakeNaturalAlignAddrLValue(V, T); 1593 } else { 1594 LV = MakeAddrLValue(V, T, Alignment); 1595 } 1596 1597 if (NonGCable) { 1598 LV.getQuals().removeObjCGCAttr(); 1599 LV.setNonGC(true); 1600 } 1601 setObjCGCLValueClass(getContext(), E, LV); 1602 return LV; 1603 } 1604 1605 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(ND)) 1606 return EmitFunctionDeclLValue(*this, E, fn); 1607 1608 llvm_unreachable("Unhandled DeclRefExpr"); 1609 } 1610 1611 LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { 1612 // __extension__ doesn't affect lvalue-ness. 1613 if (E->getOpcode() == UO_Extension) 1614 return EmitLValue(E->getSubExpr()); 1615 1616 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType()); 1617 switch (E->getOpcode()) { 1618 default: llvm_unreachable("Unknown unary operator lvalue!"); 1619 case UO_Deref: { 1620 QualType T = E->getSubExpr()->getType()->getPointeeType(); 1621 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type"); 1622 1623 LValue LV = MakeNaturalAlignAddrLValue(EmitScalarExpr(E->getSubExpr()), T); 1624 LV.getQuals().setAddressSpace(ExprTy.getAddressSpace()); 1625 1626 // We should not generate __weak write barrier on indirect reference 1627 // of a pointer to object; as in void foo (__weak id *param); *param = 0; 1628 // But, we continue to generate __strong write barrier on indirect write 1629 // into a pointer to object. 1630 if (getContext().getLangOpts().ObjC1 && 1631 getContext().getLangOpts().getGC() != LangOptions::NonGC && 1632 LV.isObjCWeak()) 1633 LV.setNonGC(!E->isOBJCGCCandidate(getContext())); 1634 return LV; 1635 } 1636 case UO_Real: 1637 case UO_Imag: { 1638 LValue LV = EmitLValue(E->getSubExpr()); 1639 assert(LV.isSimple() && "real/imag on non-ordinary l-value"); 1640 llvm::Value *Addr = LV.getAddress(); 1641 1642 // __real is valid on scalars. This is a faster way of testing that. 1643 // __imag can only produce an rvalue on scalars. 1644 if (E->getOpcode() == UO_Real && 1645 !cast<llvm::PointerType>(Addr->getType()) 1646 ->getElementType()->isStructTy()) { 1647 assert(E->getSubExpr()->getType()->isArithmeticType()); 1648 return LV; 1649 } 1650 1651 assert(E->getSubExpr()->getType()->isAnyComplexType()); 1652 1653 unsigned Idx = E->getOpcode() == UO_Imag; 1654 return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(), 1655 Idx, "idx"), 1656 ExprTy); 1657 } 1658 case UO_PreInc: 1659 case UO_PreDec: { 1660 LValue LV = EmitLValue(E->getSubExpr()); 1661 bool isInc = E->getOpcode() == UO_PreInc; 1662 1663 if (E->getType()->isAnyComplexType()) 1664 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/); 1665 else 1666 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/); 1667 return LV; 1668 } 1669 } 1670 } 1671 1672 LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { 1673 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E), 1674 E->getType()); 1675 } 1676 1677 LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) { 1678 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E), 1679 E->getType()); 1680 } 1681 1682 1683 LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { 1684 switch (E->getIdentType()) { 1685 default: 1686 return EmitUnsupportedLValue(E, "predefined expression"); 1687 1688 case PredefinedExpr::Func: 1689 case PredefinedExpr::Function: 1690 case PredefinedExpr::PrettyFunction: { 1691 unsigned Type = E->getIdentType(); 1692 std::string GlobalVarName; 1693 1694 switch (Type) { 1695 default: llvm_unreachable("Invalid type"); 1696 case PredefinedExpr::Func: 1697 GlobalVarName = "__func__."; 1698 break; 1699 case PredefinedExpr::Function: 1700 GlobalVarName = "__FUNCTION__."; 1701 break; 1702 case PredefinedExpr::PrettyFunction: 1703 GlobalVarName = "__PRETTY_FUNCTION__."; 1704 break; 1705 } 1706 1707 StringRef FnName = CurFn->getName(); 1708 if (FnName.startswith("\01")) 1709 FnName = FnName.substr(1); 1710 GlobalVarName += FnName; 1711 1712 const Decl *CurDecl = CurCodeDecl; 1713 if (CurDecl == 0) 1714 CurDecl = getContext().getTranslationUnitDecl(); 1715 1716 std::string FunctionName = 1717 (isa<BlockDecl>(CurDecl) 1718 ? FnName.str() 1719 : PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurDecl)); 1720 1721 llvm::Constant *C = 1722 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str()); 1723 return MakeAddrLValue(C, E->getType()); 1724 } 1725 } 1726 } 1727 1728 llvm::BasicBlock *CodeGenFunction::getTrapBB() { 1729 const CodeGenOptions &GCO = CGM.getCodeGenOpts(); 1730 1731 // If we are not optimzing, don't collapse all calls to trap in the function 1732 // to the same call, that way, in the debugger they can see which operation 1733 // did in fact fail. If we are optimizing, we collapse all calls to trap down 1734 // to just one per function to save on codesize. 1735 if (GCO.OptimizationLevel && TrapBB) 1736 return TrapBB; 1737 1738 llvm::BasicBlock *Cont = 0; 1739 if (HaveInsertPoint()) { 1740 Cont = createBasicBlock("cont"); 1741 EmitBranch(Cont); 1742 } 1743 TrapBB = createBasicBlock("trap"); 1744 EmitBlock(TrapBB); 1745 1746 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap); 1747 llvm::CallInst *TrapCall = Builder.CreateCall(F); 1748 TrapCall->setDoesNotReturn(); 1749 TrapCall->setDoesNotThrow(); 1750 Builder.CreateUnreachable(); 1751 1752 if (Cont) 1753 EmitBlock(Cont); 1754 return TrapBB; 1755 } 1756 1757 /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an 1758 /// array to pointer, return the array subexpression. 1759 static const Expr *isSimpleArrayDecayOperand(const Expr *E) { 1760 // If this isn't just an array->pointer decay, bail out. 1761 const CastExpr *CE = dyn_cast<CastExpr>(E); 1762 if (CE == 0 || CE->getCastKind() != CK_ArrayToPointerDecay) 1763 return 0; 1764 1765 // If this is a decay from variable width array, bail out. 1766 const Expr *SubExpr = CE->getSubExpr(); 1767 if (SubExpr->getType()->isVariableArrayType()) 1768 return 0; 1769 1770 return SubExpr; 1771 } 1772 1773 LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { 1774 // The index must always be an integer, which is not an aggregate. Emit it. 1775 llvm::Value *Idx = EmitScalarExpr(E->getIdx()); 1776 QualType IdxTy = E->getIdx()->getType(); 1777 bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType(); 1778 1779 // If the base is a vector type, then we are forming a vector element lvalue 1780 // with this subscript. 1781 if (E->getBase()->getType()->isVectorType()) { 1782 // Emit the vector as an lvalue to get its address. 1783 LValue LHS = EmitLValue(E->getBase()); 1784 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); 1785 Idx = Builder.CreateIntCast(Idx, Int32Ty, IdxSigned, "vidx"); 1786 return LValue::MakeVectorElt(LHS.getAddress(), Idx, 1787 E->getBase()->getType(), LHS.getAlignment()); 1788 } 1789 1790 // Extend or truncate the index type to 32 or 64-bits. 1791 if (Idx->getType() != IntPtrTy) 1792 Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom"); 1793 1794 // FIXME: As llvm implements the object size checking, this can come out. 1795 if (CatchUndefined) { 1796 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E->getBase())){ 1797 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { 1798 if (ICE->getCastKind() == CK_ArrayToPointerDecay) { 1799 if (const ConstantArrayType *CAT 1800 = getContext().getAsConstantArrayType(DRE->getType())) { 1801 llvm::APInt Size = CAT->getSize(); 1802 llvm::BasicBlock *Cont = createBasicBlock("cont"); 1803 Builder.CreateCondBr(Builder.CreateICmpULE(Idx, 1804 llvm::ConstantInt::get(Idx->getType(), Size)), 1805 Cont, getTrapBB()); 1806 EmitBlock(Cont); 1807 } 1808 } 1809 } 1810 } 1811 } 1812 1813 // We know that the pointer points to a type of the correct size, unless the 1814 // size is a VLA or Objective-C interface. 1815 llvm::Value *Address = 0; 1816 CharUnits ArrayAlignment; 1817 if (const VariableArrayType *vla = 1818 getContext().getAsVariableArrayType(E->getType())) { 1819 // The base must be a pointer, which is not an aggregate. Emit 1820 // it. It needs to be emitted first in case it's what captures 1821 // the VLA bounds. 1822 Address = EmitScalarExpr(E->getBase()); 1823 1824 // The element count here is the total number of non-VLA elements. 1825 llvm::Value *numElements = getVLASize(vla).first; 1826 1827 // Effectively, the multiply by the VLA size is part of the GEP. 1828 // GEP indexes are signed, and scaling an index isn't permitted to 1829 // signed-overflow, so we use the same semantics for our explicit 1830 // multiply. We suppress this if overflow is not undefined behavior. 1831 if (getLangOpts().isSignedOverflowDefined()) { 1832 Idx = Builder.CreateMul(Idx, numElements); 1833 Address = Builder.CreateGEP(Address, Idx, "arrayidx"); 1834 } else { 1835 Idx = Builder.CreateNSWMul(Idx, numElements); 1836 Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx"); 1837 } 1838 } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){ 1839 // Indexing over an interface, as in "NSString *P; P[4];" 1840 llvm::Value *InterfaceSize = 1841 llvm::ConstantInt::get(Idx->getType(), 1842 getContext().getTypeSizeInChars(OIT).getQuantity()); 1843 1844 Idx = Builder.CreateMul(Idx, InterfaceSize); 1845 1846 // The base must be a pointer, which is not an aggregate. Emit it. 1847 llvm::Value *Base = EmitScalarExpr(E->getBase()); 1848 Address = EmitCastToVoidPtr(Base); 1849 Address = Builder.CreateGEP(Address, Idx, "arrayidx"); 1850 Address = Builder.CreateBitCast(Address, Base->getType()); 1851 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) { 1852 // If this is A[i] where A is an array, the frontend will have decayed the 1853 // base to be a ArrayToPointerDecay implicit cast. While correct, it is 1854 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a 1855 // "gep x, i" here. Emit one "gep A, 0, i". 1856 assert(Array->getType()->isArrayType() && 1857 "Array to pointer decay must have array source type!"); 1858 LValue ArrayLV = EmitLValue(Array); 1859 llvm::Value *ArrayPtr = ArrayLV.getAddress(); 1860 llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0); 1861 llvm::Value *Args[] = { Zero, Idx }; 1862 1863 // Propagate the alignment from the array itself to the result. 1864 ArrayAlignment = ArrayLV.getAlignment(); 1865 1866 if (getContext().getLangOpts().isSignedOverflowDefined()) 1867 Address = Builder.CreateGEP(ArrayPtr, Args, "arrayidx"); 1868 else 1869 Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, "arrayidx"); 1870 } else { 1871 // The base must be a pointer, which is not an aggregate. Emit it. 1872 llvm::Value *Base = EmitScalarExpr(E->getBase()); 1873 if (getContext().getLangOpts().isSignedOverflowDefined()) 1874 Address = Builder.CreateGEP(Base, Idx, "arrayidx"); 1875 else 1876 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx"); 1877 } 1878 1879 QualType T = E->getBase()->getType()->getPointeeType(); 1880 assert(!T.isNull() && 1881 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type"); 1882 1883 1884 // Limit the alignment to that of the result type. 1885 LValue LV; 1886 if (!ArrayAlignment.isZero()) { 1887 CharUnits Align = getContext().getTypeAlignInChars(T); 1888 ArrayAlignment = std::min(Align, ArrayAlignment); 1889 LV = MakeAddrLValue(Address, T, ArrayAlignment); 1890 } else { 1891 LV = MakeNaturalAlignAddrLValue(Address, T); 1892 } 1893 1894 LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace()); 1895 1896 if (getContext().getLangOpts().ObjC1 && 1897 getContext().getLangOpts().getGC() != LangOptions::NonGC) { 1898 LV.setNonGC(!E->isOBJCGCCandidate(getContext())); 1899 setObjCGCLValueClass(getContext(), E, LV); 1900 } 1901 return LV; 1902 } 1903 1904 static 1905 llvm::Constant *GenerateConstantVector(CGBuilderTy &Builder, 1906 SmallVector<unsigned, 4> &Elts) { 1907 SmallVector<llvm::Constant*, 4> CElts; 1908 for (unsigned i = 0, e = Elts.size(); i != e; ++i) 1909 CElts.push_back(Builder.getInt32(Elts[i])); 1910 1911 return llvm::ConstantVector::get(CElts); 1912 } 1913 1914 LValue CodeGenFunction:: 1915 EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { 1916 // Emit the base vector as an l-value. 1917 LValue Base; 1918 1919 // ExtVectorElementExpr's base can either be a vector or pointer to vector. 1920 if (E->isArrow()) { 1921 // If it is a pointer to a vector, emit the address and form an lvalue with 1922 // it. 1923 llvm::Value *Ptr = EmitScalarExpr(E->getBase()); 1924 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>(); 1925 Base = MakeAddrLValue(Ptr, PT->getPointeeType()); 1926 Base.getQuals().removeObjCGCAttr(); 1927 } else if (E->getBase()->isGLValue()) { 1928 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x), 1929 // emit the base as an lvalue. 1930 assert(E->getBase()->getType()->isVectorType()); 1931 Base = EmitLValue(E->getBase()); 1932 } else { 1933 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such. 1934 assert(E->getBase()->getType()->isVectorType() && 1935 "Result must be a vector"); 1936 llvm::Value *Vec = EmitScalarExpr(E->getBase()); 1937 1938 // Store the vector to memory (because LValue wants an address). 1939 llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType()); 1940 Builder.CreateStore(Vec, VecMem); 1941 Base = MakeAddrLValue(VecMem, E->getBase()->getType()); 1942 } 1943 1944 QualType type = 1945 E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers()); 1946 1947 // Encode the element access list into a vector of unsigned indices. 1948 SmallVector<unsigned, 4> Indices; 1949 E->getEncodedElementAccess(Indices); 1950 1951 if (Base.isSimple()) { 1952 llvm::Constant *CV = GenerateConstantVector(Builder, Indices); 1953 return LValue::MakeExtVectorElt(Base.getAddress(), CV, type, 1954 Base.getAlignment()); 1955 } 1956 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); 1957 1958 llvm::Constant *BaseElts = Base.getExtVectorElts(); 1959 SmallVector<llvm::Constant *, 4> CElts; 1960 1961 for (unsigned i = 0, e = Indices.size(); i != e; ++i) 1962 CElts.push_back(BaseElts->getAggregateElement(Indices[i])); 1963 llvm::Constant *CV = llvm::ConstantVector::get(CElts); 1964 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, type, 1965 Base.getAlignment()); 1966 } 1967 1968 LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { 1969 bool isNonGC = false; 1970 Expr *BaseExpr = E->getBase(); 1971 llvm::Value *BaseValue = NULL; 1972 Qualifiers BaseQuals; 1973 1974 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. 1975 if (E->isArrow()) { 1976 BaseValue = EmitScalarExpr(BaseExpr); 1977 const PointerType *PTy = 1978 BaseExpr->getType()->getAs<PointerType>(); 1979 BaseQuals = PTy->getPointeeType().getQualifiers(); 1980 } else { 1981 LValue BaseLV = EmitLValue(BaseExpr); 1982 if (BaseLV.isNonGC()) 1983 isNonGC = true; 1984 // FIXME: this isn't right for bitfields. 1985 BaseValue = BaseLV.getAddress(); 1986 QualType BaseTy = BaseExpr->getType(); 1987 BaseQuals = BaseTy.getQualifiers(); 1988 } 1989 1990 NamedDecl *ND = E->getMemberDecl(); 1991 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) { 1992 LValue LV = EmitLValueForField(BaseValue, Field, 1993 BaseQuals.getCVRQualifiers()); 1994 LV.setNonGC(isNonGC); 1995 setObjCGCLValueClass(getContext(), E, LV); 1996 return LV; 1997 } 1998 1999 if (VarDecl *VD = dyn_cast<VarDecl>(ND)) 2000 return EmitGlobalVarDeclLValue(*this, E, VD); 2001 2002 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) 2003 return EmitFunctionDeclLValue(*this, E, FD); 2004 2005 llvm_unreachable("Unhandled member declaration!"); 2006 } 2007 2008 LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value *BaseValue, 2009 const FieldDecl *Field, 2010 unsigned CVRQualifiers) { 2011 const CGRecordLayout &RL = 2012 CGM.getTypes().getCGRecordLayout(Field->getParent()); 2013 const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field); 2014 return LValue::MakeBitfield(BaseValue, Info, 2015 Field->getType().withCVRQualifiers(CVRQualifiers)); 2016 } 2017 2018 /// EmitLValueForAnonRecordField - Given that the field is a member of 2019 /// an anonymous struct or union buried inside a record, and given 2020 /// that the base value is a pointer to the enclosing record, derive 2021 /// an lvalue for the ultimate field. 2022 LValue CodeGenFunction::EmitLValueForAnonRecordField(llvm::Value *BaseValue, 2023 const IndirectFieldDecl *Field, 2024 unsigned CVRQualifiers) { 2025 IndirectFieldDecl::chain_iterator I = Field->chain_begin(), 2026 IEnd = Field->chain_end(); 2027 while (true) { 2028 LValue LV = EmitLValueForField(BaseValue, cast<FieldDecl>(*I), 2029 CVRQualifiers); 2030 if (++I == IEnd) return LV; 2031 2032 assert(LV.isSimple()); 2033 BaseValue = LV.getAddress(); 2034 CVRQualifiers |= LV.getVRQualifiers(); 2035 } 2036 } 2037 2038 LValue CodeGenFunction::EmitLValueForField(llvm::Value *baseAddr, 2039 const FieldDecl *field, 2040 unsigned cvr) { 2041 if (field->isBitField()) 2042 return EmitLValueForBitfield(baseAddr, field, cvr); 2043 2044 const RecordDecl *rec = field->getParent(); 2045 QualType type = field->getType(); 2046 CharUnits alignment = getContext().getDeclAlign(field); 2047 2048 bool mayAlias = rec->hasAttr<MayAliasAttr>(); 2049 2050 llvm::Value *addr = baseAddr; 2051 if (rec->isUnion()) { 2052 // For unions, there is no pointer adjustment. 2053 assert(!type->isReferenceType() && "union has reference member"); 2054 } else { 2055 // For structs, we GEP to the field that the record layout suggests. 2056 unsigned idx = CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field); 2057 addr = Builder.CreateStructGEP(addr, idx, field->getName()); 2058 2059 // If this is a reference field, load the reference right now. 2060 if (const ReferenceType *refType = type->getAs<ReferenceType>()) { 2061 llvm::LoadInst *load = Builder.CreateLoad(addr, "ref"); 2062 if (cvr & Qualifiers::Volatile) load->setVolatile(true); 2063 load->setAlignment(alignment.getQuantity()); 2064 2065 if (CGM.shouldUseTBAA()) { 2066 llvm::MDNode *tbaa; 2067 if (mayAlias) 2068 tbaa = CGM.getTBAAInfo(getContext().CharTy); 2069 else 2070 tbaa = CGM.getTBAAInfo(type); 2071 CGM.DecorateInstruction(load, tbaa); 2072 } 2073 2074 addr = load; 2075 mayAlias = false; 2076 type = refType->getPointeeType(); 2077 if (type->isIncompleteType()) 2078 alignment = CharUnits(); 2079 else 2080 alignment = getContext().getTypeAlignInChars(type); 2081 cvr = 0; // qualifiers don't recursively apply to referencee 2082 } 2083 } 2084 2085 // Make sure that the address is pointing to the right type. This is critical 2086 // for both unions and structs. A union needs a bitcast, a struct element 2087 // will need a bitcast if the LLVM type laid out doesn't match the desired 2088 // type. 2089 addr = EmitBitCastOfLValueToProperType(*this, addr, 2090 CGM.getTypes().ConvertTypeForMem(type), 2091 field->getName()); 2092 2093 if (field->hasAttr<AnnotateAttr>()) 2094 addr = EmitFieldAnnotations(field, addr); 2095 2096 LValue LV = MakeAddrLValue(addr, type, alignment); 2097 LV.getQuals().addCVRQualifiers(cvr); 2098 2099 // __weak attribute on a field is ignored. 2100 if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak) 2101 LV.getQuals().removeObjCGCAttr(); 2102 2103 // Fields of may_alias structs act like 'char' for TBAA purposes. 2104 // FIXME: this should get propagated down through anonymous structs 2105 // and unions. 2106 if (mayAlias && LV.getTBAAInfo()) 2107 LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy)); 2108 2109 return LV; 2110 } 2111 2112 LValue 2113 CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value *BaseValue, 2114 const FieldDecl *Field, 2115 unsigned CVRQualifiers) { 2116 QualType FieldType = Field->getType(); 2117 2118 if (!FieldType->isReferenceType()) 2119 return EmitLValueForField(BaseValue, Field, CVRQualifiers); 2120 2121 const CGRecordLayout &RL = 2122 CGM.getTypes().getCGRecordLayout(Field->getParent()); 2123 unsigned idx = RL.getLLVMFieldNo(Field); 2124 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx); 2125 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs"); 2126 2127 2128 // Make sure that the address is pointing to the right type. This is critical 2129 // for both unions and structs. A union needs a bitcast, a struct element 2130 // will need a bitcast if the LLVM type laid out doesn't match the desired 2131 // type. 2132 llvm::Type *llvmType = ConvertTypeForMem(FieldType); 2133 unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace(); 2134 V = Builder.CreateBitCast(V, llvmType->getPointerTo(AS)); 2135 2136 CharUnits Alignment = getContext().getDeclAlign(Field); 2137 return MakeAddrLValue(V, FieldType, Alignment); 2138 } 2139 2140 LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){ 2141 if (E->isFileScope()) { 2142 llvm::Value *GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E); 2143 return MakeAddrLValue(GlobalPtr, E->getType()); 2144 } 2145 2146 llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral"); 2147 const Expr *InitExpr = E->getInitializer(); 2148 LValue Result = MakeAddrLValue(DeclPtr, E->getType()); 2149 2150 EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(), 2151 /*Init*/ true); 2152 2153 return Result; 2154 } 2155 2156 LValue CodeGenFunction:: 2157 EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) { 2158 if (!expr->isGLValue()) { 2159 // ?: here should be an aggregate. 2160 assert((hasAggregateLLVMType(expr->getType()) && 2161 !expr->getType()->isAnyComplexType()) && 2162 "Unexpected conditional operator!"); 2163 return EmitAggExprToLValue(expr); 2164 } 2165 2166 OpaqueValueMapping binding(*this, expr); 2167 2168 const Expr *condExpr = expr->getCond(); 2169 bool CondExprBool; 2170 if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) { 2171 const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr(); 2172 if (!CondExprBool) std::swap(live, dead); 2173 2174 if (!ContainsLabel(dead)) 2175 return EmitLValue(live); 2176 } 2177 2178 llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true"); 2179 llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false"); 2180 llvm::BasicBlock *contBlock = createBasicBlock("cond.end"); 2181 2182 ConditionalEvaluation eval(*this); 2183 EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock); 2184 2185 // Any temporaries created here are conditional. 2186 EmitBlock(lhsBlock); 2187 eval.begin(*this); 2188 LValue lhs = EmitLValue(expr->getTrueExpr()); 2189 eval.end(*this); 2190 2191 if (!lhs.isSimple()) 2192 return EmitUnsupportedLValue(expr, "conditional operator"); 2193 2194 lhsBlock = Builder.GetInsertBlock(); 2195 Builder.CreateBr(contBlock); 2196 2197 // Any temporaries created here are conditional. 2198 EmitBlock(rhsBlock); 2199 eval.begin(*this); 2200 LValue rhs = EmitLValue(expr->getFalseExpr()); 2201 eval.end(*this); 2202 if (!rhs.isSimple()) 2203 return EmitUnsupportedLValue(expr, "conditional operator"); 2204 rhsBlock = Builder.GetInsertBlock(); 2205 2206 EmitBlock(contBlock); 2207 2208 llvm::PHINode *phi = Builder.CreatePHI(lhs.getAddress()->getType(), 2, 2209 "cond-lvalue"); 2210 phi->addIncoming(lhs.getAddress(), lhsBlock); 2211 phi->addIncoming(rhs.getAddress(), rhsBlock); 2212 return MakeAddrLValue(phi, expr->getType()); 2213 } 2214 2215 /// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast. 2216 /// If the cast is a dynamic_cast, we can have the usual lvalue result, 2217 /// otherwise if a cast is needed by the code generator in an lvalue context, 2218 /// then it must mean that we need the address of an aggregate in order to 2219 /// access one of its fields. This can happen for all the reasons that casts 2220 /// are permitted with aggregate result, including noop aggregate casts, and 2221 /// cast from scalar to union. 2222 LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { 2223 switch (E->getCastKind()) { 2224 case CK_ToVoid: 2225 return EmitUnsupportedLValue(E, "unexpected cast lvalue"); 2226 2227 case CK_Dependent: 2228 llvm_unreachable("dependent cast kind in IR gen!"); 2229 2230 // These two casts are currently treated as no-ops, although they could 2231 // potentially be real operations depending on the target's ABI. 2232 case CK_NonAtomicToAtomic: 2233 case CK_AtomicToNonAtomic: 2234 2235 case CK_NoOp: 2236 case CK_LValueToRValue: 2237 if (!E->getSubExpr()->Classify(getContext()).isPRValue() 2238 || E->getType()->isRecordType()) 2239 return EmitLValue(E->getSubExpr()); 2240 // Fall through to synthesize a temporary. 2241 2242 case CK_BitCast: 2243 case CK_ArrayToPointerDecay: 2244 case CK_FunctionToPointerDecay: 2245 case CK_NullToMemberPointer: 2246 case CK_NullToPointer: 2247 case CK_IntegralToPointer: 2248 case CK_PointerToIntegral: 2249 case CK_PointerToBoolean: 2250 case CK_VectorSplat: 2251 case CK_IntegralCast: 2252 case CK_IntegralToBoolean: 2253 case CK_IntegralToFloating: 2254 case CK_FloatingToIntegral: 2255 case CK_FloatingToBoolean: 2256 case CK_FloatingCast: 2257 case CK_FloatingRealToComplex: 2258 case CK_FloatingComplexToReal: 2259 case CK_FloatingComplexToBoolean: 2260 case CK_FloatingComplexCast: 2261 case CK_FloatingComplexToIntegralComplex: 2262 case CK_IntegralRealToComplex: 2263 case CK_IntegralComplexToReal: 2264 case CK_IntegralComplexToBoolean: 2265 case CK_IntegralComplexCast: 2266 case CK_IntegralComplexToFloatingComplex: 2267 case CK_DerivedToBaseMemberPointer: 2268 case CK_BaseToDerivedMemberPointer: 2269 case CK_MemberPointerToBoolean: 2270 case CK_ReinterpretMemberPointer: 2271 case CK_AnyPointerToBlockPointerCast: 2272 case CK_ARCProduceObject: 2273 case CK_ARCConsumeObject: 2274 case CK_ARCReclaimReturnedObject: 2275 case CK_ARCExtendBlockObject: 2276 case CK_CopyAndAutoreleaseBlockObject: { 2277 // These casts only produce lvalues when we're binding a reference to a 2278 // temporary realized from a (converted) pure rvalue. Emit the expression 2279 // as a value, copy it into a temporary, and return an lvalue referring to 2280 // that temporary. 2281 llvm::Value *V = CreateMemTemp(E->getType(), "ref.temp"); 2282 EmitAnyExprToMem(E, V, E->getType().getQualifiers(), false); 2283 return MakeAddrLValue(V, E->getType()); 2284 } 2285 2286 case CK_Dynamic: { 2287 LValue LV = EmitLValue(E->getSubExpr()); 2288 llvm::Value *V = LV.getAddress(); 2289 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E); 2290 return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType()); 2291 } 2292 2293 case CK_ConstructorConversion: 2294 case CK_UserDefinedConversion: 2295 case CK_CPointerToObjCPointerCast: 2296 case CK_BlockPointerToObjCPointerCast: 2297 return EmitLValue(E->getSubExpr()); 2298 2299 case CK_UncheckedDerivedToBase: 2300 case CK_DerivedToBase: { 2301 const RecordType *DerivedClassTy = 2302 E->getSubExpr()->getType()->getAs<RecordType>(); 2303 CXXRecordDecl *DerivedClassDecl = 2304 cast<CXXRecordDecl>(DerivedClassTy->getDecl()); 2305 2306 LValue LV = EmitLValue(E->getSubExpr()); 2307 llvm::Value *This = LV.getAddress(); 2308 2309 // Perform the derived-to-base conversion 2310 llvm::Value *Base = 2311 GetAddressOfBaseClass(This, DerivedClassDecl, 2312 E->path_begin(), E->path_end(), 2313 /*NullCheckValue=*/false); 2314 2315 return MakeAddrLValue(Base, E->getType()); 2316 } 2317 case CK_ToUnion: 2318 return EmitAggExprToLValue(E); 2319 case CK_BaseToDerived: { 2320 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>(); 2321 CXXRecordDecl *DerivedClassDecl = 2322 cast<CXXRecordDecl>(DerivedClassTy->getDecl()); 2323 2324 LValue LV = EmitLValue(E->getSubExpr()); 2325 2326 // Perform the base-to-derived conversion 2327 llvm::Value *Derived = 2328 GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl, 2329 E->path_begin(), E->path_end(), 2330 /*NullCheckValue=*/false); 2331 2332 return MakeAddrLValue(Derived, E->getType()); 2333 } 2334 case CK_LValueBitCast: { 2335 // This must be a reinterpret_cast (or c-style equivalent). 2336 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E); 2337 2338 LValue LV = EmitLValue(E->getSubExpr()); 2339 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(), 2340 ConvertType(CE->getTypeAsWritten())); 2341 return MakeAddrLValue(V, E->getType()); 2342 } 2343 case CK_ObjCObjectLValueCast: { 2344 LValue LV = EmitLValue(E->getSubExpr()); 2345 QualType ToType = getContext().getLValueReferenceType(E->getType()); 2346 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(), 2347 ConvertType(ToType)); 2348 return MakeAddrLValue(V, E->getType()); 2349 } 2350 } 2351 2352 llvm_unreachable("Unhandled lvalue cast kind?"); 2353 } 2354 2355 LValue CodeGenFunction::EmitNullInitializationLValue( 2356 const CXXScalarValueInitExpr *E) { 2357 QualType Ty = E->getType(); 2358 LValue LV = MakeAddrLValue(CreateMemTemp(Ty), Ty); 2359 EmitNullInitialization(LV.getAddress(), Ty); 2360 return LV; 2361 } 2362 2363 LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) { 2364 assert(OpaqueValueMappingData::shouldBindAsLValue(e)); 2365 return getOpaqueLValueMapping(e); 2366 } 2367 2368 LValue CodeGenFunction::EmitMaterializeTemporaryExpr( 2369 const MaterializeTemporaryExpr *E) { 2370 RValue RV = EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); 2371 return MakeAddrLValue(RV.getScalarVal(), E->getType()); 2372 } 2373 2374 RValue CodeGenFunction::EmitRValueForField(llvm::Value *Addr, 2375 const FieldDecl *FD) { 2376 QualType FT = FD->getType(); 2377 // FIXME: What are the right qualifiers here? 2378 LValue LV = EmitLValueForField(Addr, FD, 0); 2379 if (FT->isAnyComplexType()) 2380 // FIXME: Volatile? 2381 return RValue::getComplex(LoadComplexFromAddr(LV.getAddress(), false)); 2382 else if (CodeGenFunction::hasAggregateLLVMType(FT)) 2383 return LV.asAggregateRValue(); 2384 2385 return EmitLoadOfLValue(LV); 2386 } 2387 2388 //===--------------------------------------------------------------------===// 2389 // Expression Emission 2390 //===--------------------------------------------------------------------===// 2391 2392 RValue CodeGenFunction::EmitCallExpr(const CallExpr *E, 2393 ReturnValueSlot ReturnValue) { 2394 if (CGDebugInfo *DI = getDebugInfo()) 2395 DI->EmitLocation(Builder, E->getLocStart()); 2396 2397 // Builtins never have block type. 2398 if (E->getCallee()->getType()->isBlockPointerType()) 2399 return EmitBlockCallExpr(E, ReturnValue); 2400 2401 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E)) 2402 return EmitCXXMemberCallExpr(CE, ReturnValue); 2403 2404 if (const CUDAKernelCallExpr *CE = dyn_cast<CUDAKernelCallExpr>(E)) 2405 return EmitCUDAKernelCallExpr(CE, ReturnValue); 2406 2407 const Decl *TargetDecl = E->getCalleeDecl(); 2408 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) { 2409 if (unsigned builtinID = FD->getBuiltinID()) 2410 return EmitBuiltinExpr(FD, builtinID, E); 2411 } 2412 2413 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E)) 2414 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl)) 2415 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue); 2416 2417 if (const CXXPseudoDestructorExpr *PseudoDtor 2418 = dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) { 2419 QualType DestroyedType = PseudoDtor->getDestroyedType(); 2420 if (getContext().getLangOpts().ObjCAutoRefCount && 2421 DestroyedType->isObjCLifetimeType() && 2422 (DestroyedType.getObjCLifetime() == Qualifiers::OCL_Strong || 2423 DestroyedType.getObjCLifetime() == Qualifiers::OCL_Weak)) { 2424 // Automatic Reference Counting: 2425 // If the pseudo-expression names a retainable object with weak or 2426 // strong lifetime, the object shall be released. 2427 Expr *BaseExpr = PseudoDtor->getBase(); 2428 llvm::Value *BaseValue = NULL; 2429 Qualifiers BaseQuals; 2430 2431 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. 2432 if (PseudoDtor->isArrow()) { 2433 BaseValue = EmitScalarExpr(BaseExpr); 2434 const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>(); 2435 BaseQuals = PTy->getPointeeType().getQualifiers(); 2436 } else { 2437 LValue BaseLV = EmitLValue(BaseExpr); 2438 BaseValue = BaseLV.getAddress(); 2439 QualType BaseTy = BaseExpr->getType(); 2440 BaseQuals = BaseTy.getQualifiers(); 2441 } 2442 2443 switch (PseudoDtor->getDestroyedType().getObjCLifetime()) { 2444 case Qualifiers::OCL_None: 2445 case Qualifiers::OCL_ExplicitNone: 2446 case Qualifiers::OCL_Autoreleasing: 2447 break; 2448 2449 case Qualifiers::OCL_Strong: 2450 EmitARCRelease(Builder.CreateLoad(BaseValue, 2451 PseudoDtor->getDestroyedType().isVolatileQualified()), 2452 /*precise*/ true); 2453 break; 2454 2455 case Qualifiers::OCL_Weak: 2456 EmitARCDestroyWeak(BaseValue); 2457 break; 2458 } 2459 } else { 2460 // C++ [expr.pseudo]p1: 2461 // The result shall only be used as the operand for the function call 2462 // operator (), and the result of such a call has type void. The only 2463 // effect is the evaluation of the postfix-expression before the dot or 2464 // arrow. 2465 EmitScalarExpr(E->getCallee()); 2466 } 2467 2468 return RValue::get(0); 2469 } 2470 2471 llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 2472 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue, 2473 E->arg_begin(), E->arg_end(), TargetDecl); 2474 } 2475 2476 LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) { 2477 // Comma expressions just emit their LHS then their RHS as an l-value. 2478 if (E->getOpcode() == BO_Comma) { 2479 EmitIgnoredExpr(E->getLHS()); 2480 EnsureInsertPoint(); 2481 return EmitLValue(E->getRHS()); 2482 } 2483 2484 if (E->getOpcode() == BO_PtrMemD || 2485 E->getOpcode() == BO_PtrMemI) 2486 return EmitPointerToDataMemberBinaryExpr(E); 2487 2488 assert(E->getOpcode() == BO_Assign && "unexpected binary l-value"); 2489 2490 // Note that in all of these cases, __block variables need the RHS 2491 // evaluated first just in case the variable gets moved by the RHS. 2492 2493 if (!hasAggregateLLVMType(E->getType())) { 2494 switch (E->getLHS()->getType().getObjCLifetime()) { 2495 case Qualifiers::OCL_Strong: 2496 return EmitARCStoreStrong(E, /*ignored*/ false).first; 2497 2498 case Qualifiers::OCL_Autoreleasing: 2499 return EmitARCStoreAutoreleasing(E).first; 2500 2501 // No reason to do any of these differently. 2502 case Qualifiers::OCL_None: 2503 case Qualifiers::OCL_ExplicitNone: 2504 case Qualifiers::OCL_Weak: 2505 break; 2506 } 2507 2508 RValue RV = EmitAnyExpr(E->getRHS()); 2509 LValue LV = EmitLValue(E->getLHS()); 2510 EmitStoreThroughLValue(RV, LV); 2511 return LV; 2512 } 2513 2514 if (E->getType()->isAnyComplexType()) 2515 return EmitComplexAssignmentLValue(E); 2516 2517 return EmitAggExprToLValue(E); 2518 } 2519 2520 LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { 2521 RValue RV = EmitCallExpr(E); 2522 2523 if (!RV.isScalar()) 2524 return MakeAddrLValue(RV.getAggregateAddr(), E->getType()); 2525 2526 assert(E->getCallReturnType()->isReferenceType() && 2527 "Can't have a scalar return unless the return type is a " 2528 "reference type!"); 2529 2530 return MakeAddrLValue(RV.getScalarVal(), E->getType()); 2531 } 2532 2533 LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) { 2534 // FIXME: This shouldn't require another copy. 2535 return EmitAggExprToLValue(E); 2536 } 2537 2538 LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) { 2539 assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor() 2540 && "binding l-value to type which needs a temporary"); 2541 AggValueSlot Slot = CreateAggTemp(E->getType()); 2542 EmitCXXConstructExpr(E, Slot); 2543 return MakeAddrLValue(Slot.getAddr(), E->getType()); 2544 } 2545 2546 LValue 2547 CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) { 2548 return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType()); 2549 } 2550 2551 LValue 2552 CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) { 2553 AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); 2554 Slot.setExternallyDestructed(); 2555 EmitAggExpr(E->getSubExpr(), Slot); 2556 EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddr()); 2557 return MakeAddrLValue(Slot.getAddr(), E->getType()); 2558 } 2559 2560 LValue 2561 CodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) { 2562 AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); 2563 EmitLambdaExpr(E, Slot); 2564 return MakeAddrLValue(Slot.getAddr(), E->getType()); 2565 } 2566 2567 LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) { 2568 RValue RV = EmitObjCMessageExpr(E); 2569 2570 if (!RV.isScalar()) 2571 return MakeAddrLValue(RV.getAggregateAddr(), E->getType()); 2572 2573 assert(E->getMethodDecl()->getResultType()->isReferenceType() && 2574 "Can't have a scalar return unless the return type is a " 2575 "reference type!"); 2576 2577 return MakeAddrLValue(RV.getScalarVal(), E->getType()); 2578 } 2579 2580 LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) { 2581 llvm::Value *V = 2582 CGM.getObjCRuntime().GetSelector(Builder, E->getSelector(), true); 2583 return MakeAddrLValue(V, E->getType()); 2584 } 2585 2586 llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface, 2587 const ObjCIvarDecl *Ivar) { 2588 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar); 2589 } 2590 2591 LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy, 2592 llvm::Value *BaseValue, 2593 const ObjCIvarDecl *Ivar, 2594 unsigned CVRQualifiers) { 2595 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue, 2596 Ivar, CVRQualifiers); 2597 } 2598 2599 LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { 2600 // FIXME: A lot of the code below could be shared with EmitMemberExpr. 2601 llvm::Value *BaseValue = 0; 2602 const Expr *BaseExpr = E->getBase(); 2603 Qualifiers BaseQuals; 2604 QualType ObjectTy; 2605 if (E->isArrow()) { 2606 BaseValue = EmitScalarExpr(BaseExpr); 2607 ObjectTy = BaseExpr->getType()->getPointeeType(); 2608 BaseQuals = ObjectTy.getQualifiers(); 2609 } else { 2610 LValue BaseLV = EmitLValue(BaseExpr); 2611 // FIXME: this isn't right for bitfields. 2612 BaseValue = BaseLV.getAddress(); 2613 ObjectTy = BaseExpr->getType(); 2614 BaseQuals = ObjectTy.getQualifiers(); 2615 } 2616 2617 LValue LV = 2618 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), 2619 BaseQuals.getCVRQualifiers()); 2620 setObjCGCLValueClass(getContext(), E, LV); 2621 return LV; 2622 } 2623 2624 LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) { 2625 // Can only get l-value for message expression returning aggregate type 2626 RValue RV = EmitAnyExprToTemp(E); 2627 return MakeAddrLValue(RV.getAggregateAddr(), E->getType()); 2628 } 2629 2630 RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee, 2631 ReturnValueSlot ReturnValue, 2632 CallExpr::const_arg_iterator ArgBeg, 2633 CallExpr::const_arg_iterator ArgEnd, 2634 const Decl *TargetDecl) { 2635 // Get the actual function type. The callee type will always be a pointer to 2636 // function type or a block pointer type. 2637 assert(CalleeType->isFunctionPointerType() && 2638 "Call must have function pointer type!"); 2639 2640 CalleeType = getContext().getCanonicalType(CalleeType); 2641 2642 const FunctionType *FnType 2643 = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType()); 2644 2645 CallArgList Args; 2646 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd); 2647 2648 const CGFunctionInfo &FnInfo = 2649 CGM.getTypes().arrangeFunctionCall(Args, FnType); 2650 2651 // C99 6.5.2.2p6: 2652 // If the expression that denotes the called function has a type 2653 // that does not include a prototype, [the default argument 2654 // promotions are performed]. If the number of arguments does not 2655 // equal the number of parameters, the behavior is undefined. If 2656 // the function is defined with a type that includes a prototype, 2657 // and either the prototype ends with an ellipsis (, ...) or the 2658 // types of the arguments after promotion are not compatible with 2659 // the types of the parameters, the behavior is undefined. If the 2660 // function is defined with a type that does not include a 2661 // prototype, and the types of the arguments after promotion are 2662 // not compatible with those of the parameters after promotion, 2663 // the behavior is undefined [except in some trivial cases]. 2664 // That is, in the general case, we should assume that a call 2665 // through an unprototyped function type works like a *non-variadic* 2666 // call. The way we make this work is to cast to the exact type 2667 // of the promoted arguments. 2668 if (isa<FunctionNoProtoType>(FnType) && !FnInfo.isVariadic()) { 2669 llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo); 2670 CalleeTy = CalleeTy->getPointerTo(); 2671 Callee = Builder.CreateBitCast(Callee, CalleeTy, "callee.knr.cast"); 2672 } 2673 2674 return EmitCall(FnInfo, Callee, ReturnValue, Args, TargetDecl); 2675 } 2676 2677 LValue CodeGenFunction:: 2678 EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) { 2679 llvm::Value *BaseV; 2680 if (E->getOpcode() == BO_PtrMemI) 2681 BaseV = EmitScalarExpr(E->getLHS()); 2682 else 2683 BaseV = EmitLValue(E->getLHS()).getAddress(); 2684 2685 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS()); 2686 2687 const MemberPointerType *MPT 2688 = E->getRHS()->getType()->getAs<MemberPointerType>(); 2689 2690 llvm::Value *AddV = 2691 CGM.getCXXABI().EmitMemberDataPointerAddress(*this, BaseV, OffsetV, MPT); 2692 2693 return MakeAddrLValue(AddV, MPT->getPointeeType()); 2694 } 2695 2696 static void 2697 EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, llvm::Value *Dest, 2698 llvm::Value *Ptr, llvm::Value *Val1, llvm::Value *Val2, 2699 uint64_t Size, unsigned Align, llvm::AtomicOrdering Order) { 2700 llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add; 2701 llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0; 2702 2703 switch (E->getOp()) { 2704 case AtomicExpr::AO__c11_atomic_init: 2705 llvm_unreachable("Already handled!"); 2706 2707 case AtomicExpr::AO__c11_atomic_compare_exchange_strong: 2708 case AtomicExpr::AO__c11_atomic_compare_exchange_weak: 2709 case AtomicExpr::AO__atomic_compare_exchange: 2710 case AtomicExpr::AO__atomic_compare_exchange_n: { 2711 // Note that cmpxchg only supports specifying one ordering and 2712 // doesn't support weak cmpxchg, at least at the moment. 2713 llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1); 2714 LoadVal1->setAlignment(Align); 2715 llvm::LoadInst *LoadVal2 = CGF.Builder.CreateLoad(Val2); 2716 LoadVal2->setAlignment(Align); 2717 llvm::AtomicCmpXchgInst *CXI = 2718 CGF.Builder.CreateAtomicCmpXchg(Ptr, LoadVal1, LoadVal2, Order); 2719 CXI->setVolatile(E->isVolatile()); 2720 llvm::StoreInst *StoreVal1 = CGF.Builder.CreateStore(CXI, Val1); 2721 StoreVal1->setAlignment(Align); 2722 llvm::Value *Cmp = CGF.Builder.CreateICmpEQ(CXI, LoadVal1); 2723 CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType())); 2724 return; 2725 } 2726 2727 case AtomicExpr::AO__c11_atomic_load: 2728 case AtomicExpr::AO__atomic_load_n: 2729 case AtomicExpr::AO__atomic_load: { 2730 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr); 2731 Load->setAtomic(Order); 2732 Load->setAlignment(Size); 2733 Load->setVolatile(E->isVolatile()); 2734 llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Load, Dest); 2735 StoreDest->setAlignment(Align); 2736 return; 2737 } 2738 2739 case AtomicExpr::AO__c11_atomic_store: 2740 case AtomicExpr::AO__atomic_store: 2741 case AtomicExpr::AO__atomic_store_n: { 2742 assert(!Dest && "Store does not return a value"); 2743 llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1); 2744 LoadVal1->setAlignment(Align); 2745 llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr); 2746 Store->setAtomic(Order); 2747 Store->setAlignment(Size); 2748 Store->setVolatile(E->isVolatile()); 2749 return; 2750 } 2751 2752 case AtomicExpr::AO__c11_atomic_exchange: 2753 case AtomicExpr::AO__atomic_exchange_n: 2754 case AtomicExpr::AO__atomic_exchange: 2755 Op = llvm::AtomicRMWInst::Xchg; 2756 break; 2757 2758 case AtomicExpr::AO__atomic_add_fetch: 2759 PostOp = llvm::Instruction::Add; 2760 // Fall through. 2761 case AtomicExpr::AO__c11_atomic_fetch_add: 2762 case AtomicExpr::AO__atomic_fetch_add: 2763 Op = llvm::AtomicRMWInst::Add; 2764 break; 2765 2766 case AtomicExpr::AO__atomic_sub_fetch: 2767 PostOp = llvm::Instruction::Sub; 2768 // Fall through. 2769 case AtomicExpr::AO__c11_atomic_fetch_sub: 2770 case AtomicExpr::AO__atomic_fetch_sub: 2771 Op = llvm::AtomicRMWInst::Sub; 2772 break; 2773 2774 case AtomicExpr::AO__atomic_and_fetch: 2775 PostOp = llvm::Instruction::And; 2776 // Fall through. 2777 case AtomicExpr::AO__c11_atomic_fetch_and: 2778 case AtomicExpr::AO__atomic_fetch_and: 2779 Op = llvm::AtomicRMWInst::And; 2780 break; 2781 2782 case AtomicExpr::AO__atomic_or_fetch: 2783 PostOp = llvm::Instruction::Or; 2784 // Fall through. 2785 case AtomicExpr::AO__c11_atomic_fetch_or: 2786 case AtomicExpr::AO__atomic_fetch_or: 2787 Op = llvm::AtomicRMWInst::Or; 2788 break; 2789 2790 case AtomicExpr::AO__atomic_xor_fetch: 2791 PostOp = llvm::Instruction::Xor; 2792 // Fall through. 2793 case AtomicExpr::AO__c11_atomic_fetch_xor: 2794 case AtomicExpr::AO__atomic_fetch_xor: 2795 Op = llvm::AtomicRMWInst::Xor; 2796 break; 2797 2798 case AtomicExpr::AO__atomic_nand_fetch: 2799 PostOp = llvm::Instruction::And; 2800 // Fall through. 2801 case AtomicExpr::AO__atomic_fetch_nand: 2802 Op = llvm::AtomicRMWInst::Nand; 2803 break; 2804 } 2805 2806 llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1); 2807 LoadVal1->setAlignment(Align); 2808 llvm::AtomicRMWInst *RMWI = 2809 CGF.Builder.CreateAtomicRMW(Op, Ptr, LoadVal1, Order); 2810 RMWI->setVolatile(E->isVolatile()); 2811 2812 // For __atomic_*_fetch operations, perform the operation again to 2813 // determine the value which was written. 2814 llvm::Value *Result = RMWI; 2815 if (PostOp) 2816 Result = CGF.Builder.CreateBinOp(PostOp, RMWI, LoadVal1); 2817 if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch) 2818 Result = CGF.Builder.CreateNot(Result); 2819 llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Result, Dest); 2820 StoreDest->setAlignment(Align); 2821 } 2822 2823 // This function emits any expression (scalar, complex, or aggregate) 2824 // into a temporary alloca. 2825 static llvm::Value * 2826 EmitValToTemp(CodeGenFunction &CGF, Expr *E) { 2827 llvm::Value *DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp"); 2828 CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(), 2829 /*Init*/ true); 2830 return DeclPtr; 2831 } 2832 2833 static RValue ConvertTempToRValue(CodeGenFunction &CGF, QualType Ty, 2834 llvm::Value *Dest) { 2835 if (Ty->isAnyComplexType()) 2836 return RValue::getComplex(CGF.LoadComplexFromAddr(Dest, false)); 2837 if (CGF.hasAggregateLLVMType(Ty)) 2838 return RValue::getAggregate(Dest); 2839 return RValue::get(CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(Dest, Ty))); 2840 } 2841 2842 RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) { 2843 QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); 2844 QualType MemTy = AtomicTy; 2845 if (const AtomicType *AT = AtomicTy->getAs<AtomicType>()) 2846 MemTy = AT->getValueType(); 2847 CharUnits sizeChars = getContext().getTypeSizeInChars(AtomicTy); 2848 uint64_t Size = sizeChars.getQuantity(); 2849 CharUnits alignChars = getContext().getTypeAlignInChars(AtomicTy); 2850 unsigned Align = alignChars.getQuantity(); 2851 unsigned MaxInlineWidth = 2852 getContext().getTargetInfo().getMaxAtomicInlineWidth(); 2853 bool UseLibcall = (Size != Align || Size > MaxInlineWidth); 2854 2855 2856 2857 llvm::Value *Ptr, *Order, *OrderFail = 0, *Val1 = 0, *Val2 = 0; 2858 Ptr = EmitScalarExpr(E->getPtr()); 2859 2860 if (E->getOp() == AtomicExpr::AO__c11_atomic_init) { 2861 assert(!Dest && "Init does not return a value"); 2862 if (!hasAggregateLLVMType(E->getVal1()->getType())) { 2863 QualType PointeeType 2864 = E->getPtr()->getType()->getAs<PointerType>()->getPointeeType(); 2865 EmitScalarInit(EmitScalarExpr(E->getVal1()), 2866 LValue::MakeAddr(Ptr, PointeeType, alignChars, 2867 getContext())); 2868 } else if (E->getType()->isAnyComplexType()) { 2869 EmitComplexExprIntoAddr(E->getVal1(), Ptr, E->isVolatile()); 2870 } else { 2871 AggValueSlot Slot = AggValueSlot::forAddr(Ptr, alignChars, 2872 AtomicTy.getQualifiers(), 2873 AggValueSlot::IsNotDestructed, 2874 AggValueSlot::DoesNotNeedGCBarriers, 2875 AggValueSlot::IsNotAliased); 2876 EmitAggExpr(E->getVal1(), Slot); 2877 } 2878 return RValue::get(0); 2879 } 2880 2881 Order = EmitScalarExpr(E->getOrder()); 2882 2883 switch (E->getOp()) { 2884 case AtomicExpr::AO__c11_atomic_init: 2885 llvm_unreachable("Already handled!"); 2886 2887 case AtomicExpr::AO__c11_atomic_load: 2888 case AtomicExpr::AO__atomic_load_n: 2889 break; 2890 2891 case AtomicExpr::AO__atomic_load: 2892 Dest = EmitScalarExpr(E->getVal1()); 2893 break; 2894 2895 case AtomicExpr::AO__atomic_store: 2896 Val1 = EmitScalarExpr(E->getVal1()); 2897 break; 2898 2899 case AtomicExpr::AO__atomic_exchange: 2900 Val1 = EmitScalarExpr(E->getVal1()); 2901 Dest = EmitScalarExpr(E->getVal2()); 2902 break; 2903 2904 case AtomicExpr::AO__c11_atomic_compare_exchange_strong: 2905 case AtomicExpr::AO__c11_atomic_compare_exchange_weak: 2906 case AtomicExpr::AO__atomic_compare_exchange_n: 2907 case AtomicExpr::AO__atomic_compare_exchange: 2908 Val1 = EmitScalarExpr(E->getVal1()); 2909 if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange) 2910 Val2 = EmitScalarExpr(E->getVal2()); 2911 else 2912 Val2 = EmitValToTemp(*this, E->getVal2()); 2913 OrderFail = EmitScalarExpr(E->getOrderFail()); 2914 // Evaluate and discard the 'weak' argument. 2915 if (E->getNumSubExprs() == 6) 2916 EmitScalarExpr(E->getWeak()); 2917 break; 2918 2919 case AtomicExpr::AO__c11_atomic_fetch_add: 2920 case AtomicExpr::AO__c11_atomic_fetch_sub: 2921 if (MemTy->isPointerType()) { 2922 // For pointer arithmetic, we're required to do a bit of math: 2923 // adding 1 to an int* is not the same as adding 1 to a uintptr_t. 2924 // ... but only for the C11 builtins. The GNU builtins expect the 2925 // user to multiply by sizeof(T). 2926 QualType Val1Ty = E->getVal1()->getType(); 2927 llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1()); 2928 CharUnits PointeeIncAmt = 2929 getContext().getTypeSizeInChars(MemTy->getPointeeType()); 2930 Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt)); 2931 Val1 = CreateMemTemp(Val1Ty, ".atomictmp"); 2932 EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Val1, Val1Ty)); 2933 break; 2934 } 2935 // Fall through. 2936 case AtomicExpr::AO__atomic_fetch_add: 2937 case AtomicExpr::AO__atomic_fetch_sub: 2938 case AtomicExpr::AO__atomic_add_fetch: 2939 case AtomicExpr::AO__atomic_sub_fetch: 2940 case AtomicExpr::AO__c11_atomic_store: 2941 case AtomicExpr::AO__c11_atomic_exchange: 2942 case AtomicExpr::AO__atomic_store_n: 2943 case AtomicExpr::AO__atomic_exchange_n: 2944 case AtomicExpr::AO__c11_atomic_fetch_and: 2945 case AtomicExpr::AO__c11_atomic_fetch_or: 2946 case AtomicExpr::AO__c11_atomic_fetch_xor: 2947 case AtomicExpr::AO__atomic_fetch_and: 2948 case AtomicExpr::AO__atomic_fetch_or: 2949 case AtomicExpr::AO__atomic_fetch_xor: 2950 case AtomicExpr::AO__atomic_fetch_nand: 2951 case AtomicExpr::AO__atomic_and_fetch: 2952 case AtomicExpr::AO__atomic_or_fetch: 2953 case AtomicExpr::AO__atomic_xor_fetch: 2954 case AtomicExpr::AO__atomic_nand_fetch: 2955 Val1 = EmitValToTemp(*this, E->getVal1()); 2956 break; 2957 } 2958 2959 if (!E->getType()->isVoidType() && !Dest) 2960 Dest = CreateMemTemp(E->getType(), ".atomicdst"); 2961 2962 // Use a library call. See: http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary . 2963 if (UseLibcall) { 2964 2965 llvm::SmallVector<QualType, 5> Params; 2966 CallArgList Args; 2967 // Size is always the first parameter 2968 Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)), 2969 getContext().getSizeType()); 2970 // Atomic address is always the second parameter 2971 Args.add(RValue::get(EmitCastToVoidPtr(Ptr)), 2972 getContext().VoidPtrTy); 2973 2974 const char* LibCallName; 2975 QualType RetTy = getContext().VoidTy; 2976 switch (E->getOp()) { 2977 // There is only one libcall for compare an exchange, because there is no 2978 // optimisation benefit possible from a libcall version of a weak compare 2979 // and exchange. 2980 // bool __atomic_compare_exchange(size_t size, void *obj, void *expected, 2981 // void *desired, int success, int failure) 2982 case AtomicExpr::AO__c11_atomic_compare_exchange_weak: 2983 case AtomicExpr::AO__c11_atomic_compare_exchange_strong: 2984 case AtomicExpr::AO__atomic_compare_exchange: 2985 case AtomicExpr::AO__atomic_compare_exchange_n: 2986 LibCallName = "__atomic_compare_exchange"; 2987 RetTy = getContext().BoolTy; 2988 Args.add(RValue::get(EmitCastToVoidPtr(Val1)), 2989 getContext().VoidPtrTy); 2990 Args.add(RValue::get(EmitCastToVoidPtr(Val2)), 2991 getContext().VoidPtrTy); 2992 Args.add(RValue::get(Order), 2993 getContext().IntTy); 2994 Order = OrderFail; 2995 break; 2996 // void __atomic_exchange(size_t size, void *mem, void *val, void *return, 2997 // int order) 2998 case AtomicExpr::AO__c11_atomic_exchange: 2999 case AtomicExpr::AO__atomic_exchange_n: 3000 case AtomicExpr::AO__atomic_exchange: 3001 LibCallName = "__atomic_exchange"; 3002 Args.add(RValue::get(EmitCastToVoidPtr(Val1)), 3003 getContext().VoidPtrTy); 3004 Args.add(RValue::get(EmitCastToVoidPtr(Dest)), 3005 getContext().VoidPtrTy); 3006 break; 3007 // void __atomic_store(size_t size, void *mem, void *val, int order) 3008 case AtomicExpr::AO__c11_atomic_store: 3009 case AtomicExpr::AO__atomic_store: 3010 case AtomicExpr::AO__atomic_store_n: 3011 LibCallName = "__atomic_store"; 3012 Args.add(RValue::get(EmitCastToVoidPtr(Val1)), 3013 getContext().VoidPtrTy); 3014 break; 3015 // void __atomic_load(size_t size, void *mem, void *return, int order) 3016 case AtomicExpr::AO__c11_atomic_load: 3017 case AtomicExpr::AO__atomic_load: 3018 case AtomicExpr::AO__atomic_load_n: 3019 LibCallName = "__atomic_load"; 3020 Args.add(RValue::get(EmitCastToVoidPtr(Dest)), 3021 getContext().VoidPtrTy); 3022 break; 3023 #if 0 3024 // These are only defined for 1-16 byte integers. It is not clear what 3025 // their semantics would be on anything else... 3026 case AtomicExpr::Add: LibCallName = "__atomic_fetch_add_generic"; break; 3027 case AtomicExpr::Sub: LibCallName = "__atomic_fetch_sub_generic"; break; 3028 case AtomicExpr::And: LibCallName = "__atomic_fetch_and_generic"; break; 3029 case AtomicExpr::Or: LibCallName = "__atomic_fetch_or_generic"; break; 3030 case AtomicExpr::Xor: LibCallName = "__atomic_fetch_xor_generic"; break; 3031 #endif 3032 default: return EmitUnsupportedRValue(E, "atomic library call"); 3033 } 3034 // order is always the last parameter 3035 Args.add(RValue::get(Order), 3036 getContext().IntTy); 3037 3038 const CGFunctionInfo &FuncInfo = 3039 CGM.getTypes().arrangeFunctionCall(RetTy, Args, 3040 FunctionType::ExtInfo(), RequiredArgs::All); 3041 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo); 3042 llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName); 3043 RValue Res = EmitCall(FuncInfo, Func, ReturnValueSlot(), Args); 3044 if (E->isCmpXChg()) 3045 return Res; 3046 if (E->getType()->isVoidType()) 3047 return RValue::get(0); 3048 return ConvertTempToRValue(*this, E->getType(), Dest); 3049 } 3050 3051 llvm::Type *IPtrTy = 3052 llvm::IntegerType::get(getLLVMContext(), Size * 8)->getPointerTo(); 3053 llvm::Value *OrigDest = Dest; 3054 Ptr = Builder.CreateBitCast(Ptr, IPtrTy); 3055 if (Val1) Val1 = Builder.CreateBitCast(Val1, IPtrTy); 3056 if (Val2) Val2 = Builder.CreateBitCast(Val2, IPtrTy); 3057 if (Dest && !E->isCmpXChg()) Dest = Builder.CreateBitCast(Dest, IPtrTy); 3058 3059 if (isa<llvm::ConstantInt>(Order)) { 3060 int ord = cast<llvm::ConstantInt>(Order)->getZExtValue(); 3061 switch (ord) { 3062 case 0: // memory_order_relaxed 3063 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3064 llvm::Monotonic); 3065 break; 3066 case 1: // memory_order_consume 3067 case 2: // memory_order_acquire 3068 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3069 llvm::Acquire); 3070 break; 3071 case 3: // memory_order_release 3072 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3073 llvm::Release); 3074 break; 3075 case 4: // memory_order_acq_rel 3076 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3077 llvm::AcquireRelease); 3078 break; 3079 case 5: // memory_order_seq_cst 3080 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3081 llvm::SequentiallyConsistent); 3082 break; 3083 default: // invalid order 3084 // We should not ever get here normally, but it's hard to 3085 // enforce that in general. 3086 break; 3087 } 3088 if (E->getType()->isVoidType()) 3089 return RValue::get(0); 3090 return ConvertTempToRValue(*this, E->getType(), OrigDest); 3091 } 3092 3093 // Long case, when Order isn't obviously constant. 3094 3095 bool IsStore = E->getOp() == AtomicExpr::AO__c11_atomic_store || 3096 E->getOp() == AtomicExpr::AO__atomic_store || 3097 E->getOp() == AtomicExpr::AO__atomic_store_n; 3098 bool IsLoad = E->getOp() == AtomicExpr::AO__c11_atomic_load || 3099 E->getOp() == AtomicExpr::AO__atomic_load || 3100 E->getOp() == AtomicExpr::AO__atomic_load_n; 3101 3102 // Create all the relevant BB's 3103 llvm::BasicBlock *MonotonicBB = 0, *AcquireBB = 0, *ReleaseBB = 0, 3104 *AcqRelBB = 0, *SeqCstBB = 0; 3105 MonotonicBB = createBasicBlock("monotonic", CurFn); 3106 if (!IsStore) 3107 AcquireBB = createBasicBlock("acquire", CurFn); 3108 if (!IsLoad) 3109 ReleaseBB = createBasicBlock("release", CurFn); 3110 if (!IsLoad && !IsStore) 3111 AcqRelBB = createBasicBlock("acqrel", CurFn); 3112 SeqCstBB = createBasicBlock("seqcst", CurFn); 3113 llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn); 3114 3115 // Create the switch for the split 3116 // MonotonicBB is arbitrarily chosen as the default case; in practice, this 3117 // doesn't matter unless someone is crazy enough to use something that 3118 // doesn't fold to a constant for the ordering. 3119 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false); 3120 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB); 3121 3122 // Emit all the different atomics 3123 Builder.SetInsertPoint(MonotonicBB); 3124 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3125 llvm::Monotonic); 3126 Builder.CreateBr(ContBB); 3127 if (!IsStore) { 3128 Builder.SetInsertPoint(AcquireBB); 3129 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3130 llvm::Acquire); 3131 Builder.CreateBr(ContBB); 3132 SI->addCase(Builder.getInt32(1), AcquireBB); 3133 SI->addCase(Builder.getInt32(2), AcquireBB); 3134 } 3135 if (!IsLoad) { 3136 Builder.SetInsertPoint(ReleaseBB); 3137 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3138 llvm::Release); 3139 Builder.CreateBr(ContBB); 3140 SI->addCase(Builder.getInt32(3), ReleaseBB); 3141 } 3142 if (!IsLoad && !IsStore) { 3143 Builder.SetInsertPoint(AcqRelBB); 3144 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3145 llvm::AcquireRelease); 3146 Builder.CreateBr(ContBB); 3147 SI->addCase(Builder.getInt32(4), AcqRelBB); 3148 } 3149 Builder.SetInsertPoint(SeqCstBB); 3150 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, 3151 llvm::SequentiallyConsistent); 3152 Builder.CreateBr(ContBB); 3153 SI->addCase(Builder.getInt32(5), SeqCstBB); 3154 3155 // Cleanup and return 3156 Builder.SetInsertPoint(ContBB); 3157 if (E->getType()->isVoidType()) 3158 return RValue::get(0); 3159 return ConvertTempToRValue(*this, E->getType(), OrigDest); 3160 } 3161 3162 void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) { 3163 assert(Val->getType()->isFPOrFPVectorTy()); 3164 if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val)) 3165 return; 3166 3167 llvm::Value *ULPs = llvm::ConstantFP::get(Builder.getFloatTy(), Accuracy); 3168 llvm::MDNode *Node = llvm::MDNode::get(getLLVMContext(), ULPs); 3169 3170 cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node); 3171 } 3172 3173 namespace { 3174 struct LValueOrRValue { 3175 LValue LV; 3176 RValue RV; 3177 }; 3178 } 3179 3180 static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF, 3181 const PseudoObjectExpr *E, 3182 bool forLValue, 3183 AggValueSlot slot) { 3184 llvm::SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; 3185 3186 // Find the result expression, if any. 3187 const Expr *resultExpr = E->getResultExpr(); 3188 LValueOrRValue result; 3189 3190 for (PseudoObjectExpr::const_semantics_iterator 3191 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { 3192 const Expr *semantic = *i; 3193 3194 // If this semantic expression is an opaque value, bind it 3195 // to the result of its source expression. 3196 if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { 3197 3198 // If this is the result expression, we may need to evaluate 3199 // directly into the slot. 3200 typedef CodeGenFunction::OpaqueValueMappingData OVMA; 3201 OVMA opaqueData; 3202 if (ov == resultExpr && ov->isRValue() && !forLValue && 3203 CodeGenFunction::hasAggregateLLVMType(ov->getType()) && 3204 !ov->getType()->isAnyComplexType()) { 3205 CGF.EmitAggExpr(ov->getSourceExpr(), slot); 3206 3207 LValue LV = CGF.MakeAddrLValue(slot.getAddr(), ov->getType()); 3208 opaqueData = OVMA::bind(CGF, ov, LV); 3209 result.RV = slot.asRValue(); 3210 3211 // Otherwise, emit as normal. 3212 } else { 3213 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); 3214 3215 // If this is the result, also evaluate the result now. 3216 if (ov == resultExpr) { 3217 if (forLValue) 3218 result.LV = CGF.EmitLValue(ov); 3219 else 3220 result.RV = CGF.EmitAnyExpr(ov, slot); 3221 } 3222 } 3223 3224 opaques.push_back(opaqueData); 3225 3226 // Otherwise, if the expression is the result, evaluate it 3227 // and remember the result. 3228 } else if (semantic == resultExpr) { 3229 if (forLValue) 3230 result.LV = CGF.EmitLValue(semantic); 3231 else 3232 result.RV = CGF.EmitAnyExpr(semantic, slot); 3233 3234 // Otherwise, evaluate the expression in an ignored context. 3235 } else { 3236 CGF.EmitIgnoredExpr(semantic); 3237 } 3238 } 3239 3240 // Unbind all the opaques now. 3241 for (unsigned i = 0, e = opaques.size(); i != e; ++i) 3242 opaques[i].unbind(CGF); 3243 3244 return result; 3245 } 3246 3247 RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E, 3248 AggValueSlot slot) { 3249 return emitPseudoObjectExpr(*this, E, false, slot).RV; 3250 } 3251 3252 LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) { 3253 return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV; 3254 } 3255