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"
233d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson#include "llvm/ADT/STLExtras.h"
24c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar#include "llvm/Target/TargetData.h"
25f85e193739c953358c865005855253af4f68a497John McCall#include "llvm/InlineAsm.h"
265508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace clang;
275508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace CodeGen;
285508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
29f85e193739c953358c865005855253af4f68a497John McCalltypedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult;
30f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult
31f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e);
32f85e193739c953358c865005855253af4f68a497John McCall
33f85e193739c953358c865005855253af4f68a497John McCall/// Given the address of a variable of pointer type, find the correct
34f85e193739c953358c865005855253af4f68a497John McCall/// null to store into it.
35f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *getNullForVariable(llvm::Value *addr) {
362acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *type =
37f85e193739c953358c865005855253af4f68a497John McCall    cast<llvm::PointerType>(addr->getType())->getElementType();
38f85e193739c953358c865005855253af4f68a497John McCall  return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type));
39f85e193739c953358c865005855253af4f68a497John McCall}
40f85e193739c953358c865005855253af4f68a497John McCall
418fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emits an instance of NSConstantString representing the object.
421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
4371fcec9abf2ce66d5e17a24bd021680e94e42f0dDaniel Dunbar{
440d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  llvm::Constant *C =
450d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall      CGM.getObjCRuntime().GenerateConstantString(E->getString());
46ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  // FIXME: This bitcast should just be made an invariant on the Runtime.
473c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
488fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner}
498fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
508fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emit a selector.
518fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattnerllvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
528fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Untyped selector.
538fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Note that this implementation allows for non-constant strings to be passed
548fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // as arguments to @selector().  Currently, the only thing preventing this
558fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // behaviour is the type checking in the front end.
566d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbar  return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
578fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner}
588fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
59ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbarllvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
60ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  // FIXME: This should pass the Decl not the name.
61ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
62ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar}
638fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
64926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// \brief Adjust the type of the result of an Objective-C message send
65926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// expression when the method has a related result type.
66926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregorstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF,
67926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                      const Expr *E,
68926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                      const ObjCMethodDecl *Method,
69926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                      RValue Result) {
70926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (!Method)
71926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return Result;
72f85e193739c953358c865005855253af4f68a497John McCall
73926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (!Method->hasRelatedResultType() ||
74926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      CGF.getContext().hasSameType(E->getType(), Method->getResultType()) ||
75926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      !Result.isScalar())
76926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return Result;
77926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
78926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  // We have applied a related result type. Cast the rvalue appropriately.
79926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
80926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                               CGF.ConvertType(E->getType())));
81926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor}
828fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
83dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// Decide whether to extend the lifetime of the receiver of a
84dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// returns-inner-pointer message.
85dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallstatic bool
86dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallshouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) {
87dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  switch (message->getReceiverKind()) {
88dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
89dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // For a normal instance message, we should extend unless the
90dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // receiver is loaded from a variable with precise lifetime.
91dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  case ObjCMessageExpr::Instance: {
92dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    const Expr *receiver = message->getInstanceReceiver();
93dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver);
94dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (!ice || ice->getCastKind() != CK_LValueToRValue) return true;
95dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    receiver = ice->getSubExpr()->IgnoreParens();
96dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
97dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // Only __strong variables.
98dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
99dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      return true;
100dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
101dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // All ivars and fields have precise lifetime.
102dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver))
103dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      return false;
104dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
105dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // Otherwise, check for variables.
106dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr());
107dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (!declRef) return true;
108dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl());
109dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (!var) return true;
110dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
111dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // All variables have precise lifetime except local variables with
112dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // automatic storage duration that aren't specially marked.
113dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    return (var->hasLocalStorage() &&
114dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall            !var->hasAttr<ObjCPreciseLifetimeAttr>());
115dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  }
116dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
117dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  case ObjCMessageExpr::Class:
118dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  case ObjCMessageExpr::SuperClass:
119dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // It's never necessary for class objects.
120dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    return false;
121dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
122dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  case ObjCMessageExpr::SuperInstance:
123dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // We generally assume that 'self' lives throughout a method call.
124dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    return false;
125dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  }
126dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
127dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  llvm_unreachable("invalid receiver kind");
128dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall}
129dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
130ef072fd2f3347cfd857d6eb787b245b950771430John McCallRValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
131ef072fd2f3347cfd857d6eb787b245b950771430John McCall                                            ReturnValueSlot Return) {
1328fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Only the lookup mechanism and first two arguments of the method
1338fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // implementation vary between runtimes.  We can get the receiver and
1348fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // arguments in generic code.
1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136f85e193739c953358c865005855253af4f68a497John McCall  bool isDelegateInit = E->isDelegateInitCall();
137f85e193739c953358c865005855253af4f68a497John McCall
138dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  const ObjCMethodDecl *method = E->getMethodDecl();
139dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
140f85e193739c953358c865005855253af4f68a497John McCall  // We don't retain the receiver in delegate init calls, and this is
141f85e193739c953358c865005855253af4f68a497John McCall  // safe because the receiver value is always loaded from 'self',
142f85e193739c953358c865005855253af4f68a497John McCall  // which we zero out.  We don't want to Block_copy block receivers,
143f85e193739c953358c865005855253af4f68a497John McCall  // though.
144f85e193739c953358c865005855253af4f68a497John McCall  bool retainSelf =
145f85e193739c953358c865005855253af4f68a497John McCall    (!isDelegateInit &&
146f85e193739c953358c865005855253af4f68a497John McCall     CGM.getLangOptions().ObjCAutoRefCount &&
147dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall     method &&
148dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall     method->hasAttr<NSConsumesSelfAttr>());
149f85e193739c953358c865005855253af4f68a497John McCall
150208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
1518fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  bool isSuperMessage = false;
152f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar  bool isClassMessage = false;
153c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall  ObjCInterfaceDecl *OID = 0;
1548fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Find the receiver
155926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  QualType ReceiverType;
1560b647a6ea18151149d624ab373e6fe0e819e4a9aDaniel Dunbar  llvm::Value *Receiver = 0;
15704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  switch (E->getReceiverKind()) {
15804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::Instance:
159926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    ReceiverType = E->getInstanceReceiver()->getType();
160f85e193739c953358c865005855253af4f68a497John McCall    if (retainSelf) {
161f85e193739c953358c865005855253af4f68a497John McCall      TryEmitResult ter = tryEmitARCRetainScalarExpr(*this,
162f85e193739c953358c865005855253af4f68a497John McCall                                                   E->getInstanceReceiver());
163f85e193739c953358c865005855253af4f68a497John McCall      Receiver = ter.getPointer();
164dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      if (ter.getInt()) retainSelf = false;
165f85e193739c953358c865005855253af4f68a497John McCall    } else
166f85e193739c953358c865005855253af4f68a497John McCall      Receiver = EmitScalarExpr(E->getInstanceReceiver());
16704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
1681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::Class: {
170926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    ReceiverType = E->getClassReceiver();
171926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>();
1723031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    assert(ObjTy && "Invalid Objective-C class message send");
1733031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    OID = ObjTy->getInterface();
1743031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    assert(OID && "Invalid Objective-C class message send");
175c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall    Receiver = Runtime.GetClass(Builder, OID);
176f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar    isClassMessage = true;
17704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
17804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  }
17904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
18004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::SuperInstance:
181926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    ReceiverType = E->getSuperType();
18204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    Receiver = LoadObjCSelf();
1838fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner    isSuperMessage = true;
18404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
18504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
18604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::SuperClass:
187926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    ReceiverType = E->getSuperType();
1888fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner    Receiver = LoadObjCSelf();
18904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    isSuperMessage = true;
19004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    isClassMessage = true;
19104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
1928fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  }
1938fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
194dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  if (retainSelf)
195dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    Receiver = EmitARCRetainNonBlock(Receiver);
196dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
197dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // In ARC, we sometimes want to "extend the lifetime"
198dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // (i.e. retain+autorelease) of receivers of returns-inner-pointer
199dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // messages.
200dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  if (getLangOptions().ObjCAutoRefCount && method &&
201dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      method->hasAttr<ObjCReturnsInnerPointerAttr>() &&
202dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      shouldExtendReceiverForInnerPointerMessage(E))
203dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver);
204dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
205f85e193739c953358c865005855253af4f68a497John McCall  QualType ResultType =
206dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    method ? method->getResultType() : E->getType();
207f85e193739c953358c865005855253af4f68a497John McCall
20819cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar  CallArgList Args;
209dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  EmitCallArgs(Args, method, E->arg_begin(), E->arg_end());
2101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
211f85e193739c953358c865005855253af4f68a497John McCall  // For delegate init calls in ARC, do an unsafe store of null into
212f85e193739c953358c865005855253af4f68a497John McCall  // self.  This represents the call taking direct ownership of that
213f85e193739c953358c865005855253af4f68a497John McCall  // value.  We have to do this after emitting the other call
214f85e193739c953358c865005855253af4f68a497John McCall  // arguments because they might also reference self, but we don't
215f85e193739c953358c865005855253af4f68a497John McCall  // have to worry about any of them modifying self because that would
216f85e193739c953358c865005855253af4f68a497John McCall  // be an undefined read and write of an object in unordered
217f85e193739c953358c865005855253af4f68a497John McCall  // expressions.
218f85e193739c953358c865005855253af4f68a497John McCall  if (isDelegateInit) {
219f85e193739c953358c865005855253af4f68a497John McCall    assert(getLangOptions().ObjCAutoRefCount &&
220f85e193739c953358c865005855253af4f68a497John McCall           "delegate init calls should only be marked in ARC");
221f85e193739c953358c865005855253af4f68a497John McCall
222f85e193739c953358c865005855253af4f68a497John McCall    // Do an unsafe store of null into self.
223f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *selfAddr =
224f85e193739c953358c865005855253af4f68a497John McCall      LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
225f85e193739c953358c865005855253af4f68a497John McCall    assert(selfAddr && "no self entry for a delegate init call?");
226f85e193739c953358c865005855253af4f68a497John McCall
227f85e193739c953358c865005855253af4f68a497John McCall    Builder.CreateStore(getNullForVariable(selfAddr), selfAddr);
228f85e193739c953358c865005855253af4f68a497John McCall  }
2297e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson
230926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  RValue result;
2318fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  if (isSuperMessage) {
2329384c768e93f270118a30ce96546083a666da284Chris Lattner    // super is only valid in an Objective-C method
2339384c768e93f270118a30ce96546083a666da284Chris Lattner    const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
2347ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian    bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
235926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
236926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              E->getSelector(),
237926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              OMD->getClassInterface(),
238926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              isCategoryImpl,
239926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              Receiver,
240926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              isClassMessage,
241926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              Args,
242dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall                                              method);
243926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  } else {
244926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    result = Runtime.GenerateMessageSend(*this, Return, ResultType,
245926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                         E->getSelector(),
246926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                         Receiver, Args, OID,
247dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall                                         method);
2488fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  }
249f85e193739c953358c865005855253af4f68a497John McCall
250f85e193739c953358c865005855253af4f68a497John McCall  // For delegate init calls in ARC, implicitly store the result of
251f85e193739c953358c865005855253af4f68a497John McCall  // the call back into self.  This takes ownership of the value.
252f85e193739c953358c865005855253af4f68a497John McCall  if (isDelegateInit) {
253f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *selfAddr =
254f85e193739c953358c865005855253af4f68a497John McCall      LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
255f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *newSelf = result.getScalarVal();
256f85e193739c953358c865005855253af4f68a497John McCall
257f85e193739c953358c865005855253af4f68a497John McCall    // The delegate return type isn't necessarily a matching type; in
258f85e193739c953358c865005855253af4f68a497John McCall    // fact, it's quite likely to be 'id'.
2592acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *selfTy =
260f85e193739c953358c865005855253af4f68a497John McCall      cast<llvm::PointerType>(selfAddr->getType())->getElementType();
261f85e193739c953358c865005855253af4f68a497John McCall    newSelf = Builder.CreateBitCast(newSelf, selfTy);
262f85e193739c953358c865005855253af4f68a497John McCall
263f85e193739c953358c865005855253af4f68a497John McCall    Builder.CreateStore(newSelf, selfAddr);
264f85e193739c953358c865005855253af4f68a497John McCall  }
265f85e193739c953358c865005855253af4f68a497John McCall
266dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  return AdjustRelatedResultType(*this, E, method, result);
2675508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
2685508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
269f85e193739c953358c865005855253af4f68a497John McCallnamespace {
270f85e193739c953358c865005855253af4f68a497John McCallstruct FinishARCDealloc : EHScopeStack::Cleanup {
271ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall  void Emit(CodeGenFunction &CGF, Flags flags) {
272f85e193739c953358c865005855253af4f68a497John McCall    const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl);
273799d34e9505a833549c71f2ac5f842da157ea031John McCall
274799d34e9505a833549c71f2ac5f842da157ea031John McCall    const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
275f85e193739c953358c865005855253af4f68a497John McCall    const ObjCInterfaceDecl *iface = impl->getClassInterface();
276f85e193739c953358c865005855253af4f68a497John McCall    if (!iface->getSuperClass()) return;
277f85e193739c953358c865005855253af4f68a497John McCall
278799d34e9505a833549c71f2ac5f842da157ea031John McCall    bool isCategory = isa<ObjCCategoryImplDecl>(impl);
279799d34e9505a833549c71f2ac5f842da157ea031John McCall
280f85e193739c953358c865005855253af4f68a497John McCall    // Call [super dealloc] if we have a superclass.
281f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *self = CGF.LoadObjCSelf();
282f85e193739c953358c865005855253af4f68a497John McCall
283f85e193739c953358c865005855253af4f68a497John McCall    CallArgList args;
284f85e193739c953358c865005855253af4f68a497John McCall    CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(),
285f85e193739c953358c865005855253af4f68a497John McCall                                                      CGF.getContext().VoidTy,
286f85e193739c953358c865005855253af4f68a497John McCall                                                      method->getSelector(),
287f85e193739c953358c865005855253af4f68a497John McCall                                                      iface,
288799d34e9505a833549c71f2ac5f842da157ea031John McCall                                                      isCategory,
289f85e193739c953358c865005855253af4f68a497John McCall                                                      self,
290f85e193739c953358c865005855253af4f68a497John McCall                                                      /*is class msg*/ false,
291f85e193739c953358c865005855253af4f68a497John McCall                                                      args,
292f85e193739c953358c865005855253af4f68a497John McCall                                                      method);
293f85e193739c953358c865005855253af4f68a497John McCall  }
294f85e193739c953358c865005855253af4f68a497John McCall};
295f85e193739c953358c865005855253af4f68a497John McCall}
296f85e193739c953358c865005855253af4f68a497John McCall
297af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
298af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// the LLVM function and sets the other context used by
299af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// CodeGenFunction.
300679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianvoid CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
3018d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel                                      const ObjCContainerDecl *CD,
3028d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel                                      SourceLocation StartLoc) {
303d26bc76c98006609002d9930f8840490e88ac5b5John McCall  FunctionArgList args;
3044800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel  // Check if we should generate debug info for this method.
305aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  if (CGM.getModuleDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
306aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel    DebugInfo = CGM.getModuleDebugInfo();
3074800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel
308679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
309f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
3100e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
3110e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
3124111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
313d26bc76c98006609002d9930f8840490e88ac5b5John McCall  args.push_back(OMD->getSelfDecl());
314d26bc76c98006609002d9930f8840490e88ac5b5John McCall  args.push_back(OMD->getCmdDecl());
3154111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
316491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = OMD->param_begin(),
31789951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = OMD->param_end(); PI != E; ++PI)
318d26bc76c98006609002d9930f8840490e88ac5b5John McCall    args.push_back(*PI);
319b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar
32014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  CurGD = OMD;
32114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
3228d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel  StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc);
323f85e193739c953358c865005855253af4f68a497John McCall
324f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, certain methods get an extra cleanup.
325f85e193739c953358c865005855253af4f68a497John McCall  if (CGM.getLangOptions().ObjCAutoRefCount &&
326f85e193739c953358c865005855253af4f68a497John McCall      OMD->isInstanceMethod() &&
327f85e193739c953358c865005855253af4f68a497John McCall      OMD->getSelector().isUnarySelector()) {
328f85e193739c953358c865005855253af4f68a497John McCall    const IdentifierInfo *ident =
329f85e193739c953358c865005855253af4f68a497John McCall      OMD->getSelector().getIdentifierInfoForSlot(0);
330f85e193739c953358c865005855253af4f68a497John McCall    if (ident->isStr("dealloc"))
331f85e193739c953358c865005855253af4f68a497John McCall      EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
332f85e193739c953358c865005855253af4f68a497John McCall  }
333af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
334af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
335f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
336f85e193739c953358c865005855253af4f68a497John McCall                                              LValue lvalue, QualType type);
337f85e193739c953358c865005855253af4f68a497John McCall
338af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// Generate an Objective-C method.  An Objective-C method is a C function with
3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// its pointer, name, and types registered in the class struture.
340af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
3418d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel  StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart());
3426fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  EmitStmt(OMD->getBody());
3436fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  FinishFunction(OMD->getBodyRBrace());
344af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
345af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
34641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructGetterCall - Call the runtime function to load a property
34741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// into the return value slot.
34841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar,
34941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                                 bool isAtomic, bool hasStrong) {
35041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  ASTContext &Context = CGF.getContext();
35141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
35241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *src =
35341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall    CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(),
35441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                          ivar, 0).getAddress();
35541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
35641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  // objc_copyStruct (ReturnValue, &structIvar,
35741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  //                  sizeof (Type of Ivar), isAtomic, false);
35841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  CallArgList args;
35941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
36041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy);
36141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(dest), Context.VoidPtrTy);
36241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
36341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy);
36441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(src), Context.VoidPtrTy);
36541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
36641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType());
36741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType());
36841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy);
36941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy);
37041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
37141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction();
37241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  CGF.EmitCall(CGF.getTypes().getFunctionInfo(Context.VoidTy, args,
37341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                                              FunctionType::ExtInfo()),
37441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall               fn, ReturnValueSlot(), args);
37541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall}
37641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
3771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Determine whether the given architecture supports unaligned atomic
3781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// accesses.  They don't have to be fast, just faster than a function
3791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// call and a mutex.
3801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasUnalignedAtomics(llvm::Triple::ArchType arch) {
381de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman  // FIXME: Allow unaligned atomic load/store on x86.  (It is not
382de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman  // currently supported by the backend.)
383de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman  return 0;
3841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
3851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
3861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Return the maximum size that permits atomic accesses for the given
3871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// architecture.
3881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM,
3891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                                        llvm::Triple::ArchType arch) {
3901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // ARM has 8-byte atomic accesses, but it's not clear whether we
3911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // want to rely on them here.
3921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
3931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // In the default case, just assume that any size up to a pointer is
3941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // fine given adequate alignment.
3951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  return CharUnits::fromQuantity(CGM.PointerSizeInBytes);
3961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
3971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
3981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallnamespace {
3991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  class PropertyImplStrategy {
4001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  public:
4011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    enum StrategyKind {
4021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// The 'native' strategy is to use the architecture's provided
4031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// reads and writes.
4041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Native,
4051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// Use objc_setProperty and objc_getProperty.
4071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      GetSetProperty,
4081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// Use objc_setProperty for the setter, but use expression
4101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// evaluation for the getter.
4111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      SetPropertyAndExpressionGet,
4121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// Use objc_copyStruct.
4141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      CopyStruct,
4151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// The 'expression' strategy is to emit normal assignment or
4171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// lvalue-to-rvalue expressions.
4181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Expression
4191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    };
4201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    StrategyKind getKind() const { return StrategyKind(Kind); }
4221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bool hasStrongMember() const { return HasStrong; }
4241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bool isAtomic() const { return IsAtomic; }
4251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bool isCopy() const { return IsCopy; }
4261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CharUnits getIvarSize() const { return IvarSize; }
4281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CharUnits getIvarAlignment() const { return IvarAlignment; }
4291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    PropertyImplStrategy(CodeGenModule &CGM,
4311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                         const ObjCPropertyImplDecl *propImpl);
4321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  private:
4341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    unsigned Kind : 8;
4351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    unsigned IsAtomic : 1;
4361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    unsigned IsCopy : 1;
4371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    unsigned HasStrong : 1;
4381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CharUnits IvarSize;
4401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CharUnits IvarAlignment;
4411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  };
4421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
4431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Pick an implementation strategy for the the given property synthesis.
4451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallPropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM,
4461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                                     const ObjCPropertyImplDecl *propImpl) {
4471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
448265941bc308d65cc270d5c4de5806f37ce405606John McCall  ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind();
4491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
450265941bc308d65cc270d5c4de5806f37ce405606John McCall  IsCopy = (setterKind == ObjCPropertyDecl::Copy);
451265941bc308d65cc270d5c4de5806f37ce405606John McCall  IsAtomic = prop->isAtomic();
4521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  HasStrong = false; // doesn't matter here.
4531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Evaluate the ivar's size and alignment.
4551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
4561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  QualType ivarType = ivar->getType();
4571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  llvm::tie(IvarSize, IvarAlignment)
4581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    = CGM.getContext().getTypeInfoInChars(ivarType);
4591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If we have a copy property, we always have to use getProperty/setProperty.
461265941bc308d65cc270d5c4de5806f37ce405606John McCall  // TODO: we could actually use setProperty and an expression for non-atomics.
4621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (IsCopy) {
4631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = GetSetProperty;
4641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
4651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
4661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
467265941bc308d65cc270d5c4de5806f37ce405606John McCall  // Handle retain.
468265941bc308d65cc270d5c4de5806f37ce405606John McCall  if (setterKind == ObjCPropertyDecl::Retain) {
4691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // In GC-only, there's nothing special that needs to be done.
470e289d81369914678db386f6aa86faf8f178e245dDouglas Gregor    if (CGM.getLangOptions().getGC() == LangOptions::GCOnly) {
4711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      // fallthrough
4721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // In ARC, if the property is non-atomic, use expression emission,
4741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // which translates to objc_storeStrong.  This isn't required, but
4751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // it's slightly nicer.
4761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    } else if (CGM.getLangOptions().ObjCAutoRefCount && !IsAtomic) {
4771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Kind = Expression;
4781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      return;
4791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Otherwise, we need to at least use setProperty.  However, if
4811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // the property isn't atomic, we can use normal expression
4821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // emission for the getter.
4831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    } else if (!IsAtomic) {
4841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Kind = SetPropertyAndExpressionGet;
4851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      return;
4861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Otherwise, we have to use both setProperty and getProperty.
4881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    } else {
4891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Kind = GetSetProperty;
4901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      return;
4911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    }
4921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
4931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
4941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If we're not atomic, just use expression accesses.
4951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!IsAtomic) {
4961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = Expression;
4971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
4981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
4991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5005889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall  // Properties on bitfield ivars need to be emitted using expression
5015889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall  // accesses even if they're nominally atomic.
5025889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall  if (ivar->isBitField()) {
5035889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall    Kind = Expression;
5045889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall    return;
5055889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall  }
5065889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall
5071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // GC-qualified or ARC-qualified ivars need to be emitted as
5081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // expressions.  This actually works out to being atomic anyway,
5091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // except for ARC __strong, but that should trigger the above code.
5101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (ivarType.hasNonTrivialObjCLifetime() ||
511e289d81369914678db386f6aa86faf8f178e245dDouglas Gregor      (CGM.getLangOptions().getGC() &&
5121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall       CGM.getContext().getObjCGCAttrKind(ivarType))) {
5131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = Expression;
5141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
5151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
5161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Compute whether the ivar has strong members.
518e289d81369914678db386f6aa86faf8f178e245dDouglas Gregor  if (CGM.getLangOptions().getGC())
5191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    if (const RecordType *recordType = ivarType->getAs<RecordType>())
5201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      HasStrong = recordType->getDecl()->hasObjectMember();
5211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // We can never access structs with object members with a native
5231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // access, because we need to use write barriers.  This is what
5241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // objc_copyStruct is for.
5251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (HasStrong) {
5261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = CopyStruct;
5271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
5281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
5291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Otherwise, this is target-dependent and based on the size and
5311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // alignment of the ivar.
532c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall
533c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall  // If the size of the ivar is not a power of two, give up.  We don't
534c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall  // want to get into the business of doing compare-and-swaps.
535c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall  if (!IvarSize.isPowerOfTwo()) {
536c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall    Kind = CopyStruct;
537c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall    return;
538c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall  }
539c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall
5401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  llvm::Triple::ArchType arch =
5411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CGM.getContext().getTargetInfo().getTriple().getArch();
5421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Most architectures require memory to fit within a single cache
5441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // line, so the alignment has to be at least the size of the access.
5451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Otherwise we have to grab a lock.
5461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) {
5471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = CopyStruct;
5481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
5491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
5501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If the ivar's size exceeds the architecture's maximum atomic
5521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // access size, we have to use CopyStruct.
5531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) {
5541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = CopyStruct;
5551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
5561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
5571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Otherwise, we can use native loads and stores.
5591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  Kind = Native;
5601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
561af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
562af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// GenerateObjCGetter - Generate an Objective-C property getter
563489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
564489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category.
565fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
566fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                         const ObjCPropertyImplDecl *PID) {
567af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
568af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
569af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  assert(OMD && "Invalid call to generate getter (empty method)");
5708d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel  StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart());
5711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  generateObjCGetterBody(IMP, PID);
5731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  FinishFunction();
5751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
5761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5776c11f0b3106263600af2ea0438ebb372821514aaJohn McCallstatic bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) {
5786c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  const Expr *getter = propImpl->getGetterCXXConstructor();
5791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!getter) return true;
5801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Sema only makes only of these when the ivar has a C++ class type,
5821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // so the form is pretty constrained.
5831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5846c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  // If the property has a reference type, we might just be binding a
5856c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  // reference, in which case the result will be a gl-value.  We should
5866c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  // treat this as a non-trivial operation.
5876c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  if (getter->isGLValue())
5886c11f0b3106263600af2ea0438ebb372821514aaJohn McCall    return false;
5896c11f0b3106263600af2ea0438ebb372821514aaJohn McCall
5901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If we selected a trivial copy-constructor, we're okay.
5911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter))
5921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return (construct->getConstructor()->isTrivial());
5931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // The constructor might require cleanups (in which case it's never
5951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // trivial).
5961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  assert(isa<ExprWithCleanups>(getter));
5971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  return false;
5981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
5991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallvoid
6011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallCodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
6021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                                        const ObjCPropertyImplDecl *propImpl) {
6031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If there's a non-trivial 'get' expression, we just have to emit that.
6041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!hasTrivialGetExpr(propImpl)) {
6051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(),
6061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                   /*nrvo*/ 0);
6071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    EmitReturnStmt(ret);
6081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
6091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
6101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
6121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  QualType propType = prop->getType();
6131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl();
6141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
6161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Pick an implementation strategy.
6181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  PropertyImplStrategy strategy(CGM, propImpl);
6191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  switch (strategy.getKind()) {
6201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::Native: {
6211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
6221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Currently, all atomic accesses have to be through integer
6241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // types, so there's no point in trying to pick a prettier type.
6251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Type *bitcastType =
6261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      llvm::Type::getIntNTy(getLLVMContext(),
6271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                            getContext().toBits(strategy.getIvarSize()));
6281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
6291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Perform an atomic load.  This does not impose ordering constraints.
6311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *ivarAddr = LV.getAddress();
6321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
6331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load");
6341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    load->setAlignment(strategy.getIvarAlignment().getQuantity());
6351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    load->setAtomic(llvm::Unordered);
6361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Store that value into the return address.  Doing this with a
6381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // bitcast is likely to produce some pretty ugly IR, but it's not
6391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // the *most* terrible thing in the world.
6401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType));
6411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Make sure we don't do an autorelease.
6431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    AutoreleaseResult = false;
6441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
6451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
6461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::GetSetProperty: {
6481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *getPropertyFn =
6491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      CGM.getObjCRuntime().GetPropertyGetFunction();
6501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    if (!getPropertyFn) {
6511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy");
652c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      return;
653c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    }
654c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar
655c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
656c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // FIXME: Can't this be simpler? This might even be worse than the
657c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // corresponding gcc code.
6581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *cmd =
6591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd");
6601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
6611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *ivarOffset =
6621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      EmitIvarOffset(classImpl->getClassInterface(), ivar);
6631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CallArgList args;
6651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    args.add(RValue::get(self), getContext().getObjCIdType());
6661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    args.add(RValue::get(cmd), getContext().getObjCSelType());
6671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
668265941bc308d65cc270d5c4de5806f37ce405606John McCall    args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
669265941bc308d65cc270d5c4de5806f37ce405606John McCall             getContext().BoolTy);
6701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
671e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar    // FIXME: We shouldn't need to get the function info here, the
672e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar    // runtime already should have computed it to build the function.
6731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    RValue RV = EmitCall(getTypes().getFunctionInfo(propType, args,
67441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                                                    FunctionType::ExtInfo()),
6751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                         getPropertyFn, ReturnValueSlot(), args);
6761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
677c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // We need to fix the type here. Ivars with copy & retain are
678c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // always objects so we don't need to worry about complex or
679c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // aggregates.
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
6811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                                           getTypes().ConvertType(propType)));
6821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    EmitReturnOfRValue(RV, propType);
684f85e193739c953358c865005855253af4f68a497John McCall
685f85e193739c953358c865005855253af4f68a497John McCall    // objc_getProperty does an autorelease, so we should suppress ours.
686f85e193739c953358c865005855253af4f68a497John McCall    AutoreleaseResult = false;
6871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
6891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
6901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::CopyStruct:
6921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    emitStructGetterCall(*this, ivar, strategy.isAtomic(),
6931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                         strategy.hasStrongMember());
6941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
6951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::Expression:
6971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::SetPropertyAndExpressionGet: {
6981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
6991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    QualType ivarType = ivar->getType();
7011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    if (ivarType->isAnyComplexType()) {
7021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      ComplexPairTy pair = LoadComplexFromAddr(LV.getAddress(),
7031b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian                                               LV.isVolatileQualified());
7041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      StoreComplexToAddr(pair, ReturnValue, LV.isVolatileQualified());
7051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    } else if (hasAggregateLLVMType(ivarType)) {
7061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      // The return value slot is guaranteed to not be aliased, but
7071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      // that's not necessarily the same as "on the stack", so
7081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      // we still potentially need objc_memmove_collectable.
7091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType);
710ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall    } else {
711ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      llvm::Value *value;
712ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      if (propType->isReferenceType()) {
713ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        value = LV.getAddress();
714ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      } else {
715ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        // We want to load and autoreleaseReturnValue ARC __weak ivars.
716ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
7171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall          value = emitARCRetainLoadOfScalar(*this, LV, ivarType);
718ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall
719ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        // Otherwise we want to do a simple load, suppressing the
720ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        // final autorelease.
721f85e193739c953358c865005855253af4f68a497John McCall        } else {
722ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall          value = EmitLoadOfLValue(LV).getScalarVal();
723ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall          AutoreleaseResult = false;
72414086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian        }
725f85e193739c953358c865005855253af4f68a497John McCall
726ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        value = Builder.CreateBitCast(value, ConvertType(propType));
727ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      }
728ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall
729ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      EmitReturnOfRValue(RValue::get(value), propType);
730ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian    }
7311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
732c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  }
733af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
7341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
7351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  llvm_unreachable("bad @property implementation strategy!");
736af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
737af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
73841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructSetterCall - Call the runtime function to store the value
73941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// from the first formal parameter into the given ivar.
74041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD,
74141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                                 ObjCIvarDecl *ivar) {
7422846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  // objc_copyStruct (&structIvar, &Arg,
7432846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  //                  sizeof (struct something), true, false);
744bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  CallArgList args;
745bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
746bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  // The first argument is the address of the ivar.
74741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
74841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                                                CGF.LoadObjCSelf(), ivar, 0)
74941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall    .getAddress();
75041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
75141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
752bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
753bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  // The second argument is the address of the parameter variable.
75441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  ParmVarDecl *argVar = *OMD->param_begin();
75541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  DeclRefExpr argRef(argVar, argVar->getType(), VK_LValue, SourceLocation());
75641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress();
75741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
75841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
759bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
760bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  // The third argument is the sizeof the type.
761bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  llvm::Value *size =
76241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall    CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType()));
76341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(size), CGF.getContext().getSizeType());
76441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
76541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  // The fourth argument is the 'isAtomic' flag.
76641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy);
767bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
76841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  // The fifth argument is the 'hasStrong' flag.
76941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  // FIXME: should this really always be false?
77041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy);
771bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
77241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction();
77341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  CGF.EmitCall(CGF.getTypes().getFunctionInfo(CGF.getContext().VoidTy, args,
77441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                                              FunctionType::ExtInfo()),
77541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall               copyStructFn, ReturnValueSlot(), args);
7762846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian}
7772846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian
7781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) {
7791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  Expr *setter = PID->getSetterCXXAssignment();
7801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!setter) return true;
7811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Sema only makes only of these when the ivar has a C++ class type,
7831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // so the form is pretty constrained.
78471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
78571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // An operator call is trivial if the function it calls is trivial.
7861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // This also implies that there's nothing non-trivial going on with
7871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // the arguments, because operator= can only be trivial if it's a
7881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // synthesized assignment operator and therefore both parameters are
7891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // references.
7901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (CallExpr *call = dyn_cast<CallExpr>(setter)) {
79171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    if (const FunctionDecl *callee
79271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall          = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl()))
79371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      if (callee->isTrivial())
79471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall        return true;
79571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    return false;
79601cb307f0b84a368cdbc0738d6680aab8ed7423fFariborz Jahanian  }
79771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
7981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  assert(isa<ExprWithCleanups>(setter));
79971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  return false;
80071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall}
80186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar
80271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid
80371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallCodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
80471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                                        const ObjCPropertyImplDecl *propImpl) {
80571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // Just use the setter expression if Sema gave us one and it's
80671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // non-trivial.  There's no way to do this atomically.
8071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!hasTrivialSetExpr(propImpl)) {
80871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    EmitStmt(propImpl->getSetterCXXAssignment());
80971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    return;
81071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  }
81171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
81271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
81371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
81471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl();
81571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
8161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  PropertyImplStrategy strategy(CGM, propImpl);
8171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  switch (strategy.getKind()) {
8181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::Native: {
8191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()];
82071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
8211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    LValue ivarLValue =
8221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0);
8231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *ivarAddr = ivarLValue.getAddress();
8241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Currently, all atomic accesses have to be through integer
8261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // types, so there's no point in trying to pick a prettier type.
8271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Type *bitcastType =
8281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      llvm::Type::getIntNTy(getLLVMContext(),
8291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                            getContext().toBits(strategy.getIvarSize()));
8301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
8311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Cast both arguments to the chosen operation type.
8331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    argAddr = Builder.CreateBitCast(argAddr, bitcastType);
8341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
83571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
8361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // This bitcast load is likely to cause some nasty IR.
8371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *load = Builder.CreateLoad(argAddr);
8381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Perform an atomic store.  There are no memory ordering requirements.
8401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr);
8411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    store->setAlignment(strategy.getIvarAlignment().getQuantity());
8421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    store->setAtomic(llvm::Unordered);
8431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
8441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
8451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::GetSetProperty:
8471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::SetPropertyAndExpressionGet: {
84871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    llvm::Value *setPropertyFn =
84971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      CGM.getObjCRuntime().GetPropertySetFunction();
85071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    if (!setPropertyFn) {
85171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy");
85286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar      return;
85386957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    }
8541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Emit objc_setProperty((id) self, _cmd, offset, arg,
85686957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    //                       <is-atomic>, <is-copy>).
85771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    llvm::Value *cmd =
85871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]);
85971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    llvm::Value *self =
86071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
86171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    llvm::Value *ivarOffset =
86271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      EmitIvarOffset(classImpl->getClassInterface(), ivar);
86371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()];
86471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy);
86571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
86671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    CallArgList args;
86771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    args.add(RValue::get(self), getContext().getObjCIdType());
86871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    args.add(RValue::get(cmd), getContext().getObjCSelType());
86971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
87071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    args.add(RValue::get(arg), getContext().getObjCIdType());
8711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
8721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall             getContext().BoolTy);
8731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    args.add(RValue::get(Builder.getInt1(strategy.isCopy())),
8741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall             getContext().BoolTy);
875f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: We shouldn't need to get the function info here, the runtime
876f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // already should have computed it to build the function.
87771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    EmitCall(getTypes().getFunctionInfo(getContext().VoidTy, args,
87871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                                        FunctionType::ExtInfo()),
87971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall             setPropertyFn, ReturnValueSlot(), args);
88071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    return;
88171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  }
88271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
8831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::CopyStruct:
88441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall    emitStructSetterCall(*this, setterMethod, ivar);
88571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    return;
8861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::Expression:
8881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    break;
88971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  }
89071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
89171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // Otherwise, fake up some ASTs and emit a normal assignment.
89271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ValueDecl *selfDecl = setterMethod->getSelfDecl();
89371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  DeclRefExpr self(selfDecl, selfDecl->getType(), VK_LValue, SourceLocation());
89471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack,
89571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                            selfDecl->getType(), CK_LValueToRValue, &self,
89671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                            VK_RValue);
89771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(),
89871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                          SourceLocation(), &selfLoad, true, true);
89971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
90071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ParmVarDecl *argDecl = *setterMethod->param_begin();
90171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  QualType argType = argDecl->getType().getNonReferenceType();
90271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  DeclRefExpr arg(argDecl, argType, VK_LValue, SourceLocation());
90371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack,
90471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                           argType.getUnqualifiedType(), CK_LValueToRValue,
90571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                           &arg, VK_RValue);
90645e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar
90771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // The property type can differ from the ivar type in some situations with
90871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // Objective-C pointer types, we can always bit cast the RHS in these cases.
90971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // The following absurdity is just to ensure well-formed IR.
91071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  CastKind argCK = CK_NoOp;
91171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  if (ivarRef.getType()->isObjCObjectPointerType()) {
91271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    if (argLoad.getType()->isObjCObjectPointerType())
91371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_BitCast;
91471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    else if (argLoad.getType()->isBlockPointerType())
91571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_BlockPointerToObjCPointerCast;
91671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    else
91771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_CPointerToObjCPointerCast;
91871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  } else if (ivarRef.getType()->isBlockPointerType()) {
91971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall     if (argLoad.getType()->isBlockPointerType())
92071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_BitCast;
92171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    else
92271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_AnyPointerToBlockPointerCast;
92371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  } else if (ivarRef.getType()->isPointerType()) {
92471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    argCK = CK_BitCast;
92586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  }
92671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ImplicitCastExpr argCast(ImplicitCastExpr::OnStack,
92771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                           ivarRef.getType(), argCK, &argLoad,
92871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                           VK_RValue);
92971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  Expr *finalArg = &argLoad;
93071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  if (!getContext().hasSameUnqualifiedType(ivarRef.getType(),
93171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                                           argLoad.getType()))
93271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    finalArg = &argCast;
93371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
93471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
93571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  BinaryOperator assign(&ivarRef, finalArg, BO_Assign,
93671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                        ivarRef.getType(), VK_RValue, OK_Ordinary,
93771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                        SourceLocation());
93871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  EmitStmt(&assign);
93971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall}
94071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
94171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall/// GenerateObjCSetter - Generate an Objective-C property setter
94271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
94371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall/// is illegal within a category.
94471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
94571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                                         const ObjCPropertyImplDecl *PID) {
94671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
94771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
94871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  assert(OMD && "Invalid call to generate setter (empty method)");
94971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart());
95071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
95171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  generateObjCSetterBody(IMP, PID);
952af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
953af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  FinishFunction();
9544111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
9554111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
956e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallnamespace {
9579928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  struct DestroyIvar : EHScopeStack::Cleanup {
9589928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  private:
9599928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    llvm::Value *addr;
960e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    const ObjCIvarDecl *ivar;
9619928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    CodeGenFunction::Destroyer &destroyer;
9629928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    bool useEHCleanupForArray;
9639928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  public:
9649928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
9659928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                CodeGenFunction::Destroyer *destroyer,
9669928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                bool useEHCleanupForArray)
9679928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      : addr(addr), ivar(ivar), destroyer(*destroyer),
9689928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall        useEHCleanupForArray(useEHCleanupForArray) {}
969e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
970ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
9719928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      LValue lvalue
9729928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall        = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
9739928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer,
974ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall                      flags.isForNormalCleanup() && useEHCleanupForArray);
975e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    }
976e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  };
977e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall}
978e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
9799928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall/// Like CodeGenFunction::destroyARCStrong, but do it with a call.
9809928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCallstatic void destroyARCStrongWithStore(CodeGenFunction &CGF,
9819928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                                      llvm::Value *addr,
9829928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                                      QualType type) {
9839928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  llvm::Value *null = getNullForVariable(addr);
9849928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
9859928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall}
986f85e193739c953358c865005855253af4f68a497John McCall
987e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallstatic void emitCXXDestructMethod(CodeGenFunction &CGF,
988e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall                                  ObjCImplementationDecl *impl) {
989e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  CodeGenFunction::RunCleanupsScope scope(CGF);
990e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
991e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  llvm::Value *self = CGF.LoadObjCSelf();
992e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
993db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  const ObjCInterfaceDecl *iface = impl->getClassInterface();
994db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
995e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall       ivar; ivar = ivar->getNextIvar()) {
996e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    QualType type = ivar->getType();
997e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
998e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    // Check whether the ivar is a destructible type.
9999928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    QualType::DestructionKind dtorKind = type.isDestructedType();
10009928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    if (!dtorKind) continue;
10019928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
10029928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    CodeGenFunction::Destroyer *destroyer = 0;
10039928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
10049928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    // Use a call to objc_storeStrong to destroy strong ivars, for the
10059928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    // general benefit of the tools.
10069928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    if (dtorKind == QualType::DK_objc_strong_lifetime) {
10079928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      destroyer = &destroyARCStrongWithStore;
10089928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
10099928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    // Otherwise use the default for the destruction kind.
10109928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    } else {
10119928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      destroyer = &CGF.getDestroyer(dtorKind);
1012e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    }
10139928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
10149928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
10159928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
10169928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
10179928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                                         cleanupKind & EHCleanup);
1018e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  }
1019e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1020e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
1021e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall}
1022e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1023109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1024109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                 ObjCMethodDecl *MD,
1025109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                 bool ctor) {
1026109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
10278d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel  StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart());
1028e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1029e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  // Emit .cxx_construct.
1030109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  if (ctor) {
1031f85e193739c953358c865005855253af4f68a497John McCall    // Suppress the final autorelease in ARC.
1032f85e193739c953358c865005855253af4f68a497John McCall    AutoreleaseResult = false;
1033f85e193739c953358c865005855253af4f68a497John McCall
10345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
1035e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
1036e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall           E = IMP->init_end(); B != E; ++B) {
1037e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      CXXCtorInitializer *IvarInit = (*B);
103800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      FieldDecl *Field = IvarInit->getAnyMember();
1039109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      ObjCIvarDecl  *Ivar = cast<ObjCIvarDecl>(Field);
10409b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian      LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
10419b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian                                    LoadObjCSelf(), Ivar, 0);
10427c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall      EmitAggExpr(IvarInit->getInit(),
10437c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall                  AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed,
1044410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall                                          AggValueSlot::DoesNotNeedGCBarriers,
1045410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall                                          AggValueSlot::IsNotAliased));
1046109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    }
1047109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    // constructor returns 'self'.
1048109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    CodeGenTypes &Types = CGM.getTypes();
1049109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    QualType IdTy(CGM.getContext().getObjCIdType());
1050109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    llvm::Value *SelfAsId =
1051109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
1052109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
1053e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1054e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  // Emit .cxx_destruct.
1055bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth  } else {
1056e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    emitCXXDestructMethod(*this, IMP);
1057109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  }
1058109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  FinishFunction();
1059109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian}
1060109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian
10610b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanianbool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
10620b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
10630b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  it++; it++;
10640b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  const ABIArgInfo &AI = it->info;
10650b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  // FIXME. Is this sufficient check?
10660b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  return (AI.getKind() == ABIArgInfo::Indirect);
10670b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian}
10680b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian
106915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanianbool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
1070e289d81369914678db386f6aa86faf8f178e245dDouglas Gregor  if (CGM.getLangOptions().getGC() == LangOptions::NonGC)
107115bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    return false;
107215bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
107315bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    return FDTTy->getDecl()->hasObjectMember();
107415bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  return false;
107515bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian}
107615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian
1077c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbarllvm::Value *CodeGenFunction::LoadObjCSelf() {
1078b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
1079b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
10804111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
10814111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
108245012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz JahanianQualType CodeGenFunction::TypeOfSelfObject() {
108345012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
108445012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
108514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
108614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    getContext().getCanonicalType(selfDecl->getType()));
108745012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  return PTy->getPointeeType();
108845012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian}
108945012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian
1090e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCallLValue
1091e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCallCodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
1092e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // This is a special l-value that just issues sends when we load or
1093e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // store through it.
1094e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
1095e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // For certain base kinds, we need to emit the base immediately.
1096e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  llvm::Value *Base;
1097e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  if (E->isSuperReceiver())
1098e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    Base = LoadObjCSelf();
1099e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  else if (E->isClassReceiver())
1100e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver());
1101e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  else
1102e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    Base = EmitScalarExpr(E->getBase());
1103e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  return LValue::MakePropertyRef(E, Base);
1104e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall}
1105e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
1106e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCallstatic RValue GenerateMessageSendSuper(CodeGenFunction &CGF,
1107e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       ReturnValueSlot Return,
1108e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       QualType ResultType,
1109e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       Selector S,
1110e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       llvm::Value *Receiver,
1111e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                       const CallArgList &CallArgs) {
1112e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl);
1113f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian  bool isClassMessage = OMD->isClassMethod();
1114f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian  bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
1115e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  return CGF.CGM.getObjCRuntime()
1116e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                .GenerateMessageSendSuper(CGF, Return, ResultType,
1117e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                          S, OMD->getClassInterface(),
1118e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                          isCategoryImpl, Receiver,
1119e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                          isClassMessage, CallArgs);
1120f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian}
1121f469557743f77918d2ca8226e2ee2888998ffd4aFariborz Jahanian
1122119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCallRValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
1123119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall                                                    ReturnValueSlot Return) {
1124119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr();
112568af13f3ca39947e3f285f864fe3b76640fddf69Fariborz Jahanian  QualType ResultType = E->getGetterResultType();
112612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  Selector S;
1127926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  const ObjCMethodDecl *method;
112812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isExplicitProperty()) {
112912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    const ObjCPropertyDecl *Property = E->getExplicitProperty();
113012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    S = Property->getGetterName();
1131926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    method = Property->getGetterMethodDecl();
1132b3589f44c5d295cd41de2c83f3475116835eeebdMike Stump  } else {
1133926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    method = E->getImplicitPropertyGetter();
1134926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    S = method->getSelector();
11355daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian  }
113612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
1137e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  llvm::Value *Receiver = LV.getPropertyRefBaseAddr();
1138e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
1139f85e193739c953358c865005855253af4f68a497John McCall  if (CGM.getLangOptions().ObjCAutoRefCount) {
1140f85e193739c953358c865005855253af4f68a497John McCall    QualType receiverType;
1141f85e193739c953358c865005855253af4f68a497John McCall    if (E->isSuperReceiver())
1142f85e193739c953358c865005855253af4f68a497John McCall      receiverType = E->getSuperReceiverType();
1143f85e193739c953358c865005855253af4f68a497John McCall    else if (E->isClassReceiver())
1144f85e193739c953358c865005855253af4f68a497John McCall      receiverType = getContext().getObjCClassType();
1145f85e193739c953358c865005855253af4f68a497John McCall    else
1146f85e193739c953358c865005855253af4f68a497John McCall      receiverType = E->getBase()->getType();
1147f85e193739c953358c865005855253af4f68a497John McCall  }
1148f85e193739c953358c865005855253af4f68a497John McCall
1149e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  // Accesses to 'super' follow a different code path.
115012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isSuperReceiver())
1151926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return AdjustRelatedResultType(*this, E, method,
1152926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                   GenerateMessageSendSuper(*this, Return,
1153926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                                            ResultType,
1154926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                                            S, Receiver,
1155926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                                            CallArgList()));
1156119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCInterfaceDecl *ReceiverClass
1157119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall    = (E->isClassReceiver() ? E->getClassReceiver() : 0);
1158926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  return AdjustRelatedResultType(*this, E, method,
1159f85e193739c953358c865005855253af4f68a497John McCall          CGM.getObjCRuntime().
1160f85e193739c953358c865005855253af4f68a497John McCall             GenerateMessageSend(*this, Return, ResultType, S,
1161f85e193739c953358c865005855253af4f68a497John McCall                                 Receiver, CallArgList(), ReceiverClass));
11629c3fc703b29a31d40bcf5027dbb4784dd393804eDaniel Dunbar}
11639c3fc703b29a31d40bcf5027dbb4784dd393804eDaniel Dunbar
1164119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCallvoid CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
1165119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall                                                        LValue Dst) {
1166119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr();
116712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  Selector S = E->getSetterSelector();
116868af13f3ca39947e3f285f864fe3b76640fddf69Fariborz Jahanian  QualType ArgType = E->getSetterArgType();
116968af13f3ca39947e3f285f864fe3b76640fddf69Fariborz Jahanian
1170b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  // FIXME. Other than scalars, AST is not adequate for setter and
1171b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  // getter type mismatches which require conversion.
1172b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  if (Src.isScalar()) {
1173b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    llvm::Value *SrcVal = Src.getScalarVal();
1174b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    QualType DstType = getContext().getCanonicalType(ArgType);
11752acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *DstTy = ConvertType(DstType);
1176b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian    if (SrcVal->getType() != DstTy)
1177b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian      Src =
1178b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian        RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType));
1179b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian  }
1180b19c76e857d21b722d0a207cb45b26d7cb20a73fFariborz Jahanian
1181e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  CallArgList Args;
118204c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  Args.add(Src, ArgType);
1183e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
1184e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  llvm::Value *Receiver = Dst.getPropertyRefBaseAddr();
1185e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall  QualType ResultType = getContext().VoidTy;
1186e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall
118712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  if (E->isSuperReceiver()) {
1188e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall    GenerateMessageSendSuper(*this, ReturnValueSlot(),
1189e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                             ResultType, S, Receiver, Args);
119012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    return;
119112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  }
119212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
1193119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  const ObjCInterfaceDecl *ReceiverClass
1194119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall    = (E->isClassReceiver() ? E->getClassReceiver() : 0);
119512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
119612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1197e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                           ResultType, S, Receiver, Args,
1198e68b9842d2d6adc2c72c81c845a2c68e58d9d3a4John McCall                                           ReceiverClass);
119985c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar}
120085c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar
120174391b48b4791cded373683a3baf67314f358d50Chris Lattnervoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Constant *EnumerationMutationFn =
1203c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CGM.getObjCRuntime().EnumerationMutationFunction();
12041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1205c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  if (!EnumerationMutationFn) {
1206c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
1207c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    return;
1208c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  }
1209c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar
1210bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  CGDebugInfo *DI = getDebugInfo();
121173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
121273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
1213bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel
12149d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel  // The local variable comes into scope immediately.
12159d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel  AutoVarEmission variable = AutoVarEmission::invalid();
12169d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
12179d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel    variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
12189d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel
1219d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1221f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // Fast enumeration state.
12220815b579b31cb3129f732bb7ea36fd6ba6949e98Douglas Gregor  QualType StateTy = CGM.getObjCFastEnumerationStateType();
1223195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
12241884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  EmitNullInitialization(StatePtr, StateTy);
12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1226f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // Number of elements in the items array.
12272abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  static const unsigned NumItems = 16;
12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1229d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the countByEnumeratingWithState:objects:count: selector.
1230ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  IdentifierInfo *II[] = {
1231ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("countByEnumeratingWithState"),
1232ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("objects"),
1233ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("count")
1234ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  };
1235ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  Selector FastEnumSel =
1236ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
1237f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1238f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  QualType ItemsTy =
1239f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson    getContext().getConstantArrayType(getContext().getObjCIdType(),
12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                      llvm::APInt(32, NumItems),
1241f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                      ArrayType::Normal, 0);
1242195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1244990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  // Emit the collection pointer.  In ARC, we do a retain.
1245990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  llvm::Value *Collection;
1246990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  if (getLangOptions().ObjCAutoRefCount) {
1247990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    Collection = EmitARCRetainScalarExpr(S.getCollection());
1248990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall
1249990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    // Enter a cleanup to do the release.
1250990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
1251990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  } else {
1252990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    Collection = EmitScalarExpr(S.getCollection());
1253990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  }
12541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12554b302d32378b364703b212834f908762e570c29cJohn McCall  // The 'continue' label needs to appear within the cleanup for the
12564b302d32378b364703b212834f908762e570c29cJohn McCall  // collection object.
12574b302d32378b364703b212834f908762e570c29cJohn McCall  JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
12584b302d32378b364703b212834f908762e570c29cJohn McCall
1259d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Send it our message:
1260f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  CallArgList Args;
1261d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1262d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The first argument is a temporary of the enumeration-state type.
126304c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy));
12641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1265d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The second argument is a temporary array with space for NumItems
1266d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // pointers.  We'll actually be loading elements from the array
1267d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // pointer written into the control state; this buffer is so that
1268d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // collections that *aren't* backed by arrays can still queue up
1269d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // batches of elements.
127004c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy));
12711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1272d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The third argument is the capacity of that temporary array.
12732acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
12744a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
127504c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  Args.add(RValue::get(Count), getContext().UnsignedLongTy);
12761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1277d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Start the enumeration.
12781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  RValue CountRV =
1279ef072fd2f3347cfd857d6eb787b245b950771430John McCall    CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1280f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             getContext().UnsignedLongTy,
1281f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             FastEnumSel,
1282c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                                             Collection, Args);
1283f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1284d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The initial number of objects that were returned in the buffer.
1285d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *initialBufferLimit = CountRV.getScalarVal();
12861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1287d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
1288d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
1289f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1290d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
1291f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1292d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If the limit pointer was zero to begin with, the collection is
1293d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // empty; skip all this.
1294d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
1295d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       EmptyBB, LoopInitBB);
12961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1297d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, initialize the loop.
1298d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(LoopInitBB);
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1300d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Save the initial mutations value.  This is the value at an
1301d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // address that was written into the state object by
1302d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // countByEnumeratingWithState:objects:count:.
13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *StateMutationsPtrPtr =
13042abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson    Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
13051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
13062abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson                                                      "mutationsptr");
13071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1308d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *initialMutations =
1309d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1311d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Start looping.  This is the point we return to whenever we have a
1312d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // fresh, non-empty batch of objects.
1313d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
1314d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(LoopBodyBB);
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1316d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The current index into the buffer.
1317bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
1318d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(zero, LoopInitBB);
1319f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1320d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The current buffer size.
1321bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
1322d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(initialBufferLimit, LoopInitBB);
1323f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1324d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Check whether the mutations value has changed from where it was
1325d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // at start.  StateMutationsPtr should actually be invariant between
1326d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // refreshes.
13272abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1328d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *currentMutations
1329d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    = Builder.CreateLoad(StateMutationsPtr, "statemutations");
13301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1331d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
1332361cf980a7d976ef11a37b49567412b6b63a89d7Dan Gohman  llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
13331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1334d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
1335d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       WasNotMutatedBB, WasMutatedBB);
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1337d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If so, call the enumeration-mutation function.
1338d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(WasMutatedBB);
13392abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  llvm::Value *V =
13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Builder.CreateBitCast(Collection,
1341578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer                          ConvertType(getContext().getObjCIdType()));
13422b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar  CallArgList Args2;
134304c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  Args2.add(RValue::get(V), getContext().getObjCIdType());
1344f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We shouldn't need to get the function info here, the runtime already
1345f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // should have computed it to build the function.
134604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
1347264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                          FunctionType::ExtInfo()),
1348f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson           EnumerationMutationFn, ReturnValueSlot(), Args2);
13491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1350d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, or if the mutation function returns, just continue.
1351d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(WasNotMutatedBB);
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1353d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Initialize the element variable.
1354d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  RunCleanupsScope elementVariableScope(*this);
135557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  bool elementIsVariable;
1356d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  LValue elementLValue;
1357d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  QualType elementType;
1358d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
135957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    // Initialize the variable, in case it's a __block variable or something.
136057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    EmitAutoVarInit(variable);
1361f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
136257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
1363d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(),
1364d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                        VK_LValue, SourceLocation());
1365d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(&tempDRE);
1366d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementType = D->getType();
136757b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    elementIsVariable = true;
13687acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall
13697acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall    if (D->isARCPseudoStrong())
13707acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall      elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone);
1371d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  } else {
1372d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = LValue(); // suppress warning
1373d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementType = cast<Expr>(S.getElement())->getType();
137457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    elementIsVariable = false;
1375d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  }
13762acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *convertedElementType = ConvertType(elementType);
1377f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1378d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the buffer out of the enumeration state.
1379d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // TODO: this pointer should actually be invariant between
1380d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // refreshes, which would help us do certain loop optimizations.
1381d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *StateItemsPtr =
1382d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
1383d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *EnumStateItems =
1384d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateLoad(StateItemsPtr, "stateitems");
1385f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1386d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the value at the current index from the buffer.
13871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *CurrentItemPtr =
1388d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
1389d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
13901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1391d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Cast that value to the right type.
1392d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
1393d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                                      "currentitem");
13941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1395d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Make sure we have an l-value.  Yes, this gets evaluated every
1396d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // time through the loop.
13977acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall  if (!elementIsVariable) {
1398d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1399545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
14007acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall  } else {
14017acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall    EmitScalarInit(CurrentItem, elementLValue);
14027acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall  }
14031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
140457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  // If we do have an element variable, this assignment is the end of
140557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  // its initialization.
140657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  if (elementIsVariable)
140757b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    EmitAutoVarCleanups(variable);
140857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
1409d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Perform the loop body, setting up break and continue labels.
1410e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
1411d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  {
1412d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    RunCleanupsScope Scope(*this);
1413d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    EmitStmt(S.getBody());
1414d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  }
1415f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  BreakContinueStack.pop_back();
14161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1417d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Destroy the element variable now.
1418d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  elementVariableScope.ForceCleanup();
1419d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1420d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Check whether there are more elements.
1421ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(AfterBody.getBlock());
14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1423d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
1424d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1425d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // First we check in the local buffer.
1426d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *indexPlusOne
1427d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
1428d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1429d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If we haven't overrun the buffer yet, we can continue.
1430d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
1431d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       LoopBodyBB, FetchMoreBB);
1432f0906c4edb37b20141428ca77fa7dfd00b976eafFariborz Jahanian
1433d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(indexPlusOne, AfterBody.getBlock());
1434d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(count, AfterBody.getBlock());
1435f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1436d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, we have to fetch more elements.
1437d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(FetchMoreBB);
14381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CountRV =
1440ef072fd2f3347cfd857d6eb787b245b950771430John McCall    CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1441f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             getContext().UnsignedLongTy,
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             FastEnumSel,
1443c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                                             Collection, Args);
14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1445d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If we got a zero count, we're done.
1446d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *refetchCount = CountRV.getScalarVal();
1447d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1448d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // (note that the message send might split FetchMoreBB)
1449d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(zero, Builder.GetInsertBlock());
1450d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(refetchCount, Builder.GetInsertBlock());
1451d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1452d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1453d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       EmptyBB, LoopBodyBB);
14541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1455f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // No more elements.
1456d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(EmptyBB);
1457f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
145857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  if (!elementIsVariable) {
1459f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson    // If the element was not a declaration, set it to be null.
1460f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1461d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1462d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1463545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    EmitStoreThroughLValue(RValue::get(null), elementLValue);
1464f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  }
1465f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
146673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
146773fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
1468bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel
1469990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  // Leave the cleanup we entered in ARC.
1470990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  if (getLangOptions().ObjCAutoRefCount)
1471990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    PopCleanupBlock();
1472990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall
1473ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopEnd.getBlock());
14743d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
14753d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
14761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
1477f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGM.getObjCRuntime().EmitTryStmt(*this, S);
147864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
147964d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
14801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
148164d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  CGM.getObjCRuntime().EmitThrowStmt(*this, S);
148264d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
148364d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
148410cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattnervoid CodeGenFunction::EmitObjCAtSynchronizedStmt(
14851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              const ObjCAtSynchronizedStmt &S) {
1486f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
148710cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner}
148810cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner
148933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCProduceObject.  Just does a
1490f85e193739c953358c865005855253af4f68a497John McCall/// primitive retain.
1491f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type,
1492f85e193739c953358c865005855253af4f68a497John McCall                                                    llvm::Value *value) {
1493f85e193739c953358c865005855253af4f68a497John McCall  return EmitARCRetain(type, value);
1494f85e193739c953358c865005855253af4f68a497John McCall}
1495f85e193739c953358c865005855253af4f68a497John McCall
1496f85e193739c953358c865005855253af4f68a497John McCallnamespace {
1497f85e193739c953358c865005855253af4f68a497John McCall  struct CallObjCRelease : EHScopeStack::Cleanup {
1498bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall    CallObjCRelease(llvm::Value *object) : object(object) {}
1499bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall    llvm::Value *object;
1500f85e193739c953358c865005855253af4f68a497John McCall
1501ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
1502f85e193739c953358c865005855253af4f68a497John McCall      CGF.EmitARCRelease(object, /*precise*/ true);
1503f85e193739c953358c865005855253af4f68a497John McCall    }
1504f85e193739c953358c865005855253af4f68a497John McCall  };
1505f85e193739c953358c865005855253af4f68a497John McCall}
1506f85e193739c953358c865005855253af4f68a497John McCall
150733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCConsumeObject.  Does a primitive
1508f85e193739c953358c865005855253af4f68a497John McCall/// release at the end of the full-expression.
1509f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type,
1510f85e193739c953358c865005855253af4f68a497John McCall                                                    llvm::Value *object) {
1511f85e193739c953358c865005855253af4f68a497John McCall  // If we're in a conditional branch, we need to make the cleanup
1512bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall  // conditional.
1513bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall  pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object);
1514f85e193739c953358c865005855253af4f68a497John McCall  return object;
1515f85e193739c953358c865005855253af4f68a497John McCall}
1516f85e193739c953358c865005855253af4f68a497John McCall
1517f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type,
1518f85e193739c953358c865005855253af4f68a497John McCall                                                           llvm::Value *value) {
1519f85e193739c953358c865005855253af4f68a497John McCall  return EmitARCRetainAutorelease(type, value);
1520f85e193739c953358c865005855253af4f68a497John McCall}
1521f85e193739c953358c865005855253af4f68a497John McCall
1522f85e193739c953358c865005855253af4f68a497John McCall
1523f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM,
15242acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                                llvm::FunctionType *type,
15255f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                                StringRef fnName) {
1526f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName);
1527f85e193739c953358c865005855253af4f68a497John McCall
1528f85e193739c953358c865005855253af4f68a497John McCall  // In -fobjc-no-arc-runtime, emit weak references to the runtime
1529f85e193739c953358c865005855253af4f68a497John McCall  // support library.
15309f084a3166b684573ba49df28fc5792bc37d92e1John McCall  if (!CGM.getCodeGenOpts().ObjCRuntimeHasARC)
1531f85e193739c953358c865005855253af4f68a497John McCall    if (llvm::Function *f = dyn_cast<llvm::Function>(fn))
1532f85e193739c953358c865005855253af4f68a497John McCall      f->setLinkage(llvm::Function::ExternalWeakLinkage);
1533f85e193739c953358c865005855253af4f68a497John McCall
1534f85e193739c953358c865005855253af4f68a497John McCall  return fn;
1535f85e193739c953358c865005855253af4f68a497John McCall}
1536f85e193739c953358c865005855253af4f68a497John McCall
1537f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the signature
1538f85e193739c953358c865005855253af4f68a497John McCall///   i8* (i8*)
1539f85e193739c953358c865005855253af4f68a497John McCall/// where a null input causes a no-op and returns null.
1540f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCValueOperation(CodeGenFunction &CGF,
1541f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Value *value,
1542f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Constant *&fn,
15435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                          StringRef fnName) {
1544f85e193739c953358c865005855253af4f68a497John McCall  if (isa<llvm::ConstantPointerNull>(value)) return value;
1545f85e193739c953358c865005855253af4f68a497John McCall
1546f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
15479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    std::vector<llvm::Type*> args(1, CGF.Int8PtrTy);
15482acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
1549f85e193739c953358c865005855253af4f68a497John McCall      llvm::FunctionType::get(CGF.Int8PtrTy, args, false);
1550f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1551f85e193739c953358c865005855253af4f68a497John McCall  }
1552f85e193739c953358c865005855253af4f68a497John McCall
1553f85e193739c953358c865005855253af4f68a497John McCall  // Cast the argument to 'id'.
15542acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *origType = value->getType();
1555f85e193739c953358c865005855253af4f68a497John McCall  value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
1556f85e193739c953358c865005855253af4f68a497John McCall
1557f85e193739c953358c865005855253af4f68a497John McCall  // Call the function.
1558f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *call = CGF.Builder.CreateCall(fn, value);
1559f85e193739c953358c865005855253af4f68a497John McCall  call->setDoesNotThrow();
1560f85e193739c953358c865005855253af4f68a497John McCall
1561f85e193739c953358c865005855253af4f68a497John McCall  // Cast the result back to the original type.
1562f85e193739c953358c865005855253af4f68a497John McCall  return CGF.Builder.CreateBitCast(call, origType);
1563f85e193739c953358c865005855253af4f68a497John McCall}
1564f85e193739c953358c865005855253af4f68a497John McCall
1565f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature:
1566f85e193739c953358c865005855253af4f68a497John McCall///   i8* (i8**)
1567f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF,
1568f85e193739c953358c865005855253af4f68a497John McCall                                         llvm::Value *addr,
1569f85e193739c953358c865005855253af4f68a497John McCall                                         llvm::Constant *&fn,
15705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                         StringRef fnName) {
1571f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
15729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    std::vector<llvm::Type*> args(1, CGF.Int8PtrPtrTy);
15732acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
1574f85e193739c953358c865005855253af4f68a497John McCall      llvm::FunctionType::get(CGF.Int8PtrTy, args, false);
1575f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1576f85e193739c953358c865005855253af4f68a497John McCall  }
1577f85e193739c953358c865005855253af4f68a497John McCall
1578f85e193739c953358c865005855253af4f68a497John McCall  // Cast the argument to 'id*'.
15792acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *origType = addr->getType();
1580f85e193739c953358c865005855253af4f68a497John McCall  addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
1581f85e193739c953358c865005855253af4f68a497John McCall
1582f85e193739c953358c865005855253af4f68a497John McCall  // Call the function.
1583f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *call = CGF.Builder.CreateCall(fn, addr);
1584f85e193739c953358c865005855253af4f68a497John McCall  call->setDoesNotThrow();
1585f85e193739c953358c865005855253af4f68a497John McCall
1586f85e193739c953358c865005855253af4f68a497John McCall  // Cast the result back to a dereference of the original type.
1587f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *result = call;
1588f85e193739c953358c865005855253af4f68a497John McCall  if (origType != CGF.Int8PtrPtrTy)
1589f85e193739c953358c865005855253af4f68a497John McCall    result = CGF.Builder.CreateBitCast(result,
1590f85e193739c953358c865005855253af4f68a497John McCall                        cast<llvm::PointerType>(origType)->getElementType());
1591f85e193739c953358c865005855253af4f68a497John McCall
1592f85e193739c953358c865005855253af4f68a497John McCall  return result;
1593f85e193739c953358c865005855253af4f68a497John McCall}
1594f85e193739c953358c865005855253af4f68a497John McCall
1595f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature:
1596f85e193739c953358c865005855253af4f68a497John McCall///   i8* (i8**, i8*)
1597f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF,
1598f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Value *addr,
1599f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Value *value,
1600f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Constant *&fn,
16015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                          StringRef fnName,
1602f85e193739c953358c865005855253af4f68a497John McCall                                          bool ignored) {
1603f85e193739c953358c865005855253af4f68a497John McCall  assert(cast<llvm::PointerType>(addr->getType())->getElementType()
1604f85e193739c953358c865005855253af4f68a497John McCall           == value->getType());
1605f85e193739c953358c865005855253af4f68a497John McCall
1606f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
16071d236ab930816f5da27bade92904914c44b73b4cBenjamin Kramer    llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy };
1608f85e193739c953358c865005855253af4f68a497John McCall
16092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType
1610f85e193739c953358c865005855253af4f68a497John McCall      = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false);
1611f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1612f85e193739c953358c865005855253af4f68a497John McCall  }
1613f85e193739c953358c865005855253af4f68a497John McCall
16142acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *origType = value->getType();
1615f85e193739c953358c865005855253af4f68a497John McCall
1616f85e193739c953358c865005855253af4f68a497John McCall  addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
1617f85e193739c953358c865005855253af4f68a497John McCall  value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
1618f85e193739c953358c865005855253af4f68a497John McCall
1619f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *result = CGF.Builder.CreateCall2(fn, addr, value);
1620f85e193739c953358c865005855253af4f68a497John McCall  result->setDoesNotThrow();
1621f85e193739c953358c865005855253af4f68a497John McCall
1622f85e193739c953358c865005855253af4f68a497John McCall  if (ignored) return 0;
1623f85e193739c953358c865005855253af4f68a497John McCall
1624f85e193739c953358c865005855253af4f68a497John McCall  return CGF.Builder.CreateBitCast(result, origType);
1625f85e193739c953358c865005855253af4f68a497John McCall}
1626f85e193739c953358c865005855253af4f68a497John McCall
1627f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature:
1628f85e193739c953358c865005855253af4f68a497John McCall///   void (i8**, i8**)
1629f85e193739c953358c865005855253af4f68a497John McCallstatic void emitARCCopyOperation(CodeGenFunction &CGF,
1630f85e193739c953358c865005855253af4f68a497John McCall                                 llvm::Value *dst,
1631f85e193739c953358c865005855253af4f68a497John McCall                                 llvm::Value *src,
1632f85e193739c953358c865005855253af4f68a497John McCall                                 llvm::Constant *&fn,
16335f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                 StringRef fnName) {
1634f85e193739c953358c865005855253af4f68a497John McCall  assert(dst->getType() == src->getType());
1635f85e193739c953358c865005855253af4f68a497John McCall
1636f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
16379cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    std::vector<llvm::Type*> argTypes(2, CGF.Int8PtrPtrTy);
16382acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType
1639f85e193739c953358c865005855253af4f68a497John McCall      = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false);
1640f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1641f85e193739c953358c865005855253af4f68a497John McCall  }
1642f85e193739c953358c865005855253af4f68a497John McCall
1643f85e193739c953358c865005855253af4f68a497John McCall  dst = CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy);
1644f85e193739c953358c865005855253af4f68a497John McCall  src = CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy);
1645f85e193739c953358c865005855253af4f68a497John McCall
1646f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *result = CGF.Builder.CreateCall2(fn, dst, src);
1647f85e193739c953358c865005855253af4f68a497John McCall  result->setDoesNotThrow();
1648f85e193739c953358c865005855253af4f68a497John McCall}
1649f85e193739c953358c865005855253af4f68a497John McCall
1650f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a retain.  Based on the type, calls one of:
1651f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_retain(i8* %value)
1652f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_retainBlock(i8* %value)
1653f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
1654f85e193739c953358c865005855253af4f68a497John McCall  if (type->isBlockPointerType())
1655348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    return EmitARCRetainBlock(value, /*mandatory*/ false);
1656f85e193739c953358c865005855253af4f68a497John McCall  else
1657f85e193739c953358c865005855253af4f68a497John McCall    return EmitARCRetainNonBlock(value);
1658f85e193739c953358c865005855253af4f68a497John McCall}
1659f85e193739c953358c865005855253af4f68a497John McCall
1660f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object, with normal retain semantics.
1661f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_retain(i8* %value)
1662f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
1663f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
1664f85e193739c953358c865005855253af4f68a497John McCall                               CGM.getARCEntrypoints().objc_retain,
1665f85e193739c953358c865005855253af4f68a497John McCall                               "objc_retain");
1666f85e193739c953358c865005855253af4f68a497John McCall}
1667f85e193739c953358c865005855253af4f68a497John McCall
1668f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given block, with _Block_copy semantics.
1669f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_retainBlock(i8* %value)
1670348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall///
1671348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// \param mandatory - If false, emit the call with metadata
1672348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// indicating that it's okay for the optimizer to eliminate this call
1673348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// if it can prove that the block never escapes except down the stack.
1674348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value,
1675348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall                                                 bool mandatory) {
1676348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  llvm::Value *result
1677348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    = emitARCValueOperation(*this, value,
1678348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall                            CGM.getARCEntrypoints().objc_retainBlock,
1679348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall                            "objc_retainBlock");
1680348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
1681348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  // If the copy isn't mandatory, add !clang.arc.copy_on_escape to
1682348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  // tell the optimizer that it doesn't need to do this copy if the
1683348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  // block doesn't escape, where being passed as an argument doesn't
1684348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  // count as escaping.
1685348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  if (!mandatory && isa<llvm::Instruction>(result)) {
1686348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    llvm::CallInst *call
1687348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall      = cast<llvm::CallInst>(result->stripPointerCasts());
1688348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock);
1689348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
1690348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    SmallVector<llvm::Value*,1> args;
1691348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    call->setMetadata("clang.arc.copy_on_escape",
1692348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall                      llvm::MDNode::get(Builder.getContext(), args));
1693348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  }
1694348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
1695348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  return result;
1696f85e193739c953358c865005855253af4f68a497John McCall}
1697f85e193739c953358c865005855253af4f68a497John McCall
1698f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object which is the result of a function call.
1699f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_retainAutoreleasedReturnValue(i8* %value)
1700f85e193739c953358c865005855253af4f68a497John McCall///
1701f85e193739c953358c865005855253af4f68a497John McCall/// Yes, this function name is one character away from a different
1702f85e193739c953358c865005855253af4f68a497John McCall/// call with completely different semantics.
1703f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
1704f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) {
1705f85e193739c953358c865005855253af4f68a497John McCall  // Fetch the void(void) inline asm which marks that we're going to
1706f85e193739c953358c865005855253af4f68a497John McCall  // retain the autoreleased return value.
1707f85e193739c953358c865005855253af4f68a497John McCall  llvm::InlineAsm *&marker
1708f85e193739c953358c865005855253af4f68a497John McCall    = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker;
1709f85e193739c953358c865005855253af4f68a497John McCall  if (!marker) {
17105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    StringRef assembly
1711f85e193739c953358c865005855253af4f68a497John McCall      = CGM.getTargetCodeGenInfo()
1712f85e193739c953358c865005855253af4f68a497John McCall           .getARCRetainAutoreleasedReturnValueMarker();
1713f85e193739c953358c865005855253af4f68a497John McCall
1714f85e193739c953358c865005855253af4f68a497John McCall    // If we have an empty assembly string, there's nothing to do.
1715f85e193739c953358c865005855253af4f68a497John McCall    if (assembly.empty()) {
1716f85e193739c953358c865005855253af4f68a497John McCall
1717f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, at -O0, build an inline asm that we're going to call
1718f85e193739c953358c865005855253af4f68a497John McCall    // in a moment.
1719f85e193739c953358c865005855253af4f68a497John McCall    } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1720f85e193739c953358c865005855253af4f68a497John McCall      llvm::FunctionType *type =
1721f85e193739c953358c865005855253af4f68a497John McCall        llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()),
1722f85e193739c953358c865005855253af4f68a497John McCall                                /*variadic*/ false);
1723f85e193739c953358c865005855253af4f68a497John McCall
1724f85e193739c953358c865005855253af4f68a497John McCall      marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
1725f85e193739c953358c865005855253af4f68a497John McCall
1726f85e193739c953358c865005855253af4f68a497John McCall    // If we're at -O1 and above, we don't want to litter the code
1727f85e193739c953358c865005855253af4f68a497John McCall    // with this marker yet, so leave a breadcrumb for the ARC
1728f85e193739c953358c865005855253af4f68a497John McCall    // optimizer to pick up.
1729f85e193739c953358c865005855253af4f68a497John McCall    } else {
1730f85e193739c953358c865005855253af4f68a497John McCall      llvm::NamedMDNode *metadata =
1731f85e193739c953358c865005855253af4f68a497John McCall        CGM.getModule().getOrInsertNamedMetadata(
1732f85e193739c953358c865005855253af4f68a497John McCall                            "clang.arc.retainAutoreleasedReturnValueMarker");
1733f85e193739c953358c865005855253af4f68a497John McCall      assert(metadata->getNumOperands() <= 1);
1734f85e193739c953358c865005855253af4f68a497John McCall      if (metadata->getNumOperands() == 0) {
1735f85e193739c953358c865005855253af4f68a497John McCall        llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly);
1736da549e8995c447542d5631b8b67fcc3a9582797aJay Foad        metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string));
1737f85e193739c953358c865005855253af4f68a497John McCall      }
1738f85e193739c953358c865005855253af4f68a497John McCall    }
1739f85e193739c953358c865005855253af4f68a497John McCall  }
1740f85e193739c953358c865005855253af4f68a497John McCall
1741f85e193739c953358c865005855253af4f68a497John McCall  // Call the marker asm if we made one, which we do only at -O0.
1742f85e193739c953358c865005855253af4f68a497John McCall  if (marker) Builder.CreateCall(marker);
1743f85e193739c953358c865005855253af4f68a497John McCall
1744f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
1745f85e193739c953358c865005855253af4f68a497John McCall                     CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue,
1746f85e193739c953358c865005855253af4f68a497John McCall                               "objc_retainAutoreleasedReturnValue");
1747f85e193739c953358c865005855253af4f68a497John McCall}
1748f85e193739c953358c865005855253af4f68a497John McCall
1749f85e193739c953358c865005855253af4f68a497John McCall/// Release the given object.
1750f85e193739c953358c865005855253af4f68a497John McCall///   call void @objc_release(i8* %value)
1751f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCRelease(llvm::Value *value, bool precise) {
1752f85e193739c953358c865005855253af4f68a497John McCall  if (isa<llvm::ConstantPointerNull>(value)) return;
1753f85e193739c953358c865005855253af4f68a497John McCall
1754f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release;
1755f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
17569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    std::vector<llvm::Type*> args(1, Int8PtrTy);
17572acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
1758f85e193739c953358c865005855253af4f68a497John McCall      llvm::FunctionType::get(Builder.getVoidTy(), args, false);
1759f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_release");
1760f85e193739c953358c865005855253af4f68a497John McCall  }
1761f85e193739c953358c865005855253af4f68a497John McCall
1762f85e193739c953358c865005855253af4f68a497John McCall  // Cast the argument to 'id'.
1763f85e193739c953358c865005855253af4f68a497John McCall  value = Builder.CreateBitCast(value, Int8PtrTy);
1764f85e193739c953358c865005855253af4f68a497John McCall
1765f85e193739c953358c865005855253af4f68a497John McCall  // Call objc_release.
1766f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *call = Builder.CreateCall(fn, value);
1767f85e193739c953358c865005855253af4f68a497John McCall  call->setDoesNotThrow();
1768f85e193739c953358c865005855253af4f68a497John McCall
1769f85e193739c953358c865005855253af4f68a497John McCall  if (!precise) {
17705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<llvm::Value*,1> args;
1771f85e193739c953358c865005855253af4f68a497John McCall    call->setMetadata("clang.imprecise_release",
1772f85e193739c953358c865005855253af4f68a497John McCall                      llvm::MDNode::get(Builder.getContext(), args));
1773f85e193739c953358c865005855253af4f68a497John McCall  }
1774f85e193739c953358c865005855253af4f68a497John McCall}
1775f85e193739c953358c865005855253af4f68a497John McCall
1776f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object.  Always calls this:
1777f85e193739c953358c865005855253af4f68a497John McCall///   call void @objc_storeStrong(i8** %addr, i8* %value)
1778f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr,
1779f85e193739c953358c865005855253af4f68a497John McCall                                                     llvm::Value *value,
1780f85e193739c953358c865005855253af4f68a497John McCall                                                     bool ignored) {
1781f85e193739c953358c865005855253af4f68a497John McCall  assert(cast<llvm::PointerType>(addr->getType())->getElementType()
1782f85e193739c953358c865005855253af4f68a497John McCall           == value->getType());
1783f85e193739c953358c865005855253af4f68a497John McCall
1784f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong;
1785f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
17869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy };
17872acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType
1788f85e193739c953358c865005855253af4f68a497John McCall      = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false);
1789f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong");
1790f85e193739c953358c865005855253af4f68a497John McCall  }
1791f85e193739c953358c865005855253af4f68a497John McCall
1792f85e193739c953358c865005855253af4f68a497John McCall  addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
1793f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *castValue = Builder.CreateBitCast(value, Int8PtrTy);
1794f85e193739c953358c865005855253af4f68a497John McCall
1795f85e193739c953358c865005855253af4f68a497John McCall  Builder.CreateCall2(fn, addr, castValue)->setDoesNotThrow();
1796f85e193739c953358c865005855253af4f68a497John McCall
1797f85e193739c953358c865005855253af4f68a497John McCall  if (ignored) return 0;
1798f85e193739c953358c865005855253af4f68a497John McCall  return value;
1799f85e193739c953358c865005855253af4f68a497John McCall}
1800f85e193739c953358c865005855253af4f68a497John McCall
1801f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object.  Sometimes calls this:
1802f85e193739c953358c865005855253af4f68a497John McCall///   call void @objc_storeStrong(i8** %addr, i8* %value)
1803f85e193739c953358c865005855253af4f68a497John McCall/// Other times, breaks it down into components.
1804545d996ec5a3113f046944f11b27cc2d6cb055b4John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst,
1805f85e193739c953358c865005855253af4f68a497John McCall                                                 llvm::Value *newValue,
1806f85e193739c953358c865005855253af4f68a497John McCall                                                 bool ignored) {
1807545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  QualType type = dst.getType();
1808f85e193739c953358c865005855253af4f68a497John McCall  bool isBlock = type->isBlockPointerType();
1809f85e193739c953358c865005855253af4f68a497John McCall
1810f85e193739c953358c865005855253af4f68a497John McCall  // Use a store barrier at -O0 unless this is a block type or the
1811f85e193739c953358c865005855253af4f68a497John McCall  // lvalue is inadequately aligned.
1812f85e193739c953358c865005855253af4f68a497John McCall  if (shouldUseFusedARCCalls() &&
1813f85e193739c953358c865005855253af4f68a497John McCall      !isBlock &&
1814f85e193739c953358c865005855253af4f68a497John McCall      !(dst.getAlignment() && dst.getAlignment() < PointerAlignInBytes)) {
1815f85e193739c953358c865005855253af4f68a497John McCall    return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored);
1816f85e193739c953358c865005855253af4f68a497John McCall  }
1817f85e193739c953358c865005855253af4f68a497John McCall
1818f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, split it out.
1819f85e193739c953358c865005855253af4f68a497John McCall
1820f85e193739c953358c865005855253af4f68a497John McCall  // Retain the new value.
1821f85e193739c953358c865005855253af4f68a497John McCall  newValue = EmitARCRetain(type, newValue);
1822f85e193739c953358c865005855253af4f68a497John McCall
1823f85e193739c953358c865005855253af4f68a497John McCall  // Read the old value.
1824545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  llvm::Value *oldValue = EmitLoadOfScalar(dst);
1825f85e193739c953358c865005855253af4f68a497John McCall
1826f85e193739c953358c865005855253af4f68a497John McCall  // Store.  We do this before the release so that any deallocs won't
1827f85e193739c953358c865005855253af4f68a497John McCall  // see the old value.
1828545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  EmitStoreOfScalar(newValue, dst);
1829f85e193739c953358c865005855253af4f68a497John McCall
1830f85e193739c953358c865005855253af4f68a497John McCall  // Finally, release the old value.
1831f85e193739c953358c865005855253af4f68a497John McCall  EmitARCRelease(oldValue, /*precise*/ false);
1832f85e193739c953358c865005855253af4f68a497John McCall
1833f85e193739c953358c865005855253af4f68a497John McCall  return newValue;
1834f85e193739c953358c865005855253af4f68a497John McCall}
1835f85e193739c953358c865005855253af4f68a497John McCall
1836f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object.
1837f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_autorelease(i8* %value)
1838f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
1839f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
1840f85e193739c953358c865005855253af4f68a497John McCall                               CGM.getARCEntrypoints().objc_autorelease,
1841f85e193739c953358c865005855253af4f68a497John McCall                               "objc_autorelease");
1842f85e193739c953358c865005855253af4f68a497John McCall}
1843f85e193739c953358c865005855253af4f68a497John McCall
1844f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object.
1845f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_autoreleaseReturnValue(i8* %value)
1846f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
1847f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) {
1848f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
1849f85e193739c953358c865005855253af4f68a497John McCall                            CGM.getARCEntrypoints().objc_autoreleaseReturnValue,
1850f85e193739c953358c865005855253af4f68a497John McCall                               "objc_autoreleaseReturnValue");
1851f85e193739c953358c865005855253af4f68a497John McCall}
1852f85e193739c953358c865005855253af4f68a497John McCall
1853f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object.
1854f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_retainAutoreleaseReturnValue(i8* %value)
1855f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
1856f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) {
1857f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
1858f85e193739c953358c865005855253af4f68a497John McCall                     CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue,
1859f85e193739c953358c865005855253af4f68a497John McCall                               "objc_retainAutoreleaseReturnValue");
1860f85e193739c953358c865005855253af4f68a497John McCall}
1861f85e193739c953358c865005855253af4f68a497John McCall
1862f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object.
1863f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_retainAutorelease(i8* %value)
1864f85e193739c953358c865005855253af4f68a497John McCall/// or
1865f85e193739c953358c865005855253af4f68a497John McCall///   %retain = call i8* @objc_retainBlock(i8* %value)
1866f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_autorelease(i8* %retain)
1867f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type,
1868f85e193739c953358c865005855253af4f68a497John McCall                                                       llvm::Value *value) {
1869f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isBlockPointerType())
1870f85e193739c953358c865005855253af4f68a497John McCall    return EmitARCRetainAutoreleaseNonBlock(value);
1871f85e193739c953358c865005855253af4f68a497John McCall
1872f85e193739c953358c865005855253af4f68a497John McCall  if (isa<llvm::ConstantPointerNull>(value)) return value;
1873f85e193739c953358c865005855253af4f68a497John McCall
18742acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *origType = value->getType();
1875f85e193739c953358c865005855253af4f68a497John McCall  value = Builder.CreateBitCast(value, Int8PtrTy);
1876348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  value = EmitARCRetainBlock(value, /*mandatory*/ true);
1877f85e193739c953358c865005855253af4f68a497John McCall  value = EmitARCAutorelease(value);
1878f85e193739c953358c865005855253af4f68a497John McCall  return Builder.CreateBitCast(value, origType);
1879f85e193739c953358c865005855253af4f68a497John McCall}
1880f85e193739c953358c865005855253af4f68a497John McCall
1881f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object.
1882f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_retainAutorelease(i8* %value)
1883f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
1884f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) {
1885f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
1886f85e193739c953358c865005855253af4f68a497John McCall                               CGM.getARCEntrypoints().objc_retainAutorelease,
1887f85e193739c953358c865005855253af4f68a497John McCall                               "objc_retainAutorelease");
1888f85e193739c953358c865005855253af4f68a497John McCall}
1889f85e193739c953358c865005855253af4f68a497John McCall
1890f85e193739c953358c865005855253af4f68a497John McCall/// i8* @objc_loadWeak(i8** %addr)
1891f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
1892f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) {
1893f85e193739c953358c865005855253af4f68a497John McCall  return emitARCLoadOperation(*this, addr,
1894f85e193739c953358c865005855253af4f68a497John McCall                              CGM.getARCEntrypoints().objc_loadWeak,
1895f85e193739c953358c865005855253af4f68a497John McCall                              "objc_loadWeak");
1896f85e193739c953358c865005855253af4f68a497John McCall}
1897f85e193739c953358c865005855253af4f68a497John McCall
1898f85e193739c953358c865005855253af4f68a497John McCall/// i8* @objc_loadWeakRetained(i8** %addr)
1899f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) {
1900f85e193739c953358c865005855253af4f68a497John McCall  return emitARCLoadOperation(*this, addr,
1901f85e193739c953358c865005855253af4f68a497John McCall                              CGM.getARCEntrypoints().objc_loadWeakRetained,
1902f85e193739c953358c865005855253af4f68a497John McCall                              "objc_loadWeakRetained");
1903f85e193739c953358c865005855253af4f68a497John McCall}
1904f85e193739c953358c865005855253af4f68a497John McCall
1905f85e193739c953358c865005855253af4f68a497John McCall/// i8* @objc_storeWeak(i8** %addr, i8* %value)
1906f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value.
1907f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr,
1908f85e193739c953358c865005855253af4f68a497John McCall                                               llvm::Value *value,
1909f85e193739c953358c865005855253af4f68a497John McCall                                               bool ignored) {
1910f85e193739c953358c865005855253af4f68a497John McCall  return emitARCStoreOperation(*this, addr, value,
1911f85e193739c953358c865005855253af4f68a497John McCall                               CGM.getARCEntrypoints().objc_storeWeak,
1912f85e193739c953358c865005855253af4f68a497John McCall                               "objc_storeWeak", ignored);
1913f85e193739c953358c865005855253af4f68a497John McCall}
1914f85e193739c953358c865005855253af4f68a497John McCall
1915f85e193739c953358c865005855253af4f68a497John McCall/// i8* @objc_initWeak(i8** %addr, i8* %value)
1916f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value.  %addr is known to not have a current weak entry.
1917f85e193739c953358c865005855253af4f68a497John McCall/// Essentially equivalent to:
1918f85e193739c953358c865005855253af4f68a497John McCall///   *addr = nil; objc_storeWeak(addr, value);
1919f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) {
1920f85e193739c953358c865005855253af4f68a497John McCall  // If we're initializing to null, just write null to memory; no need
1921f85e193739c953358c865005855253af4f68a497John McCall  // to get the runtime involved.  But don't do this if optimization
1922f85e193739c953358c865005855253af4f68a497John McCall  // is enabled, because accounting for this would make the optimizer
1923f85e193739c953358c865005855253af4f68a497John McCall  // much more complicated.
1924f85e193739c953358c865005855253af4f68a497John McCall  if (isa<llvm::ConstantPointerNull>(value) &&
1925f85e193739c953358c865005855253af4f68a497John McCall      CGM.getCodeGenOpts().OptimizationLevel == 0) {
1926f85e193739c953358c865005855253af4f68a497John McCall    Builder.CreateStore(value, addr);
1927f85e193739c953358c865005855253af4f68a497John McCall    return;
1928f85e193739c953358c865005855253af4f68a497John McCall  }
1929f85e193739c953358c865005855253af4f68a497John McCall
1930f85e193739c953358c865005855253af4f68a497John McCall  emitARCStoreOperation(*this, addr, value,
1931f85e193739c953358c865005855253af4f68a497John McCall                        CGM.getARCEntrypoints().objc_initWeak,
1932f85e193739c953358c865005855253af4f68a497John McCall                        "objc_initWeak", /*ignored*/ true);
1933f85e193739c953358c865005855253af4f68a497John McCall}
1934f85e193739c953358c865005855253af4f68a497John McCall
1935f85e193739c953358c865005855253af4f68a497John McCall/// void @objc_destroyWeak(i8** %addr)
1936f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_storeWeak(addr, nil).
1937f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) {
1938f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak;
1939f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
19409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    std::vector<llvm::Type*> args(1, Int8PtrPtrTy);
19412acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
1942f85e193739c953358c865005855253af4f68a497John McCall      llvm::FunctionType::get(Builder.getVoidTy(), args, false);
1943f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak");
1944f85e193739c953358c865005855253af4f68a497John McCall  }
1945f85e193739c953358c865005855253af4f68a497John McCall
1946f85e193739c953358c865005855253af4f68a497John McCall  // Cast the argument to 'id*'.
1947f85e193739c953358c865005855253af4f68a497John McCall  addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
1948f85e193739c953358c865005855253af4f68a497John McCall
1949f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *call = Builder.CreateCall(fn, addr);
1950f85e193739c953358c865005855253af4f68a497John McCall  call->setDoesNotThrow();
1951f85e193739c953358c865005855253af4f68a497John McCall}
1952f85e193739c953358c865005855253af4f68a497John McCall
1953f85e193739c953358c865005855253af4f68a497John McCall/// void @objc_moveWeak(i8** %dest, i8** %src)
1954f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest.  Leaves %src pointing to nothing.
1955f85e193739c953358c865005855253af4f68a497John McCall/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
1956f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) {
1957f85e193739c953358c865005855253af4f68a497John McCall  emitARCCopyOperation(*this, dst, src,
1958f85e193739c953358c865005855253af4f68a497John McCall                       CGM.getARCEntrypoints().objc_moveWeak,
1959f85e193739c953358c865005855253af4f68a497John McCall                       "objc_moveWeak");
1960f85e193739c953358c865005855253af4f68a497John McCall}
1961f85e193739c953358c865005855253af4f68a497John McCall
1962f85e193739c953358c865005855253af4f68a497John McCall/// void @objc_copyWeak(i8** %dest, i8** %src)
1963f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest.  Essentially
1964f85e193739c953358c865005855253af4f68a497John McCall///   objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
1965f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) {
1966f85e193739c953358c865005855253af4f68a497John McCall  emitARCCopyOperation(*this, dst, src,
1967f85e193739c953358c865005855253af4f68a497John McCall                       CGM.getARCEntrypoints().objc_copyWeak,
1968f85e193739c953358c865005855253af4f68a497John McCall                       "objc_copyWeak");
1969f85e193739c953358c865005855253af4f68a497John McCall}
1970f85e193739c953358c865005855253af4f68a497John McCall
1971f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a objc_autoreleasepool_push.
1972f85e193739c953358c865005855253af4f68a497John McCall///   call i8* @objc_autoreleasePoolPush(void)
1973f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() {
1974f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush;
1975f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
19762acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
1977f85e193739c953358c865005855253af4f68a497John McCall      llvm::FunctionType::get(Int8PtrTy, false);
1978f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush");
1979f85e193739c953358c865005855253af4f68a497John McCall  }
1980f85e193739c953358c865005855253af4f68a497John McCall
1981f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *call = Builder.CreateCall(fn);
1982f85e193739c953358c865005855253af4f68a497John McCall  call->setDoesNotThrow();
1983f85e193739c953358c865005855253af4f68a497John McCall
1984f85e193739c953358c865005855253af4f68a497John McCall  return call;
1985f85e193739c953358c865005855253af4f68a497John McCall}
1986f85e193739c953358c865005855253af4f68a497John McCall
1987f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release.
1988f85e193739c953358c865005855253af4f68a497John McCall///   call void @objc_autoreleasePoolPop(i8* %ptr)
1989f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) {
1990f85e193739c953358c865005855253af4f68a497John McCall  assert(value->getType() == Int8PtrTy);
1991f85e193739c953358c865005855253af4f68a497John McCall
1992f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop;
1993f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
19949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    std::vector<llvm::Type*> args(1, Int8PtrTy);
19952acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
1996f85e193739c953358c865005855253af4f68a497John McCall      llvm::FunctionType::get(Builder.getVoidTy(), args, false);
1997f85e193739c953358c865005855253af4f68a497John McCall
1998f85e193739c953358c865005855253af4f68a497John McCall    // We don't want to use a weak import here; instead we should not
1999f85e193739c953358c865005855253af4f68a497John McCall    // fall into this path.
2000f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop");
2001f85e193739c953358c865005855253af4f68a497John McCall  }
2002f85e193739c953358c865005855253af4f68a497John McCall
2003f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *call = Builder.CreateCall(fn, value);
2004f85e193739c953358c865005855253af4f68a497John McCall  call->setDoesNotThrow();
2005f85e193739c953358c865005855253af4f68a497John McCall}
2006f85e193739c953358c865005855253af4f68a497John McCall
2007f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do an MRR version objc_autoreleasepool_push.
2008f85e193739c953358c865005855253af4f68a497John McCall/// Which is: [[NSAutoreleasePool alloc] init];
2009f85e193739c953358c865005855253af4f68a497John McCall/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
2010f85e193739c953358c865005855253af4f68a497John McCall/// init is declared as: - (id) init; in its NSObject super class.
2011f85e193739c953358c865005855253af4f68a497John McCall///
2012f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() {
2013f85e193739c953358c865005855253af4f68a497John McCall  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
2014f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(Builder);
2015f85e193739c953358c865005855253af4f68a497John McCall  // [NSAutoreleasePool alloc]
2016f85e193739c953358c865005855253af4f68a497John McCall  IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
2017f85e193739c953358c865005855253af4f68a497John McCall  Selector AllocSel = getContext().Selectors.getSelector(0, &II);
2018f85e193739c953358c865005855253af4f68a497John McCall  CallArgList Args;
2019f85e193739c953358c865005855253af4f68a497John McCall  RValue AllocRV =
2020f85e193739c953358c865005855253af4f68a497John McCall    Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2021f85e193739c953358c865005855253af4f68a497John McCall                                getContext().getObjCIdType(),
2022f85e193739c953358c865005855253af4f68a497John McCall                                AllocSel, Receiver, Args);
2023f85e193739c953358c865005855253af4f68a497John McCall
2024f85e193739c953358c865005855253af4f68a497John McCall  // [Receiver init]
2025f85e193739c953358c865005855253af4f68a497John McCall  Receiver = AllocRV.getScalarVal();
2026f85e193739c953358c865005855253af4f68a497John McCall  II = &CGM.getContext().Idents.get("init");
2027f85e193739c953358c865005855253af4f68a497John McCall  Selector InitSel = getContext().Selectors.getSelector(0, &II);
2028f85e193739c953358c865005855253af4f68a497John McCall  RValue InitRV =
2029f85e193739c953358c865005855253af4f68a497John McCall    Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2030f85e193739c953358c865005855253af4f68a497John McCall                                getContext().getObjCIdType(),
2031f85e193739c953358c865005855253af4f68a497John McCall                                InitSel, Receiver, Args);
2032f85e193739c953358c865005855253af4f68a497John McCall  return InitRV.getScalarVal();
2033f85e193739c953358c865005855253af4f68a497John McCall}
2034f85e193739c953358c865005855253af4f68a497John McCall
2035f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release.
2036f85e193739c953358c865005855253af4f68a497John McCall/// [tmp drain];
2037f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) {
2038f85e193739c953358c865005855253af4f68a497John McCall  IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
2039f85e193739c953358c865005855253af4f68a497John McCall  Selector DrainSel = getContext().Selectors.getSelector(0, &II);
2040f85e193739c953358c865005855253af4f68a497John McCall  CallArgList Args;
2041f85e193739c953358c865005855253af4f68a497John McCall  CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2042f85e193739c953358c865005855253af4f68a497John McCall                              getContext().VoidTy, DrainSel, Arg, Args);
2043f85e193739c953358c865005855253af4f68a497John McCall}
2044f85e193739c953358c865005855253af4f68a497John McCall
2045bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF,
2046bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              llvm::Value *addr,
2047bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              QualType type) {
2048bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *ptr = CGF.Builder.CreateLoad(addr, "strongdestroy");
2049bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  CGF.EmitARCRelease(ptr, /*precise*/ true);
2050bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
2051bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
2052bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF,
2053bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                                llvm::Value *addr,
2054bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                                QualType type) {
2055bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *ptr = CGF.Builder.CreateLoad(addr, "strongdestroy");
2056bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  CGF.EmitARCRelease(ptr, /*precise*/ false);
2057bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
2058bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
2059bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF,
2060bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                     llvm::Value *addr,
2061bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                     QualType type) {
2062bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  CGF.EmitARCDestroyWeak(addr);
2063bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
2064bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
2065f85e193739c953358c865005855253af4f68a497John McCallnamespace {
2066f85e193739c953358c865005855253af4f68a497John McCall  struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup {
2067f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *Token;
2068f85e193739c953358c865005855253af4f68a497John McCall
2069f85e193739c953358c865005855253af4f68a497John McCall    CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2070f85e193739c953358c865005855253af4f68a497John McCall
2071ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
2072f85e193739c953358c865005855253af4f68a497John McCall      CGF.EmitObjCAutoreleasePoolPop(Token);
2073f85e193739c953358c865005855253af4f68a497John McCall    }
2074f85e193739c953358c865005855253af4f68a497John McCall  };
2075f85e193739c953358c865005855253af4f68a497John McCall  struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup {
2076f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *Token;
2077f85e193739c953358c865005855253af4f68a497John McCall
2078f85e193739c953358c865005855253af4f68a497John McCall    CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2079f85e193739c953358c865005855253af4f68a497John McCall
2080ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
2081f85e193739c953358c865005855253af4f68a497John McCall      CGF.EmitObjCMRRAutoreleasePoolPop(Token);
2082f85e193739c953358c865005855253af4f68a497John McCall    }
2083f85e193739c953358c865005855253af4f68a497John McCall  };
2084f85e193739c953358c865005855253af4f68a497John McCall}
2085f85e193739c953358c865005855253af4f68a497John McCall
2086f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) {
2087f85e193739c953358c865005855253af4f68a497John McCall  if (CGM.getLangOptions().ObjCAutoRefCount)
2088f85e193739c953358c865005855253af4f68a497John McCall    EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
2089f85e193739c953358c865005855253af4f68a497John McCall  else
2090f85e193739c953358c865005855253af4f68a497John McCall    EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
2091f85e193739c953358c865005855253af4f68a497John McCall}
2092f85e193739c953358c865005855253af4f68a497John McCall
2093f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2094f85e193739c953358c865005855253af4f68a497John McCall                                                  LValue lvalue,
2095f85e193739c953358c865005855253af4f68a497John McCall                                                  QualType type) {
2096f85e193739c953358c865005855253af4f68a497John McCall  switch (type.getObjCLifetime()) {
2097f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_None:
2098f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_ExplicitNone:
2099f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Strong:
2100f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Autoreleasing:
2101545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(),
2102f85e193739c953358c865005855253af4f68a497John McCall                         false);
2103f85e193739c953358c865005855253af4f68a497John McCall
2104f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Weak:
2105f85e193739c953358c865005855253af4f68a497John McCall    return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()),
2106f85e193739c953358c865005855253af4f68a497John McCall                         true);
2107f85e193739c953358c865005855253af4f68a497John McCall  }
2108f85e193739c953358c865005855253af4f68a497John McCall
2109f85e193739c953358c865005855253af4f68a497John McCall  llvm_unreachable("impossible lifetime!");
2110f85e193739c953358c865005855253af4f68a497John McCall  return TryEmitResult();
2111f85e193739c953358c865005855253af4f68a497John McCall}
2112f85e193739c953358c865005855253af4f68a497John McCall
2113f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2114f85e193739c953358c865005855253af4f68a497John McCall                                                  const Expr *e) {
2115f85e193739c953358c865005855253af4f68a497John McCall  e = e->IgnoreParens();
2116f85e193739c953358c865005855253af4f68a497John McCall  QualType type = e->getType();
2117f85e193739c953358c865005855253af4f68a497John McCall
21182148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  // If we're loading retained from a __strong xvalue, we can avoid
21192148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  // an extra retain/release pair by zeroing out the source of this
21202148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  // "move" operation.
21212148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  if (e->isXValue() &&
21222148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall      !type.isConstQualified() &&
21232148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall      type.getObjCLifetime() == Qualifiers::OCL_Strong) {
21242148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    // Emit the lvalue.
21252148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    LValue lv = CGF.EmitLValue(e);
21262148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall
21272148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    // Load the object pointer.
21282148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal();
21292148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall
21302148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    // Set the source pointer to NULL.
21312148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv);
21322148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall
21332148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    return TryEmitResult(result, true);
21342148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  }
21352148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall
2136f85e193739c953358c865005855253af4f68a497John McCall  // As a very special optimization, in ARC++, if the l-value is the
2137f85e193739c953358c865005855253af4f68a497John McCall  // result of a non-volatile assignment, do a simple retain of the
2138f85e193739c953358c865005855253af4f68a497John McCall  // result of the call to objc_storeWeak instead of reloading.
2139f85e193739c953358c865005855253af4f68a497John McCall  if (CGF.getLangOptions().CPlusPlus &&
2140f85e193739c953358c865005855253af4f68a497John McCall      !type.isVolatileQualified() &&
2141f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() == Qualifiers::OCL_Weak &&
2142f85e193739c953358c865005855253af4f68a497John McCall      isa<BinaryOperator>(e) &&
2143f85e193739c953358c865005855253af4f68a497John McCall      cast<BinaryOperator>(e)->getOpcode() == BO_Assign)
2144f85e193739c953358c865005855253af4f68a497John McCall    return TryEmitResult(CGF.EmitScalarExpr(e), false);
2145f85e193739c953358c865005855253af4f68a497John McCall
2146f85e193739c953358c865005855253af4f68a497John McCall  return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
2147f85e193739c953358c865005855253af4f68a497John McCall}
2148f85e193739c953358c865005855253af4f68a497John McCall
2149f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
2150f85e193739c953358c865005855253af4f68a497John McCall                                           llvm::Value *value);
2151f85e193739c953358c865005855253af4f68a497John McCall
2152f85e193739c953358c865005855253af4f68a497John McCall/// Given that the given expression is some sort of call (which does
2153f85e193739c953358c865005855253af4f68a497John McCall/// not return retained), emit a retain following it.
2154f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) {
2155f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = CGF.EmitScalarExpr(e);
2156f85e193739c953358c865005855253af4f68a497John McCall  return emitARCRetainAfterCall(CGF, value);
2157f85e193739c953358c865005855253af4f68a497John McCall}
2158f85e193739c953358c865005855253af4f68a497John McCall
2159f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
2160f85e193739c953358c865005855253af4f68a497John McCall                                           llvm::Value *value) {
2161f85e193739c953358c865005855253af4f68a497John McCall  if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
2162f85e193739c953358c865005855253af4f68a497John McCall    CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2163f85e193739c953358c865005855253af4f68a497John McCall
2164f85e193739c953358c865005855253af4f68a497John McCall    // Place the retain immediately following the call.
2165f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.SetInsertPoint(call->getParent(),
2166f85e193739c953358c865005855253af4f68a497John McCall                               ++llvm::BasicBlock::iterator(call));
2167f85e193739c953358c865005855253af4f68a497John McCall    value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
2168f85e193739c953358c865005855253af4f68a497John McCall
2169f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.restoreIP(ip);
2170f85e193739c953358c865005855253af4f68a497John McCall    return value;
2171f85e193739c953358c865005855253af4f68a497John McCall  } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
2172f85e193739c953358c865005855253af4f68a497John McCall    CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2173f85e193739c953358c865005855253af4f68a497John McCall
2174f85e193739c953358c865005855253af4f68a497John McCall    // Place the retain at the beginning of the normal destination block.
2175f85e193739c953358c865005855253af4f68a497John McCall    llvm::BasicBlock *BB = invoke->getNormalDest();
2176f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.SetInsertPoint(BB, BB->begin());
2177f85e193739c953358c865005855253af4f68a497John McCall    value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
2178f85e193739c953358c865005855253af4f68a497John McCall
2179f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.restoreIP(ip);
2180f85e193739c953358c865005855253af4f68a497John McCall    return value;
2181f85e193739c953358c865005855253af4f68a497John McCall
2182f85e193739c953358c865005855253af4f68a497John McCall  // Bitcasts can arise because of related-result returns.  Rewrite
2183f85e193739c953358c865005855253af4f68a497John McCall  // the operand.
2184f85e193739c953358c865005855253af4f68a497John McCall  } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
2185f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *operand = bitcast->getOperand(0);
2186f85e193739c953358c865005855253af4f68a497John McCall    operand = emitARCRetainAfterCall(CGF, operand);
2187f85e193739c953358c865005855253af4f68a497John McCall    bitcast->setOperand(0, operand);
2188f85e193739c953358c865005855253af4f68a497John McCall    return bitcast;
2189f85e193739c953358c865005855253af4f68a497John McCall
2190f85e193739c953358c865005855253af4f68a497John McCall  // Generic fall-back case.
2191f85e193739c953358c865005855253af4f68a497John McCall  } else {
2192f85e193739c953358c865005855253af4f68a497John McCall    // Retain using the non-block variant: we never need to do a copy
2193f85e193739c953358c865005855253af4f68a497John McCall    // of a block that's been returned to us.
2194f85e193739c953358c865005855253af4f68a497John McCall    return CGF.EmitARCRetainNonBlock(value);
2195f85e193739c953358c865005855253af4f68a497John McCall  }
2196f85e193739c953358c865005855253af4f68a497John McCall}
2197f85e193739c953358c865005855253af4f68a497John McCall
2198dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// Determine whether it might be important to emit a separate
2199dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// objc_retain_block on the result of the given expression, or
2200dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// whether it's okay to just emit it in a +1 context.
2201dc05b11c67331016473fbc7909827b1b89c9616bJohn McCallstatic bool shouldEmitSeparateBlockRetain(const Expr *e) {
2202dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  assert(e->getType()->isBlockPointerType());
2203dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  e = e->IgnoreParens();
2204dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2205dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  // For future goodness, emit block expressions directly in +1
2206dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  // contexts if we can.
2207dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  if (isa<BlockExpr>(e))
2208dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    return false;
2209dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2210dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  if (const CastExpr *cast = dyn_cast<CastExpr>(e)) {
2211dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    switch (cast->getCastKind()) {
2212dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    // Emitting these operations in +1 contexts is goodness.
2213dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    case CK_LValueToRValue:
221433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    case CK_ARCReclaimReturnedObject:
221533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    case CK_ARCConsumeObject:
221633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    case CK_ARCProduceObject:
2217dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      return false;
2218dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2219dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    // These operations preserve a block type.
2220dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    case CK_NoOp:
2221dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    case CK_BitCast:
2222dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      return shouldEmitSeparateBlockRetain(cast->getSubExpr());
2223dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2224dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    // These operations are known to be bad (or haven't been considered).
2225dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    case CK_AnyPointerToBlockPointerCast:
2226dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    default:
2227dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      return true;
2228dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    }
2229dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  }
2230dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2231dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  return true;
2232dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall}
2233dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2234f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult
2235f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) {
2236990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  // Look through cleanups.
2237990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
2238990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    CodeGenFunction::RunCleanupsScope scope(CGF);
2239990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    return tryEmitARCRetainScalarExpr(CGF, cleanups->getSubExpr());
2240990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  }
2241990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall
2242f85e193739c953358c865005855253af4f68a497John McCall  // The desired result type, if it differs from the type of the
2243f85e193739c953358c865005855253af4f68a497John McCall  // ultimate opaque expression.
22442acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *resultType = 0;
2245f85e193739c953358c865005855253af4f68a497John McCall
2246f85e193739c953358c865005855253af4f68a497John McCall  while (true) {
2247f85e193739c953358c865005855253af4f68a497John McCall    e = e->IgnoreParens();
2248f85e193739c953358c865005855253af4f68a497John McCall
2249f85e193739c953358c865005855253af4f68a497John McCall    // There's a break at the end of this if-chain;  anything
2250f85e193739c953358c865005855253af4f68a497John McCall    // that wants to keep looping has to explicitly continue.
2251f85e193739c953358c865005855253af4f68a497John McCall    if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
2252f85e193739c953358c865005855253af4f68a497John McCall      switch (ce->getCastKind()) {
2253f85e193739c953358c865005855253af4f68a497John McCall      // No-op casts don't change the type, so we just ignore them.
2254f85e193739c953358c865005855253af4f68a497John McCall      case CK_NoOp:
2255f85e193739c953358c865005855253af4f68a497John McCall        e = ce->getSubExpr();
2256f85e193739c953358c865005855253af4f68a497John McCall        continue;
2257f85e193739c953358c865005855253af4f68a497John McCall
2258f85e193739c953358c865005855253af4f68a497John McCall      case CK_LValueToRValue: {
2259f85e193739c953358c865005855253af4f68a497John McCall        TryEmitResult loadResult
2260f85e193739c953358c865005855253af4f68a497John McCall          = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr());
2261f85e193739c953358c865005855253af4f68a497John McCall        if (resultType) {
2262f85e193739c953358c865005855253af4f68a497John McCall          llvm::Value *value = loadResult.getPointer();
2263f85e193739c953358c865005855253af4f68a497John McCall          value = CGF.Builder.CreateBitCast(value, resultType);
2264f85e193739c953358c865005855253af4f68a497John McCall          loadResult.setPointer(value);
2265f85e193739c953358c865005855253af4f68a497John McCall        }
2266f85e193739c953358c865005855253af4f68a497John McCall        return loadResult;
2267f85e193739c953358c865005855253af4f68a497John McCall      }
2268f85e193739c953358c865005855253af4f68a497John McCall
2269f85e193739c953358c865005855253af4f68a497John McCall      // These casts can change the type, so remember that and
2270f85e193739c953358c865005855253af4f68a497John McCall      // soldier on.  We only need to remember the outermost such
2271f85e193739c953358c865005855253af4f68a497John McCall      // cast, though.
22721d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      case CK_CPointerToObjCPointerCast:
22731d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      case CK_BlockPointerToObjCPointerCast:
2274f85e193739c953358c865005855253af4f68a497John McCall      case CK_AnyPointerToBlockPointerCast:
2275f85e193739c953358c865005855253af4f68a497John McCall      case CK_BitCast:
2276f85e193739c953358c865005855253af4f68a497John McCall        if (!resultType)
2277f85e193739c953358c865005855253af4f68a497John McCall          resultType = CGF.ConvertType(ce->getType());
2278f85e193739c953358c865005855253af4f68a497John McCall        e = ce->getSubExpr();
2279f85e193739c953358c865005855253af4f68a497John McCall        assert(e->getType()->hasPointerRepresentation());
2280f85e193739c953358c865005855253af4f68a497John McCall        continue;
2281f85e193739c953358c865005855253af4f68a497John McCall
2282f85e193739c953358c865005855253af4f68a497John McCall      // For consumptions, just emit the subexpression and thus elide
2283f85e193739c953358c865005855253af4f68a497John McCall      // the retain/release pair.
228433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall      case CK_ARCConsumeObject: {
2285f85e193739c953358c865005855253af4f68a497John McCall        llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr());
2286f85e193739c953358c865005855253af4f68a497John McCall        if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2287f85e193739c953358c865005855253af4f68a497John McCall        return TryEmitResult(result, true);
2288f85e193739c953358c865005855253af4f68a497John McCall      }
2289f85e193739c953358c865005855253af4f68a497John McCall
2290dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      // Block extends are net +0.  Naively, we could just recurse on
2291dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      // the subexpression, but actually we need to ensure that the
2292dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      // value is copied as a block, so there's a little filter here.
229333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall      case CK_ARCExtendBlockObject: {
2294dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        llvm::Value *result; // will be a +0 value
2295dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2296dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        // If we can't safely assume the sub-expression will produce a
2297dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        // block-copied value, emit the sub-expression at +0.
2298dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) {
2299dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          result = CGF.EmitScalarExpr(ce->getSubExpr());
2300dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2301dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        // Otherwise, try to emit the sub-expression at +1 recursively.
2302dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        } else {
2303dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          TryEmitResult subresult
2304dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall            = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr());
2305dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          result = subresult.getPointer();
2306dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2307dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          // If that produced a retained value, just use that,
2308dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          // possibly casting down.
2309dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          if (subresult.getInt()) {
2310dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall            if (resultType)
2311dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall              result = CGF.Builder.CreateBitCast(result, resultType);
2312dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall            return TryEmitResult(result, true);
2313dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          }
2314dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2315dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          // Otherwise it's +0.
2316dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        }
2317dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2318dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        // Retain the object as a block, then cast down.
2319348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall        result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true);
2320dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2321dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        return TryEmitResult(result, true);
2322dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      }
2323dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
23247e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall      // For reclaims, emit the subexpression as a retained call and
23257e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall      // skip the consumption.
232633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall      case CK_ARCReclaimReturnedObject: {
23277e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall        llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr());
23287e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall        if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
23297e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall        return TryEmitResult(result, true);
23307e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall      }
23317e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall
2332f85e193739c953358c865005855253af4f68a497John McCall      case CK_GetObjCProperty: {
2333f85e193739c953358c865005855253af4f68a497John McCall        llvm::Value *result = emitARCRetainCall(CGF, ce);
2334f85e193739c953358c865005855253af4f68a497John McCall        if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2335f85e193739c953358c865005855253af4f68a497John McCall        return TryEmitResult(result, true);
2336f85e193739c953358c865005855253af4f68a497John McCall      }
2337f85e193739c953358c865005855253af4f68a497John McCall
2338f85e193739c953358c865005855253af4f68a497John McCall      default:
2339f85e193739c953358c865005855253af4f68a497John McCall        break;
2340f85e193739c953358c865005855253af4f68a497John McCall      }
2341f85e193739c953358c865005855253af4f68a497John McCall
2342f85e193739c953358c865005855253af4f68a497John McCall    // Skip __extension__.
2343f85e193739c953358c865005855253af4f68a497John McCall    } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) {
2344f85e193739c953358c865005855253af4f68a497John McCall      if (op->getOpcode() == UO_Extension) {
2345f85e193739c953358c865005855253af4f68a497John McCall        e = op->getSubExpr();
2346f85e193739c953358c865005855253af4f68a497John McCall        continue;
2347f85e193739c953358c865005855253af4f68a497John McCall      }
2348f85e193739c953358c865005855253af4f68a497John McCall
2349f85e193739c953358c865005855253af4f68a497John McCall    // For calls and message sends, use the retained-call logic.
2350f85e193739c953358c865005855253af4f68a497John McCall    // Delegate inits are a special case in that they're the only
2351f85e193739c953358c865005855253af4f68a497John McCall    // returns-retained expression that *isn't* surrounded by
2352f85e193739c953358c865005855253af4f68a497John McCall    // a consume.
2353f85e193739c953358c865005855253af4f68a497John McCall    } else if (isa<CallExpr>(e) ||
2354f85e193739c953358c865005855253af4f68a497John McCall               (isa<ObjCMessageExpr>(e) &&
2355f85e193739c953358c865005855253af4f68a497John McCall                !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) {
2356f85e193739c953358c865005855253af4f68a497John McCall      llvm::Value *result = emitARCRetainCall(CGF, e);
2357f85e193739c953358c865005855253af4f68a497John McCall      if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2358f85e193739c953358c865005855253af4f68a497John McCall      return TryEmitResult(result, true);
2359f85e193739c953358c865005855253af4f68a497John McCall    }
2360f85e193739c953358c865005855253af4f68a497John McCall
2361f85e193739c953358c865005855253af4f68a497John McCall    // Conservatively halt the search at any other expression kind.
2362f85e193739c953358c865005855253af4f68a497John McCall    break;
2363f85e193739c953358c865005855253af4f68a497John McCall  }
2364f85e193739c953358c865005855253af4f68a497John McCall
2365f85e193739c953358c865005855253af4f68a497John McCall  // We didn't find an obvious production, so emit what we've got and
2366f85e193739c953358c865005855253af4f68a497John McCall  // tell the caller that we didn't manage to retain.
2367f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *result = CGF.EmitScalarExpr(e);
2368f85e193739c953358c865005855253af4f68a497John McCall  if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2369f85e193739c953358c865005855253af4f68a497John McCall  return TryEmitResult(result, false);
2370f85e193739c953358c865005855253af4f68a497John McCall}
2371f85e193739c953358c865005855253af4f68a497John McCall
2372f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2373f85e193739c953358c865005855253af4f68a497John McCall                                                LValue lvalue,
2374f85e193739c953358c865005855253af4f68a497John McCall                                                QualType type) {
2375f85e193739c953358c865005855253af4f68a497John McCall  TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
2376f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = result.getPointer();
2377f85e193739c953358c865005855253af4f68a497John McCall  if (!result.getInt())
2378f85e193739c953358c865005855253af4f68a497John McCall    value = CGF.EmitARCRetain(type, value);
2379f85e193739c953358c865005855253af4f68a497John McCall  return value;
2380f85e193739c953358c865005855253af4f68a497John McCall}
2381f85e193739c953358c865005855253af4f68a497John McCall
2382f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainScalarExpr - Semantically equivalent to
2383f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
2384f85e193739c953358c865005855253af4f68a497John McCall/// best-effort attempt to peephole expressions that naturally produce
2385f85e193739c953358c865005855253af4f68a497John McCall/// retained objects.
2386f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) {
2387f85e193739c953358c865005855253af4f68a497John McCall  TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2388f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = result.getPointer();
2389f85e193739c953358c865005855253af4f68a497John McCall  if (!result.getInt())
2390f85e193739c953358c865005855253af4f68a497John McCall    value = EmitARCRetain(e->getType(), value);
2391f85e193739c953358c865005855253af4f68a497John McCall  return value;
2392f85e193739c953358c865005855253af4f68a497John McCall}
2393f85e193739c953358c865005855253af4f68a497John McCall
2394f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
2395f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) {
2396f85e193739c953358c865005855253af4f68a497John McCall  TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2397f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = result.getPointer();
2398f85e193739c953358c865005855253af4f68a497John McCall  if (result.getInt())
2399f85e193739c953358c865005855253af4f68a497John McCall    value = EmitARCAutorelease(value);
2400f85e193739c953358c865005855253af4f68a497John McCall  else
2401f85e193739c953358c865005855253af4f68a497John McCall    value = EmitARCRetainAutorelease(e->getType(), value);
2402f85e193739c953358c865005855253af4f68a497John McCall  return value;
2403f85e193739c953358c865005855253af4f68a497John McCall}
2404f85e193739c953358c865005855253af4f68a497John McCall
2405348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) {
2406348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  llvm::Value *result;
2407348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  bool doRetain;
2408348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
2409348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  if (shouldEmitSeparateBlockRetain(e)) {
2410348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    result = EmitScalarExpr(e);
2411348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    doRetain = true;
2412348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  } else {
2413348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e);
2414348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    result = subresult.getPointer();
2415348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    doRetain = !subresult.getInt();
2416348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  }
2417348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
2418348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  if (doRetain)
2419348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    result = EmitARCRetainBlock(result, /*mandatory*/ true);
2420348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  return EmitObjCConsumeObject(e->getType(), result);
2421348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall}
2422348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
24232b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCallllvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) {
24242b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // In ARC, retain and autorelease the expression.
24252b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  if (getLangOptions().ObjCAutoRefCount) {
24262b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    // Do so before running any cleanups for the full-expression.
24272b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    // tryEmitARCRetainScalarExpr does make an effort to do things
24282b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    // inside cleanups, but there are crazy cases like
24292b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    //   @throw A().foo;
24302b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    // where a full retain+autorelease is required and would
24312b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    // otherwise happen after the destructor for the temporary.
24322b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    CodeGenFunction::RunCleanupsScope cleanups(*this);
24332b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(expr))
24342b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall      expr = ewc->getSubExpr();
24352b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall
24362b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    return EmitARCRetainAutoreleaseScalarExpr(expr);
24372b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  }
24382b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall
24392b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // Otherwise, use the normal scalar-expression emission.  The
24402b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // exception machinery doesn't do anything special with the
24412b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // exception like retaining it, so there's no safety associated with
24422b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // only running cleanups after the throw has started, and when it
24432b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // matters it tends to be substantially inferior code.
24442b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  return EmitScalarExpr(expr);
24452b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall}
24462b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall
2447f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*>
2448f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e,
2449f85e193739c953358c865005855253af4f68a497John McCall                                    bool ignored) {
2450f85e193739c953358c865005855253af4f68a497John McCall  // Evaluate the RHS first.
2451f85e193739c953358c865005855253af4f68a497John McCall  TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
2452f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = result.getPointer();
2453f85e193739c953358c865005855253af4f68a497John McCall
2454fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  bool hasImmediateRetain = result.getInt();
2455fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall
2456fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  // If we didn't emit a retained object, and the l-value is of block
2457fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  // type, then we need to emit the block-retain immediately in case
2458fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  // it invalidates the l-value.
2459fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  if (!hasImmediateRetain && e->getType()->isBlockPointerType()) {
2460348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    value = EmitARCRetainBlock(value, /*mandatory*/ false);
2461fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall    hasImmediateRetain = true;
2462fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  }
2463fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall
2464f85e193739c953358c865005855253af4f68a497John McCall  LValue lvalue = EmitLValue(e->getLHS());
2465f85e193739c953358c865005855253af4f68a497John McCall
2466f85e193739c953358c865005855253af4f68a497John McCall  // If the RHS was emitted retained, expand this.
2467fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  if (hasImmediateRetain) {
2468f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *oldValue =
2469f85e193739c953358c865005855253af4f68a497John McCall      EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatileQualified(),
2470f85e193739c953358c865005855253af4f68a497John McCall                       lvalue.getAlignment(), e->getType(),
2471f85e193739c953358c865005855253af4f68a497John McCall                       lvalue.getTBAAInfo());
2472f85e193739c953358c865005855253af4f68a497John McCall    EmitStoreOfScalar(value, lvalue.getAddress(),
2473f85e193739c953358c865005855253af4f68a497John McCall                      lvalue.isVolatileQualified(), lvalue.getAlignment(),
2474f85e193739c953358c865005855253af4f68a497John McCall                      e->getType(), lvalue.getTBAAInfo());
2475f85e193739c953358c865005855253af4f68a497John McCall    EmitARCRelease(oldValue, /*precise*/ false);
2476f85e193739c953358c865005855253af4f68a497John McCall  } else {
2477545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    value = EmitARCStoreStrong(lvalue, value, ignored);
2478f85e193739c953358c865005855253af4f68a497John McCall  }
2479f85e193739c953358c865005855253af4f68a497John McCall
2480f85e193739c953358c865005855253af4f68a497John McCall  return std::pair<LValue,llvm::Value*>(lvalue, value);
2481f85e193739c953358c865005855253af4f68a497John McCall}
2482f85e193739c953358c865005855253af4f68a497John McCall
2483f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*>
2484f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) {
2485f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
2486f85e193739c953358c865005855253af4f68a497John McCall  LValue lvalue = EmitLValue(e->getLHS());
2487f85e193739c953358c865005855253af4f68a497John McCall
2488f85e193739c953358c865005855253af4f68a497John McCall  EmitStoreOfScalar(value, lvalue.getAddress(),
2489f85e193739c953358c865005855253af4f68a497John McCall                    lvalue.isVolatileQualified(), lvalue.getAlignment(),
2490f85e193739c953358c865005855253af4f68a497John McCall                    e->getType(), lvalue.getTBAAInfo());
2491f85e193739c953358c865005855253af4f68a497John McCall
2492f85e193739c953358c865005855253af4f68a497John McCall  return std::pair<LValue,llvm::Value*>(lvalue, value);
2493f85e193739c953358c865005855253af4f68a497John McCall}
2494f85e193739c953358c865005855253af4f68a497John McCall
2495f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolStmt(
2496f85e193739c953358c865005855253af4f68a497John McCall                                             const ObjCAutoreleasePoolStmt &ARPS) {
2497f85e193739c953358c865005855253af4f68a497John McCall  const Stmt *subStmt = ARPS.getSubStmt();
2498f85e193739c953358c865005855253af4f68a497John McCall  const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
2499f85e193739c953358c865005855253af4f68a497John McCall
2500f85e193739c953358c865005855253af4f68a497John McCall  CGDebugInfo *DI = getDebugInfo();
250173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
250273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getLBracLoc());
2503f85e193739c953358c865005855253af4f68a497John McCall
2504f85e193739c953358c865005855253af4f68a497John McCall  // Keep track of the current cleanup stack depth.
2505f85e193739c953358c865005855253af4f68a497John McCall  RunCleanupsScope Scope(*this);
25069f084a3166b684573ba49df28fc5792bc37d92e1John McCall  if (CGM.getCodeGenOpts().ObjCRuntimeHasARC) {
2507f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *token = EmitObjCAutoreleasePoolPush();
2508f85e193739c953358c865005855253af4f68a497John McCall    EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
2509f85e193739c953358c865005855253af4f68a497John McCall  } else {
2510f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
2511f85e193739c953358c865005855253af4f68a497John McCall    EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
2512f85e193739c953358c865005855253af4f68a497John McCall  }
2513f85e193739c953358c865005855253af4f68a497John McCall
2514f85e193739c953358c865005855253af4f68a497John McCall  for (CompoundStmt::const_body_iterator I = S.body_begin(),
2515f85e193739c953358c865005855253af4f68a497John McCall       E = S.body_end(); I != E; ++I)
2516f85e193739c953358c865005855253af4f68a497John McCall    EmitStmt(*I);
2517f85e193739c953358c865005855253af4f68a497John McCall
251873fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
251973fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc());
2520f85e193739c953358c865005855253af4f68a497John McCall}
25210c24c808e4dce43e88abf2d5f98546b901bd4315John McCall
25220c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
25230c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// make sure it survives garbage collection until this point.
25240c24c808e4dce43e88abf2d5f98546b901bd4315John McCallvoid CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
25250c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  // We just use an inline assembly.
25260c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  llvm::FunctionType *extenderType
2527da549e8995c447542d5631b8b67fcc3a9582797aJay Foad    = llvm::FunctionType::get(VoidTy, VoidPtrTy, /*variadic*/ false);
25280c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  llvm::Value *extender
25290c24c808e4dce43e88abf2d5f98546b901bd4315John McCall    = llvm::InlineAsm::get(extenderType,
25300c24c808e4dce43e88abf2d5f98546b901bd4315John McCall                           /* assembly */ "",
25310c24c808e4dce43e88abf2d5f98546b901bd4315John McCall                           /* constraints */ "r",
25320c24c808e4dce43e88abf2d5f98546b901bd4315John McCall                           /* side effects */ true);
25330c24c808e4dce43e88abf2d5f98546b901bd4315John McCall
25340c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  object = Builder.CreateBitCast(object, VoidPtrTy);
25350c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  Builder.CreateCall(extender, object)->setDoesNotThrow();
25360c24c808e4dce43e88abf2d5f98546b901bd4315John McCall}
25370c24c808e4dce43e88abf2d5f98546b901bd4315John McCall
25382979ec73b4f974d85f2ce84167712177a44c6f09Ted KremenekCGObjCRuntime::~CGObjCRuntime() {}
2539