15508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// 25508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 35508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// The LLVM Compiler Infrastructure 45508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 85508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===----------------------------------------------------------------------===// 95508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 105508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// This contains code to emit Objective-C code as LLVM code. 115508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 125508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===----------------------------------------------------------------------===// 135508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 14bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel#include "CGDebugInfo.h" 152979ec73b4f974d85f2ce84167712177a44c6f09Ted Kremenek#include "CGObjCRuntime.h" 165508518a2702b00be3b15a26d772bde968972f54Anders Carlsson#include "CodeGenFunction.h" 175508518a2702b00be3b15a26d772bde968972f54Anders Carlsson#include "CodeGenModule.h" 18f85e193739c953358c865005855253af4f68a497John McCall#include "TargetInfo.h" 1985c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar#include "clang/AST/ASTContext.h" 20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2116f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h" 22e66f4e3e3ae9d7d11b0c302211066fad69228abaDaniel Dunbar#include "clang/Basic/Diagnostic.h" 238b54999a831bb195c08541ca995ef0505c96193fMark Lacey#include "clang/CodeGen/CGFunctionInfo.h" 243d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson#include "llvm/ADT/STLExtras.h" 25651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/IR/CallSite.h" 263b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h" 273b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/InlineAsm.h" 285508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace clang; 295508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace CodeGen; 305508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 31f85e193739c953358c865005855253af4f68a497John McCalltypedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult; 32f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult 33f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e); 3487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic RValue AdjustObjCObjectType(CodeGenFunction &CGF, 3587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar QualType ET, 3687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar RValue Result); 37f85e193739c953358c865005855253af4f68a497John McCall 38f85e193739c953358c865005855253af4f68a497John McCall/// Given the address of a variable of pointer type, find the correct 39f85e193739c953358c865005855253af4f68a497John McCall/// null to store into it. 4087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic llvm::Constant *getNullForVariable(Address addr) { 4187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Type *type = addr.getElementType(); 42f85e193739c953358c865005855253af4f68a497John McCall return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); 43f85e193739c953358c865005855253af4f68a497John McCall} 44f85e193739c953358c865005855253af4f68a497John McCall 458fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emits an instance of NSConstantString representing the object. 461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) 4771fcec9abf2ce66d5e17a24bd021680e94e42f0dDaniel Dunbar{ 480d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall llvm::Constant *C = 4987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCRuntime().GenerateConstantString(E->getString()).getPointer(); 50ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar // FIXME: This bitcast should just be made an invariant on the Runtime. 513c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 528fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner} 538fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 54eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// EmitObjCBoxedExpr - This routine generates code to call 55eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// the appropriate expression boxing method. This will either be 5687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:], 5787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// or [NSValue valueWithBytes:objCType:]. 58ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// 5916098f366097305c348b356a44638d6c959c6e60Eric Christopherllvm::Value * 60eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick BeardCodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) { 61ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the correct selector for this literal's concrete type. 62ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Get the method. 63eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod(); 6487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar const Expr *SubExpr = E->getSubExpr(); 65eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard assert(BoxingMethod && "BoxingMethod is null"); 66eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method"); 67eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard Selector Sel = BoxingMethod->getSelector(); 68ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 69ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate a reference to the class pointer, which will be the receiver. 70eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard // Assumes that the method was introduced in the class that should be 71eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard // messaged (avoids pulling it out of the result type). 72ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 73eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface(); 74bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl); 750e2c34f92f00628d48968dfea096d36381f494cbStephen Hines 76ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CallArgList Args; 7787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar const ParmVarDecl *ArgDecl = *BoxingMethod->param_begin(); 7887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar QualType ArgQT = ArgDecl->getType().getUnqualifiedType(); 7987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 8087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // ObjCBoxedExpr supports boxing of structs and unions 8187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // via [NSValue valueWithBytes:objCType:] 8287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar const QualType ValueType(SubExpr->getType().getCanonicalType()); 8387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (ValueType->isObjCBoxableRecordType()) { 8487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // Emit CodeGen for first parameter 8587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // and cast value to correct type 8687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address Temporary = CreateMemTemp(SubExpr->getType()); 8787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar EmitAnyExprToMem(SubExpr, Temporary, Qualifiers(), /*isInit*/ true); 8887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address BitCast = Builder.CreateBitCast(Temporary, ConvertType(ArgQT)); 8987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Args.add(RValue::get(BitCast.getPointer()), ArgQT); 9087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 9187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // Create char array to store type encoding 9287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar std::string Str; 9387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar getContext().getObjCEncodingForType(ValueType, Str); 9487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Constant *GV = CGM.GetAddrOfConstantCString(Str).getPointer(); 9587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 9687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // Cast type encoding to correct type 9787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar const ParmVarDecl *EncodingDecl = BoxingMethod->parameters()[1]; 9887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar QualType EncodingQT = EncodingDecl->getType().getUnqualifiedType(); 9987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Value *Cast = Builder.CreateBitCast(GV, ConvertType(EncodingQT)); 10087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 10187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Args.add(RValue::get(Cast), EncodingQT); 10287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } else { 10387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Args.add(EmitAnyExpr(SubExpr), ArgQT); 10487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } 105651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 106651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RValue result = Runtime.GenerateMessageSend( 107651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines *this, ReturnValueSlot(), BoxingMethod->getReturnType(), Sel, Receiver, 108651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Args, ClassDecl, BoxingMethod); 109ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return Builder.CreateBitCast(result.getScalarVal(), 110ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ConvertType(E->getType())); 111ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 112ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 113ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, 114ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCMethodDecl *MethodWithObjects) { 115ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ASTContext &Context = CGM.getContext(); 1166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines const ObjCDictionaryLiteral *DLE = nullptr; 117ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E); 118ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!ALE) 119ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek DLE = cast<ObjCDictionaryLiteral>(E); 120ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 121ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Compute the type of the array we're initializing. 122ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek uint64_t NumElements = 123ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ALE ? ALE->getNumElements() : DLE->getNumElements(); 124ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()), 125ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek NumElements); 126ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ElementType = Context.getObjCIdType().withConst(); 127ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ElementArrayType 128ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = Context.getConstantArrayType(ElementType, APNumElements, 129ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArrayType::Normal, /*IndexTypeQuals=*/0); 130ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 131ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Allocate the temporary array(s). 13287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address Objects = CreateMemTemp(ElementArrayType, "objects"); 13387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address Keys = Address::invalid(); 134ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (DLE) 135ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Keys = CreateMemTemp(ElementArrayType, "keys"); 136ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 137527842f97b684831de2b1a455649131ed381ece4John McCall // In ARC, we may need to do extra work to keep all the keys and 138527842f97b684831de2b1a455649131ed381ece4John McCall // values alive until after the call. 139527842f97b684831de2b1a455649131ed381ece4John McCall SmallVector<llvm::Value *, 16> NeededObjects; 140527842f97b684831de2b1a455649131ed381ece4John McCall bool TrackNeededObjects = 141527842f97b684831de2b1a455649131ed381ece4John McCall (getLangOpts().ObjCAutoRefCount && 142527842f97b684831de2b1a455649131ed381ece4John McCall CGM.getCodeGenOpts().OptimizationLevel != 0); 143527842f97b684831de2b1a455649131ed381ece4John McCall 144ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Perform the actual initialialization of the array(s). 145ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek for (uint64_t i = 0; i < NumElements; i++) { 146ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (ALE) { 147527842f97b684831de2b1a455649131ed381ece4John McCall // Emit the element and store it to the appropriate array slot. 148ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Rhs = ALE->getElement(i); 14987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar LValue LV = MakeAddrLValue( 15087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Builder.CreateConstArrayGEP(Objects, i, getPointerSize()), 15187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar ElementType, AlignmentSource::Decl); 152527842f97b684831de2b1a455649131ed381ece4John McCall 153527842f97b684831de2b1a455649131ed381ece4John McCall llvm::Value *value = EmitScalarExpr(Rhs); 154527842f97b684831de2b1a455649131ed381ece4John McCall EmitStoreThroughLValue(RValue::get(value), LV, true); 155527842f97b684831de2b1a455649131ed381ece4John McCall if (TrackNeededObjects) { 156527842f97b684831de2b1a455649131ed381ece4John McCall NeededObjects.push_back(value); 157527842f97b684831de2b1a455649131ed381ece4John McCall } 158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else { 159527842f97b684831de2b1a455649131ed381ece4John McCall // Emit the key and store it to the appropriate array slot. 160ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Key = DLE->getKeyValueElement(i).Key; 16187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar LValue KeyLV = MakeAddrLValue( 16287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Builder.CreateConstArrayGEP(Keys, i, getPointerSize()), 16387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar ElementType, AlignmentSource::Decl); 164527842f97b684831de2b1a455649131ed381ece4John McCall llvm::Value *keyValue = EmitScalarExpr(Key); 165527842f97b684831de2b1a455649131ed381ece4John McCall EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true); 166ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 167527842f97b684831de2b1a455649131ed381ece4John McCall // Emit the value and store it to the appropriate array slot. 16858878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar const Expr *Value = DLE->getKeyValueElement(i).Value; 16987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar LValue ValueLV = MakeAddrLValue( 17087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Builder.CreateConstArrayGEP(Objects, i, getPointerSize()), 17187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar ElementType, AlignmentSource::Decl); 172527842f97b684831de2b1a455649131ed381ece4John McCall llvm::Value *valueValue = EmitScalarExpr(Value); 173527842f97b684831de2b1a455649131ed381ece4John McCall EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true); 174527842f97b684831de2b1a455649131ed381ece4John McCall if (TrackNeededObjects) { 175527842f97b684831de2b1a455649131ed381ece4John McCall NeededObjects.push_back(keyValue); 176527842f97b684831de2b1a455649131ed381ece4John McCall NeededObjects.push_back(valueValue); 177527842f97b684831de2b1a455649131ed381ece4John McCall } 178ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 179ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 180ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 181ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the argument list. 182ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CallArgList Args; 183ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin(); 184ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ParmVarDecl *argDecl = *PI++; 185ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ArgQT = argDecl->getType().getUnqualifiedType(); 18687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Args.add(RValue::get(Objects.getPointer()), ArgQT); 187ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (DLE) { 188ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek argDecl = *PI++; 189ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArgQT = argDecl->getType().getUnqualifiedType(); 19087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Args.add(RValue::get(Keys.getPointer()), ArgQT); 191ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 192ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek argDecl = *PI; 193ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArgQT = argDecl->getType().getUnqualifiedType(); 194ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Count = 195ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements); 196ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Count), ArgQT); 197ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 198ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate a reference to the class pointer, which will be the receiver. 199ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Selector Sel = MethodWithObjects->getSelector(); 200ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ResultType = E->getType(); 201ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCObjectPointerType *InterfacePointerType 202ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = ResultType->getAsObjCInterfacePointerType(); 203ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ObjCInterfaceDecl *Class 204ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = InterfacePointerType->getObjectType()->getInterface(); 205ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 206bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.GetClass(*this, Class); 207ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 208ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the message send. 209651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RValue result = Runtime.GenerateMessageSend( 210651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines *this, ReturnValueSlot(), MethodWithObjects->getReturnType(), Sel, 211651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Receiver, Args, Class, MethodWithObjects); 212527842f97b684831de2b1a455649131ed381ece4John McCall 213527842f97b684831de2b1a455649131ed381ece4John McCall // The above message send needs these objects, but in ARC they are 214527842f97b684831de2b1a455649131ed381ece4John McCall // passed in a buffer that is essentially __unsafe_unretained. 215527842f97b684831de2b1a455649131ed381ece4John McCall // Therefore we must prevent the optimizer from releasing them until 216527842f97b684831de2b1a455649131ed381ece4John McCall // after the call. 217527842f97b684831de2b1a455649131ed381ece4John McCall if (TrackNeededObjects) { 218527842f97b684831de2b1a455649131ed381ece4John McCall EmitARCIntrinsicUse(NeededObjects); 219527842f97b684831de2b1a455649131ed381ece4John McCall } 220527842f97b684831de2b1a455649131ed381ece4John McCall 221ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return Builder.CreateBitCast(result.getScalarVal(), 222ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ConvertType(E->getType())); 223ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 224ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 225ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) { 226ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod()); 227ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 228ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 229ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral( 230ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCDictionaryLiteral *E) { 231ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod()); 232ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 233ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 2348fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emit a selector. 2358fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattnerllvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { 2368fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Untyped selector. 2378fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Note that this implementation allows for non-constant strings to be passed 2388fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // as arguments to @selector(). Currently, the only thing preventing this 2398fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // behaviour is the type checking in the front end. 240bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return CGM.getObjCRuntime().GetSelector(*this, E->getSelector()); 2418fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner} 2428fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 243ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbarllvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { 244ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar // FIXME: This should pass the Decl not the name. 245bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol()); 246ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar} 2478fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 24887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// \brief Adjust the type of an Objective-C object that doesn't match up due 24987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// to type erasure at various points, e.g., related result types or the use 25087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// of parameterized classes. 25187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic RValue AdjustObjCObjectType(CodeGenFunction &CGF, QualType ExpT, 25287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar RValue Result) { 25387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (!ExpT->isObjCRetainableType()) 254926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Result; 255f85e193739c953358c865005855253af4f68a497John McCall 25687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // If the converted types are the same, we're done. 25787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Type *ExpLLVMTy = CGF.ConvertType(ExpT); 25887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (ExpLLVMTy == Result.getScalarVal()->getType()) 259926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Result; 26087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 26187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // We have applied a substitution. Cast the rvalue appropriately. 262926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), 26387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar ExpLLVMTy)); 264926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor} 2658fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 266dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// Decide whether to extend the lifetime of the receiver of a 267dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// returns-inner-pointer message. 268dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallstatic bool 269dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallshouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) { 270dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall switch (message->getReceiverKind()) { 271dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 272dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // For a normal instance message, we should extend unless the 273dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // receiver is loaded from a variable with precise lifetime. 274dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::Instance: { 275dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const Expr *receiver = message->getInstanceReceiver(); 27687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 27787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // Look through OVEs. 27887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) { 27987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (opaque->getSourceExpr()) 28087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar receiver = opaque->getSourceExpr()->IgnoreParens(); 28187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } 28287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 283dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver); 284dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!ice || ice->getCastKind() != CK_LValueToRValue) return true; 285dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall receiver = ice->getSubExpr()->IgnoreParens(); 286dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 28787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // Look through OVEs. 28887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) { 28987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (opaque->getSourceExpr()) 29087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar receiver = opaque->getSourceExpr()->IgnoreParens(); 29187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } 29287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 293dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // Only __strong variables. 294dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 295dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return true; 296dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 297dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // All ivars and fields have precise lifetime. 298dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver)) 299dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 300dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 301dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // Otherwise, check for variables. 302dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr()); 303dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!declRef) return true; 304dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl()); 305dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!var) return true; 306dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 307dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // All variables have precise lifetime except local variables with 308dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // automatic storage duration that aren't specially marked. 309dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return (var->hasLocalStorage() && 310dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall !var->hasAttr<ObjCPreciseLifetimeAttr>()); 311dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall } 312dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 313dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::Class: 314dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::SuperClass: 315dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // It's never necessary for class objects. 316dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 317dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 318dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::SuperInstance: 319dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // We generally assume that 'self' lives throughout a method call. 320dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 321dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall } 322dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 323dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall llvm_unreachable("invalid receiver kind"); 324dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall} 325dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 32687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// Given an expression of ObjC pointer type, check whether it was 32787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// immediately loaded from an ARC __weak l-value. 32887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic const Expr *findWeakLValue(const Expr *E) { 32987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar assert(E->getType()->isObjCRetainableType()); 33087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar E = E->IgnoreParens(); 33187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (auto CE = dyn_cast<CastExpr>(E)) { 33287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (CE->getCastKind() == CK_LValueToRValue) { 33387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (CE->getSubExpr()->getType().getObjCLifetime() == Qualifiers::OCL_Weak) 33487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar return CE->getSubExpr(); 33587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } 33687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } 33787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 33887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar return nullptr; 33987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar} 34087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 341ef072fd2f3347cfd857d6eb787b245b950771430John McCallRValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, 342ef072fd2f3347cfd857d6eb787b245b950771430John McCall ReturnValueSlot Return) { 3438fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Only the lookup mechanism and first two arguments of the method 3448fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // implementation vary between runtimes. We can get the receiver and 3458fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // arguments in generic code. 3461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 347f85e193739c953358c865005855253af4f68a497John McCall bool isDelegateInit = E->isDelegateInitCall(); 348f85e193739c953358c865005855253af4f68a497John McCall 349dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const ObjCMethodDecl *method = E->getMethodDecl(); 3504e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian 35187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // If the method is -retain, and the receiver's being loaded from 35287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar // a __weak variable, peephole the entire operation to objc_loadWeakRetained. 35387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (method && E->getReceiverKind() == ObjCMessageExpr::Instance && 35487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar method->getMethodFamily() == OMF_retain) { 35587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (auto lvalueExpr = findWeakLValue(E->getInstanceReceiver())) { 35687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar LValue lvalue = EmitLValue(lvalueExpr); 35787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Value *result = EmitARCLoadWeakRetained(lvalue.getAddress()); 35887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar return AdjustObjCObjectType(*this, E->getType(), RValue::get(result)); 35987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } 36087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } 36187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 362f85e193739c953358c865005855253af4f68a497John McCall // We don't retain the receiver in delegate init calls, and this is 363f85e193739c953358c865005855253af4f68a497John McCall // safe because the receiver value is always loaded from 'self', 364f85e193739c953358c865005855253af4f68a497John McCall // which we zero out. We don't want to Block_copy block receivers, 365f85e193739c953358c865005855253af4f68a497John McCall // though. 366f85e193739c953358c865005855253af4f68a497John McCall bool retainSelf = 367f85e193739c953358c865005855253af4f68a497John McCall (!isDelegateInit && 3684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie CGM.getLangOpts().ObjCAutoRefCount && 369dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method && 370dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method->hasAttr<NSConsumesSelfAttr>()); 371f85e193739c953358c865005855253af4f68a497John McCall 372208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3738fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner bool isSuperMessage = false; 374f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar bool isClassMessage = false; 3756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines ObjCInterfaceDecl *OID = nullptr; 3768fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Find the receiver 377926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor QualType ReceiverType; 3786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines llvm::Value *Receiver = nullptr; 37904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor switch (E->getReceiverKind()) { 38004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::Instance: 381926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getInstanceReceiver()->getType(); 382f85e193739c953358c865005855253af4f68a497John McCall if (retainSelf) { 383f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, 384f85e193739c953358c865005855253af4f68a497John McCall E->getInstanceReceiver()); 385f85e193739c953358c865005855253af4f68a497John McCall Receiver = ter.getPointer(); 386dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (ter.getInt()) retainSelf = false; 387f85e193739c953358c865005855253af4f68a497John McCall } else 388f85e193739c953358c865005855253af4f68a497John McCall Receiver = EmitScalarExpr(E->getInstanceReceiver()); 38904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 3901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 39104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::Class: { 392926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getClassReceiver(); 393926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); 3943031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall assert(ObjTy && "Invalid Objective-C class message send"); 3953031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall OID = ObjTy->getInterface(); 3963031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall assert(OID && "Invalid Objective-C class message send"); 397bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Receiver = Runtime.GetClass(*this, OID); 398f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar isClassMessage = true; 39904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 40004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor } 40104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 40204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::SuperInstance: 403926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getSuperType(); 40404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor Receiver = LoadObjCSelf(); 4058fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner isSuperMessage = true; 40604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 40704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 40804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::SuperClass: 409926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getSuperType(); 4108fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner Receiver = LoadObjCSelf(); 41104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor isSuperMessage = true; 41204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor isClassMessage = true; 41304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 4148fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner } 4158fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 416dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (retainSelf) 417dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall Receiver = EmitARCRetainNonBlock(Receiver); 418dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 419dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // In ARC, we sometimes want to "extend the lifetime" 420dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // (i.e. retain+autorelease) of receivers of returns-inner-pointer 421dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // messages. 4224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount && method && 423dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method->hasAttr<ObjCReturnsInnerPointerAttr>() && 424dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall shouldExtendReceiverForInnerPointerMessage(E)) 425dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver); 426dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 427651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines QualType ResultType = method ? method->getReturnType() : E->getType(); 428f85e193739c953358c865005855253af4f68a497John McCall 42919cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar CallArgList Args; 43087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar EmitCallArgs(Args, method, E->arguments()); 4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 432f85e193739c953358c865005855253af4f68a497John McCall // For delegate init calls in ARC, do an unsafe store of null into 433f85e193739c953358c865005855253af4f68a497John McCall // self. This represents the call taking direct ownership of that 434f85e193739c953358c865005855253af4f68a497John McCall // value. We have to do this after emitting the other call 435f85e193739c953358c865005855253af4f68a497John McCall // arguments because they might also reference self, but we don't 436f85e193739c953358c865005855253af4f68a497John McCall // have to worry about any of them modifying self because that would 437f85e193739c953358c865005855253af4f68a497John McCall // be an undefined read and write of an object in unordered 438f85e193739c953358c865005855253af4f68a497John McCall // expressions. 439f85e193739c953358c865005855253af4f68a497John McCall if (isDelegateInit) { 4404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().ObjCAutoRefCount && 441f85e193739c953358c865005855253af4f68a497John McCall "delegate init calls should only be marked in ARC"); 442f85e193739c953358c865005855253af4f68a497John McCall 443f85e193739c953358c865005855253af4f68a497John McCall // Do an unsafe store of null into self. 44487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address selfAddr = 44587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()); 446f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); 447f85e193739c953358c865005855253af4f68a497John McCall } 4487e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson 449926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor RValue result; 4508fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner if (isSuperMessage) { 4519384c768e93f270118a30ce96546083a666da284Chris Lattner // super is only valid in an Objective-C method 4529384c768e93f270118a30ce96546083a666da284Chris Lattner const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 4537ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); 454926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, 455926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor E->getSelector(), 456926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor OMD->getClassInterface(), 457926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor isCategoryImpl, 458926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Receiver, 459926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor isClassMessage, 460926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Args, 461dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method); 462926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else { 463926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor result = Runtime.GenerateMessageSend(*this, Return, ResultType, 464926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor E->getSelector(), 465926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Receiver, Args, OID, 466dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method); 4678fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner } 468f85e193739c953358c865005855253af4f68a497John McCall 469f85e193739c953358c865005855253af4f68a497John McCall // For delegate init calls in ARC, implicitly store the result of 470f85e193739c953358c865005855253af4f68a497John McCall // the call back into self. This takes ownership of the value. 471f85e193739c953358c865005855253af4f68a497John McCall if (isDelegateInit) { 47287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address selfAddr = 47387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()); 474f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *newSelf = result.getScalarVal(); 475f85e193739c953358c865005855253af4f68a497John McCall 476f85e193739c953358c865005855253af4f68a497John McCall // The delegate return type isn't necessarily a matching type; in 477f85e193739c953358c865005855253af4f68a497John McCall // fact, it's quite likely to be 'id'. 47887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Type *selfTy = selfAddr.getElementType(); 479f85e193739c953358c865005855253af4f68a497John McCall newSelf = Builder.CreateBitCast(newSelf, selfTy); 480f85e193739c953358c865005855253af4f68a497John McCall 481f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(newSelf, selfAddr); 482f85e193739c953358c865005855253af4f68a497John McCall } 4834e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian 48487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar return AdjustObjCObjectType(*this, E->getType(), result); 4855508518a2702b00be3b15a26d772bde968972f54Anders Carlsson} 4865508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 487f85e193739c953358c865005855253af4f68a497John McCallnamespace { 48887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstruct FinishARCDealloc final : EHScopeStack::Cleanup { 489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 490f85e193739c953358c865005855253af4f68a497John McCall const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); 491799d34e9505a833549c71f2ac5f842da157ea031John McCall 492799d34e9505a833549c71f2ac5f842da157ea031John McCall const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); 493f85e193739c953358c865005855253af4f68a497John McCall const ObjCInterfaceDecl *iface = impl->getClassInterface(); 494f85e193739c953358c865005855253af4f68a497John McCall if (!iface->getSuperClass()) return; 495f85e193739c953358c865005855253af4f68a497John McCall 496799d34e9505a833549c71f2ac5f842da157ea031John McCall bool isCategory = isa<ObjCCategoryImplDecl>(impl); 497799d34e9505a833549c71f2ac5f842da157ea031John McCall 498f85e193739c953358c865005855253af4f68a497John McCall // Call [super dealloc] if we have a superclass. 499f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *self = CGF.LoadObjCSelf(); 500f85e193739c953358c865005855253af4f68a497John McCall 501f85e193739c953358c865005855253af4f68a497John McCall CallArgList args; 502f85e193739c953358c865005855253af4f68a497John McCall CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), 503f85e193739c953358c865005855253af4f68a497John McCall CGF.getContext().VoidTy, 504f85e193739c953358c865005855253af4f68a497John McCall method->getSelector(), 505f85e193739c953358c865005855253af4f68a497John McCall iface, 506799d34e9505a833549c71f2ac5f842da157ea031John McCall isCategory, 507f85e193739c953358c865005855253af4f68a497John McCall self, 508f85e193739c953358c865005855253af4f68a497John McCall /*is class msg*/ false, 509f85e193739c953358c865005855253af4f68a497John McCall args, 510f85e193739c953358c865005855253af4f68a497John McCall method); 511f85e193739c953358c865005855253af4f68a497John McCall } 512f85e193739c953358c865005855253af4f68a497John McCall}; 513f85e193739c953358c865005855253af4f68a497John McCall} 514f85e193739c953358c865005855253af4f68a497John McCall 515af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// StartObjCMethod - Begin emission of an ObjCMethod. This generates 516af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// the LLVM function and sets the other context used by 517af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// CodeGenFunction. 518679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianvoid CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, 5190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines const ObjCContainerDecl *CD) { 5200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines SourceLocation StartLoc = OMD->getLocStart(); 521d26bc76c98006609002d9930f8840490e88ac5b5John McCall FunctionArgList args; 5224800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel // Check if we should generate debug info for this method. 523c3030bc285d90a139fb74629daadef5617283203David Blaikie if (OMD->hasAttr<NoDebugAttr>()) 5246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines DebugInfo = nullptr; // disable debug info indefinitely for this function 5254800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel 526679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); 527f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 528de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD); 5290e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar CGM.SetInternalFunctionAttributes(OMD, Fn, FI); 5304111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 531d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(OMD->getSelfDecl()); 532d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(OMD->getCmdDecl()); 5334111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 5340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines args.append(OMD->param_begin(), OMD->param_end()); 535b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar 53614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne CurGD = OMD; 5370e2c34f92f00628d48968dfea096d36381f494cbStephen Hines CurEHLocation = OMD->getLocEnd(); 53814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne 5396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines StartFunction(OMD, OMD->getReturnType(), Fn, FI, args, 5406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines OMD->getLocation(), StartLoc); 541f85e193739c953358c865005855253af4f68a497John McCall 542f85e193739c953358c865005855253af4f68a497John McCall // In ARC, certain methods get an extra cleanup. 5434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount && 544f85e193739c953358c865005855253af4f68a497John McCall OMD->isInstanceMethod() && 545f85e193739c953358c865005855253af4f68a497John McCall OMD->getSelector().isUnarySelector()) { 546f85e193739c953358c865005855253af4f68a497John McCall const IdentifierInfo *ident = 547f85e193739c953358c865005855253af4f68a497John McCall OMD->getSelector().getIdentifierInfoForSlot(0); 548f85e193739c953358c865005855253af4f68a497John McCall if (ident->isStr("dealloc")) 549f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); 550f85e193739c953358c865005855253af4f68a497John McCall } 551af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 552af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 553f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 554f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, QualType type); 555f85e193739c953358c865005855253af4f68a497John McCall 556af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// Generate an Objective-C method. An Objective-C method is a C function with 5571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// its pointer, name, and types registered in the class struture. 558af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { 5590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines StartObjCMethod(OMD, OMD->getClassInterface()); 56087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar PGO.assignRegionCounters(GlobalDecl(OMD), CurFn); 561651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines assert(isa<CompoundStmt>(OMD->getBody())); 562b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar incrementProfileCounter(OMD->getBody()); 563651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody())); 5646fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis FinishFunction(OMD->getBodyRBrace()); 565af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 566af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 56741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructGetterCall - Call the runtime function to load a property 56841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// into the return value slot. 56941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, 57041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall bool isAtomic, bool hasStrong) { 57141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ASTContext &Context = CGF.getContext(); 57241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 57387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address src = 57487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0) 57587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar .getAddress(); 57641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 57741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // objc_copyStruct (ReturnValue, &structIvar, 57841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // sizeof (Type of Ivar), isAtomic, false); 57941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CallArgList args; 58041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 58187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy); 58287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar args.add(RValue::get(dest.getPointer()), Context.VoidPtrTy); 58341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 58441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy); 58587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar args.add(RValue::get(src.getPointer()), Context.VoidPtrTy); 58641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 58741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType()); 58841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType()); 58941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy); 59041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy); 59141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 59241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction(); 5934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.EmitCall(CGF.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, args), 59441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall fn, ReturnValueSlot(), args); 59541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall} 59641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 5971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Determine whether the given architecture supports unaligned atomic 5981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// accesses. They don't have to be fast, just faster than a function 5991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// call and a mutex. 6001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasUnalignedAtomics(llvm::Triple::ArchType arch) { 601de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman // FIXME: Allow unaligned atomic load/store on x86. (It is not 602de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman // currently supported by the backend.) 603de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman return 0; 6041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 6051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Return the maximum size that permits atomic accesses for the given 6071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// architecture. 6081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, 6091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Triple::ArchType arch) { 6101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // ARM has 8-byte atomic accesses, but it's not clear whether we 6111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // want to rely on them here. 6121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In the default case, just assume that any size up to a pointer is 6141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // fine given adequate alignment. 6151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return CharUnits::fromQuantity(CGM.PointerSizeInBytes); 6161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 6171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallnamespace { 6191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall class PropertyImplStrategy { 6201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall public: 6211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall enum StrategyKind { 6221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// The 'native' strategy is to use the architecture's provided 6231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// reads and writes. 6241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Native, 6251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_setProperty and objc_getProperty. 6271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall GetSetProperty, 6281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_setProperty for the setter, but use expression 6301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// evaluation for the getter. 6311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall SetPropertyAndExpressionGet, 6321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_copyStruct. 6341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CopyStruct, 6351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// The 'expression' strategy is to emit normal assignment or 6371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// lvalue-to-rvalue expressions. 6381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Expression 6391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall }; 6401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall StrategyKind getKind() const { return StrategyKind(Kind); } 6421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool hasStrongMember() const { return HasStrong; } 6441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool isAtomic() const { return IsAtomic; } 6451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool isCopy() const { return IsCopy; } 6461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits getIvarSize() const { return IvarSize; } 6481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits getIvarAlignment() const { return IvarAlignment; } 6491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy(CodeGenModule &CGM, 6511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyImplDecl *propImpl); 6521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall private: 6541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned Kind : 8; 6551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned IsAtomic : 1; 6561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned IsCopy : 1; 6571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned HasStrong : 1; 6581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits IvarSize; 6601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits IvarAlignment; 6611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall }; 6621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 6631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 664bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru/// Pick an implementation strategy for the given property synthesis. 6651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallPropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, 6661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyImplDecl *propImpl) { 6671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 668265941bc308d65cc270d5c4de5806f37ce405606John McCall ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind(); 6691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 670265941bc308d65cc270d5c4de5806f37ce405606John McCall IsCopy = (setterKind == ObjCPropertyDecl::Copy); 671265941bc308d65cc270d5c4de5806f37ce405606John McCall IsAtomic = prop->isAtomic(); 6721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall HasStrong = false; // doesn't matter here. 6731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Evaluate the ivar's size and alignment. 6751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 6761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType ivarType = ivar->getType(); 677651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::tie(IvarSize, IvarAlignment) = 678651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines CGM.getContext().getTypeInfoInChars(ivarType); 6791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we have a copy property, we always have to use getProperty/setProperty. 681265941bc308d65cc270d5c4de5806f37ce405606John McCall // TODO: we could actually use setProperty and an expression for non-atomics. 6821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IsCopy) { 6831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = GetSetProperty; 6841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 687265941bc308d65cc270d5c4de5806f37ce405606John McCall // Handle retain. 688265941bc308d65cc270d5c4de5806f37ce405606John McCall if (setterKind == ObjCPropertyDecl::Retain) { 6891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In GC-only, there's nothing special that needs to be done. 6904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { 6911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // fallthrough 6921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In ARC, if the property is non-atomic, use expression emission, 6941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // which translates to objc_storeStrong. This isn't required, but 6951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // it's slightly nicer. 6964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) { 697d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // Using standard expression emission for the setter is only 698d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // acceptable if the ivar is __strong, which won't be true if 699d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // the property is annotated with __attribute__((NSObject)). 700d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // TODO: falling all the way back to objc_setProperty here is 701d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // just laziness, though; we could still use objc_storeStrong 702d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // if we hacked it right. 703d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong) 704d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall Kind = Expression; 705d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall else 706d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall Kind = SetPropertyAndExpressionGet; 7071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we need to at least use setProperty. However, if 7101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the property isn't atomic, we can use normal expression 7111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // emission for the getter. 7121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } else if (!IsAtomic) { 7131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = SetPropertyAndExpressionGet; 7141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we have to use both setProperty and getProperty. 7171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } else { 7181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = GetSetProperty; 7191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we're not atomic, just use expression accesses. 7241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!IsAtomic) { 7251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Expression; 7261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7295889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall // Properties on bitfield ivars need to be emitted using expression 7305889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall // accesses even if they're nominally atomic. 7315889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall if (ivar->isBitField()) { 7325889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall Kind = Expression; 7335889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall return; 7345889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall } 7355889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall 7361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // GC-qualified or ARC-qualified ivars need to be emitted as 7371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // expressions. This actually works out to being atomic anyway, 7381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // except for ARC __strong, but that should trigger the above code. 7391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (ivarType.hasNonTrivialObjCLifetime() || 7404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie (CGM.getLangOpts().getGC() && 7411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.getContext().getObjCGCAttrKind(ivarType))) { 7421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Expression; 7431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Compute whether the ivar has strong members. 7474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC()) 7481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (const RecordType *recordType = ivarType->getAs<RecordType>()) 7491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall HasStrong = recordType->getDecl()->hasObjectMember(); 7501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // We can never access structs with object members with a native 7521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // access, because we need to use write barriers. This is what 7531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // objc_copyStruct is for. 7541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (HasStrong) { 7551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, this is target-dependent and based on the size and 7601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // alignment of the ivar. 761c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall 762c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall // If the size of the ivar is not a power of two, give up. We don't 763c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall // want to get into the business of doing compare-and-swaps. 764c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall if (!IvarSize.isPowerOfTwo()) { 765c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall Kind = CopyStruct; 766c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall return; 767c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall } 768c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall 7691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Triple::ArchType arch = 77064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall CGM.getTarget().getTriple().getArch(); 7711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Most architectures require memory to fit within a single cache 7731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // line, so the alignment has to be at least the size of the access. 7741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise we have to grab a lock. 7751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) { 7761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If the ivar's size exceeds the architecture's maximum atomic 7811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // access size, we have to use CopyStruct. 7821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) { 7831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we can use native loads and stores. 7881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Native; 7891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 790af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 7912ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property getter function. 7922ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// 7932ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize 794489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category. 795fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, 796fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const ObjCPropertyImplDecl *PID) { 797176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::Constant *AtomicHelperFn = 798176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines CodeGenFunction(CGM).GenerateObjCAtomicGetterCopyHelperFunction(PID); 799af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 800af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); 801af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar assert(OMD && "Invalid call to generate getter (empty method)"); 8020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines StartObjCMethod(OMD, IMP->getClassInterface()); 8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 804490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn); 8051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall FinishFunction(); 8071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 8081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8096c11f0b3106263600af2ea0438ebb372821514aaJohn McCallstatic bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) { 8106c11f0b3106263600af2ea0438ebb372821514aaJohn McCall const Expr *getter = propImpl->getGetterCXXConstructor(); 8111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!getter) return true; 8121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Sema only makes only of these when the ivar has a C++ class type, 8141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // so the form is pretty constrained. 8151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8166c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // If the property has a reference type, we might just be binding a 8176c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // reference, in which case the result will be a gl-value. We should 8186c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // treat this as a non-trivial operation. 8196c11f0b3106263600af2ea0438ebb372821514aaJohn McCall if (getter->isGLValue()) 8206c11f0b3106263600af2ea0438ebb372821514aaJohn McCall return false; 8216c11f0b3106263600af2ea0438ebb372821514aaJohn McCall 8221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we selected a trivial copy-constructor, we're okay. 8231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter)) 8241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return (construct->getConstructor()->isTrivial()); 8251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // The constructor might require cleanups (in which case it's never 8271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // trivial). 8281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall assert(isa<ExprWithCleanups>(getter)); 8291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return false; 8301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 8311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 83220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// emitCPPObjectAtomicGetterCall - Call the runtime function to 83320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// copy the ivar into the resturn slot. 83420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianstatic void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, 83520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *returnAddr, 83620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ObjCIvarDecl *ivar, 83720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Constant *AtomicHelperFn) { 83820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar, 83920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // AtomicHelperFn); 84020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CallArgList args; 84120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 84220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // The 1st argument is the return Slot. 84320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy); 84420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 84520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // The 2nd argument is the address of the ivar. 84620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *ivarAddr = 84787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 84887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGF.LoadObjCSelf(), ivar, 0).getPointer(); 84920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 85020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 85120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 85220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // Third argument is the helper function. 85320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 85420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 85520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *copyCppAtomicObjectFn = 856d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction(); 8574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.EmitCall( 8584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args), 85920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian copyCppAtomicObjectFn, ReturnValueSlot(), args); 86020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian} 86120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 8621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallvoid 8631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallCodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, 864b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian const ObjCPropertyImplDecl *propImpl, 865490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian const ObjCMethodDecl *GetterMethodDecl, 866b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn) { 8671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If there's a non-trivial 'get' expression, we just have to emit that. 8681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!hasTrivialGetExpr(propImpl)) { 86920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (!AtomicHelperFn) { 87020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(), 8716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines /*nrvo*/ nullptr); 87220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian EmitReturnStmt(ret); 87320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian } 87420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian else { 87520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 87687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar emitCPPObjectAtomicGetterCall(*this, ReturnValue.getPointer(), 87720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ivar, AtomicHelperFn); 87820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian } 8791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 8801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 8811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 8831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType propType = prop->getType(); 8841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl(); 8851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 8871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Pick an implementation strategy. 8891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy strategy(CGM, propImpl); 8901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall switch (strategy.getKind()) { 8911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Native: { 892aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman // We don't need to do anything for a zero-size struct. 893aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman if (strategy.getIvarSize().isZero()) 894aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman return; 895aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman 8961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 8971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Currently, all atomic accesses have to be through integer 8991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // types, so there's no point in trying to pick a prettier type. 9004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar uint64_t ivarSize = getContext().toBits(strategy.getIvarSize()); 9014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Type *bitcastType = llvm::Type::getIntNTy(getLLVMContext(), ivarSize); 9021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 9031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Perform an atomic load. This does not impose ordering constraints. 90587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address ivarAddr = LV.getAddress(); 9061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 9071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); 9084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar load->setAtomic(llvm::AtomicOrdering::Unordered); 9091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Store that value into the return address. Doing this with a 9111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // bitcast is likely to produce some pretty ugly IR, but it's not 9121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the *most* terrible thing in the world. 9134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Type *retTy = ConvertType(getterMethod->getReturnType()); 9144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar uint64_t retTySize = CGM.getDataLayout().getTypeSizeInBits(retTy); 9154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *ivarVal = load; 9164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar if (ivarSize > retTySize) { 9174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Type *newTy = llvm::Type::getIntNTy(getLLVMContext(), retTySize); 9184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar ivarVal = Builder.CreateTrunc(load, newTy); 9194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar bitcastType = newTy->getPointerTo(); 9204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 9214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Builder.CreateStore(ivarVal, 9224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Builder.CreateBitCast(ReturnValue, bitcastType)); 9231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Make sure we don't do an autorelease. 9251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall AutoreleaseResult = false; 9261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 9271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 9281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::GetSetProperty: { 9301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *getPropertyFn = 9311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.getObjCRuntime().GetPropertyGetFunction(); 9321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!getPropertyFn) { 9331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy"); 934c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar return; 935c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 936c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar 937c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). 938c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // FIXME: Can't this be simpler? This might even be worse than the 939c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // corresponding gcc code. 9401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *cmd = 94187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Builder.CreateLoad(GetAddrOfLocalVar(getterMethod->getCmdDecl()), "cmd"); 9421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 9431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarOffset = 9441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitIvarOffset(classImpl->getClassInterface(), ivar); 9451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CallArgList args; 9471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(self), getContext().getObjCIdType()); 9481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(cmd), getContext().getObjCSelType()); 9491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 950265941bc308d65cc270d5c4de5806f37ce405606John McCall args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 951265941bc308d65cc270d5c4de5806f37ce405606John McCall getContext().BoolTy); 9521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 953e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar // FIXME: We shouldn't need to get the function info here, the 954e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar // runtime already should have computed it to build the function. 955651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines llvm::Instruction *CallInstruction; 95687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar RValue RV = EmitCall( 9574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar getTypes().arrangeBuiltinFunctionCall(propType, args), 95887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar getPropertyFn, ReturnValueSlot(), args, CGCalleeInfo(), 95987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar &CallInstruction); 960651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(CallInstruction)) 961651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines call->setTailCall(); 9621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 963c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // We need to fix the type here. Ivars with copy & retain are 964c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // always objects so we don't need to worry about complex or 965c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // aggregates. 966651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RV = RValue::get(Builder.CreateBitCast( 967651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RV.getScalarVal(), 968651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines getTypes().ConvertType(getterMethod->getReturnType()))); 9691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitReturnOfRValue(RV, propType); 971f85e193739c953358c865005855253af4f68a497John McCall 972f85e193739c953358c865005855253af4f68a497John McCall // objc_getProperty does an autorelease, so we should suppress ours. 973f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = false; 9741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 9761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 9771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::CopyStruct: 9791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall emitStructGetterCall(*this, ivar, strategy.isAtomic(), 9801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall strategy.hasStrongMember()); 9811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 9821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Expression: 9841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::SetPropertyAndExpressionGet: { 9851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 9861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType ivarType = ivar->getType(); 9889d232c884ea9872d6555df0fd7359699819bc1f1John McCall switch (getEvaluationKind(ivarType)) { 9899d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Complex: { 9904ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky ComplexPairTy pair = EmitLoadOfComplex(LV, SourceLocation()); 99187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar EmitStoreOfComplex(pair, MakeAddrLValue(ReturnValue, ivarType), 9929d232c884ea9872d6555df0fd7359699819bc1f1John McCall /*init*/ true); 9939d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 9949d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 9959d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Aggregate: 9961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // The return value slot is guaranteed to not be aliased, but 9971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // that's not necessarily the same as "on the stack", so 9981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // we still potentially need objc_memmove_collectable. 999649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType); 10009d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 10019d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Scalar: { 1002ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall llvm::Value *value; 1003ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (propType->isReferenceType()) { 100487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar value = LV.getAddress().getPointer(); 1005ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } else { 1006ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // We want to load and autoreleaseReturnValue ARC __weak ivars. 1007ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { 100887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (getLangOpts().ObjCAutoRefCount) { 100987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar value = emitARCRetainLoadOfScalar(*this, LV, ivarType); 101087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } else { 101187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar value = EmitARCLoadWeak(LV.getAddress()); 101287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar } 1013ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 1014ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // Otherwise we want to do a simple load, suppressing the 1015ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // final autorelease. 1016f85e193739c953358c865005855253af4f68a497John McCall } else { 10174ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky value = EmitLoadOfLValue(LV, SourceLocation()).getScalarVal(); 1018ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall AutoreleaseResult = false; 101914086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian } 1020f85e193739c953358c865005855253af4f68a497John McCall 1021651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines value = Builder.CreateBitCast( 1022651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines value, ConvertType(GetterMethodDecl->getReturnType())); 1023ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } 1024ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 1025ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall EmitReturnOfRValue(RValue::get(value), propType); 10269d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 1027ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian } 10289d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 10299d232c884ea9872d6555df0fd7359699819bc1f1John McCall llvm_unreachable("bad evaluation kind"); 1030c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 1031af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 10321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 10331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm_unreachable("bad @property implementation strategy!"); 1034af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1035af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 103641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructSetterCall - Call the runtime function to store the value 103741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// from the first formal parameter into the given ivar. 103841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, 103941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ObjCIvarDecl *ivar) { 10402846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // objc_copyStruct (&structIvar, &Arg, 10412846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // sizeof (struct something), true, false); 1042bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall CallArgList args; 1043bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 1044bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The first argument is the address of the ivar. 104541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 104641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.LoadObjCSelf(), ivar, 0) 104787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar .getPointer(); 104841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 104941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 1050bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 1051bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The second argument is the address of the parameter variable. 105241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ParmVarDecl *argVar = *OMD->param_begin(); 1053f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 1054c3953aab1b6167b7f394ca87096e91ab840026adFariborz Jahanian VK_LValue, SourceLocation()); 105587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(); 105641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 105741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1058bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 1059bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The third argument is the sizeof the type. 1060bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall llvm::Value *size = 106141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); 106241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(size), CGF.getContext().getSizeType()); 106341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 106441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fourth argument is the 'isAtomic' flag. 106541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); 1066bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 106741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fifth argument is the 'hasStrong' flag. 106841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // FIXME: should this really always be false? 106941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); 1070bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 107141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); 10724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.EmitCall( 10734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args), 107441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall copyStructFn, ReturnValueSlot(), args); 10752846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian} 10762846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian 1077cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// emitCPPObjectAtomicSetterCall - Call the runtime function to store 1078cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the value from the first formal parameter into the given ivar, using 1079cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the Cpp API for atomic Cpp objects with non-trivial copy assignment. 1080cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanianstatic void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, 1081cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *OMD, 1082cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar, 1083cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 1084cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg, 1085cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // AtomicHelperFn); 1086cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CallArgList args; 1087cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1088cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The first argument is the address of the ivar. 1089cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *ivarAddr = 1090cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 109187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGF.LoadObjCSelf(), ivar, 0).getPointer(); 1092cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 1093cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 1094cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1095cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The second argument is the address of the parameter variable. 1096cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ParmVarDecl *argVar = *OMD->param_begin(); 1097f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 1098cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian VK_LValue, SourceLocation()); 109987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(); 1100cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 1101cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1102cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1103cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // Third argument is the helper function. 1104cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 1105cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1106cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *copyCppAtomicObjectFn = 1107d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction(); 11084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.EmitCall( 11094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args), 1110cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian copyCppAtomicObjectFn, ReturnValueSlot(), args); 1111cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian} 1112cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 111320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 11141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { 11151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Expr *setter = PID->getSetterCXXAssignment(); 11161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!setter) return true; 11171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Sema only makes only of these when the ivar has a C++ class type, 11191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // so the form is pretty constrained. 112071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 112171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // An operator call is trivial if the function it calls is trivial. 11221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This also implies that there's nothing non-trivial going on with 11231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the arguments, because operator= can only be trivial if it's a 11241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // synthesized assignment operator and therefore both parameters are 11251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // references. 11261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (CallExpr *call = dyn_cast<CallExpr>(setter)) { 112771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (const FunctionDecl *callee 112871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) 112971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (callee->isTrivial()) 113071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return true; 113171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 113201cb307f0b84a368cdbc0738d6680aab8ed7423fFariborz Jahanian } 113371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall assert(isa<ExprWithCleanups>(setter)); 113571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 113671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 113786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar 11384e494cf56325f26a6c3eb5a065c35e54835f14e6Benjamin Kramerstatic bool UseOptimizedSetter(CodeGenModule &CGM) { 11394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() != LangOptions::NonGC) 1140ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return false; 11417a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter(); 1142ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 1143ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 114471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid 114571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallCodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, 1146cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyImplDecl *propImpl, 1147cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 1148cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 1149cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 1150cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl(); 1151cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 115271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Just use the setter expression if Sema gave us one and it's 1153cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // non-trivial. 11541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!hasTrivialSetExpr(propImpl)) { 1155cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian if (!AtomicHelperFn) 1156cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If non-atomic, assignment is called directly. 1157cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian EmitStmt(propImpl->getSetterCXXAssignment()); 1158cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian else 1159cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If atomic, assignment is called via a locking api. 1160cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, 1161cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian AtomicHelperFn); 116271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 116371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 116471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy strategy(CGM, propImpl); 11661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall switch (strategy.getKind()) { 11671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Native: { 1168aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman // We don't need to do anything for a zero-size struct. 1169aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman if (strategy.getIvarSize().isZero()) 1170aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman return; 1171aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman 117287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin()); 117371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue ivarLValue = 11751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0); 117687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address ivarAddr = ivarLValue.getAddress(); 11771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Currently, all atomic accesses have to be through integer 11791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // types, so there's no point in trying to pick a prettier type. 11801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type *bitcastType = 11811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type::getIntNTy(getLLVMContext(), 11821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getContext().toBits(strategy.getIvarSize())); 11831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Cast both arguments to the chosen operation type. 118587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar argAddr = Builder.CreateElementBitCast(argAddr, bitcastType); 118687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar ivarAddr = Builder.CreateElementBitCast(ivarAddr, bitcastType); 118771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This bitcast load is likely to cause some nasty IR. 11891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *load = Builder.CreateLoad(argAddr); 11901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Perform an atomic store. There are no memory ordering requirements. 11921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); 11934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar store->setAtomic(llvm::AtomicOrdering::Unordered); 11941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 11951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 11961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::GetSetProperty: 11981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::SetPropertyAndExpressionGet: { 11996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 12006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines llvm::Value *setOptimizedPropertyFn = nullptr; 12016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines llvm::Value *setPropertyFn = nullptr; 1202ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (UseOptimizedSetter(CGM)) { 12037a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar // 10.8 and iOS 6.0 code and GC is off 1204ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn = 120516098f366097305c348b356a44638d6c959c6e60Eric Christopher CGM.getObjCRuntime() 120616098f366097305c348b356a44638d6c959c6e60Eric Christopher .GetOptimizedPropertySetFunction(strategy.isAtomic(), 120716098f366097305c348b356a44638d6c959c6e60Eric Christopher strategy.isCopy()); 1208ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setOptimizedPropertyFn) { 1209ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI"); 1210ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1211ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 121286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 1213ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek else { 1214ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction(); 1215ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setPropertyFn) { 1216ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); 1217ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1218ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1219ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1220ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Emit objc_setProperty((id) self, _cmd, offset, arg, 122286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar // <is-atomic>, <is-copy>). 122371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *cmd = 122487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Builder.CreateLoad(GetAddrOfLocalVar(setterMethod->getCmdDecl())); 122571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *self = 122671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 122771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *ivarOffset = 122871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitIvarOffset(classImpl->getClassInterface(), ivar); 122987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin()); 123087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Value *arg = Builder.CreateLoad(argAddr, "arg"); 123187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar arg = Builder.CreateBitCast(arg, VoidPtrTy); 123271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 123371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CallArgList args; 123471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(self), getContext().getObjCIdType()); 123571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(cmd), getContext().getObjCSelType()); 1236ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (setOptimizedPropertyFn) { 1237ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1238ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 12394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args), 1240ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn, ReturnValueSlot(), args); 1241ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else { 1242ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1243ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1244ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 1245ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1246ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isCopy())), 1247ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1248ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // FIXME: We shouldn't need to get the function info here, the runtime 1249ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // already should have computed it to build the function. 12504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args), 1251ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn, ReturnValueSlot(), args); 1252ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1253ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 125471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 125571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 125671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 12571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::CopyStruct: 125841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall emitStructSetterCall(*this, setterMethod, ivar); 125971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 12601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 12611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Expression: 12621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall break; 126371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 126471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 126571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Otherwise, fake up some ASTs and emit a normal assignment. 126671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ValueDecl *selfDecl = setterMethod->getSelfDecl(); 1267f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr self(selfDecl, false, selfDecl->getType(), 1268f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, SourceLocation()); 126971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, 127071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall selfDecl->getType(), CK_LValueToRValue, &self, 127171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 127271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), 12730c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian SourceLocation(), SourceLocation(), 12740c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian &selfLoad, true, true); 127571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 127671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ParmVarDecl *argDecl = *setterMethod->param_begin(); 127771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall QualType argType = argDecl->getType().getNonReferenceType(); 1278f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation()); 127971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, 128071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argType.getUnqualifiedType(), CK_LValueToRValue, 128171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall &arg, VK_RValue); 128245e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar 128371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The property type can differ from the ivar type in some situations with 128471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Objective-C pointer types, we can always bit cast the RHS in these cases. 128571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The following absurdity is just to ensure well-formed IR. 128671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CastKind argCK = CK_NoOp; 128771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (ivarRef.getType()->isObjCObjectPointerType()) { 128871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isObjCObjectPointerType()) 128971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 129071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else if (argLoad.getType()->isBlockPointerType()) 129171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BlockPointerToObjCPointerCast; 129271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 129371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_CPointerToObjCPointerCast; 129471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isBlockPointerType()) { 129571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isBlockPointerType()) 129671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 129771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 129871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_AnyPointerToBlockPointerCast; 129971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isPointerType()) { 130071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 130186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 130271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, 130371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), argCK, &argLoad, 130471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 130571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Expr *finalArg = &argLoad; 130671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), 130771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argLoad.getType())) 130871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall finalArg = &argCast; 130971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 131071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 131171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall BinaryOperator assign(&ivarRef, finalArg, BO_Assign, 131271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), VK_RValue, OK_Ordinary, 1313be9af1288881110e406b87914162eaa59f1e5918Lang Hames SourceLocation(), false); 131471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitStmt(&assign); 131571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 131671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 13172ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property setter function. 13182ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// 13192ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize 132071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall/// is illegal within a category. 132171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, 132271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyImplDecl *PID) { 1323176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::Constant *AtomicHelperFn = 1324176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines CodeGenFunction(CGM).GenerateObjCAtomicSetterCopyHelperFunction(PID); 132571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 132671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); 132771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall assert(OMD && "Invalid call to generate setter (empty method)"); 13280e2c34f92f00628d48968dfea096d36381f494cbStephen Hines StartObjCMethod(OMD, IMP->getClassInterface()); 132971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 1330cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian generateObjCSetterBody(IMP, PID, AtomicHelperFn); 1331af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1332af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar FinishFunction(); 13334111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 13344111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 1335e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallnamespace { 133687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar struct DestroyIvar final : EHScopeStack::Cleanup { 13379928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall private: 13389928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *addr; 1339e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall const ObjCIvarDecl *ivar; 1340516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne CodeGenFunction::Destroyer *destroyer; 13419928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray; 13429928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall public: 13439928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, 13449928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CodeGenFunction::Destroyer *destroyer, 13459928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray) 1346516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne : addr(addr), ivar(ivar), destroyer(destroyer), 13479928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall useEHCleanupForArray(useEHCleanupForArray) {} 1348e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1349651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 13509928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall LValue lvalue 13519928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); 13529928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer, 1353ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall flags.isForNormalCleanup() && useEHCleanupForArray); 1354e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1355e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall }; 1356e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1357e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 13589928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall/// Like CodeGenFunction::destroyARCStrong, but do it with a call. 13599928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCallstatic void destroyARCStrongWithStore(CodeGenFunction &CGF, 136087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address addr, 13619928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType type) { 13629928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *null = getNullForVariable(addr); 13639928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 13649928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall} 1365f85e193739c953358c865005855253af4f68a497John McCall 1366e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallstatic void emitCXXDestructMethod(CodeGenFunction &CGF, 1367e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ObjCImplementationDecl *impl) { 1368e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall CodeGenFunction::RunCleanupsScope scope(CGF); 1369e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1370e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall llvm::Value *self = CGF.LoadObjCSelf(); 1371e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1372db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose const ObjCInterfaceDecl *iface = impl->getClassInterface(); 1373db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 1374e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ivar; ivar = ivar->getNextIvar()) { 1375e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall QualType type = ivar->getType(); 1376e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1377e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Check whether the ivar is a destructible type. 13789928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType::DestructionKind dtorKind = type.isDestructedType(); 13799928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (!dtorKind) continue; 13809928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines CodeGenFunction::Destroyer *destroyer = nullptr; 13829928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13839928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Use a call to objc_storeStrong to destroy strong ivars, for the 13849928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // general benefit of the tools. 13859928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (dtorKind == QualType::DK_objc_strong_lifetime) { 1386516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = destroyARCStrongWithStore; 13879928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13889928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Otherwise use the default for the destruction kind. 13899928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall } else { 1390516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = CGF.getDestroyer(dtorKind); 1391e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 13929928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13939928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); 13949928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13959928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, 13969928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall cleanupKind & EHCleanup); 1397e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1398e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1399e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); 1400e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1401e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1402109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, 1403109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCMethodDecl *MD, 1404109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian bool ctor) { 1405109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); 14060e2c34f92f00628d48968dfea096d36381f494cbStephen Hines StartObjCMethod(MD, IMP->getClassInterface()); 1407e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1408e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_construct. 1409109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian if (ctor) { 1410f85e193739c953358c865005855253af4f68a497John McCall // Suppress the final autorelease in ARC. 1411f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = false; 1412f85e193739c953358c865005855253af4f68a497John McCall 1413651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto *IvarInit : IMP->inits()) { 141400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet FieldDecl *Field = IvarInit->getAnyMember(); 1415651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); 14169b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LValue LV = EmitLValueForIvar(TypeOfSelfObject(), 14179b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LoadObjCSelf(), Ivar, 0); 14187c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall EmitAggExpr(IvarInit->getInit(), 14197c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed, 1420410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 1421649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 1422109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1423109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian // constructor returns 'self'. 1424109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian CodeGenTypes &Types = CGM.getTypes(); 1425109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian QualType IdTy(CGM.getContext().getObjCIdType()); 1426109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian llvm::Value *SelfAsId = 1427109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); 1428109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); 1429e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1430e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_destruct. 1431bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth } else { 1432e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall emitCXXDestructMethod(*this, IMP); 1433109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1434109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian FinishFunction(); 1435109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian} 1436109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian 1437c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbarllvm::Value *CodeGenFunction::LoadObjCSelf() { 1438f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl(); 1439f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall DeclRefExpr DRE(Self, /*is enclosing local*/ (CurFuncDecl != CurCodeDecl), 1440f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall Self->getType(), VK_LValue, SourceLocation()); 14414ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation()); 14424111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 14434111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 144445012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz JahanianQualType CodeGenFunction::TypeOfSelfObject() { 144545012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 144645012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); 144714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( 144814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff getContext().getCanonicalType(selfDecl->getType())); 144945012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian return PTy->getPointeeType(); 145045012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian} 145145012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian 145274391b48b4791cded373683a3baf67314f358d50Chris Lattnervoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ 14531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *EnumerationMutationFn = 1454c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.getObjCRuntime().EnumerationMutationFunction(); 14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1456c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar if (!EnumerationMutationFn) { 1457c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); 1458c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar return; 1459c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 1460c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar 1461bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel CGDebugInfo *DI = getDebugInfo(); 146273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 146373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 1464bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 14659d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel // The local variable comes into scope immediately. 14669d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel AutoVarEmission variable = AutoVarEmission::invalid(); 14679d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) 14689d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); 14699d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel 1470d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); 14711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1472f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Fast enumeration state. 14730815b579b31cb3129f732bb7ea36fd6ba6949e98Douglas Gregor QualType StateTy = CGM.getObjCFastEnumerationStateType(); 147487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address StatePtr = CreateMemTemp(StateTy, "state.ptr"); 14751884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson EmitNullInitialization(StatePtr, StateTy); 14761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1477f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Number of elements in the items array. 14782abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson static const unsigned NumItems = 16; 14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1480d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the countByEnumeratingWithState:objects:count: selector. 1481ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer IdentifierInfo *II[] = { 1482ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("countByEnumeratingWithState"), 1483ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("objects"), 1484ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("count") 1485ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer }; 1486ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer Selector FastEnumSel = 1487ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); 1488f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1489f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson QualType ItemsTy = 1490f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().getConstantArrayType(getContext().getObjCIdType(), 14911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::APInt(32, NumItems), 1492f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson ArrayType::Normal, 0); 149387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); 14941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar RunCleanupsScope ForScope(*this); 14964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 1497990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Emit the collection pointer. In ARC, we do a retain. 1498990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall llvm::Value *Collection; 14994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 1500990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitARCRetainScalarExpr(S.getCollection()); 1501990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1502990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Enter a cleanup to do the release. 1503990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall EmitObjCConsumeObject(S.getCollection()->getType(), Collection); 1504990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } else { 1505990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitScalarExpr(S.getCollection()); 1506990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15084b302d32378b364703b212834f908762e570c29cJohn McCall // The 'continue' label needs to appear within the cleanup for the 15094b302d32378b364703b212834f908762e570c29cJohn McCall // collection object. 15104b302d32378b364703b212834f908762e570c29cJohn McCall JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); 15114b302d32378b364703b212834f908762e570c29cJohn McCall 1512d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Send it our message: 1513f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson CallArgList Args; 1514d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1515d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The first argument is a temporary of the enumeration-state type. 151687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Args.add(RValue::get(StatePtr.getPointer()), 151787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar getContext().getPointerType(StateTy)); 15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1519d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The second argument is a temporary array with space for NumItems 1520d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointers. We'll actually be loading elements from the array 1521d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointer written into the control state; this buffer is so that 1522d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // collections that *aren't* backed by arrays can still queue up 1523d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // batches of elements. 152487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Args.add(RValue::get(ItemsPtr.getPointer()), 152587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar getContext().getPointerType(ItemsTy)); 15261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1527d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The third argument is the capacity of that temporary array. 15282acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); 15294a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); 153004c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(Count), getContext().UnsignedLongTy); 15311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1532d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start the enumeration. 15331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump RValue CountRV = 1534ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1535f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 1536f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson FastEnumSel, 1537c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 1538f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1539d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The initial number of objects that were returned in the buffer. 1540d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialBufferLimit = CountRV.getScalarVal(); 15411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1542d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); 1543d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); 1544f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1545d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); 1546f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1547d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If the limit pointer was zero to begin with, the collection is 1548651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // empty; skip all this. Set the branch weight assuming this has the same 1549651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // probability of exiting the loop as any other loop exit. 1550b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar uint64_t EntryCount = getCurrentProfileCount(); 1551b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar Builder.CreateCondBr( 1552b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), EmptyBB, 1553b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar LoopInitBB, 1554b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar createProfileWeights(EntryCount, getProfileCount(S.getBody()))); 15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1556d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, initialize the loop. 1557d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopInitBB); 15581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1559d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Save the initial mutations value. This is the value at an 1560d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // address that was written into the state object by 1561d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // countByEnumeratingWithState:objects:count:. 156287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address StateMutationsPtrPtr = Builder.CreateStructGEP( 156387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar StatePtr, 2, 2 * getPointerSize(), "mutationsptr.ptr"); 156487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Value *StateMutationsPtr 156587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); 15661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1567d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialMutations = 156887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(), 156987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar "forcoll.initial-mutations"); 15701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1571d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start looping. This is the point we return to whenever we have a 1572d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // fresh, non-empty batch of objects. 1573d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); 1574d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopBodyBB); 15751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1576d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current index into the buffer. 1577bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index"); 1578d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, LoopInitBB); 1579f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1580d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current buffer size. 1581bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count"); 1582d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(initialBufferLimit, LoopInitBB); 1583f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1584b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar incrementProfileCounter(&S); 1585651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 1586d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether the mutations value has changed from where it was 1587d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // at start. StateMutationsPtr should actually be invariant between 1588d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes. 15892abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); 1590d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *currentMutations 159187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar = Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(), 159287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar "statemutations"); 15931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1594d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); 1595361cf980a7d976ef11a37b49567412b6b63a89d7Dan Gohman llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); 15961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1597d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), 1598d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall WasNotMutatedBB, WasMutatedBB); 15991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1600d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If so, call the enumeration-mutation function. 1601d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasMutatedBB); 16022abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson llvm::Value *V = 16031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Builder.CreateBitCast(Collection, 1604578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer ConvertType(getContext().getObjCIdType())); 16052b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar CallArgList Args2; 160604c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args2.add(RValue::get(V), getContext().getObjCIdType()); 1607f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We shouldn't need to get the function info here, the runtime already 1608f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // should have computed it to build the function. 16094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar EmitCall( 16104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGM.getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, Args2), 1611f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson EnumerationMutationFn, ReturnValueSlot(), Args2); 16121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1613d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, or if the mutation function returns, just continue. 1614d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasNotMutatedBB); 16151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1616d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Initialize the element variable. 1617d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope elementVariableScope(*this); 161857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall bool elementIsVariable; 1619d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall LValue elementLValue; 1620d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall QualType elementType; 1621d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { 162257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // Initialize the variable, in case it's a __block variable or something. 162357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarInit(variable); 1624f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 162557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); 1626f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(), 1627d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall VK_LValue, SourceLocation()); 1628d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(&tempDRE); 1629d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = D->getType(); 163057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = true; 16317acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 16327acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (D->isARCPseudoStrong()) 16337acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); 1634d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } else { 1635d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = LValue(); // suppress warning 1636d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = cast<Expr>(S.getElement())->getType(); 163757b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = false; 1638d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 16392acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *convertedElementType = ConvertType(elementType); 1640f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1641d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the buffer out of the enumeration state. 1642d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // TODO: this pointer should actually be invariant between 1643d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes, which would help us do certain loop optimizations. 164487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address StateItemsPtr = Builder.CreateStructGEP( 164587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar StatePtr, 1, getPointerSize(), "stateitems.ptr"); 1646d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *EnumStateItems = 1647d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateLoad(StateItemsPtr, "stateitems"); 1648f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1649d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the value at the current index from the buffer. 16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *CurrentItemPtr = 1651d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); 165287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Value *CurrentItem = 165387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Builder.CreateAlignedLoad(CurrentItemPtr, getPointerAlign()); 16541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1655d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Cast that value to the right type. 1656d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, 1657d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall "currentitem"); 16581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1659d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Make sure we have an l-value. Yes, this gets evaluated every 1660d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // time through the loop. 16617acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (!elementIsVariable) { 1662d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1663545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); 16647acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } else { 16657acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall EmitScalarInit(CurrentItem, elementLValue); 16667acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } 16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 166857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // If we do have an element variable, this assignment is the end of 166957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // its initialization. 167057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (elementIsVariable) 167157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarCleanups(variable); 167257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall 1673d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Perform the loop body, setting up break and continue labels. 1674e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); 1675d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall { 1676d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope Scope(*this); 1677d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitStmt(S.getBody()); 1678d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 1679f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson BreakContinueStack.pop_back(); 16801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1681d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Destroy the element variable now. 1682d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementVariableScope.ForceCleanup(); 1683d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1684d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether there are more elements. 1685ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(AfterBody.getBlock()); 16861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1687d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); 1688d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1689d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // First we check in the local buffer. 1690d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *indexPlusOne 1691d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); 1692d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1693d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we haven't overrun the buffer yet, we can continue. 1694651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // Set the branch weights based on the simplifying assumption that this is 1695651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // like a while-loop, i.e., ignoring that the false branch fetches more 1696651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // elements and then returns to the loop. 1697b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar Builder.CreateCondBr( 1698b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar Builder.CreateICmpULT(indexPlusOne, count), LoopBodyBB, FetchMoreBB, 1699b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar createProfileWeights(getProfileCount(S.getBody()), EntryCount)); 1700f0906c4edb37b20141428ca77fa7dfd00b976eafFariborz Jahanian 1701d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(indexPlusOne, AfterBody.getBlock()); 1702d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(count, AfterBody.getBlock()); 1703f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1704d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, we have to fetch more elements. 1705d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(FetchMoreBB); 17061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CountRV = 1708ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1709f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 17101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FastEnumSel, 1711c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 17121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1713d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we got a zero count, we're done. 1714d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *refetchCount = CountRV.getScalarVal(); 1715d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1716d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // (note that the message send might split FetchMoreBB) 1717d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, Builder.GetInsertBlock()); 1718d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(refetchCount, Builder.GetInsertBlock()); 1719d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1720d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), 1721d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmptyBB, LoopBodyBB); 17221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1723f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // No more elements. 1724d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(EmptyBB); 1725f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 172657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (!elementIsVariable) { 1727f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // If the element was not a declaration, set it to be null. 1728f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1729d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); 1730d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1731545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(null), elementLValue); 1732f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson } 1733f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 173473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 173573fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 1736bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 17374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar ForScope.ForceCleanup(); 1738ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopEnd.getBlock()); 17393d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson} 17403d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 17411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { 1742f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitTryStmt(*this, S); 174364d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 174464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 17451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { 174664d5d6c5903157c521af496479d06dc26032d718Anders Carlsson CGM.getObjCRuntime().EmitThrowStmt(*this, S); 174764d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 174864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 174910cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattnervoid CodeGenFunction::EmitObjCAtSynchronizedStmt( 17501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ObjCAtSynchronizedStmt &S) { 1751f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); 175210cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner} 175310cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner 1754f85e193739c953358c865005855253af4f68a497John McCallnamespace { 175587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar struct CallObjCRelease final : EHScopeStack::Cleanup { 1756bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall CallObjCRelease(llvm::Value *object) : object(object) {} 1757bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall llvm::Value *object; 1758f85e193739c953358c865005855253af4f68a497John McCall 1759651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 17605b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall // Releases at the end of the full-expression are imprecise. 17615b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCRelease(object, ARCImpreciseLifetime); 1762f85e193739c953358c865005855253af4f68a497John McCall } 1763f85e193739c953358c865005855253af4f68a497John McCall }; 1764f85e193739c953358c865005855253af4f68a497John McCall} 1765f85e193739c953358c865005855253af4f68a497John McCall 176633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCConsumeObject. Does a primitive 1767f85e193739c953358c865005855253af4f68a497John McCall/// release at the end of the full-expression. 1768f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, 1769f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *object) { 1770f85e193739c953358c865005855253af4f68a497John McCall // If we're in a conditional branch, we need to make the cleanup 1771bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall // conditional. 1772bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); 1773f85e193739c953358c865005855253af4f68a497John McCall return object; 1774f85e193739c953358c865005855253af4f68a497John McCall} 1775f85e193739c953358c865005855253af4f68a497John McCall 1776f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, 1777f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 1778f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutorelease(type, value); 1779f85e193739c953358c865005855253af4f68a497John McCall} 1780f85e193739c953358c865005855253af4f68a497John McCall 1781b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// Given a number of pointers, inform the optimizer that they're 1782b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// being intrinsically used up until this point in the program. 1783b6a6079449a5275c283982e19b0c38e165833bb2John McCallvoid CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) { 178487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Constant *&fn = CGM.getObjCEntrypoints().clang_arc_use; 1785b6a6079449a5275c283982e19b0c38e165833bb2John McCall if (!fn) { 1786b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::FunctionType *fnType = 1787176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::FunctionType::get(CGM.VoidTy, None, true); 1788b6a6079449a5275c283982e19b0c38e165833bb2John McCall fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use"); 1789b6a6079449a5275c283982e19b0c38e165833bb2John McCall } 1790b6a6079449a5275c283982e19b0c38e165833bb2John McCall 1791b6a6079449a5275c283982e19b0c38e165833bb2John McCall // This isn't really a "runtime" function, but as an intrinsic it 1792b6a6079449a5275c283982e19b0c38e165833bb2John McCall // doesn't really matter as long as we align things up. 1793b6a6079449a5275c283982e19b0c38e165833bb2John McCall EmitNounwindRuntimeCall(fn, values); 1794b6a6079449a5275c283982e19b0c38e165833bb2John McCall} 1795b6a6079449a5275c283982e19b0c38e165833bb2John McCall 1796f85e193739c953358c865005855253af4f68a497John McCall 1797f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM, 17982acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *type, 17995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1800f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName); 1801f85e193739c953358c865005855253af4f68a497John McCall 1802554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) { 1803cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // If the target runtime doesn't naturally support ARC, emit weak 1804cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // references to the runtime support library. We don't really 1805cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // permit this to fail, but we need a particular relocation style. 1806554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 1807f85e193739c953358c865005855253af4f68a497John McCall f->setLinkage(llvm::Function::ExternalWeakLinkage); 1808554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } else if (fnName == "objc_retain" || fnName == "objc_release") { 1809554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // If we have Native ARC, set nonlazybind attribute for these APIs for 1810554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // performance. 181172390b39c545426023ec104afe8706395d732badBill Wendling f->addFnAttr(llvm::Attribute::NonLazyBind); 1812db99e8b72ae2b66d3c2f8d896f49513b8d8e2633Michael Gottesman } 1813554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } 1814f85e193739c953358c865005855253af4f68a497John McCall 1815f85e193739c953358c865005855253af4f68a497John McCall return fn; 1816f85e193739c953358c865005855253af4f68a497John McCall} 1817f85e193739c953358c865005855253af4f68a497John McCall 1818f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the signature 1819f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8*) 1820f85e193739c953358c865005855253af4f68a497John McCall/// where a null input causes a no-op and returns null. 1821f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCValueOperation(CodeGenFunction &CGF, 1822f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1823f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 1824df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier StringRef fnName, 1825df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier bool isTailCall = false) { 1826f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 1827f85e193739c953358c865005855253af4f68a497John McCall 1828f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 18292acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 183076ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false); 1831f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1832f85e193739c953358c865005855253af4f68a497John McCall } 1833f85e193739c953358c865005855253af4f68a497John McCall 1834f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 18352acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1836f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); 1837f85e193739c953358c865005855253af4f68a497John McCall 1838f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 1839bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value); 1840df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier if (isTailCall) 1841df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier call->setTailCall(); 1842f85e193739c953358c865005855253af4f68a497John McCall 1843f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to the original type. 1844f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(call, origType); 1845f85e193739c953358c865005855253af4f68a497John McCall} 1846f85e193739c953358c865005855253af4f68a497John McCall 1847f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1848f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**) 1849f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, 185087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address addr, 1851f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1853f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 18542acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 185576ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false); 1856f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1857f85e193739c953358c865005855253af4f68a497John McCall } 1858f85e193739c953358c865005855253af4f68a497John McCall 1859f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 186087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Type *origType = addr.getElementType(); 1861f85e193739c953358c865005855253af4f68a497John McCall addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); 1862f85e193739c953358c865005855253af4f68a497John McCall 1863f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 186487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr.getPointer()); 1865f85e193739c953358c865005855253af4f68a497John McCall 1866f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to a dereference of the original type. 186787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar if (origType != CGF.Int8PtrTy) 186887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar result = CGF.Builder.CreateBitCast(result, origType); 1869f85e193739c953358c865005855253af4f68a497John McCall 1870f85e193739c953358c865005855253af4f68a497John McCall return result; 1871f85e193739c953358c865005855253af4f68a497John McCall} 1872f85e193739c953358c865005855253af4f68a497John McCall 1873f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1874f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**, i8*) 1875f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, 187687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address addr, 1877f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1878f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName, 1880f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 188187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar assert(addr.getElementType() == value->getType()); 1882f85e193739c953358c865005855253af4f68a497John McCall 1883f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 18841d236ab930816f5da27bade92904914c44b73b4cBenjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy }; 1885f85e193739c953358c865005855253af4f68a497John McCall 18862acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1887f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false); 1888f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1889f85e193739c953358c865005855253af4f68a497John McCall } 1890f85e193739c953358c865005855253af4f68a497John McCall 18912acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1892f85e193739c953358c865005855253af4f68a497John McCall 1893bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 189487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGF.Builder.CreateBitCast(addr.getPointer(), CGF.Int8PtrPtrTy), 1895bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy) 1896bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1897bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args); 1898f85e193739c953358c865005855253af4f68a497John McCall 18996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (ignored) return nullptr; 1900f85e193739c953358c865005855253af4f68a497John McCall 1901f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(result, origType); 1902f85e193739c953358c865005855253af4f68a497John McCall} 1903f85e193739c953358c865005855253af4f68a497John McCall 1904f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1905f85e193739c953358c865005855253af4f68a497John McCall/// void (i8**, i8**) 1906f85e193739c953358c865005855253af4f68a497John McCallstatic void emitARCCopyOperation(CodeGenFunction &CGF, 190787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address dst, 190887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address src, 1909f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 19105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 191187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar assert(dst.getType() == src.getType()); 1912f85e193739c953358c865005855253af4f68a497John McCall 1913f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 191476ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy }; 191576ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer 19162acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1917f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false); 1918f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1919f85e193739c953358c865005855253af4f68a497John McCall } 1920f85e193739c953358c865005855253af4f68a497John McCall 1921bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 192287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGF.Builder.CreateBitCast(dst.getPointer(), CGF.Int8PtrPtrTy), 192387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGF.Builder.CreateBitCast(src.getPointer(), CGF.Int8PtrPtrTy) 1924bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1925bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.EmitNounwindRuntimeCall(fn, args); 1926f85e193739c953358c865005855253af4f68a497John McCall} 1927f85e193739c953358c865005855253af4f68a497John McCall 1928f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a retain. Based on the type, calls one of: 19299d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 19309d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1931f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { 1932f85e193739c953358c865005855253af4f68a497John McCall if (type->isBlockPointerType()) 1933348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitARCRetainBlock(value, /*mandatory*/ false); 1934f85e193739c953358c865005855253af4f68a497John McCall else 1935f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainNonBlock(value); 1936f85e193739c953358c865005855253af4f68a497John McCall} 1937f85e193739c953358c865005855253af4f68a497John McCall 1938f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object, with normal retain semantics. 19399d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 1940f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { 1941f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 194287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_retain, 1943f85e193739c953358c865005855253af4f68a497John McCall "objc_retain"); 1944f85e193739c953358c865005855253af4f68a497John McCall} 1945f85e193739c953358c865005855253af4f68a497John McCall 1946f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given block, with _Block_copy semantics. 19479d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1948348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// 1949348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// \param mandatory - If false, emit the call with metadata 1950348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// indicating that it's okay for the optimizer to eliminate this call 1951348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// if it can prove that the block never escapes except down the stack. 1952348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, 1953348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool mandatory) { 1954348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result 1955348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = emitARCValueOperation(*this, value, 195687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_retainBlock, 1957348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall "objc_retainBlock"); 1958348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1959348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // If the copy isn't mandatory, add !clang.arc.copy_on_escape to 1960348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // tell the optimizer that it doesn't need to do this copy if the 1961348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // block doesn't escape, where being passed as an argument doesn't 1962348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // count as escaping. 1963348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (!mandatory && isa<llvm::Instruction>(result)) { 1964348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::CallInst *call 1965348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = cast<llvm::CallInst>(result->stripPointerCasts()); 196687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar assert(call->getCalledValue() == CGM.getObjCEntrypoints().objc_retainBlock); 1967348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1968348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall call->setMetadata("clang.arc.copy_on_escape", 19690e2c34f92f00628d48968dfea096d36381f494cbStephen Hines llvm::MDNode::get(Builder.getContext(), None)); 1970348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 1971348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1972348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return result; 1973f85e193739c953358c865005855253af4f68a497John McCall} 1974f85e193739c953358c865005855253af4f68a497John McCall 19754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic void emitAutoreleasedReturnValueMarker(CodeGenFunction &CGF) { 1976f85e193739c953358c865005855253af4f68a497John McCall // Fetch the void(void) inline asm which marks that we're going to 19774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // do something with the autoreleased return value. 1978f85e193739c953358c865005855253af4f68a497John McCall llvm::InlineAsm *&marker 19794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar = CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker; 1980f85e193739c953358c865005855253af4f68a497John McCall if (!marker) { 19815f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef assembly 19824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar = CGF.CGM.getTargetCodeGenInfo() 1983f85e193739c953358c865005855253af4f68a497John McCall .getARCRetainAutoreleasedReturnValueMarker(); 1984f85e193739c953358c865005855253af4f68a497John McCall 1985f85e193739c953358c865005855253af4f68a497John McCall // If we have an empty assembly string, there's nothing to do. 1986f85e193739c953358c865005855253af4f68a497John McCall if (assembly.empty()) { 1987f85e193739c953358c865005855253af4f68a497John McCall 1988f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, at -O0, build an inline asm that we're going to call 1989f85e193739c953358c865005855253af4f68a497John McCall // in a moment. 19904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } else if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) { 1991f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType *type = 19924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::FunctionType::get(CGF.VoidTy, /*variadic*/false); 1993f85e193739c953358c865005855253af4f68a497John McCall 1994f85e193739c953358c865005855253af4f68a497John McCall marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); 1995f85e193739c953358c865005855253af4f68a497John McCall 1996f85e193739c953358c865005855253af4f68a497John McCall // If we're at -O1 and above, we don't want to litter the code 1997f85e193739c953358c865005855253af4f68a497John McCall // with this marker yet, so leave a breadcrumb for the ARC 1998f85e193739c953358c865005855253af4f68a497John McCall // optimizer to pick up. 1999f85e193739c953358c865005855253af4f68a497John McCall } else { 2000f85e193739c953358c865005855253af4f68a497John McCall llvm::NamedMDNode *metadata = 20014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.CGM.getModule().getOrInsertNamedMetadata( 2002f85e193739c953358c865005855253af4f68a497John McCall "clang.arc.retainAutoreleasedReturnValueMarker"); 2003f85e193739c953358c865005855253af4f68a497John McCall assert(metadata->getNumOperands() <= 1); 2004f85e193739c953358c865005855253af4f68a497John McCall if (metadata->getNumOperands() == 0) { 20054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar auto &ctx = CGF.getLLVMContext(); 20064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar metadata->addOperand(llvm::MDNode::get(ctx, 20074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::MDString::get(ctx, assembly))); 2008f85e193739c953358c865005855253af4f68a497John McCall } 2009f85e193739c953358c865005855253af4f68a497John McCall } 2010f85e193739c953358c865005855253af4f68a497John McCall } 2011f85e193739c953358c865005855253af4f68a497John McCall 2012f85e193739c953358c865005855253af4f68a497John McCall // Call the marker asm if we made one, which we do only at -O0. 2013b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar if (marker) 20144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.Builder.CreateCall(marker); 20154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 2016f85e193739c953358c865005855253af4f68a497John McCall 20174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Retain the given object which is the result of a function call. 20184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value) 20194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// 20204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Yes, this function name is one character away from a different 20214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// call with completely different semantics. 20224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarllvm::Value * 20234967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarCodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { 20244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar emitAutoreleasedReturnValueMarker(*this); 2025f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 20264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_retainAutoreleasedReturnValue, 2027f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutoreleasedReturnValue"); 2028f85e193739c953358c865005855253af4f68a497John McCall} 2029f85e193739c953358c865005855253af4f68a497John McCall 20304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Claim a possibly-autoreleased return value at +0. This is only 20314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// valid to do in contexts which do not rely on the retain to keep 20324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// the object valid for for all of its uses; for example, when 20334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// the value is ignored, or when it is being assigned to an 20344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// __unsafe_unretained variable. 20354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// 20364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// call i8* \@objc_unsafeClaimAutoreleasedReturnValue(i8* %value) 20374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarllvm::Value * 20384967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarCodeGenFunction::EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value) { 20394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar emitAutoreleasedReturnValueMarker(*this); 20404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return emitARCValueOperation(*this, value, 20414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_unsafeClaimAutoreleasedReturnValue, 20424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar "objc_unsafeClaimAutoreleasedReturnValue"); 20434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 20444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 2045f85e193739c953358c865005855253af4f68a497John McCall/// Release the given object. 20469d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_release(i8* %value) 20475b07e8077a20b80fee90bd76c43c6150c676e4a8John McCallvoid CodeGenFunction::EmitARCRelease(llvm::Value *value, 20485b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall ARCPreciseLifetime_t precise) { 2049f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return; 2050f85e193739c953358c865005855253af4f68a497John McCall 205187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_release; 2052f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 20532acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 205476ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2055f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_release"); 2056f85e193739c953358c865005855253af4f68a497John McCall } 2057f85e193739c953358c865005855253af4f68a497John McCall 2058f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 2059f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 2060f85e193739c953358c865005855253af4f68a497John McCall 2061f85e193739c953358c865005855253af4f68a497John McCall // Call objc_release. 2062bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value); 2063f85e193739c953358c865005855253af4f68a497John McCall 20645b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall if (precise == ARCImpreciseLifetime) { 2065f85e193739c953358c865005855253af4f68a497John McCall call->setMetadata("clang.imprecise_release", 20660e2c34f92f00628d48968dfea096d36381f494cbStephen Hines llvm::MDNode::get(Builder.getContext(), None)); 2067f85e193739c953358c865005855253af4f68a497John McCall } 2068f85e193739c953358c865005855253af4f68a497John McCall} 2069f85e193739c953358c865005855253af4f68a497John McCall 2070015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// Destroy a __strong variable. 2071015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 2072015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O0, emit a call to store 'null' into the address; 2073015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// instrumenting tools prefer this because the address is exposed, 2074015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// but it's relatively cumbersome to optimize. 2075015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 2076015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O1 and above, just load and call objc_release. 2077015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 2078015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// call void \@objc_storeStrong(i8** %addr, i8* null) 207987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarvoid CodeGenFunction::EmitARCDestroyStrong(Address addr, 20805b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall ARCPreciseLifetime_t precise) { 2081015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 208287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Value *null = getNullForVariable(addr); 2083015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 2084015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall return; 2085015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall } 2086015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 2087015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::Value *value = Builder.CreateLoad(addr); 2088015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCRelease(value, precise); 2089015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall} 2090015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 2091f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Always calls this: 20929d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 209387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarllvm::Value *CodeGenFunction::EmitARCStoreStrongCall(Address addr, 2094f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 2095f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 209687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar assert(addr.getElementType() == value->getType()); 2097f85e193739c953358c865005855253af4f68a497John McCall 209887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_storeStrong; 2099f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 21009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy }; 21012acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 2102f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false); 2103f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong"); 2104f85e193739c953358c865005855253af4f68a497John McCall } 2105f85e193739c953358c865005855253af4f68a497John McCall 2106bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 210787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Builder.CreateBitCast(addr.getPointer(), Int8PtrPtrTy), 2108bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Builder.CreateBitCast(value, Int8PtrTy) 2109bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 2110bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, args); 2111f85e193739c953358c865005855253af4f68a497John McCall 21126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (ignored) return nullptr; 2113f85e193739c953358c865005855253af4f68a497John McCall return value; 2114f85e193739c953358c865005855253af4f68a497John McCall} 2115f85e193739c953358c865005855253af4f68a497John McCall 2116f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Sometimes calls this: 21179d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2118f85e193739c953358c865005855253af4f68a497John McCall/// Other times, breaks it down into components. 2119545d996ec5a3113f046944f11b27cc2d6cb055b4John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, 2120f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *newValue, 2121f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2122545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall QualType type = dst.getType(); 2123f85e193739c953358c865005855253af4f68a497John McCall bool isBlock = type->isBlockPointerType(); 2124f85e193739c953358c865005855253af4f68a497John McCall 2125f85e193739c953358c865005855253af4f68a497John McCall // Use a store barrier at -O0 unless this is a block type or the 2126f85e193739c953358c865005855253af4f68a497John McCall // lvalue is inadequately aligned. 2127f85e193739c953358c865005855253af4f68a497John McCall if (shouldUseFusedARCCalls() && 2128f85e193739c953358c865005855253af4f68a497John McCall !isBlock && 21296da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman (dst.getAlignment().isZero() || 21306da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { 2131f85e193739c953358c865005855253af4f68a497John McCall return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); 2132f85e193739c953358c865005855253af4f68a497John McCall } 2133f85e193739c953358c865005855253af4f68a497John McCall 2134f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, split it out. 2135f85e193739c953358c865005855253af4f68a497John McCall 2136f85e193739c953358c865005855253af4f68a497John McCall // Retain the new value. 2137f85e193739c953358c865005855253af4f68a497John McCall newValue = EmitARCRetain(type, newValue); 2138f85e193739c953358c865005855253af4f68a497John McCall 2139f85e193739c953358c865005855253af4f68a497John McCall // Read the old value. 21404ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation()); 2141f85e193739c953358c865005855253af4f68a497John McCall 2142f85e193739c953358c865005855253af4f68a497John McCall // Store. We do this before the release so that any deallocs won't 2143f85e193739c953358c865005855253af4f68a497John McCall // see the old value. 2144545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreOfScalar(newValue, dst); 2145f85e193739c953358c865005855253af4f68a497John McCall 2146f85e193739c953358c865005855253af4f68a497John McCall // Finally, release the old value. 21475b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall EmitARCRelease(oldValue, dst.isARCPreciseLifetime()); 2148f85e193739c953358c865005855253af4f68a497John McCall 2149f85e193739c953358c865005855253af4f68a497John McCall return newValue; 2150f85e193739c953358c865005855253af4f68a497John McCall} 2151f85e193739c953358c865005855253af4f68a497John McCall 2152f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 21539d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %value) 2154f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { 2155f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 215687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_autorelease, 2157f85e193739c953358c865005855253af4f68a497John McCall "objc_autorelease"); 2158f85e193739c953358c865005855253af4f68a497John McCall} 2159f85e193739c953358c865005855253af4f68a497John McCall 2160f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 21619d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleaseReturnValue(i8* %value) 2162f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2163f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { 2164f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 216587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_autoreleaseReturnValue, 2166df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_autoreleaseReturnValue", 2167df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2168f85e193739c953358c865005855253af4f68a497John McCall} 2169f85e193739c953358c865005855253af4f68a497John McCall 2170f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 21719d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value) 2172f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2173f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { 2174f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 217587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_retainAutoreleaseReturnValue, 2176df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_retainAutoreleaseReturnValue", 2177df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2178f85e193739c953358c865005855253af4f68a497John McCall} 2179f85e193739c953358c865005855253af4f68a497John McCall 2180f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 21819d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2182f85e193739c953358c865005855253af4f68a497John McCall/// or 21839d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// %retain = call i8* \@objc_retainBlock(i8* %value) 21849d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %retain) 2185f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, 2186f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 2187f85e193739c953358c865005855253af4f68a497John McCall if (!type->isBlockPointerType()) 2188f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutoreleaseNonBlock(value); 2189f85e193739c953358c865005855253af4f68a497John McCall 2190f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 2191f85e193739c953358c865005855253af4f68a497John McCall 21922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 2193f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 2194348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ true); 2195f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2196f85e193739c953358c865005855253af4f68a497John McCall return Builder.CreateBitCast(value, origType); 2197f85e193739c953358c865005855253af4f68a497John McCall} 2198f85e193739c953358c865005855253af4f68a497John McCall 2199f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 22009d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2201f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2202f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { 2203f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 220487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_retainAutorelease, 2205f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutorelease"); 2206f85e193739c953358c865005855253af4f68a497John McCall} 2207f85e193739c953358c865005855253af4f68a497John McCall 22089d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeak(i8** %addr) 2209f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_autorelease(objc_loadWeakRetained(addr)). 221087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarllvm::Value *CodeGenFunction::EmitARCLoadWeak(Address addr) { 2211f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 221287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_loadWeak, 2213f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeak"); 2214f85e193739c953358c865005855253af4f68a497John McCall} 2215f85e193739c953358c865005855253af4f68a497John McCall 22169d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeakRetained(i8** %addr) 221787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarllvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(Address addr) { 2218f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 221987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_loadWeakRetained, 2220f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeakRetained"); 2221f85e193739c953358c865005855253af4f68a497John McCall} 2222f85e193739c953358c865005855253af4f68a497John McCall 22239d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_storeWeak(i8** %addr, i8* %value) 2224f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. 222587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarllvm::Value *CodeGenFunction::EmitARCStoreWeak(Address addr, 2226f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 2227f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2228f85e193739c953358c865005855253af4f68a497John McCall return emitARCStoreOperation(*this, addr, value, 222987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_storeWeak, 2230f85e193739c953358c865005855253af4f68a497John McCall "objc_storeWeak", ignored); 2231f85e193739c953358c865005855253af4f68a497John McCall} 2232f85e193739c953358c865005855253af4f68a497John McCall 22339d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_initWeak(i8** %addr, i8* %value) 2234f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. %addr is known to not have a current weak entry. 2235f85e193739c953358c865005855253af4f68a497John McCall/// Essentially equivalent to: 2236f85e193739c953358c865005855253af4f68a497John McCall/// *addr = nil; objc_storeWeak(addr, value); 223787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarvoid CodeGenFunction::EmitARCInitWeak(Address addr, llvm::Value *value) { 2238f85e193739c953358c865005855253af4f68a497John McCall // If we're initializing to null, just write null to memory; no need 2239f85e193739c953358c865005855253af4f68a497John McCall // to get the runtime involved. But don't do this if optimization 2240f85e193739c953358c865005855253af4f68a497John McCall // is enabled, because accounting for this would make the optimizer 2241f85e193739c953358c865005855253af4f68a497John McCall // much more complicated. 2242f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value) && 2243f85e193739c953358c865005855253af4f68a497John McCall CGM.getCodeGenOpts().OptimizationLevel == 0) { 2244f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(value, addr); 2245f85e193739c953358c865005855253af4f68a497John McCall return; 2246f85e193739c953358c865005855253af4f68a497John McCall } 2247f85e193739c953358c865005855253af4f68a497John McCall 2248f85e193739c953358c865005855253af4f68a497John McCall emitARCStoreOperation(*this, addr, value, 224987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_initWeak, 2250f85e193739c953358c865005855253af4f68a497John McCall "objc_initWeak", /*ignored*/ true); 2251f85e193739c953358c865005855253af4f68a497John McCall} 2252f85e193739c953358c865005855253af4f68a497John McCall 22539d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_destroyWeak(i8** %addr) 2254f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_storeWeak(addr, nil). 225587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarvoid CodeGenFunction::EmitARCDestroyWeak(Address addr) { 225687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_destroyWeak; 2257f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22582acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 225976ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false); 2260f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak"); 2261f85e193739c953358c865005855253af4f68a497John McCall } 2262f85e193739c953358c865005855253af4f68a497John McCall 2263f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 2264f85e193739c953358c865005855253af4f68a497John McCall addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); 2265f85e193739c953358c865005855253af4f68a497John McCall 226687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar EmitNounwindRuntimeCall(fn, addr.getPointer()); 2267f85e193739c953358c865005855253af4f68a497John McCall} 2268f85e193739c953358c865005855253af4f68a497John McCall 22699d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_moveWeak(i8** %dest, i8** %src) 2270f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Leaves %src pointing to nothing. 2271f85e193739c953358c865005855253af4f68a497John McCall/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). 227287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarvoid CodeGenFunction::EmitARCMoveWeak(Address dst, Address src) { 2273f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 227487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_moveWeak, 2275f85e193739c953358c865005855253af4f68a497John McCall "objc_moveWeak"); 2276f85e193739c953358c865005855253af4f68a497John McCall} 2277f85e193739c953358c865005855253af4f68a497John McCall 22789d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_copyWeak(i8** %dest, i8** %src) 2279f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Essentially 2280f85e193739c953358c865005855253af4f68a497John McCall/// objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) 228187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarvoid CodeGenFunction::EmitARCCopyWeak(Address dst, Address src) { 2282f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 228387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.getObjCEntrypoints().objc_copyWeak, 2284f85e193739c953358c865005855253af4f68a497John McCall "objc_copyWeak"); 2285f85e193739c953358c865005855253af4f68a497John McCall} 2286f85e193739c953358c865005855253af4f68a497John McCall 2287f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a objc_autoreleasepool_push. 22889d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleasePoolPush(void) 2289f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { 229087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPush; 2291f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 2293f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType::get(Int8PtrTy, false); 2294f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush"); 2295f85e193739c953358c865005855253af4f68a497John McCall } 2296f85e193739c953358c865005855253af4f68a497John McCall 2297bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return EmitNounwindRuntimeCall(fn); 2298f85e193739c953358c865005855253af4f68a497John McCall} 2299f85e193739c953358c865005855253af4f68a497John McCall 2300f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 23019d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_autoreleasePoolPop(i8* %ptr) 2302f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { 2303f85e193739c953358c865005855253af4f68a497John McCall assert(value->getType() == Int8PtrTy); 2304f85e193739c953358c865005855253af4f68a497John McCall 230587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPop; 2306f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 23072acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 230876ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2309f85e193739c953358c865005855253af4f68a497John McCall 2310f85e193739c953358c865005855253af4f68a497John McCall // We don't want to use a weak import here; instead we should not 2311f85e193739c953358c865005855253af4f68a497John McCall // fall into this path. 2312f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop"); 2313f85e193739c953358c865005855253af4f68a497John McCall } 2314f85e193739c953358c865005855253af4f68a497John McCall 2315b57f6b3d8f84e9241fd978e2739d6a9da0870546John McCall // objc_autoreleasePoolPop can throw. 2316b57f6b3d8f84e9241fd978e2739d6a9da0870546John McCall EmitRuntimeCallOrInvoke(fn, value); 2317f85e193739c953358c865005855253af4f68a497John McCall} 2318f85e193739c953358c865005855253af4f68a497John McCall 2319f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do an MRR version objc_autoreleasepool_push. 2320f85e193739c953358c865005855253af4f68a497John McCall/// Which is: [[NSAutoreleasePool alloc] init]; 2321f85e193739c953358c865005855253af4f68a497John McCall/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. 2322f85e193739c953358c865005855253af4f68a497John McCall/// init is declared as: - (id) init; in its NSObject super class. 2323f85e193739c953358c865005855253af4f68a497John McCall/// 2324f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { 2325f85e193739c953358c865005855253af4f68a497John McCall CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 2326bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this); 2327f85e193739c953358c865005855253af4f68a497John McCall // [NSAutoreleasePool alloc] 2328f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); 2329f85e193739c953358c865005855253af4f68a497John McCall Selector AllocSel = getContext().Selectors.getSelector(0, &II); 2330f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2331f85e193739c953358c865005855253af4f68a497John McCall RValue AllocRV = 2332f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2333f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2334f85e193739c953358c865005855253af4f68a497John McCall AllocSel, Receiver, Args); 2335f85e193739c953358c865005855253af4f68a497John McCall 2336f85e193739c953358c865005855253af4f68a497John McCall // [Receiver init] 2337f85e193739c953358c865005855253af4f68a497John McCall Receiver = AllocRV.getScalarVal(); 2338f85e193739c953358c865005855253af4f68a497John McCall II = &CGM.getContext().Idents.get("init"); 2339f85e193739c953358c865005855253af4f68a497John McCall Selector InitSel = getContext().Selectors.getSelector(0, &II); 2340f85e193739c953358c865005855253af4f68a497John McCall RValue InitRV = 2341f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2342f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2343f85e193739c953358c865005855253af4f68a497John McCall InitSel, Receiver, Args); 2344f85e193739c953358c865005855253af4f68a497John McCall return InitRV.getScalarVal(); 2345f85e193739c953358c865005855253af4f68a497John McCall} 2346f85e193739c953358c865005855253af4f68a497John McCall 2347f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 2348f85e193739c953358c865005855253af4f68a497John McCall/// [tmp drain]; 2349f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { 2350f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); 2351f85e193739c953358c865005855253af4f68a497John McCall Selector DrainSel = getContext().Selectors.getSelector(0, &II); 2352f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2353f85e193739c953358c865005855253af4f68a497John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 2354f85e193739c953358c865005855253af4f68a497John McCall getContext().VoidTy, DrainSel, Arg, Args); 2355f85e193739c953358c865005855253af4f68a497John McCall} 2356f85e193739c953358c865005855253af4f68a497John McCall 2357bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, 235887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address addr, 2359bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 23605b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime); 2361bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2362bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2363bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, 236487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address addr, 2365bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 23665b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime); 2367bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2368bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2369bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, 237087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Address addr, 2371bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 2372bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall CGF.EmitARCDestroyWeak(addr); 2373bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2374bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2375f85e193739c953358c865005855253af4f68a497John McCallnamespace { 237687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar struct CallObjCAutoreleasePoolObject final : EHScopeStack::Cleanup { 2377f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2378f85e193739c953358c865005855253af4f68a497John McCall 2379f85e193739c953358c865005855253af4f68a497John McCall CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2380f85e193739c953358c865005855253af4f68a497John McCall 2381651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 2382f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCAutoreleasePoolPop(Token); 2383f85e193739c953358c865005855253af4f68a497John McCall } 2384f85e193739c953358c865005855253af4f68a497John McCall }; 238587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar struct CallObjCMRRAutoreleasePoolObject final : EHScopeStack::Cleanup { 2386f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2387f85e193739c953358c865005855253af4f68a497John McCall 2388f85e193739c953358c865005855253af4f68a497John McCall CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2389f85e193739c953358c865005855253af4f68a497John McCall 2390651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 2391f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCMRRAutoreleasePoolPop(Token); 2392f85e193739c953358c865005855253af4f68a497John McCall } 2393f85e193739c953358c865005855253af4f68a497John McCall }; 2394f85e193739c953358c865005855253af4f68a497John McCall} 2395f85e193739c953358c865005855253af4f68a497John McCall 2396f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { 23974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount) 2398f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); 2399f85e193739c953358c865005855253af4f68a497John McCall else 2400f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); 2401f85e193739c953358c865005855253af4f68a497John McCall} 2402f85e193739c953358c865005855253af4f68a497John McCall 2403f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2404f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2405f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2406f85e193739c953358c865005855253af4f68a497John McCall switch (type.getObjCLifetime()) { 2407f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_None: 2408f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_ExplicitNone: 2409f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Strong: 2410f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Autoreleasing: 24114ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky return TryEmitResult(CGF.EmitLoadOfLValue(lvalue, 24124ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky SourceLocation()).getScalarVal(), 2413f85e193739c953358c865005855253af4f68a497John McCall false); 2414f85e193739c953358c865005855253af4f68a497John McCall 2415f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Weak: 2416f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()), 2417f85e193739c953358c865005855253af4f68a497John McCall true); 2418f85e193739c953358c865005855253af4f68a497John McCall } 2419f85e193739c953358c865005855253af4f68a497John McCall 2420f85e193739c953358c865005855253af4f68a497John McCall llvm_unreachable("impossible lifetime!"); 2421f85e193739c953358c865005855253af4f68a497John McCall} 2422f85e193739c953358c865005855253af4f68a497John McCall 2423f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2424f85e193739c953358c865005855253af4f68a497John McCall const Expr *e) { 2425f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 2426f85e193739c953358c865005855253af4f68a497John McCall QualType type = e->getType(); 2427f85e193739c953358c865005855253af4f68a497John McCall 24282148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // If we're loading retained from a __strong xvalue, we can avoid 24292148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // an extra retain/release pair by zeroing out the source of this 24302148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // "move" operation. 24312148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall if (e->isXValue() && 24322148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall !type.isConstQualified() && 24332148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall type.getObjCLifetime() == Qualifiers::OCL_Strong) { 24342148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Emit the lvalue. 24352148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall LValue lv = CGF.EmitLValue(e); 24362148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 24372148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Load the object pointer. 24384ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky llvm::Value *result = CGF.EmitLoadOfLValue(lv, 24394ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky SourceLocation()).getScalarVal(); 24402148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 24412148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Set the source pointer to NULL. 24422148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv); 24432148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 24442148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall return TryEmitResult(result, true); 24452148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall } 24462148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 2447f85e193739c953358c865005855253af4f68a497John McCall // As a very special optimization, in ARC++, if the l-value is the 2448f85e193739c953358c865005855253af4f68a497John McCall // result of a non-volatile assignment, do a simple retain of the 2449f85e193739c953358c865005855253af4f68a497John McCall // result of the call to objc_storeWeak instead of reloading. 24504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGF.getLangOpts().CPlusPlus && 2451f85e193739c953358c865005855253af4f68a497John McCall !type.isVolatileQualified() && 2452f85e193739c953358c865005855253af4f68a497John McCall type.getObjCLifetime() == Qualifiers::OCL_Weak && 2453f85e193739c953358c865005855253af4f68a497John McCall isa<BinaryOperator>(e) && 2454f85e193739c953358c865005855253af4f68a497John McCall cast<BinaryOperator>(e)->getOpcode() == BO_Assign) 2455f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitScalarExpr(e), false); 2456f85e193739c953358c865005855253af4f68a497John McCall 2457f85e193739c953358c865005855253af4f68a497John McCall return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); 2458f85e193739c953358c865005855253af4f68a497John McCall} 2459f85e193739c953358c865005855253af4f68a497John McCall 24604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartypedef llvm::function_ref<llvm::Value *(CodeGenFunction &CGF, 24614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *value)> 24624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar ValueTransform; 2463f85e193739c953358c865005855253af4f68a497John McCall 24644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Insert code immediately after a call. 24654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic llvm::Value *emitARCOperationAfterCall(CodeGenFunction &CGF, 24664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *value, 24674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar ValueTransform doAfterCall, 24684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar ValueTransform doFallback) { 2469f85e193739c953358c865005855253af4f68a497John McCall if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { 2470f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2471f85e193739c953358c865005855253af4f68a497John McCall 2472f85e193739c953358c865005855253af4f68a497John McCall // Place the retain immediately following the call. 2473f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(call->getParent(), 2474f85e193739c953358c865005855253af4f68a497John McCall ++llvm::BasicBlock::iterator(call)); 24754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar value = doAfterCall(CGF, value); 2476f85e193739c953358c865005855253af4f68a497John McCall 2477f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2478f85e193739c953358c865005855253af4f68a497John McCall return value; 2479f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { 2480f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2481f85e193739c953358c865005855253af4f68a497John McCall 2482f85e193739c953358c865005855253af4f68a497John McCall // Place the retain at the beginning of the normal destination block. 2483f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *BB = invoke->getNormalDest(); 2484f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(BB, BB->begin()); 24854967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar value = doAfterCall(CGF, value); 2486f85e193739c953358c865005855253af4f68a497John McCall 2487f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2488f85e193739c953358c865005855253af4f68a497John McCall return value; 2489f85e193739c953358c865005855253af4f68a497John McCall 2490f85e193739c953358c865005855253af4f68a497John McCall // Bitcasts can arise because of related-result returns. Rewrite 2491f85e193739c953358c865005855253af4f68a497John McCall // the operand. 2492f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { 2493f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *operand = bitcast->getOperand(0); 24944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback); 2495f85e193739c953358c865005855253af4f68a497John McCall bitcast->setOperand(0, operand); 2496f85e193739c953358c865005855253af4f68a497John McCall return bitcast; 2497f85e193739c953358c865005855253af4f68a497John McCall 2498f85e193739c953358c865005855253af4f68a497John McCall // Generic fall-back case. 2499f85e193739c953358c865005855253af4f68a497John McCall } else { 2500f85e193739c953358c865005855253af4f68a497John McCall // Retain using the non-block variant: we never need to do a copy 2501f85e193739c953358c865005855253af4f68a497John McCall // of a block that's been returned to us. 25024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return doFallback(CGF, value); 25034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 25044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 25054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 25064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Given that the given expression is some sort of call (which does 25074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// not return retained), emit a retain following it. 25084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic llvm::Value *emitARCRetainCallResult(CodeGenFunction &CGF, 25094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar const Expr *e) { 25104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *value = CGF.EmitScalarExpr(e); 25114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return emitARCOperationAfterCall(CGF, value, 25124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar [](CodeGenFunction &CGF, llvm::Value *value) { 25134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.EmitARCRetainAutoreleasedReturnValue(value); 25144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar }, 25154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar [](CodeGenFunction &CGF, llvm::Value *value) { 25164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.EmitARCRetainNonBlock(value); 25174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar }); 25184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 25194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 25204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Given that the given expression is some sort of call (which does 25214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// not return retained), perform an unsafeClaim following it. 25224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic llvm::Value *emitARCUnsafeClaimCallResult(CodeGenFunction &CGF, 25234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar const Expr *e) { 25244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *value = CGF.EmitScalarExpr(e); 25254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return emitARCOperationAfterCall(CGF, value, 25264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar [](CodeGenFunction &CGF, llvm::Value *value) { 25274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.EmitARCUnsafeClaimAutoreleasedReturnValue(value); 25284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar }, 25294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar [](CodeGenFunction &CGF, llvm::Value *value) { 25304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return value; 25314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar }); 25324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 25334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 25344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarllvm::Value *CodeGenFunction::EmitARCReclaimReturnedObject(const Expr *E, 25354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar bool allowUnsafeClaim) { 25364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar if (allowUnsafeClaim && 25374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGM.getLangOpts().ObjCRuntime.hasARCUnsafeClaimAutoreleasedReturnValue()) { 25384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return emitARCUnsafeClaimCallResult(*this, E); 25394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } else { 25404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *value = emitARCRetainCallResult(*this, E); 25414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return EmitObjCConsumeObject(E->getType(), value); 2542f85e193739c953358c865005855253af4f68a497John McCall } 2543f85e193739c953358c865005855253af4f68a497John McCall} 2544f85e193739c953358c865005855253af4f68a497John McCall 2545dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// Determine whether it might be important to emit a separate 2546dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// objc_retain_block on the result of the given expression, or 2547dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// whether it's okay to just emit it in a +1 context. 2548dc05b11c67331016473fbc7909827b1b89c9616bJohn McCallstatic bool shouldEmitSeparateBlockRetain(const Expr *e) { 2549dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall assert(e->getType()->isBlockPointerType()); 2550dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall e = e->IgnoreParens(); 2551dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2552dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // For future goodness, emit block expressions directly in +1 2553dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // contexts if we can. 2554dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (isa<BlockExpr>(e)) 2555dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2556dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2557dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { 2558dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall switch (cast->getCastKind()) { 2559dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Emitting these operations in +1 contexts is goodness. 2560dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_LValueToRValue: 256133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 256233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 256333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 2564dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2565dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2566dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations preserve a block type. 2567dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_NoOp: 2568dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_BitCast: 2569dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return shouldEmitSeparateBlockRetain(cast->getSubExpr()); 2570dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2571dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations are known to be bad (or haven't been considered). 2572dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_AnyPointerToBlockPointerCast: 2573dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall default: 2574dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2575dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2576dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2577dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2578dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2579dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall} 2580dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 25814967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarnamespace { 25824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// A CRTP base class for emitting expressions of retainable object 25834967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// pointer type in ARC. 25844967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> class ARCExprEmitter { 25854967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarprotected: 25864967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CodeGenFunction &CGF; 25874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Impl &asImpl() { return *static_cast<Impl*>(this); } 25884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 25894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar ARCExprEmitter(CodeGenFunction &CGF) : CGF(CGF) {} 25904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 25914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarpublic: 25924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result visit(const Expr *e); 25934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result visitCastExpr(const CastExpr *e); 25944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result visitPseudoObjectExpr(const PseudoObjectExpr *e); 25954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result visitBinaryOperator(const BinaryOperator *e); 25964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result visitBinAssign(const BinaryOperator *e); 25974967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result visitBinAssignUnsafeUnretained(const BinaryOperator *e); 25984967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result visitBinAssignAutoreleasing(const BinaryOperator *e); 25994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result visitBinAssignWeak(const BinaryOperator *e); 26004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result visitBinAssignStrong(const BinaryOperator *e); 26014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 26024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Minimal implementation: 26034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Result visitLValueToRValue(const Expr *e) 26044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Result visitConsumeObject(const Expr *e) 26054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Result visitExtendBlockObject(const Expr *e) 26064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Result visitReclaimReturnedObject(const Expr *e) 26074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Result visitCall(const Expr *e) 26084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Result visitExpr(const Expr *e) 26094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // 26104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Result emitBitCast(Result result, llvm::Type *resultType) 26114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // llvm::Value *getValueOfResult(Result result) 26124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar}; 26134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 26144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 26154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Try to emit a PseudoObjectExpr under special ARC rules. 26164b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// 26174b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// This massively duplicates emitPseudoObjectRValue. 26184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> 26194967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarResult 26204967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarARCExprEmitter<Impl,Result>::visitPseudoObjectExpr(const PseudoObjectExpr *E) { 2621cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; 26224b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26234b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Find the result expression. 26244b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *resultExpr = E->getResultExpr(); 26254b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(resultExpr); 26264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result result; 26274b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26284b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (PseudoObjectExpr::const_semantics_iterator 26294b9c2d235fb9449e249d74f48ecfec601650de93John McCall i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { 26304b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *semantic = *i; 26314b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26324b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic expression is an opaque value, bind it 26334b9c2d235fb9449e249d74f48ecfec601650de93John McCall // to the result of its source expression. 26344b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { 26354b9c2d235fb9449e249d74f48ecfec601650de93John McCall typedef CodeGenFunction::OpaqueValueMappingData OVMA; 26364b9c2d235fb9449e249d74f48ecfec601650de93John McCall OVMA opaqueData; 26374b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26384b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic is the result of the pseudo-object 26394b9c2d235fb9449e249d74f48ecfec601650de93John McCall // expression, try to evaluate the source as +1. 26404b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (ov == resultExpr) { 26414b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(!OVMA::shouldBindAsLValue(ov)); 26424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar result = asImpl().visit(ov->getSourceExpr()); 26434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar opaqueData = OVMA::bind(CGF, ov, 26444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar RValue::get(asImpl().getValueOfResult(result))); 26454b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26464b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, just bind it. 26474b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 26484b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); 26494b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 26504b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques.push_back(opaqueData); 26514b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26524b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, if the expression is the result, evaluate it 26534b9c2d235fb9449e249d74f48ecfec601650de93John McCall // and remember the result. 26544b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (semantic == resultExpr) { 26554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar result = asImpl().visit(semantic); 26564b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26574b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, evaluate the expression in an ignored context. 26584b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 26594b9c2d235fb9449e249d74f48ecfec601650de93John McCall CGF.EmitIgnoredExpr(semantic); 26604b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 26614b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 26624b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26634b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Unbind all the opaques now. 26644b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (unsigned i = 0, e = opaques.size(); i != e; ++i) 26654b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques[i].unbind(CGF); 26664b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26674b9c2d235fb9449e249d74f48ecfec601650de93John McCall return result; 26684b9c2d235fb9449e249d74f48ecfec601650de93John McCall} 26694b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26704967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> 26714967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarResult ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) { 26724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar switch (e->getCastKind()) { 26734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 26744967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // No-op casts don't change the type, so we just ignore them. 26754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case CK_NoOp: 26764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visit(e->getSubExpr()); 26774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 26784967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // These casts can change the type. 26794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case CK_CPointerToObjCPointerCast: 26804967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case CK_BlockPointerToObjCPointerCast: 26814967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case CK_AnyPointerToBlockPointerCast: 26824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case CK_BitCast: { 26834967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Type *resultType = CGF.ConvertType(e->getType()); 26844967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar assert(e->getSubExpr()->getType()->hasPointerRepresentation()); 26854967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result result = asImpl().visit(e->getSubExpr()); 26864967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().emitBitCast(result, resultType); 26874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 26884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 26894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Handle some casts specially. 26904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case CK_LValueToRValue: 26914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitLValueToRValue(e->getSubExpr()); 26924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case CK_ARCConsumeObject: 26934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitConsumeObject(e->getSubExpr()); 26944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case CK_ARCExtendBlockObject: 26954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitExtendBlockObject(e->getSubExpr()); 26964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case CK_ARCReclaimReturnedObject: 26974967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitReclaimReturnedObject(e->getSubExpr()); 26984967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 26994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Otherwise, use the default logic. 27004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar default: 27014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitExpr(e); 27024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 27034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 27044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> 27064967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarResult 27074967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) { 27084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar switch (e->getOpcode()) { 27094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case BO_Comma: 27104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.EmitIgnoredExpr(e->getLHS()); 27114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.EnsureInsertPoint(); 27124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visit(e->getRHS()); 27134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case BO_Assign: 27154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitBinAssign(e); 27164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar default: 27184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitExpr(e); 27194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 27204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 27214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> 27234967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarResult ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) { 27244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar switch (e->getLHS()->getType().getObjCLifetime()) { 27254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case Qualifiers::OCL_ExplicitNone: 27264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitBinAssignUnsafeUnretained(e); 27274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case Qualifiers::OCL_Weak: 27294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitBinAssignWeak(e); 27304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case Qualifiers::OCL_Autoreleasing: 27324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitBinAssignAutoreleasing(e); 27334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case Qualifiers::OCL_Strong: 27354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitBinAssignStrong(e); 27364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar case Qualifiers::OCL_None: 27384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitExpr(e); 27394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 27404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm_unreachable("bad ObjC ownership qualifier"); 27414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 27424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// The default rule for __unsafe_unretained emits the RHS recursively, 27444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// stores into the unsafe variable, and propagates the result outward. 27454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> 27464967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarResult ARCExprEmitter<Impl,Result>:: 27474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar visitBinAssignUnsafeUnretained(const BinaryOperator *e) { 27484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Recursively emit the RHS. 27494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // For __block safety, do this before emitting the LHS. 27504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar Result result = asImpl().visit(e->getRHS()); 27514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Perform the store. 27534967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar LValue lvalue = 27544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store); 27554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)), 27564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar lvalue); 27574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return result; 27594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 27604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> 27624967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarResult 27634967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarARCExprEmitter<Impl,Result>::visitBinAssignAutoreleasing(const BinaryOperator *e) { 27644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitExpr(e); 27654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 27664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> 27684967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarResult 27694967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarARCExprEmitter<Impl,Result>::visitBinAssignWeak(const BinaryOperator *e) { 27704967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitExpr(e); 27714967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 27724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> 27744967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarResult 27754967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) { 27764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitExpr(e); 27774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 27784967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 27794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// The general expression-emission logic. 27804967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainartemplate <typename Impl, typename Result> 27814967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarResult ARCExprEmitter<Impl,Result>::visit(const Expr *e) { 278272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // We should *never* see a nested full-expression here, because if 278372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // we fail to emit at +1, our caller must not retain after we close 27844967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // out the full-expression. This isn't as important in the unsafe 27854967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // emitter. 278672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall assert(!isa<ExprWithCleanups>(e)); 2787990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 27884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Look through parens, __extension__, generic selection, etc. 27894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar e = e->IgnoreParens(); 2790f85e193739c953358c865005855253af4f68a497John McCall 27914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Handle certain kinds of casts. 27924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { 27934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitCastExpr(ce); 2794f85e193739c953358c865005855253af4f68a497John McCall 27954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Handle the comma operator. 27964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } else if (auto op = dyn_cast<BinaryOperator>(e)) { 27974967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitBinaryOperator(op); 2798dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 27994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // TODO: handle conditional operators here 2800dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 28014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // For calls and message sends, use the retained-call logic. 28024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Delegate inits are a special case in that they're the only 28034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // returns-retained expression that *isn't* surrounded by 28044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // a consume. 28054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } else if (isa<CallExpr>(e) || 28064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar (isa<ObjCMessageExpr>(e) && 28074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { 28084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitCall(e); 2809dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 28104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Look through pseudo-object expressions. 28114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 28124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitPseudoObjectExpr(pseudo); 28134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 2814dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 28154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return asImpl().visitExpr(e); 28164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 2817dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 28184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarnamespace { 28197e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall 28204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// An emitter for +1 results. 28214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstruct ARCRetainExprEmitter : 28224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar public ARCExprEmitter<ARCRetainExprEmitter, TryEmitResult> { 2823f85e193739c953358c865005855253af4f68a497John McCall 28244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar ARCRetainExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {} 28254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *getValueOfResult(TryEmitResult result) { 28274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return result.getPointer(); 28284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 2829f85e193739c953358c865005855253af4f68a497John McCall 28304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar TryEmitResult emitBitCast(TryEmitResult result, llvm::Type *resultType) { 28314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *value = result.getPointer(); 28324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar value = CGF.Builder.CreateBitCast(value, resultType); 28334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar result.setPointer(value); 28344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return result; 28354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 28364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar TryEmitResult visitLValueToRValue(const Expr *e) { 28384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return tryEmitARCRetainLoadOfScalar(CGF, e); 28394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 28404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// For consumptions, just emit the subexpression and thus elide 28424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// the retain/release pair. 28434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar TryEmitResult visitConsumeObject(const Expr *e) { 28444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *result = CGF.EmitScalarExpr(e); 28454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return TryEmitResult(result, true); 28464967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 28474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// Block extends are net +0. Naively, we could just recurse on 28494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// the subexpression, but actually we need to ensure that the 28504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// value is copied as a block, so there's a little filter here. 28514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar TryEmitResult visitExtendBlockObject(const Expr *e) { 28524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *result; // will be a +0 value 28534967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // If we can't safely assume the sub-expression will produce a 28554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // block-copied value, emit the sub-expression at +0. 28564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar if (shouldEmitSeparateBlockRetain(e)) { 28574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar result = CGF.EmitScalarExpr(e); 28584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Otherwise, try to emit the sub-expression at +1 recursively. 28604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } else { 28614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar TryEmitResult subresult = asImpl().visit(e); 28624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28634967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // If that produced a retained value, just use that. 28644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar if (subresult.getInt()) { 28654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return subresult; 28664b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 28674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Otherwise it's +0. 28694967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar result = subresult.getPointer(); 2870f85e193739c953358c865005855253af4f68a497John McCall } 2871f85e193739c953358c865005855253af4f68a497John McCall 28724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Retain the object as a block. 28734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true); 28744967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return TryEmitResult(result, true); 2875f85e193739c953358c865005855253af4f68a497John McCall } 2876f85e193739c953358c865005855253af4f68a497John McCall 28774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// For reclaims, emit the subexpression as a retained call and 28784967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// skip the consumption. 28794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar TryEmitResult visitReclaimReturnedObject(const Expr *e) { 28804967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *result = emitARCRetainCallResult(CGF, e); 28814967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return TryEmitResult(result, true); 28824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 28834967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28844967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// When we have an undecorated call, retroactively do a claim. 28854967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar TryEmitResult visitCall(const Expr *e) { 28864967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *result = emitARCRetainCallResult(CGF, e); 28874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return TryEmitResult(result, true); 28884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 28894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // TODO: maybe special-case visitBinAssignWeak? 28914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 28924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar TryEmitResult visitExpr(const Expr *e) { 28934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // We didn't find an obvious production, so emit what we've got and 28944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // tell the caller that we didn't manage to retain. 28954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *result = CGF.EmitScalarExpr(e); 28964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return TryEmitResult(result, false); 28974967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 28984967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar}; 28994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 29004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 29014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic TryEmitResult 29024967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainartryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { 29034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return ARCRetainExprEmitter(CGF).visit(e); 2904f85e193739c953358c865005855253af4f68a497John McCall} 2905f85e193739c953358c865005855253af4f68a497John McCall 2906f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2907f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2908f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2909f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); 2910f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2911f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2912f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetain(type, value); 2913f85e193739c953358c865005855253af4f68a497John McCall return value; 2914f85e193739c953358c865005855253af4f68a497John McCall} 2915f85e193739c953358c865005855253af4f68a497John McCall 2916f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainScalarExpr - Semantically equivalent to 2917f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a 2918f85e193739c953358c865005855253af4f68a497John McCall/// best-effort attempt to peephole expressions that naturally produce 2919f85e193739c953358c865005855253af4f68a497John McCall/// retained objects. 2920f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { 292172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 292272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 292372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 292472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 292572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainScalarExpr(cleanups->getSubExpr()); 292672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 292772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2928f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2929f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2930f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2931f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetain(e->getType(), value); 2932f85e193739c953358c865005855253af4f68a497John McCall return value; 2933f85e193739c953358c865005855253af4f68a497John McCall} 2934f85e193739c953358c865005855253af4f68a497John McCall 2935f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2936f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { 293772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 293872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 293972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 294072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 294172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr()); 294272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 294372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2944f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2945f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2946f85e193739c953358c865005855253af4f68a497John McCall if (result.getInt()) 2947f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2948f85e193739c953358c865005855253af4f68a497John McCall else 2949f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetainAutorelease(e->getType(), value); 2950f85e193739c953358c865005855253af4f68a497John McCall return value; 2951f85e193739c953358c865005855253af4f68a497John McCall} 2952f85e193739c953358c865005855253af4f68a497John McCall 2953348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { 2954348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result; 2955348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool doRetain; 2956348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2957348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (shouldEmitSeparateBlockRetain(e)) { 2958348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitScalarExpr(e); 2959348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = true; 2960348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } else { 2961348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); 2962348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = subresult.getPointer(); 2963348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = !subresult.getInt(); 2964348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 2965348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2966348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (doRetain) 2967348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitARCRetainBlock(result, /*mandatory*/ true); 2968348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitObjCConsumeObject(e->getType(), result); 2969348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall} 2970348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 29712b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCallllvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { 29722b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // In ARC, retain and autorelease the expression. 29734e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 29742b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Do so before running any cleanups for the full-expression. 297572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // EmitARCRetainAutoreleaseScalarExpr does this for us. 29762b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitARCRetainAutoreleaseScalarExpr(expr); 29772b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall } 29782b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 29792b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Otherwise, use the normal scalar-expression emission. The 29802b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception machinery doesn't do anything special with the 29812b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception like retaining it, so there's no safety associated with 29822b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // only running cleanups after the throw has started, and when it 29832b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // matters it tends to be substantially inferior code. 29842b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitScalarExpr(expr); 29852b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall} 29862b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 29874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarnamespace { 29884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 29894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// An emitter for assigning into an __unsafe_unretained context. 29904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstruct ARCUnsafeUnretainedExprEmitter : 29914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar public ARCExprEmitter<ARCUnsafeUnretainedExprEmitter, llvm::Value*> { 29924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 29934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar ARCUnsafeUnretainedExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {} 29944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 29954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *getValueOfResult(llvm::Value *value) { 29964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return value; 29974967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 29984967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 29994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *emitBitCast(llvm::Value *value, llvm::Type *resultType) { 30004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.Builder.CreateBitCast(value, resultType); 30014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 30024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *visitLValueToRValue(const Expr *e) { 30044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.EmitScalarExpr(e); 30054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 30064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// For consumptions, just emit the subexpression and perform the 30084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// consumption like normal. 30094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *visitConsumeObject(const Expr *e) { 30104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *value = CGF.EmitScalarExpr(e); 30114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.EmitObjCConsumeObject(e->getType(), value); 30124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 30134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// No special logic for block extensions. (This probably can't 30154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// actually happen in this emitter, though.) 30164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *visitExtendBlockObject(const Expr *e) { 30174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.EmitARCExtendBlockObject(e); 30184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 30194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// For reclaims, perform an unsafeClaim if that's enabled. 30214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *visitReclaimReturnedObject(const Expr *e) { 30224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.EmitARCReclaimReturnedObject(e, /*unsafe*/ true); 30234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 30244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// When we have an undecorated call, just emit it without adding 30264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// the unsafeClaim. 30274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *visitCall(const Expr *e) { 30284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.EmitScalarExpr(e); 30294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 30304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar /// Just do normal scalar emission in the default case. 30324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *visitExpr(const Expr *e) { 30334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return CGF.EmitScalarExpr(e); 30344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 30354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar}; 30364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 30374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic llvm::Value *emitARCUnsafeUnretainedScalarExpr(CodeGenFunction &CGF, 30394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar const Expr *e) { 30404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return ARCUnsafeUnretainedExprEmitter(CGF).visit(e); 30414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 30424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to 30444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// immediately releasing the resut of EmitARCRetainScalarExpr, but 30454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// avoiding any spurious retains, including by performing reclaims 30464967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// with objc_unsafeClaimAutoreleasedReturnValue. 30474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarllvm::Value *CodeGenFunction::EmitARCUnsafeUnretainedScalarExpr(const Expr *e) { 30484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Look through full-expressions. 30494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 30504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar enterFullExpression(cleanups); 30514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar RunCleanupsScope scope(*this); 30524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return emitARCUnsafeUnretainedScalarExpr(*this, cleanups->getSubExpr()); 30534967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 30544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return emitARCUnsafeUnretainedScalarExpr(*this, e); 30564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 30574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstd::pair<LValue,llvm::Value*> 30594967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarCodeGenFunction::EmitARCStoreUnsafeUnretained(const BinaryOperator *e, 30604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar bool ignored) { 30614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Evaluate the RHS first. If we're ignoring the result, assume 30624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // that we can emit at an unsafe +0. 30634967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar llvm::Value *value; 30644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar if (ignored) { 30654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar value = EmitARCUnsafeUnretainedScalarExpr(e->getRHS()); 30664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } else { 30674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar value = EmitScalarExpr(e->getRHS()); 30684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar } 30694967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30704967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar // Emit the LHS and perform the store. 30714967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar LValue lvalue = EmitLValue(e->getLHS()); 30724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar EmitStoreOfScalar(value, lvalue); 30734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 30744967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar return std::pair<LValue,llvm::Value*>(std::move(lvalue), value); 30754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} 30764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar 3077f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 3078f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, 3079f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 3080f85e193739c953358c865005855253af4f68a497John McCall // Evaluate the RHS first. 3081f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); 3082f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 3083f85e193739c953358c865005855253af4f68a497John McCall 3084fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall bool hasImmediateRetain = result.getInt(); 3085fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 3086fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // If we didn't emit a retained object, and the l-value is of block 3087fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // type, then we need to emit the block-retain immediately in case 3088fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // it invalidates the l-value. 3089fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { 3090348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ false); 3091fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall hasImmediateRetain = true; 3092fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall } 3093fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 3094f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 3095f85e193739c953358c865005855253af4f68a497John McCall 3096f85e193739c953358c865005855253af4f68a497John McCall // If the RHS was emitted retained, expand this. 3097fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (hasImmediateRetain) { 3098c53143cc5f4edbd0490eefa9ecc1de4f9c24d8ddNick Lewycky llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation()); 30996da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 31005b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime()); 3101f85e193739c953358c865005855253af4f68a497John McCall } else { 3102545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall value = EmitARCStoreStrong(lvalue, value, ignored); 3103f85e193739c953358c865005855253af4f68a497John McCall } 3104f85e193739c953358c865005855253af4f68a497John McCall 3105f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 3106f85e193739c953358c865005855253af4f68a497John McCall} 3107f85e193739c953358c865005855253af4f68a497John McCall 3108f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 3109f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { 3110f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); 3111f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 3112f85e193739c953358c865005855253af4f68a497John McCall 31136da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 3114f85e193739c953358c865005855253af4f68a497John McCall 3115f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 3116f85e193739c953358c865005855253af4f68a497John McCall} 3117f85e193739c953358c865005855253af4f68a497John McCall 3118f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolStmt( 311916098f366097305c348b356a44638d6c959c6e60Eric Christopher const ObjCAutoreleasePoolStmt &ARPS) { 3120f85e193739c953358c865005855253af4f68a497John McCall const Stmt *subStmt = ARPS.getSubStmt(); 3121f85e193739c953358c865005855253af4f68a497John McCall const CompoundStmt &S = cast<CompoundStmt>(*subStmt); 3122f85e193739c953358c865005855253af4f68a497John McCall 3123f85e193739c953358c865005855253af4f68a497John McCall CGDebugInfo *DI = getDebugInfo(); 312473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 312573fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); 3126f85e193739c953358c865005855253af4f68a497John McCall 3127f85e193739c953358c865005855253af4f68a497John McCall // Keep track of the current cleanup stack depth. 3128f85e193739c953358c865005855253af4f68a497John McCall RunCleanupsScope Scope(*this); 31290a7dd788dbef975f35f273c7ab913f480f7edd60John McCall if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 3130f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCAutoreleasePoolPush(); 3131f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); 3132f85e193739c953358c865005855253af4f68a497John McCall } else { 3133f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); 3134f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); 3135f85e193739c953358c865005855253af4f68a497John McCall } 3136f85e193739c953358c865005855253af4f68a497John McCall 3137651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto *I : S.body()) 3138651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines EmitStmt(I); 3139f85e193739c953358c865005855253af4f68a497John McCall 314073fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 314173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); 3142f85e193739c953358c865005855253af4f68a497John McCall} 31430c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 31440c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// EmitExtendGCLifetime - Given a pointer to an Objective-C object, 31450c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// make sure it survives garbage collection until this point. 31460c24c808e4dce43e88abf2d5f98546b901bd4315John McCallvoid CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { 31470c24c808e4dce43e88abf2d5f98546b901bd4315John McCall // We just use an inline assembly. 31480c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::FunctionType *extenderType 3149de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All); 31500c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::Value *extender 31510c24c808e4dce43e88abf2d5f98546b901bd4315John McCall = llvm::InlineAsm::get(extenderType, 31520c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* assembly */ "", 31530c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* constraints */ "r", 31540c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* side effects */ true); 31550c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 31560c24c808e4dce43e88abf2d5f98546b901bd4315John McCall object = Builder.CreateBitCast(object, VoidPtrTy); 3157bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(extender, object); 31580c24c808e4dce43e88abf2d5f98546b901bd4315John McCall} 31590c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 316020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with 316184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// non-trivial copy assignment function, produce following helper function. 316284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; } 316384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// 316484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanianllvm::Constant * 316520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction( 316620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 3167260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 316890f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 31696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 317084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType Ty = PID->getPropertyIvarDecl()->getType(); 317184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian if (!Ty->isRecordType()) 31726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 317384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 317420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 31756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 31766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines llvm::Constant *HelperFn = nullptr; 317720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialSetExpr(PID)) 31786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 317920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null"); 318020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty))) 318120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 318284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 318384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ASTContext &C = getContext(); 318484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian IdentifierInfo *II 318520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__assign_helper_atomic_property_"); 318684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 318784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian C.getTranslationUnitDecl(), 318884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SourceLocation(), 31896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines SourceLocation(), II, C.VoidTy, 31906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines nullptr, SC_Static, 319184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian false, 3192b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 31936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 319484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 319584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType SrcTy = Ty; 319684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy.addConst(); 319784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 319884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 319984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionArgList args; 32006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines ImplicitParamDecl dstDecl(getContext(), FD, SourceLocation(), nullptr,DestTy); 320184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&dstDecl); 32026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines ImplicitParamDecl srcDecl(getContext(), FD, SourceLocation(), nullptr, SrcTy); 320384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&srcDecl); 3204651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 32054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar const CGFunctionInfo &FI = 32064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, args); 3207651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 3208de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 320984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 321084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function *Fn = 321184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 321216098f366097305c348b356a44638d6c959c6e60Eric Christopher "__assign_helper_atomic_property_", 321316098f366097305c348b356a44638d6c959c6e60Eric Christopher &CGM.getModule()); 321487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 321587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.SetInternalFunctionAttributes(nullptr, Fn, FI); 321687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 32176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines StartFunction(FD, C.VoidTy, Fn, FI, args); 321884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 3219f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 3220f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 3221f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(), 3222f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 322384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 3224f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 3225f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 3226f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 3227f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 322884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 3229f4b88a45902af1802a1cb42ba48b1c474474f228John McCall Expr *Args[2] = { &DST, &SRC }; 323020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment()); 3231f4b88a45902af1802a1cb42ba48b1c474474f228John McCall CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(), 32323b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer Args, DestTy->getPointeeType(), 3233be9af1288881110e406b87914162eaa59f1e5918Lang Hames VK_LValue, SourceLocation(), false); 3234f4b88a45902af1802a1cb42ba48b1c474474f228John McCall 3235f4b88a45902af1802a1cb42ba48b1c474474f228John McCall EmitStmt(&TheCall); 323684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 323784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FinishFunction(); 3238cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 323920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicSetterHelperFnMap(Ty, HelperFn); 3240cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian return HelperFn; 324120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian} 324220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 324320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianllvm::Constant * 324420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction( 324520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 3246260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 324790f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 32486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 324920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 325020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType Ty = PD->getType(); 325120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (!Ty->isRecordType()) 32526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 325320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 32546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 32556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines llvm::Constant *HelperFn = nullptr; 32566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 325720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialGetExpr(PID)) 32586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 325920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null"); 326020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty))) 326120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 326220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 326320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 326420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ASTContext &C = getContext(); 326520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian IdentifierInfo *II 326620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__copy_helper_atomic_property_"); 326720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 326820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian C.getTranslationUnitDecl(), 326920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SourceLocation(), 32706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines SourceLocation(), II, C.VoidTy, 32716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines nullptr, SC_Static, 327220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian false, 3273b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 32746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 327520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 327620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType SrcTy = Ty; 327720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy.addConst(); 327820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 327920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 328020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionArgList args; 32816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines ImplicitParamDecl dstDecl(getContext(), FD, SourceLocation(), nullptr,DestTy); 328220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&dstDecl); 32836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines ImplicitParamDecl srcDecl(getContext(), FD, SourceLocation(), nullptr, SrcTy); 328420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&srcDecl); 3285651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 32864967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar const CGFunctionInfo &FI = 32874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, args); 3288651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 3289de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 329020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 329120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function *Fn = 329220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 329320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian "__copy_helper_atomic_property_", &CGM.getModule()); 329484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 329587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar CGM.SetInternalFunctionAttributes(nullptr, Fn, FI); 329687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 32976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines StartFunction(FD, C.VoidTy, Fn, FI, args); 329820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 3299f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 330020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian VK_RValue, SourceLocation()); 330120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 3302f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 3303f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 330420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 330520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *CXXConstExpr = 330620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian cast<CXXConstructExpr>(PID->getGetterCXXConstructor()); 330720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 330820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SmallVector<Expr*, 4> ConstructorArgs; 3309f4b88a45902af1802a1cb42ba48b1c474474f228John McCall ConstructorArgs.push_back(&SRC); 3310b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar ConstructorArgs.append(std::next(CXXConstExpr->arg_begin()), 3311b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar CXXConstExpr->arg_end()); 3312b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar 331320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *TheCXXConstructExpr = 331420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr::Create(C, Ty, SourceLocation(), 331520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->getConstructor(), 331620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->isElidable(), 33173b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer ConstructorArgs, 33185b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->hadMultipleCandidates(), 33195b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->isListInitialization(), 3320176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines CXXConstExpr->isStdInitListInitialization(), 332120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->requiresZeroInitialization(), 332216098f366097305c348b356a44638d6c959c6e60Eric Christopher CXXConstExpr->getConstructionKind(), 332316098f366097305c348b356a44638d6c959c6e60Eric Christopher SourceRange()); 332420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 3325f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 3326f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 332720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 3328f4b88a45902af1802a1cb42ba48b1c474474f228John McCall RValue DV = EmitAnyExpr(&DstExpr); 332916098f366097305c348b356a44638d6c959c6e60Eric Christopher CharUnits Alignment 333016098f366097305c348b356a44638d6c959c6e60Eric Christopher = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType()); 333120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian EmitAggExpr(TheCXXConstructExpr, 333287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar AggValueSlot::forAddr(Address(DV.getScalarVal(), Alignment), 333387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar Qualifiers(), 333420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::IsDestructed, 333520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::DoesNotNeedGCBarriers, 3336649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 333720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 333820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FinishFunction(); 333920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 334020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicGetterHelperFnMap(Ty, HelperFn); 334120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 334284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian} 334384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 3344cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedmanllvm::Value * 3345cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli FriedmanCodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { 3346cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Get selectors for retain/autorelease. 33478c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman IdentifierInfo *CopyID = &getContext().Idents.get("copy"); 33488c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Selector CopySelector = 33498c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman getContext().Selectors.getNullarySelector(CopyID); 3350cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease"); 3351cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Selector AutoreleaseSelector = 3352cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman getContext().Selectors.getNullarySelector(AutoreleaseID); 3353cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 3354cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Emit calls to retain/autorelease. 3355cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3356cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman llvm::Value *Val = Block; 3357cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman RValue Result; 3358cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 33598c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Ty, CopySelector, 33606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Val, CallArgList(), nullptr, nullptr); 3361cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3362cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 3363cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Ty, AutoreleaseSelector, 33646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Val, CallArgList(), nullptr, nullptr); 3365cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3366cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman return Val; 3367cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman} 3368cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 336984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 33702979ec73b4f974d85f2ce84167712177a44c6f09Ted KremenekCGObjCRuntime::~CGObjCRuntime() {} 3371