CGObjC.cpp revision 527842f97b684831de2b1a455649131ed381ece4
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"
243b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h"
253b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/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);
32ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF,
33490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian                                      QualType ET,
34ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                      const ObjCMethodDecl *Method,
35ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                      RValue Result);
36f85e193739c953358c865005855253af4f68a497John McCall
37f85e193739c953358c865005855253af4f68a497John McCall/// Given the address of a variable of pointer type, find the correct
38f85e193739c953358c865005855253af4f68a497John McCall/// null to store into it.
39f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *getNullForVariable(llvm::Value *addr) {
402acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *type =
41f85e193739c953358c865005855253af4f68a497John McCall    cast<llvm::PointerType>(addr->getType())->getElementType();
42f85e193739c953358c865005855253af4f68a497John McCall  return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type));
43f85e193739c953358c865005855253af4f68a497John McCall}
44f85e193739c953358c865005855253af4f68a497John McCall
458fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emits an instance of NSConstantString representing the object.
461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
4771fcec9abf2ce66d5e17a24bd021680e94e42f0dDaniel Dunbar{
480d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  llvm::Constant *C =
490d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall      CGM.getObjCRuntime().GenerateConstantString(E->getString());
50ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  // FIXME: This bitcast should just be made an invariant on the Runtime.
513c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
528fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner}
538fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
54eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// EmitObjCBoxedExpr - This routine generates code to call
55eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// the appropriate expression boxing method. This will either be
56eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:].
57ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///
5816098f366097305c348b356a44638d6c959c6e60Eric Christopherllvm::Value *
59eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick BeardCodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) {
60ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Generate the correct selector for this literal's concrete type.
61eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  const Expr *SubExpr = E->getSubExpr();
62ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Get the method.
63eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod();
64eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  assert(BoxingMethod && "BoxingMethod is null");
65eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method");
66eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  Selector Sel = BoxingMethod->getSelector();
67ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
68ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Generate a reference to the class pointer, which will be the receiver.
69eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  // Assumes that the method was introduced in the class that should be
70eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  // messaged (avoids pulling it out of the result type).
71ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
72eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface();
73bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl);
74eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard
75eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  const ParmVarDecl *argDecl = *BoxingMethod->param_begin();
76ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  QualType ArgQT = argDecl->getType().getUnqualifiedType();
77eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  RValue RV = EmitAnyExpr(SubExpr);
78ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  CallArgList Args;
79ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  Args.add(RV, ArgQT);
80eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard
81ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  RValue result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
82eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard                                              BoxingMethod->getResultType(), Sel, Receiver, Args,
83eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard                                              ClassDecl, BoxingMethod);
84ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return Builder.CreateBitCast(result.getScalarVal(),
85ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                               ConvertType(E->getType()));
86ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
87ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
88ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E,
89ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                    const ObjCMethodDecl *MethodWithObjects) {
90ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ASTContext &Context = CGM.getContext();
91ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  const ObjCDictionaryLiteral *DLE = 0;
92ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E);
93ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  if (!ALE)
94ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    DLE = cast<ObjCDictionaryLiteral>(E);
95ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
96ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Compute the type of the array we're initializing.
97ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  uint64_t NumElements =
98ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ALE ? ALE->getNumElements() : DLE->getNumElements();
99ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()),
100ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                            NumElements);
101ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  QualType ElementType = Context.getObjCIdType().withConst();
102ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  QualType ElementArrayType
103ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    = Context.getConstantArrayType(ElementType, APNumElements,
104ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                   ArrayType::Normal, /*IndexTypeQuals=*/0);
105ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
106ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Allocate the temporary array(s).
107ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  llvm::Value *Objects = CreateMemTemp(ElementArrayType, "objects");
108ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  llvm::Value *Keys = 0;
109ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  if (DLE)
110ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    Keys = CreateMemTemp(ElementArrayType, "keys");
111ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
112527842f97b684831de2b1a455649131ed381ece4John McCall  // In ARC, we may need to do extra work to keep all the keys and
113527842f97b684831de2b1a455649131ed381ece4John McCall  // values alive until after the call.
114527842f97b684831de2b1a455649131ed381ece4John McCall  SmallVector<llvm::Value *, 16> NeededObjects;
115527842f97b684831de2b1a455649131ed381ece4John McCall  bool TrackNeededObjects =
116527842f97b684831de2b1a455649131ed381ece4John McCall    (getLangOpts().ObjCAutoRefCount &&
117527842f97b684831de2b1a455649131ed381ece4John McCall    CGM.getCodeGenOpts().OptimizationLevel != 0);
118527842f97b684831de2b1a455649131ed381ece4John McCall
119ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Perform the actual initialialization of the array(s).
120ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  for (uint64_t i = 0; i < NumElements; i++) {
121ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (ALE) {
122527842f97b684831de2b1a455649131ed381ece4John McCall      // Emit the element and store it to the appropriate array slot.
123ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      const Expr *Rhs = ALE->getElement(i);
124ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      LValue LV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i),
125ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                   ElementType,
126ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                   Context.getTypeAlignInChars(Rhs->getType()),
127ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                   Context);
128527842f97b684831de2b1a455649131ed381ece4John McCall
129527842f97b684831de2b1a455649131ed381ece4John McCall      llvm::Value *value = EmitScalarExpr(Rhs);
130527842f97b684831de2b1a455649131ed381ece4John McCall      EmitStoreThroughLValue(RValue::get(value), LV, true);
131527842f97b684831de2b1a455649131ed381ece4John McCall      if (TrackNeededObjects) {
132527842f97b684831de2b1a455649131ed381ece4John McCall        NeededObjects.push_back(value);
133527842f97b684831de2b1a455649131ed381ece4John McCall      }
134ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    } else {
135527842f97b684831de2b1a455649131ed381ece4John McCall      // Emit the key and store it to the appropriate array slot.
136ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      const Expr *Key = DLE->getKeyValueElement(i).Key;
137ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      LValue KeyLV = LValue::MakeAddr(Builder.CreateStructGEP(Keys, i),
138ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                      ElementType,
139ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                    Context.getTypeAlignInChars(Key->getType()),
140ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                      Context);
141527842f97b684831de2b1a455649131ed381ece4John McCall      llvm::Value *keyValue = EmitScalarExpr(Key);
142527842f97b684831de2b1a455649131ed381ece4John McCall      EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true);
143ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
144527842f97b684831de2b1a455649131ed381ece4John McCall      // Emit the value and store it to the appropriate array slot.
145ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      const Expr *Value = DLE->getKeyValueElement(i).Value;
146ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      LValue ValueLV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i),
147ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                        ElementType,
148ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                  Context.getTypeAlignInChars(Value->getType()),
149ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                        Context);
150527842f97b684831de2b1a455649131ed381ece4John McCall      llvm::Value *valueValue = EmitScalarExpr(Value);
151527842f97b684831de2b1a455649131ed381ece4John McCall      EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true);
152527842f97b684831de2b1a455649131ed381ece4John McCall      if (TrackNeededObjects) {
153527842f97b684831de2b1a455649131ed381ece4John McCall        NeededObjects.push_back(keyValue);
154527842f97b684831de2b1a455649131ed381ece4John McCall        NeededObjects.push_back(valueValue);
155527842f97b684831de2b1a455649131ed381ece4John McCall      }
156ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
157ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
159ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Generate the argument list.
160ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  CallArgList Args;
161ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin();
162ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  const ParmVarDecl *argDecl = *PI++;
163ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  QualType ArgQT = argDecl->getType().getUnqualifiedType();
164ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  Args.add(RValue::get(Objects), ArgQT);
165ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  if (DLE) {
166ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    argDecl = *PI++;
167ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ArgQT = argDecl->getType().getUnqualifiedType();
168ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    Args.add(RValue::get(Keys), ArgQT);
169ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
170ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  argDecl = *PI;
171ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ArgQT = argDecl->getType().getUnqualifiedType();
172ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  llvm::Value *Count =
173ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements);
174ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  Args.add(RValue::get(Count), ArgQT);
175ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
176ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Generate a reference to the class pointer, which will be the receiver.
177ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  Selector Sel = MethodWithObjects->getSelector();
178ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  QualType ResultType = E->getType();
179ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  const ObjCObjectPointerType *InterfacePointerType
180ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    = ResultType->getAsObjCInterfacePointerType();
181ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ObjCInterfaceDecl *Class
182ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    = InterfacePointerType->getObjectType()->getInterface();
183ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
184bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *Receiver = Runtime.GetClass(*this, Class);
185ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
186ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Generate the message send.
18716098f366097305c348b356a44638d6c959c6e60Eric Christopher  RValue result
18816098f366097305c348b356a44638d6c959c6e60Eric Christopher    = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
18916098f366097305c348b356a44638d6c959c6e60Eric Christopher                                  MethodWithObjects->getResultType(),
19016098f366097305c348b356a44638d6c959c6e60Eric Christopher                                  Sel,
19116098f366097305c348b356a44638d6c959c6e60Eric Christopher                                  Receiver, Args, Class,
19216098f366097305c348b356a44638d6c959c6e60Eric Christopher                                  MethodWithObjects);
193527842f97b684831de2b1a455649131ed381ece4John McCall
194527842f97b684831de2b1a455649131ed381ece4John McCall  // The above message send needs these objects, but in ARC they are
195527842f97b684831de2b1a455649131ed381ece4John McCall  // passed in a buffer that is essentially __unsafe_unretained.
196527842f97b684831de2b1a455649131ed381ece4John McCall  // Therefore we must prevent the optimizer from releasing them until
197527842f97b684831de2b1a455649131ed381ece4John McCall  // after the call.
198527842f97b684831de2b1a455649131ed381ece4John McCall  if (TrackNeededObjects) {
199527842f97b684831de2b1a455649131ed381ece4John McCall    EmitARCIntrinsicUse(NeededObjects);
200527842f97b684831de2b1a455649131ed381ece4John McCall  }
201527842f97b684831de2b1a455649131ed381ece4John McCall
202ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return Builder.CreateBitCast(result.getScalarVal(),
203ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                               ConvertType(E->getType()));
204ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
205ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
206ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) {
207ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod());
208ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
209ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
210ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral(
211ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                            const ObjCDictionaryLiteral *E) {
212ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod());
213ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
214ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2158fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emit a selector.
2168fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattnerllvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
2178fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Untyped selector.
2188fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Note that this implementation allows for non-constant strings to be passed
2198fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // as arguments to @selector().  Currently, the only thing preventing this
2208fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // behaviour is the type checking in the front end.
221bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return CGM.getObjCRuntime().GetSelector(*this, E->getSelector());
2228fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner}
2238fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
224ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbarllvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
225ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  // FIXME: This should pass the Decl not the name.
226bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol());
227ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar}
2288fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
229926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// \brief Adjust the type of the result of an Objective-C message send
230926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// expression when the method has a related result type.
231926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregorstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF,
232490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian                                      QualType ExpT,
233926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                      const ObjCMethodDecl *Method,
234926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                      RValue Result) {
235926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (!Method)
236926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return Result;
237f85e193739c953358c865005855253af4f68a497John McCall
238926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (!Method->hasRelatedResultType() ||
239490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian      CGF.getContext().hasSameType(ExpT, Method->getResultType()) ||
240926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      !Result.isScalar())
241926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return Result;
242926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
243926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  // We have applied a related result type. Cast the rvalue appropriately.
244926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
245490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian                                               CGF.ConvertType(ExpT)));
246926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor}
2478fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
248dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// Decide whether to extend the lifetime of the receiver of a
249dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// returns-inner-pointer message.
250dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallstatic bool
251dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallshouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) {
252dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  switch (message->getReceiverKind()) {
253dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
254dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // For a normal instance message, we should extend unless the
255dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // receiver is loaded from a variable with precise lifetime.
256dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  case ObjCMessageExpr::Instance: {
257dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    const Expr *receiver = message->getInstanceReceiver();
258dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver);
259dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (!ice || ice->getCastKind() != CK_LValueToRValue) return true;
260dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    receiver = ice->getSubExpr()->IgnoreParens();
261dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
262dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // Only __strong variables.
263dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
264dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      return true;
265dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
266dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // All ivars and fields have precise lifetime.
267dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver))
268dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      return false;
269dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
270dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // Otherwise, check for variables.
271dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr());
272dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (!declRef) return true;
273dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl());
274dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    if (!var) return true;
275dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
276dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // All variables have precise lifetime except local variables with
277dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // automatic storage duration that aren't specially marked.
278dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    return (var->hasLocalStorage() &&
279dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall            !var->hasAttr<ObjCPreciseLifetimeAttr>());
280dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  }
281dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
282dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  case ObjCMessageExpr::Class:
283dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  case ObjCMessageExpr::SuperClass:
284dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // It's never necessary for class objects.
285dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    return false;
286dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
287dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  case ObjCMessageExpr::SuperInstance:
288dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    // We generally assume that 'self' lives throughout a method call.
289dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    return false;
290dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  }
291dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
292dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  llvm_unreachable("invalid receiver kind");
293dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall}
294dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
295ef072fd2f3347cfd857d6eb787b245b950771430John McCallRValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
296ef072fd2f3347cfd857d6eb787b245b950771430John McCall                                            ReturnValueSlot Return) {
2978fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Only the lookup mechanism and first two arguments of the method
2988fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // implementation vary between runtimes.  We can get the receiver and
2998fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // arguments in generic code.
3001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
301f85e193739c953358c865005855253af4f68a497John McCall  bool isDelegateInit = E->isDelegateInitCall();
302f85e193739c953358c865005855253af4f68a497John McCall
303dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  const ObjCMethodDecl *method = E->getMethodDecl();
3044e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian
305f85e193739c953358c865005855253af4f68a497John McCall  // We don't retain the receiver in delegate init calls, and this is
306f85e193739c953358c865005855253af4f68a497John McCall  // safe because the receiver value is always loaded from 'self',
307f85e193739c953358c865005855253af4f68a497John McCall  // which we zero out.  We don't want to Block_copy block receivers,
308f85e193739c953358c865005855253af4f68a497John McCall  // though.
309f85e193739c953358c865005855253af4f68a497John McCall  bool retainSelf =
310f85e193739c953358c865005855253af4f68a497John McCall    (!isDelegateInit &&
3114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie     CGM.getLangOpts().ObjCAutoRefCount &&
312dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall     method &&
313dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall     method->hasAttr<NSConsumesSelfAttr>());
314f85e193739c953358c865005855253af4f68a497John McCall
315208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
3168fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  bool isSuperMessage = false;
317f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar  bool isClassMessage = false;
318c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall  ObjCInterfaceDecl *OID = 0;
3198fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  // Find the receiver
320926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  QualType ReceiverType;
3210b647a6ea18151149d624ab373e6fe0e819e4a9aDaniel Dunbar  llvm::Value *Receiver = 0;
32204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  switch (E->getReceiverKind()) {
32304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::Instance:
324926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    ReceiverType = E->getInstanceReceiver()->getType();
325f85e193739c953358c865005855253af4f68a497John McCall    if (retainSelf) {
326f85e193739c953358c865005855253af4f68a497John McCall      TryEmitResult ter = tryEmitARCRetainScalarExpr(*this,
327f85e193739c953358c865005855253af4f68a497John McCall                                                   E->getInstanceReceiver());
328f85e193739c953358c865005855253af4f68a497John McCall      Receiver = ter.getPointer();
329dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      if (ter.getInt()) retainSelf = false;
330f85e193739c953358c865005855253af4f68a497John McCall    } else
331f85e193739c953358c865005855253af4f68a497John McCall      Receiver = EmitScalarExpr(E->getInstanceReceiver());
33204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::Class: {
335926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    ReceiverType = E->getClassReceiver();
336926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>();
3373031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    assert(ObjTy && "Invalid Objective-C class message send");
3383031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    OID = ObjTy->getInterface();
3393031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall    assert(OID && "Invalid Objective-C class message send");
340bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    Receiver = Runtime.GetClass(*this, OID);
341f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar    isClassMessage = true;
34204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
34304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  }
34404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
34504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::SuperInstance:
346926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    ReceiverType = E->getSuperType();
34704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    Receiver = LoadObjCSelf();
3488fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner    isSuperMessage = true;
34904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
35004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
35104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case ObjCMessageExpr::SuperClass:
352926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    ReceiverType = E->getSuperType();
3538fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner    Receiver = LoadObjCSelf();
35404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    isSuperMessage = true;
35504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    isClassMessage = true;
35604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
3578fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  }
3588fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
359dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  if (retainSelf)
360dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    Receiver = EmitARCRetainNonBlock(Receiver);
361dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
362dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // In ARC, we sometimes want to "extend the lifetime"
363dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // (i.e. retain+autorelease) of receivers of returns-inner-pointer
364dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  // messages.
3654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount && method &&
366dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      method->hasAttr<ObjCReturnsInnerPointerAttr>() &&
367dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall      shouldExtendReceiverForInnerPointerMessage(E))
368dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver);
369dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall
370f85e193739c953358c865005855253af4f68a497John McCall  QualType ResultType =
371dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall    method ? method->getResultType() : E->getType();
372f85e193739c953358c865005855253af4f68a497John McCall
37319cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar  CallArgList Args;
374dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall  EmitCallArgs(Args, method, E->arg_begin(), E->arg_end());
3751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
376f85e193739c953358c865005855253af4f68a497John McCall  // For delegate init calls in ARC, do an unsafe store of null into
377f85e193739c953358c865005855253af4f68a497John McCall  // self.  This represents the call taking direct ownership of that
378f85e193739c953358c865005855253af4f68a497John McCall  // value.  We have to do this after emitting the other call
379f85e193739c953358c865005855253af4f68a497John McCall  // arguments because they might also reference self, but we don't
380f85e193739c953358c865005855253af4f68a497John McCall  // have to worry about any of them modifying self because that would
381f85e193739c953358c865005855253af4f68a497John McCall  // be an undefined read and write of an object in unordered
382f85e193739c953358c865005855253af4f68a497John McCall  // expressions.
383f85e193739c953358c865005855253af4f68a497John McCall  if (isDelegateInit) {
3844e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(getLangOpts().ObjCAutoRefCount &&
385f85e193739c953358c865005855253af4f68a497John McCall           "delegate init calls should only be marked in ARC");
386f85e193739c953358c865005855253af4f68a497John McCall
387f85e193739c953358c865005855253af4f68a497John McCall    // Do an unsafe store of null into self.
388f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *selfAddr =
389f85e193739c953358c865005855253af4f68a497John McCall      LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
390f85e193739c953358c865005855253af4f68a497John McCall    assert(selfAddr && "no self entry for a delegate init call?");
391f85e193739c953358c865005855253af4f68a497John McCall
392f85e193739c953358c865005855253af4f68a497John McCall    Builder.CreateStore(getNullForVariable(selfAddr), selfAddr);
393f85e193739c953358c865005855253af4f68a497John McCall  }
3947e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson
395926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  RValue result;
3968fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  if (isSuperMessage) {
3979384c768e93f270118a30ce96546083a666da284Chris Lattner    // super is only valid in an Objective-C method
3989384c768e93f270118a30ce96546083a666da284Chris Lattner    const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
3997ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian    bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
400926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
401926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              E->getSelector(),
402926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              OMD->getClassInterface(),
403926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              isCategoryImpl,
404926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              Receiver,
405926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              isClassMessage,
406926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                              Args,
407dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall                                              method);
408926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  } else {
409926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    result = Runtime.GenerateMessageSend(*this, Return, ResultType,
410926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                         E->getSelector(),
411926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                         Receiver, Args, OID,
412dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall                                         method);
4138fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  }
414f85e193739c953358c865005855253af4f68a497John McCall
415f85e193739c953358c865005855253af4f68a497John McCall  // For delegate init calls in ARC, implicitly store the result of
416f85e193739c953358c865005855253af4f68a497John McCall  // the call back into self.  This takes ownership of the value.
417f85e193739c953358c865005855253af4f68a497John McCall  if (isDelegateInit) {
418f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *selfAddr =
419f85e193739c953358c865005855253af4f68a497John McCall      LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
420f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *newSelf = result.getScalarVal();
421f85e193739c953358c865005855253af4f68a497John McCall
422f85e193739c953358c865005855253af4f68a497John McCall    // The delegate return type isn't necessarily a matching type; in
423f85e193739c953358c865005855253af4f68a497John McCall    // fact, it's quite likely to be 'id'.
4242acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *selfTy =
425f85e193739c953358c865005855253af4f68a497John McCall      cast<llvm::PointerType>(selfAddr->getType())->getElementType();
426f85e193739c953358c865005855253af4f68a497John McCall    newSelf = Builder.CreateBitCast(newSelf, selfTy);
427f85e193739c953358c865005855253af4f68a497John McCall
428f85e193739c953358c865005855253af4f68a497John McCall    Builder.CreateStore(newSelf, selfAddr);
429f85e193739c953358c865005855253af4f68a497John McCall  }
4304e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian
431490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian  return AdjustRelatedResultType(*this, E->getType(), method, result);
4325508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
4335508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
434f85e193739c953358c865005855253af4f68a497John McCallnamespace {
435f85e193739c953358c865005855253af4f68a497John McCallstruct FinishARCDealloc : EHScopeStack::Cleanup {
436ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall  void Emit(CodeGenFunction &CGF, Flags flags) {
437f85e193739c953358c865005855253af4f68a497John McCall    const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl);
438799d34e9505a833549c71f2ac5f842da157ea031John McCall
439799d34e9505a833549c71f2ac5f842da157ea031John McCall    const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
440f85e193739c953358c865005855253af4f68a497John McCall    const ObjCInterfaceDecl *iface = impl->getClassInterface();
441f85e193739c953358c865005855253af4f68a497John McCall    if (!iface->getSuperClass()) return;
442f85e193739c953358c865005855253af4f68a497John McCall
443799d34e9505a833549c71f2ac5f842da157ea031John McCall    bool isCategory = isa<ObjCCategoryImplDecl>(impl);
444799d34e9505a833549c71f2ac5f842da157ea031John McCall
445f85e193739c953358c865005855253af4f68a497John McCall    // Call [super dealloc] if we have a superclass.
446f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *self = CGF.LoadObjCSelf();
447f85e193739c953358c865005855253af4f68a497John McCall
448f85e193739c953358c865005855253af4f68a497John McCall    CallArgList args;
449f85e193739c953358c865005855253af4f68a497John McCall    CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(),
450f85e193739c953358c865005855253af4f68a497John McCall                                                      CGF.getContext().VoidTy,
451f85e193739c953358c865005855253af4f68a497John McCall                                                      method->getSelector(),
452f85e193739c953358c865005855253af4f68a497John McCall                                                      iface,
453799d34e9505a833549c71f2ac5f842da157ea031John McCall                                                      isCategory,
454f85e193739c953358c865005855253af4f68a497John McCall                                                      self,
455f85e193739c953358c865005855253af4f68a497John McCall                                                      /*is class msg*/ false,
456f85e193739c953358c865005855253af4f68a497John McCall                                                      args,
457f85e193739c953358c865005855253af4f68a497John McCall                                                      method);
458f85e193739c953358c865005855253af4f68a497John McCall  }
459f85e193739c953358c865005855253af4f68a497John McCall};
460f85e193739c953358c865005855253af4f68a497John McCall}
461f85e193739c953358c865005855253af4f68a497John McCall
462af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
463af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// the LLVM function and sets the other context used by
464af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// CodeGenFunction.
465679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianvoid CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
4668d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel                                      const ObjCContainerDecl *CD,
4678d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel                                      SourceLocation StartLoc) {
468d26bc76c98006609002d9930f8840490e88ac5b5John McCall  FunctionArgList args;
4694800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel  // Check if we should generate debug info for this method.
470a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov  if (!OMD->hasAttr<NoDebugAttr>())
471a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov    maybeInitializeDebugInfo();
4724800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel
473679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
474f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
475de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD);
4760e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar  CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
4774111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
478d26bc76c98006609002d9930f8840490e88ac5b5John McCall  args.push_back(OMD->getSelfDecl());
479d26bc76c98006609002d9930f8840490e88ac5b5John McCall  args.push_back(OMD->getCmdDecl());
4804111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
481491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = OMD->param_begin(),
48216098f366097305c348b356a44638d6c959c6e60Eric Christopher         E = OMD->param_end(); PI != E; ++PI)
483d26bc76c98006609002d9930f8840490e88ac5b5John McCall    args.push_back(*PI);
484b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar
48514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  CurGD = OMD;
48614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
4878d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel  StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc);
488f85e193739c953358c865005855253af4f68a497John McCall
489f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, certain methods get an extra cleanup.
4904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().ObjCAutoRefCount &&
491f85e193739c953358c865005855253af4f68a497John McCall      OMD->isInstanceMethod() &&
492f85e193739c953358c865005855253af4f68a497John McCall      OMD->getSelector().isUnarySelector()) {
493f85e193739c953358c865005855253af4f68a497John McCall    const IdentifierInfo *ident =
494f85e193739c953358c865005855253af4f68a497John McCall      OMD->getSelector().getIdentifierInfoForSlot(0);
495f85e193739c953358c865005855253af4f68a497John McCall    if (ident->isStr("dealloc"))
496f85e193739c953358c865005855253af4f68a497John McCall      EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
497f85e193739c953358c865005855253af4f68a497John McCall  }
498af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
499af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
500f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
501f85e193739c953358c865005855253af4f68a497John McCall                                              LValue lvalue, QualType type);
502f85e193739c953358c865005855253af4f68a497John McCall
503af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// Generate an Objective-C method.  An Objective-C method is a C function with
5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// its pointer, name, and types registered in the class struture.
505af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
5068d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel  StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart());
5076fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  EmitStmt(OMD->getBody());
5086fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  FinishFunction(OMD->getBodyRBrace());
509af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
510af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
51141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructGetterCall - Call the runtime function to load a property
51241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// into the return value slot.
51341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar,
51441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                                 bool isAtomic, bool hasStrong) {
51541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  ASTContext &Context = CGF.getContext();
51641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
51741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *src =
51841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall    CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(),
51941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                          ivar, 0).getAddress();
52041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
52141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  // objc_copyStruct (ReturnValue, &structIvar,
52241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  //                  sizeof (Type of Ivar), isAtomic, false);
52341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  CallArgList args;
52441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
52541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy);
52641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(dest), Context.VoidPtrTy);
52741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
52841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy);
52941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(src), Context.VoidPtrTy);
53041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
53141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType());
53241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType());
53341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy);
53441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy);
53541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
53641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction();
5370f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Context.VoidTy, args,
5380f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      FunctionType::ExtInfo(),
5390f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      RequiredArgs::All),
54041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall               fn, ReturnValueSlot(), args);
54141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall}
54241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
5431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Determine whether the given architecture supports unaligned atomic
5441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// accesses.  They don't have to be fast, just faster than a function
5451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// call and a mutex.
5461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasUnalignedAtomics(llvm::Triple::ArchType arch) {
547de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman  // FIXME: Allow unaligned atomic load/store on x86.  (It is not
548de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman  // currently supported by the backend.)
549de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman  return 0;
5501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
5511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Return the maximum size that permits atomic accesses for the given
5531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// architecture.
5541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM,
5551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                                        llvm::Triple::ArchType arch) {
5561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // ARM has 8-byte atomic accesses, but it's not clear whether we
5571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // want to rely on them here.
5581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // In the default case, just assume that any size up to a pointer is
5601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // fine given adequate alignment.
5611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  return CharUnits::fromQuantity(CGM.PointerSizeInBytes);
5621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
5631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallnamespace {
5651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  class PropertyImplStrategy {
5661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  public:
5671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    enum StrategyKind {
5681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// The 'native' strategy is to use the architecture's provided
5691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// reads and writes.
5701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Native,
5711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// Use objc_setProperty and objc_getProperty.
5731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      GetSetProperty,
5741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// Use objc_setProperty for the setter, but use expression
5761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// evaluation for the getter.
5771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      SetPropertyAndExpressionGet,
5781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// Use objc_copyStruct.
5801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      CopyStruct,
5811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// The 'expression' strategy is to emit normal assignment or
5831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      /// lvalue-to-rvalue expressions.
5841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Expression
5851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    };
5861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    StrategyKind getKind() const { return StrategyKind(Kind); }
5881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bool hasStrongMember() const { return HasStrong; }
5901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bool isAtomic() const { return IsAtomic; }
5911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bool isCopy() const { return IsCopy; }
5921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CharUnits getIvarSize() const { return IvarSize; }
5941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CharUnits getIvarAlignment() const { return IvarAlignment; }
5951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    PropertyImplStrategy(CodeGenModule &CGM,
5971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                         const ObjCPropertyImplDecl *propImpl);
5981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
5991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  private:
6001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    unsigned Kind : 8;
6011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    unsigned IsAtomic : 1;
6021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    unsigned IsCopy : 1;
6031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    unsigned HasStrong : 1;
6041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CharUnits IvarSize;
6061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CharUnits IvarAlignment;
6071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  };
6081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
6091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
610bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru/// Pick an implementation strategy for the given property synthesis.
6111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallPropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM,
6121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                                     const ObjCPropertyImplDecl *propImpl) {
6131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
614265941bc308d65cc270d5c4de5806f37ce405606John McCall  ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind();
6151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
616265941bc308d65cc270d5c4de5806f37ce405606John McCall  IsCopy = (setterKind == ObjCPropertyDecl::Copy);
617265941bc308d65cc270d5c4de5806f37ce405606John McCall  IsAtomic = prop->isAtomic();
6181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  HasStrong = false; // doesn't matter here.
6191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Evaluate the ivar's size and alignment.
6211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
6221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  QualType ivarType = ivar->getType();
6231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  llvm::tie(IvarSize, IvarAlignment)
6241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    = CGM.getContext().getTypeInfoInChars(ivarType);
6251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If we have a copy property, we always have to use getProperty/setProperty.
627265941bc308d65cc270d5c4de5806f37ce405606John McCall  // TODO: we could actually use setProperty and an expression for non-atomics.
6281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (IsCopy) {
6291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = GetSetProperty;
6301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
6311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
6321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
633265941bc308d65cc270d5c4de5806f37ce405606John McCall  // Handle retain.
634265941bc308d65cc270d5c4de5806f37ce405606John McCall  if (setterKind == ObjCPropertyDecl::Retain) {
6351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // In GC-only, there's nothing special that needs to be done.
6364e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
6371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      // fallthrough
6381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // In ARC, if the property is non-atomic, use expression emission,
6401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // which translates to objc_storeStrong.  This isn't required, but
6411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // it's slightly nicer.
6424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) {
643d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall      // Using standard expression emission for the setter is only
644d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall      // acceptable if the ivar is __strong, which won't be true if
645d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall      // the property is annotated with __attribute__((NSObject)).
646d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall      // TODO: falling all the way back to objc_setProperty here is
647d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall      // just laziness, though;  we could still use objc_storeStrong
648d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall      // if we hacked it right.
649d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall      if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong)
650d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall        Kind = Expression;
651d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall      else
652d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall        Kind = SetPropertyAndExpressionGet;
6531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      return;
6541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Otherwise, we need to at least use setProperty.  However, if
6561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // the property isn't atomic, we can use normal expression
6571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // emission for the getter.
6581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    } else if (!IsAtomic) {
6591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Kind = SetPropertyAndExpressionGet;
6601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      return;
6611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Otherwise, we have to use both setProperty and getProperty.
6631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    } else {
6641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Kind = GetSetProperty;
6651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      return;
6661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    }
6671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
6681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If we're not atomic, just use expression accesses.
6701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!IsAtomic) {
6711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = Expression;
6721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
6731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
6741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6755889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall  // Properties on bitfield ivars need to be emitted using expression
6765889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall  // accesses even if they're nominally atomic.
6775889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall  if (ivar->isBitField()) {
6785889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall    Kind = Expression;
6795889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall    return;
6805889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall  }
6815889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall
6821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // GC-qualified or ARC-qualified ivars need to be emitted as
6831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // expressions.  This actually works out to being atomic anyway,
6841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // except for ARC __strong, but that should trigger the above code.
6851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (ivarType.hasNonTrivialObjCLifetime() ||
6864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      (CGM.getLangOpts().getGC() &&
6871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall       CGM.getContext().getObjCGCAttrKind(ivarType))) {
6881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = Expression;
6891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
6901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
6911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Compute whether the ivar has strong members.
6934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().getGC())
6941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    if (const RecordType *recordType = ivarType->getAs<RecordType>())
6951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      HasStrong = recordType->getDecl()->hasObjectMember();
6961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
6971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // We can never access structs with object members with a native
6981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // access, because we need to use write barriers.  This is what
6991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // objc_copyStruct is for.
7001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (HasStrong) {
7011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = CopyStruct;
7021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
7031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
7041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Otherwise, this is target-dependent and based on the size and
7061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // alignment of the ivar.
707c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall
708c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall  // If the size of the ivar is not a power of two, give up.  We don't
709c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall  // want to get into the business of doing compare-and-swaps.
710c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall  if (!IvarSize.isPowerOfTwo()) {
711c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall    Kind = CopyStruct;
712c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall    return;
713c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall  }
714c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall
7151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  llvm::Triple::ArchType arch =
7161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CGM.getContext().getTargetInfo().getTriple().getArch();
7171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Most architectures require memory to fit within a single cache
7191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // line, so the alignment has to be at least the size of the access.
7201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Otherwise we have to grab a lock.
7211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) {
7221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = CopyStruct;
7231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
7241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
7251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If the ivar's size exceeds the architecture's maximum atomic
7271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // access size, we have to use CopyStruct.
7281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) {
7291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Kind = CopyStruct;
7301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
7311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
7321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Otherwise, we can use native loads and stores.
7341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  Kind = Native;
7351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
736af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
7372ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property getter function.
7382ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett///
7392ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize
740489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category.
741fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
742fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                                         const ObjCPropertyImplDecl *PID) {
743b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian  llvm::Constant *AtomicHelperFn =
74420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    GenerateObjCAtomicGetterCopyHelperFunction(PID);
745af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
746af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
747af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  assert(OMD && "Invalid call to generate getter (empty method)");
748ea32047660159811a4c14d008a4b7e3807a705d6Eric Christopher  StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart());
7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
750490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian  generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn);
7511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  FinishFunction();
7531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
7541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7556c11f0b3106263600af2ea0438ebb372821514aaJohn McCallstatic bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) {
7566c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  const Expr *getter = propImpl->getGetterCXXConstructor();
7571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!getter) return true;
7581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Sema only makes only of these when the ivar has a C++ class type,
7601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // so the form is pretty constrained.
7611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7626c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  // If the property has a reference type, we might just be binding a
7636c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  // reference, in which case the result will be a gl-value.  We should
7646c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  // treat this as a non-trivial operation.
7656c11f0b3106263600af2ea0438ebb372821514aaJohn McCall  if (getter->isGLValue())
7666c11f0b3106263600af2ea0438ebb372821514aaJohn McCall    return false;
7676c11f0b3106263600af2ea0438ebb372821514aaJohn McCall
7681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If we selected a trivial copy-constructor, we're okay.
7691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter))
7701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return (construct->getConstructor()->isTrivial());
7711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
7721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // The constructor might require cleanups (in which case it's never
7731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // trivial).
7741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  assert(isa<ExprWithCleanups>(getter));
7751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  return false;
7761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall}
7771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
77820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// emitCPPObjectAtomicGetterCall - Call the runtime function to
77920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// copy the ivar into the resturn slot.
78020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianstatic void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF,
78120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                          llvm::Value *returnAddr,
78220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                          ObjCIvarDecl *ivar,
78320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                          llvm::Constant *AtomicHelperFn) {
78420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar,
78520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  //                           AtomicHelperFn);
78620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  CallArgList args;
78720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
78820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  // The 1st argument is the return Slot.
78920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy);
79020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
79120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  // The 2nd argument is the address of the ivar.
79220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  llvm::Value *ivarAddr =
79320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
79420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                        CGF.LoadObjCSelf(), ivar, 0).getAddress();
79520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
79620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
79720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
79820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  // Third argument is the helper function.
79920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
80020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
80120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  llvm::Value *copyCppAtomicObjectFn =
802d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction();
8030f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy,
8040f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      args,
8050f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      FunctionType::ExtInfo(),
8060f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      RequiredArgs::All),
80720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian               copyCppAtomicObjectFn, ReturnValueSlot(), args);
80820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian}
80920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
8101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallvoid
8111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallCodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
812b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian                                        const ObjCPropertyImplDecl *propImpl,
813490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian                                        const ObjCMethodDecl *GetterMethodDecl,
814b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian                                        llvm::Constant *AtomicHelperFn) {
8151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // If there's a non-trivial 'get' expression, we just have to emit that.
8161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!hasTrivialGetExpr(propImpl)) {
81720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    if (!AtomicHelperFn) {
81820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian      ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(),
81920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                     /*nrvo*/ 0);
82020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian      EmitReturnStmt(ret);
82120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    }
82220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    else {
82320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian      ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
82420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian      emitCPPObjectAtomicGetterCall(*this, ReturnValue,
82520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                    ivar, AtomicHelperFn);
82620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    }
8271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
8281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
8291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
8311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  QualType propType = prop->getType();
8321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl();
8331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
8351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Pick an implementation strategy.
8371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  PropertyImplStrategy strategy(CGM, propImpl);
8381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  switch (strategy.getKind()) {
8391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::Native: {
840aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman    // We don't need to do anything for a zero-size struct.
841aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman    if (strategy.getIvarSize().isZero())
842aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman      return;
843aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman
8441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
8451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Currently, all atomic accesses have to be through integer
8471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // types, so there's no point in trying to pick a prettier type.
8481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Type *bitcastType =
8491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      llvm::Type::getIntNTy(getLLVMContext(),
8501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                            getContext().toBits(strategy.getIvarSize()));
8511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
8521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Perform an atomic load.  This does not impose ordering constraints.
8541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *ivarAddr = LV.getAddress();
8551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
8561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load");
8571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    load->setAlignment(strategy.getIvarAlignment().getQuantity());
8581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    load->setAtomic(llvm::Unordered);
8591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Store that value into the return address.  Doing this with a
8611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // bitcast is likely to produce some pretty ugly IR, but it's not
8621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // the *most* terrible thing in the world.
8631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType));
8641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Make sure we don't do an autorelease.
8661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    AutoreleaseResult = false;
8671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
8681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
8691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::GetSetProperty: {
8711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *getPropertyFn =
8721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      CGM.getObjCRuntime().GetPropertyGetFunction();
8731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    if (!getPropertyFn) {
8741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy");
875c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar      return;
876c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    }
877c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar
878c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
879c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // FIXME: Can't this be simpler? This might even be worse than the
880c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // corresponding gcc code.
8811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *cmd =
8821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd");
8831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
8841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *ivarOffset =
8851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      EmitIvarOffset(classImpl->getClassInterface(), ivar);
8861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
8871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    CallArgList args;
8881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    args.add(RValue::get(self), getContext().getObjCIdType());
8891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    args.add(RValue::get(cmd), getContext().getObjCSelType());
8901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
891265941bc308d65cc270d5c4de5806f37ce405606John McCall    args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
892265941bc308d65cc270d5c4de5806f37ce405606John McCall             getContext().BoolTy);
8931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
894e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar    // FIXME: We shouldn't need to get the function info here, the
895e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar    // runtime already should have computed it to build the function.
8960f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall    RValue RV = EmitCall(getTypes().arrangeFreeFunctionCall(propType, args,
8970f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                       FunctionType::ExtInfo(),
8980f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                            RequiredArgs::All),
8991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                         getPropertyFn, ReturnValueSlot(), args);
9001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
901c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // We need to fix the type here. Ivars with copy & retain are
902c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // always objects so we don't need to worry about complex or
903c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    // aggregates.
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
90552c18b0933006acaf5c865f6e86a4694b5f288e7Fariborz Jahanian           getTypes().ConvertType(getterMethod->getResultType())));
9061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
9071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    EmitReturnOfRValue(RV, propType);
908f85e193739c953358c865005855253af4f68a497John McCall
909f85e193739c953358c865005855253af4f68a497John McCall    // objc_getProperty does an autorelease, so we should suppress ours.
910f85e193739c953358c865005855253af4f68a497John McCall    AutoreleaseResult = false;
9111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
9121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
9131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
9141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
9151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::CopyStruct:
9161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    emitStructGetterCall(*this, ivar, strategy.isAtomic(),
9171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                         strategy.hasStrongMember());
9181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
9191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
9201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::Expression:
9211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::SetPropertyAndExpressionGet: {
9221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
9231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
9241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    QualType ivarType = ivar->getType();
9259d232c884ea9872d6555df0fd7359699819bc1f1John McCall    switch (getEvaluationKind(ivarType)) {
9269d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Complex: {
9279d232c884ea9872d6555df0fd7359699819bc1f1John McCall      ComplexPairTy pair = EmitLoadOfComplex(LV);
9289d232c884ea9872d6555df0fd7359699819bc1f1John McCall      EmitStoreOfComplex(pair,
9299d232c884ea9872d6555df0fd7359699819bc1f1John McCall                         MakeNaturalAlignAddrLValue(ReturnValue, ivarType),
9309d232c884ea9872d6555df0fd7359699819bc1f1John McCall                         /*init*/ true);
9319d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return;
9329d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
9339d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Aggregate:
9341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      // The return value slot is guaranteed to not be aliased, but
9351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      // that's not necessarily the same as "on the stack", so
9361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      // we still potentially need objc_memmove_collectable.
937649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType);
9389d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return;
9399d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Scalar: {
940ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      llvm::Value *value;
941ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      if (propType->isReferenceType()) {
942ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        value = LV.getAddress();
943ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      } else {
944ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        // We want to load and autoreleaseReturnValue ARC __weak ivars.
945ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
9461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall          value = emitARCRetainLoadOfScalar(*this, LV, ivarType);
947ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall
948ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        // Otherwise we want to do a simple load, suppressing the
949ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        // final autorelease.
950f85e193739c953358c865005855253af4f68a497John McCall        } else {
951ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall          value = EmitLoadOfLValue(LV).getScalarVal();
952ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall          AutoreleaseResult = false;
95314086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian        }
954f85e193739c953358c865005855253af4f68a497John McCall
955ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall        value = Builder.CreateBitCast(value, ConvertType(propType));
956490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian        value = Builder.CreateBitCast(value,
957490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian                  ConvertType(GetterMethodDecl->getResultType()));
958ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      }
959ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall
960ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall      EmitReturnOfRValue(RValue::get(value), propType);
9619d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return;
962ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian    }
9639d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
9649d232c884ea9872d6555df0fd7359699819bc1f1John McCall    llvm_unreachable("bad evaluation kind");
965c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  }
966af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
9671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
9681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  llvm_unreachable("bad @property implementation strategy!");
969af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar}
970af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
97141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructSetterCall - Call the runtime function to store the value
97241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// from the first formal parameter into the given ivar.
97341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD,
97441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                                 ObjCIvarDecl *ivar) {
9752846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  // objc_copyStruct (&structIvar, &Arg,
9762846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  //                  sizeof (struct something), true, false);
977bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  CallArgList args;
978bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
979bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  // The first argument is the address of the ivar.
98041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
98141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall                                                CGF.LoadObjCSelf(), ivar, 0)
98241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall    .getAddress();
98341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
98441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
985bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
986bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  // The second argument is the address of the parameter variable.
98741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  ParmVarDecl *argVar = *OMD->param_begin();
988f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(),
989c3953aab1b6167b7f394ca87096e91ab840026adFariborz Jahanian                     VK_LValue, SourceLocation());
99041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress();
99141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
99241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
993bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
994bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  // The third argument is the sizeof the type.
995bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall  llvm::Value *size =
99641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall    CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType()));
99741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(size), CGF.getContext().getSizeType());
99841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall
99941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  // The fourth argument is the 'isAtomic' flag.
100041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy);
1001bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
100241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  // The fifth argument is the 'hasStrong' flag.
100341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  // FIXME: should this really always be false?
100441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy);
1005bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall
100641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall  llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction();
10070f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy,
10080f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      args,
10090f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      FunctionType::ExtInfo(),
10100f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      RequiredArgs::All),
101141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall               copyStructFn, ReturnValueSlot(), args);
10122846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian}
10132846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian
1014cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// emitCPPObjectAtomicSetterCall - Call the runtime function to store
1015cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the value from the first formal parameter into the given ivar, using
1016cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the Cpp API for atomic Cpp objects with non-trivial copy assignment.
1017cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanianstatic void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF,
1018cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                                          ObjCMethodDecl *OMD,
1019cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                                          ObjCIvarDecl *ivar,
1020cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                                          llvm::Constant *AtomicHelperFn) {
1021cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg,
1022cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  //                           AtomicHelperFn);
1023cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  CallArgList args;
1024cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian
1025cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  // The first argument is the address of the ivar.
1026cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  llvm::Value *ivarAddr =
1027cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian    CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
1028cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                          CGF.LoadObjCSelf(), ivar, 0).getAddress();
1029cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1030cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1031cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian
1032cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  // The second argument is the address of the parameter variable.
1033cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  ParmVarDecl *argVar = *OMD->param_begin();
1034f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(),
1035cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                     VK_LValue, SourceLocation());
1036cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress();
1037cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
1038cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1039cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian
1040cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  // Third argument is the helper function.
1041cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
1042cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian
1043cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  llvm::Value *copyCppAtomicObjectFn =
1044d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction();
10450f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy,
10460f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      args,
10470f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      FunctionType::ExtInfo(),
10480f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                      RequiredArgs::All),
1049cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian               copyCppAtomicObjectFn, ReturnValueSlot(), args);
1050cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian
1051cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian
1052cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian}
1053cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian
105420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
10551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) {
10561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  Expr *setter = PID->getSetterCXXAssignment();
10571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!setter) return true;
10581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
10591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // Sema only makes only of these when the ivar has a C++ class type,
10601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // so the form is pretty constrained.
106171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
106271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // An operator call is trivial if the function it calls is trivial.
10631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // This also implies that there's nothing non-trivial going on with
10641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // the arguments, because operator= can only be trivial if it's a
10651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // synthesized assignment operator and therefore both parameters are
10661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  // references.
10671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (CallExpr *call = dyn_cast<CallExpr>(setter)) {
106871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    if (const FunctionDecl *callee
106971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall          = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl()))
107071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      if (callee->isTrivial())
107171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall        return true;
107271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    return false;
107301cb307f0b84a368cdbc0738d6680aab8ed7423fFariborz Jahanian  }
107471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
10751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  assert(isa<ExprWithCleanups>(setter));
107671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  return false;
107771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall}
107886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar
10794e494cf56325f26a6c3eb5a065c35e54835f14e6Benjamin Kramerstatic bool UseOptimizedSetter(CodeGenModule &CGM) {
10804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
1081ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return false;
10827a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar  return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter();
1083ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
1084ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
108571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid
108671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallCodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1087cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                                        const ObjCPropertyImplDecl *propImpl,
1088cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                                        llvm::Constant *AtomicHelperFn) {
1089cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
1090cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1091cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl();
1092cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian
109371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // Just use the setter expression if Sema gave us one and it's
1094cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  // non-trivial.
10951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  if (!hasTrivialSetExpr(propImpl)) {
1096cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian    if (!AtomicHelperFn)
1097cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian      // If non-atomic, assignment is called directly.
1098cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian      EmitStmt(propImpl->getSetterCXXAssignment());
1099cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian    else
1100cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian      // If atomic, assignment is called via a locking api.
1101cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian      emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar,
1102cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                                    AtomicHelperFn);
110371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    return;
110471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  }
110571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
11061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  PropertyImplStrategy strategy(CGM, propImpl);
11071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  switch (strategy.getKind()) {
11081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::Native: {
1109aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman    // We don't need to do anything for a zero-size struct.
1110aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman    if (strategy.getIvarSize().isZero())
1111aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman      return;
1112aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman
11131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()];
111471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
11151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    LValue ivarLValue =
11161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0);
11171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *ivarAddr = ivarLValue.getAddress();
11181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
11191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Currently, all atomic accesses have to be through integer
11201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // types, so there's no point in trying to pick a prettier type.
11211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Type *bitcastType =
11221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall      llvm::Type::getIntNTy(getLLVMContext(),
11231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall                            getContext().toBits(strategy.getIvarSize()));
11241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
11251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
11261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Cast both arguments to the chosen operation type.
11271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    argAddr = Builder.CreateBitCast(argAddr, bitcastType);
11281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
112971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
11301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // This bitcast load is likely to cause some nasty IR.
11311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::Value *load = Builder.CreateLoad(argAddr);
11321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
11331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    // Perform an atomic store.  There are no memory ordering requirements.
11341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr);
11351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    store->setAlignment(strategy.getIvarAlignment().getQuantity());
11361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    store->setAtomic(llvm::Unordered);
11371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    return;
11381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  }
11391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
11401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::GetSetProperty:
11411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::SetPropertyAndExpressionGet: {
1142ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
1143ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    llvm::Value *setOptimizedPropertyFn = 0;
1144ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    llvm::Value *setPropertyFn = 0;
1145ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (UseOptimizedSetter(CGM)) {
11467a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar      // 10.8 and iOS 6.0 code and GC is off
1147ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      setOptimizedPropertyFn =
114816098f366097305c348b356a44638d6c959c6e60Eric Christopher        CGM.getObjCRuntime()
114916098f366097305c348b356a44638d6c959c6e60Eric Christopher           .GetOptimizedPropertySetFunction(strategy.isAtomic(),
115016098f366097305c348b356a44638d6c959c6e60Eric Christopher                                            strategy.isCopy());
1151ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      if (!setOptimizedPropertyFn) {
1152ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI");
1153ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        return;
1154ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      }
115586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    }
1156ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    else {
1157ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction();
1158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      if (!setPropertyFn) {
1159ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy");
1160ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        return;
1161ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      }
1162ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
1163ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
11641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Emit objc_setProperty((id) self, _cmd, offset, arg,
116586957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar    //                       <is-atomic>, <is-copy>).
116671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    llvm::Value *cmd =
116771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]);
116871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    llvm::Value *self =
116971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
117071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    llvm::Value *ivarOffset =
117171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      EmitIvarOffset(classImpl->getClassInterface(), ivar);
117271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()];
117371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy);
117471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
117571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    CallArgList args;
117671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    args.add(RValue::get(self), getContext().getObjCIdType());
117771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    args.add(RValue::get(cmd), getContext().getObjCSelType());
1178ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (setOptimizedPropertyFn) {
1179ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      args.add(RValue::get(arg), getContext().getObjCIdType());
1180ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
11810f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall      EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args,
11820f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                  FunctionType::ExtInfo(),
11830f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                  RequiredArgs::All),
1184ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek               setOptimizedPropertyFn, ReturnValueSlot(), args);
1185ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    } else {
1186ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1187ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      args.add(RValue::get(arg), getContext().getObjCIdType());
1188ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
1189ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek               getContext().BoolTy);
1190ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      args.add(RValue::get(Builder.getInt1(strategy.isCopy())),
1191ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek               getContext().BoolTy);
1192ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // FIXME: We shouldn't need to get the function info here, the runtime
1193ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // already should have computed it to build the function.
11940f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall      EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args,
11950f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                  FunctionType::ExtInfo(),
11960f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                  RequiredArgs::All),
1197ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek               setPropertyFn, ReturnValueSlot(), args);
1198ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
1199ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
120071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    return;
120171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  }
120271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
12031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::CopyStruct:
120441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall    emitStructSetterCall(*this, setterMethod, ivar);
120571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    return;
12061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall
12071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  case PropertyImplStrategy::Expression:
12081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall    break;
120971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  }
121071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
121171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // Otherwise, fake up some ASTs and emit a normal assignment.
121271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ValueDecl *selfDecl = setterMethod->getSelfDecl();
1213f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  DeclRefExpr self(selfDecl, false, selfDecl->getType(),
1214f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                   VK_LValue, SourceLocation());
121571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack,
121671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                            selfDecl->getType(), CK_LValueToRValue, &self,
121771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                            VK_RValue);
121871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(),
12190c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian                          SourceLocation(), SourceLocation(),
12200c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian                          &selfLoad, true, true);
122171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
122271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ParmVarDecl *argDecl = *setterMethod->param_begin();
122371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  QualType argType = argDecl->getType().getNonReferenceType();
1224f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation());
122571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack,
122671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                           argType.getUnqualifiedType(), CK_LValueToRValue,
122771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                           &arg, VK_RValue);
122845e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar
122971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // The property type can differ from the ivar type in some situations with
123071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // Objective-C pointer types, we can always bit cast the RHS in these cases.
123171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  // The following absurdity is just to ensure well-formed IR.
123271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  CastKind argCK = CK_NoOp;
123371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  if (ivarRef.getType()->isObjCObjectPointerType()) {
123471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    if (argLoad.getType()->isObjCObjectPointerType())
123571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_BitCast;
123671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    else if (argLoad.getType()->isBlockPointerType())
123771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_BlockPointerToObjCPointerCast;
123871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    else
123971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_CPointerToObjCPointerCast;
124071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  } else if (ivarRef.getType()->isBlockPointerType()) {
124171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall     if (argLoad.getType()->isBlockPointerType())
124271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_BitCast;
124371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    else
124471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall      argCK = CK_AnyPointerToBlockPointerCast;
124571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  } else if (ivarRef.getType()->isPointerType()) {
124671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    argCK = CK_BitCast;
124786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar  }
124871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ImplicitCastExpr argCast(ImplicitCastExpr::OnStack,
124971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                           ivarRef.getType(), argCK, &argLoad,
125071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                           VK_RValue);
125171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  Expr *finalArg = &argLoad;
125271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  if (!getContext().hasSameUnqualifiedType(ivarRef.getType(),
125371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                                           argLoad.getType()))
125471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall    finalArg = &argCast;
125571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
125671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
125771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  BinaryOperator assign(&ivarRef, finalArg, BO_Assign,
125871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                        ivarRef.getType(), VK_RValue, OK_Ordinary,
1259be9af1288881110e406b87914162eaa59f1e5918Lang Hames                        SourceLocation(), false);
126071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  EmitStmt(&assign);
126171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall}
126271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
12632ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property setter function.
12642ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett///
12652ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize
126671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall/// is illegal within a category.
126771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
126871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall                                         const ObjCPropertyImplDecl *PID) {
1269b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian  llvm::Constant *AtomicHelperFn =
127020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    GenerateObjCAtomicSetterCopyHelperFunction(PID);
127171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
127271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
127371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  assert(OMD && "Invalid call to generate setter (empty method)");
1274ea32047660159811a4c14d008a4b7e3807a705d6Eric Christopher  StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart());
127571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall
1276cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  generateObjCSetterBody(IMP, PID, AtomicHelperFn);
1277af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1278af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  FinishFunction();
12794111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
12804111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
1281e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallnamespace {
12829928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  struct DestroyIvar : EHScopeStack::Cleanup {
12839928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  private:
12849928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    llvm::Value *addr;
1285e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    const ObjCIvarDecl *ivar;
1286516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne    CodeGenFunction::Destroyer *destroyer;
12879928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    bool useEHCleanupForArray;
12889928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  public:
12899928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
12909928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                CodeGenFunction::Destroyer *destroyer,
12919928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                bool useEHCleanupForArray)
1292516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne      : addr(addr), ivar(ivar), destroyer(destroyer),
12939928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall        useEHCleanupForArray(useEHCleanupForArray) {}
1294e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1295ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
12969928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      LValue lvalue
12979928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall        = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
12989928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer,
1299ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall                      flags.isForNormalCleanup() && useEHCleanupForArray);
1300e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    }
1301e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  };
1302e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall}
1303e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
13049928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall/// Like CodeGenFunction::destroyARCStrong, but do it with a call.
13059928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCallstatic void destroyARCStrongWithStore(CodeGenFunction &CGF,
13069928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                                      llvm::Value *addr,
13079928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                                      QualType type) {
13089928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  llvm::Value *null = getNullForVariable(addr);
13099928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
13109928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall}
1311f85e193739c953358c865005855253af4f68a497John McCall
1312e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallstatic void emitCXXDestructMethod(CodeGenFunction &CGF,
1313e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall                                  ObjCImplementationDecl *impl) {
1314e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  CodeGenFunction::RunCleanupsScope scope(CGF);
1315e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1316e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  llvm::Value *self = CGF.LoadObjCSelf();
1317e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1318db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  const ObjCInterfaceDecl *iface = impl->getClassInterface();
1319db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
1320e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall       ivar; ivar = ivar->getNextIvar()) {
1321e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    QualType type = ivar->getType();
1322e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1323e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    // Check whether the ivar is a destructible type.
13249928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    QualType::DestructionKind dtorKind = type.isDestructedType();
13259928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    if (!dtorKind) continue;
13269928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
13279928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    CodeGenFunction::Destroyer *destroyer = 0;
13289928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
13299928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    // Use a call to objc_storeStrong to destroy strong ivars, for the
13309928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    // general benefit of the tools.
13319928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    if (dtorKind == QualType::DK_objc_strong_lifetime) {
1332516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne      destroyer = destroyARCStrongWithStore;
13339928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
13349928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    // Otherwise use the default for the destruction kind.
13359928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    } else {
1336516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne      destroyer = CGF.getDestroyer(dtorKind);
1337e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    }
13389928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
13399928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
13409928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
13419928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
13429928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                                         cleanupKind & EHCleanup);
1343e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  }
1344e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1345e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
1346e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall}
1347e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1348109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1349109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                 ObjCMethodDecl *MD,
1350109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                                 bool ctor) {
1351109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
13528d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel  StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart());
1353e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1354e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  // Emit .cxx_construct.
1355109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  if (ctor) {
1356f85e193739c953358c865005855253af4f68a497John McCall    // Suppress the final autorelease in ARC.
1357f85e193739c953358c865005855253af4f68a497John McCall    AutoreleaseResult = false;
1358f85e193739c953358c865005855253af4f68a497John McCall
13595f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
1360e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
1361e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall           E = IMP->init_end(); B != E; ++B) {
1362e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall      CXXCtorInitializer *IvarInit = (*B);
136300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      FieldDecl *Field = IvarInit->getAnyMember();
1364109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      ObjCIvarDecl  *Ivar = cast<ObjCIvarDecl>(Field);
13659b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian      LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
13669b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian                                    LoadObjCSelf(), Ivar, 0);
13677c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall      EmitAggExpr(IvarInit->getInit(),
13687c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall                  AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed,
1369410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall                                          AggValueSlot::DoesNotNeedGCBarriers,
1370649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                          AggValueSlot::IsNotAliased));
1371109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    }
1372109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    // constructor returns 'self'.
1373109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    CodeGenTypes &Types = CGM.getTypes();
1374109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    QualType IdTy(CGM.getContext().getObjCIdType());
1375109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    llvm::Value *SelfAsId =
1376109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian      Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
1377109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian    EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
1378e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall
1379e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall  // Emit .cxx_destruct.
1380bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth  } else {
1381e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall    emitCXXDestructMethod(*this, IMP);
1382109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  }
1383109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  FinishFunction();
1384109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian}
1385109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian
13860b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanianbool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
13870b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
13880b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  it++; it++;
13890b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  const ABIArgInfo &AI = it->info;
13900b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  // FIXME. Is this sufficient check?
13910b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  return (AI.getKind() == ABIArgInfo::Indirect);
13920b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian}
13930b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian
139415bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanianbool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
13954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().getGC() == LangOptions::NonGC)
139615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    return false;
139715bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
139815bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian    return FDTTy->getDecl()->hasObjectMember();
139915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  return false;
140015bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian}
140115bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian
1402c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbarllvm::Value *CodeGenFunction::LoadObjCSelf() {
1403b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
1404b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
14054111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
14064111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
140745012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz JahanianQualType CodeGenFunction::TypeOfSelfObject() {
140845012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
140945012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
141014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
141114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    getContext().getCanonicalType(selfDecl->getType()));
141245012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  return PTy->getPointeeType();
141345012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian}
141445012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian
141574391b48b4791cded373683a3baf67314f358d50Chris Lattnervoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
14161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Constant *EnumerationMutationFn =
1417c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CGM.getObjCRuntime().EnumerationMutationFunction();
14181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1419c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  if (!EnumerationMutationFn) {
1420c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
1421c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar    return;
1422c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar  }
1423c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar
1424bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel  CGDebugInfo *DI = getDebugInfo();
142573fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
142673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
1427bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel
14289d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel  // The local variable comes into scope immediately.
14299d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel  AutoVarEmission variable = AutoVarEmission::invalid();
14309d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
14319d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel    variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
14329d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel
1433d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1435f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // Fast enumeration state.
14360815b579b31cb3129f732bb7ea36fd6ba6949e98Douglas Gregor  QualType StateTy = CGM.getObjCFastEnumerationStateType();
1437195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
14381884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  EmitNullInitialization(StatePtr, StateTy);
14391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1440f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // Number of elements in the items array.
14412abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  static const unsigned NumItems = 16;
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1443d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the countByEnumeratingWithState:objects:count: selector.
1444ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  IdentifierInfo *II[] = {
1445ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("countByEnumeratingWithState"),
1446ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("objects"),
1447ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    &CGM.getContext().Idents.get("count")
1448ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  };
1449ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer  Selector FastEnumSel =
1450ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer    CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
1451f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1452f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  QualType ItemsTy =
1453f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson    getContext().getConstantArrayType(getContext().getObjCIdType(),
14541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                      llvm::APInt(32, NumItems),
1455f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                      ArrayType::Normal, 0);
1456195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
14571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1458990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  // Emit the collection pointer.  In ARC, we do a retain.
1459990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  llvm::Value *Collection;
14604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
1461990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    Collection = EmitARCRetainScalarExpr(S.getCollection());
1462990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall
1463990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    // Enter a cleanup to do the release.
1464990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
1465990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  } else {
1466990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    Collection = EmitScalarExpr(S.getCollection());
1467990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  }
14681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14694b302d32378b364703b212834f908762e570c29cJohn McCall  // The 'continue' label needs to appear within the cleanup for the
14704b302d32378b364703b212834f908762e570c29cJohn McCall  // collection object.
14714b302d32378b364703b212834f908762e570c29cJohn McCall  JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
14724b302d32378b364703b212834f908762e570c29cJohn McCall
1473d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Send it our message:
1474f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  CallArgList Args;
1475d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1476d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The first argument is a temporary of the enumeration-state type.
147704c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy));
14781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1479d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The second argument is a temporary array with space for NumItems
1480d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // pointers.  We'll actually be loading elements from the array
1481d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // pointer written into the control state; this buffer is so that
1482d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // collections that *aren't* backed by arrays can still queue up
1483d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // batches of elements.
148404c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy));
14851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1486d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The third argument is the capacity of that temporary array.
14872acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
14884a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
148904c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  Args.add(RValue::get(Count), getContext().UnsignedLongTy);
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1491d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Start the enumeration.
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  RValue CountRV =
1493ef072fd2f3347cfd857d6eb787b245b950771430John McCall    CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1494f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             getContext().UnsignedLongTy,
1495f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             FastEnumSel,
1496c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                                             Collection, Args);
1497f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1498d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The initial number of objects that were returned in the buffer.
1499d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *initialBufferLimit = CountRV.getScalarVal();
15001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1501d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
1502d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
1503f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1504d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
1505f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1506d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If the limit pointer was zero to begin with, the collection is
1507d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // empty; skip all this.
1508d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
1509d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       EmptyBB, LoopInitBB);
15101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1511d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, initialize the loop.
1512d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(LoopInitBB);
15131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1514d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Save the initial mutations value.  This is the value at an
1515d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // address that was written into the state object by
1516d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // countByEnumeratingWithState:objects:count:.
15171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *StateMutationsPtrPtr =
15182abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson    Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
15202abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson                                                      "mutationsptr");
15211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1522d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *initialMutations =
1523d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
15241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1525d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Start looping.  This is the point we return to whenever we have a
1526d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // fresh, non-empty batch of objects.
1527d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
1528d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(LoopBodyBB);
15291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1530d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The current index into the buffer.
1531bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
1532d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(zero, LoopInitBB);
1533f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1534d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // The current buffer size.
1535bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
1536d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(initialBufferLimit, LoopInitBB);
1537f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1538d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Check whether the mutations value has changed from where it was
1539d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // at start.  StateMutationsPtr should actually be invariant between
1540d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // refreshes.
15412abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1542d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *currentMutations
1543d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    = Builder.CreateLoad(StateMutationsPtr, "statemutations");
15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1545d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
1546361cf980a7d976ef11a37b49567412b6b63a89d7Dan Gohman  llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1548d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
1549d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       WasNotMutatedBB, WasMutatedBB);
15501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1551d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If so, call the enumeration-mutation function.
1552d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(WasMutatedBB);
15532abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson  llvm::Value *V =
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Builder.CreateBitCast(Collection,
1555578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer                          ConvertType(getContext().getObjCIdType()));
15562b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar  CallArgList Args2;
155704c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  Args2.add(RValue::get(V), getContext().getObjCIdType());
1558f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We shouldn't need to get the function info here, the runtime already
1559f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // should have computed it to build the function.
15600f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  EmitCall(CGM.getTypes().arrangeFreeFunctionCall(getContext().VoidTy, Args2,
15610f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                  FunctionType::ExtInfo(),
15620f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                                  RequiredArgs::All),
1563f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson           EnumerationMutationFn, ReturnValueSlot(), Args2);
15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1565d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, or if the mutation function returns, just continue.
1566d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(WasNotMutatedBB);
15671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1568d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Initialize the element variable.
1569d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  RunCleanupsScope elementVariableScope(*this);
157057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  bool elementIsVariable;
1571d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  LValue elementLValue;
1572d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  QualType elementType;
1573d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
157457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    // Initialize the variable, in case it's a __block variable or something.
157557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    EmitAutoVarInit(variable);
1576f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
157757b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
1578f4b88a45902af1802a1cb42ba48b1c474474f228John McCall    DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(),
1579d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                        VK_LValue, SourceLocation());
1580d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(&tempDRE);
1581d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementType = D->getType();
158257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    elementIsVariable = true;
15837acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall
15847acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall    if (D->isARCPseudoStrong())
15857acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall      elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone);
1586d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  } else {
1587d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = LValue(); // suppress warning
1588d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementType = cast<Expr>(S.getElement())->getType();
158957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    elementIsVariable = false;
1590d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  }
15912acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *convertedElementType = ConvertType(elementType);
1592f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1593d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the buffer out of the enumeration state.
1594d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // TODO: this pointer should actually be invariant between
1595d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // refreshes, which would help us do certain loop optimizations.
1596d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *StateItemsPtr =
1597d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
1598d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *EnumStateItems =
1599d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateLoad(StateItemsPtr, "stateitems");
1600f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1601d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Fetch the value at the current index from the buffer.
16021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value *CurrentItemPtr =
1603d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
1604d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
16051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1606d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Cast that value to the right type.
1607d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
1608d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                                      "currentitem");
16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1610d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Make sure we have an l-value.  Yes, this gets evaluated every
1611d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // time through the loop.
16127acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall  if (!elementIsVariable) {
1613d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1614545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
16157acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall  } else {
16167acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall    EmitScalarInit(CurrentItem, elementLValue);
16177acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall  }
16181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
161957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  // If we do have an element variable, this assignment is the end of
162057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  // its initialization.
162157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  if (elementIsVariable)
162257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    EmitAutoVarCleanups(variable);
162357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
1624d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Perform the loop body, setting up break and continue labels.
1625e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
1626d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  {
1627d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    RunCleanupsScope Scope(*this);
1628d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    EmitStmt(S.getBody());
1629d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  }
1630f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  BreakContinueStack.pop_back();
16311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1632d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Destroy the element variable now.
1633d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  elementVariableScope.ForceCleanup();
1634d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1635d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Check whether there are more elements.
1636ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(AfterBody.getBlock());
16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1638d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
1639d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1640d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // First we check in the local buffer.
1641d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *indexPlusOne
1642d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
1643d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1644d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If we haven't overrun the buffer yet, we can continue.
1645d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
1646d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       LoopBodyBB, FetchMoreBB);
1647f0906c4edb37b20141428ca77fa7dfd00b976eafFariborz Jahanian
1648d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(indexPlusOne, AfterBody.getBlock());
1649d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(count, AfterBody.getBlock());
1650f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1651d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // Otherwise, we have to fetch more elements.
1652d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(FetchMoreBB);
16531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CountRV =
1655ef072fd2f3347cfd857d6eb787b245b950771430John McCall    CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1656f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson                                             getContext().UnsignedLongTy,
16571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             FastEnumSel,
1658c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                                             Collection, Args);
16591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1660d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // If we got a zero count, we're done.
1661d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  llvm::Value *refetchCount = CountRV.getScalarVal();
1662d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1663d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  // (note that the message send might split FetchMoreBB)
1664d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  index->addIncoming(zero, Builder.GetInsertBlock());
1665d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  count->addIncoming(refetchCount, Builder.GetInsertBlock());
1666d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall
1667d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1668d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall                       EmptyBB, LoopBodyBB);
16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1670f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  // No more elements.
1671d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall  EmitBlock(EmptyBB);
1672f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
167357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall  if (!elementIsVariable) {
1674f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson    // If the element was not a declaration, set it to be null.
1675f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
1676d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1677d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1678545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    EmitStoreThroughLValue(RValue::get(null), elementLValue);
1679f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson  }
1680f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson
168173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
168273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
1683bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel
1684990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall  // Leave the cleanup we entered in ARC.
16854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1686990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall    PopCleanupBlock();
1687990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall
1688ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(LoopEnd.getBlock());
16893d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
16903d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
16911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
1692f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGM.getObjCRuntime().EmitTryStmt(*this, S);
169364d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
169464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
16951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
169664d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  CGM.getObjCRuntime().EmitThrowStmt(*this, S);
169764d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
169864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
169910cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattnervoid CodeGenFunction::EmitObjCAtSynchronizedStmt(
17001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              const ObjCAtSynchronizedStmt &S) {
1701f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
170210cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner}
170310cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner
170433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCProduceObject.  Just does a
1705f85e193739c953358c865005855253af4f68a497John McCall/// primitive retain.
1706f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type,
1707f85e193739c953358c865005855253af4f68a497John McCall                                                    llvm::Value *value) {
1708f85e193739c953358c865005855253af4f68a497John McCall  return EmitARCRetain(type, value);
1709f85e193739c953358c865005855253af4f68a497John McCall}
1710f85e193739c953358c865005855253af4f68a497John McCall
1711f85e193739c953358c865005855253af4f68a497John McCallnamespace {
1712f85e193739c953358c865005855253af4f68a497John McCall  struct CallObjCRelease : EHScopeStack::Cleanup {
1713bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall    CallObjCRelease(llvm::Value *object) : object(object) {}
1714bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall    llvm::Value *object;
1715f85e193739c953358c865005855253af4f68a497John McCall
1716ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
17175b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall      // Releases at the end of the full-expression are imprecise.
17185b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall      CGF.EmitARCRelease(object, ARCImpreciseLifetime);
1719f85e193739c953358c865005855253af4f68a497John McCall    }
1720f85e193739c953358c865005855253af4f68a497John McCall  };
1721f85e193739c953358c865005855253af4f68a497John McCall}
1722f85e193739c953358c865005855253af4f68a497John McCall
172333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCConsumeObject.  Does a primitive
1724f85e193739c953358c865005855253af4f68a497John McCall/// release at the end of the full-expression.
1725f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type,
1726f85e193739c953358c865005855253af4f68a497John McCall                                                    llvm::Value *object) {
1727f85e193739c953358c865005855253af4f68a497John McCall  // If we're in a conditional branch, we need to make the cleanup
1728bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall  // conditional.
1729bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall  pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object);
1730f85e193739c953358c865005855253af4f68a497John McCall  return object;
1731f85e193739c953358c865005855253af4f68a497John McCall}
1732f85e193739c953358c865005855253af4f68a497John McCall
1733f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type,
1734f85e193739c953358c865005855253af4f68a497John McCall                                                           llvm::Value *value) {
1735f85e193739c953358c865005855253af4f68a497John McCall  return EmitARCRetainAutorelease(type, value);
1736f85e193739c953358c865005855253af4f68a497John McCall}
1737f85e193739c953358c865005855253af4f68a497John McCall
1738b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// Given a number of pointers, inform the optimizer that they're
1739b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// being intrinsically used up until this point in the program.
1740b6a6079449a5275c283982e19b0c38e165833bb2John McCallvoid CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) {
1741b6a6079449a5275c283982e19b0c38e165833bb2John McCall  llvm::Constant *&fn = CGM.getARCEntrypoints().clang_arc_use;
1742b6a6079449a5275c283982e19b0c38e165833bb2John McCall  if (!fn) {
1743b6a6079449a5275c283982e19b0c38e165833bb2John McCall    llvm::FunctionType *fnType =
1744b6a6079449a5275c283982e19b0c38e165833bb2John McCall      llvm::FunctionType::get(CGM.VoidTy, ArrayRef<llvm::Type*>(), true);
1745b6a6079449a5275c283982e19b0c38e165833bb2John McCall    fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use");
1746b6a6079449a5275c283982e19b0c38e165833bb2John McCall  }
1747b6a6079449a5275c283982e19b0c38e165833bb2John McCall
1748b6a6079449a5275c283982e19b0c38e165833bb2John McCall  // This isn't really a "runtime" function, but as an intrinsic it
1749b6a6079449a5275c283982e19b0c38e165833bb2John McCall  // doesn't really matter as long as we align things up.
1750b6a6079449a5275c283982e19b0c38e165833bb2John McCall  EmitNounwindRuntimeCall(fn, values);
1751b6a6079449a5275c283982e19b0c38e165833bb2John McCall}
1752b6a6079449a5275c283982e19b0c38e165833bb2John McCall
1753f85e193739c953358c865005855253af4f68a497John McCall
1754f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM,
17552acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                                llvm::FunctionType *type,
17565f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                                StringRef fnName) {
1757f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName);
1758f85e193739c953358c865005855253af4f68a497John McCall
1759554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman  if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) {
1760cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman    // If the target runtime doesn't naturally support ARC, emit weak
1761cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman    // references to the runtime support library.  We don't really
1762cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman    // permit this to fail, but we need a particular relocation style.
1763554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman    if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
1764f85e193739c953358c865005855253af4f68a497John McCall      f->setLinkage(llvm::Function::ExternalWeakLinkage);
1765554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman    } else if (fnName == "objc_retain" || fnName  == "objc_release") {
1766554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman      // If we have Native ARC, set nonlazybind attribute for these APIs for
1767554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman      // performance.
176872390b39c545426023ec104afe8706395d732badBill Wendling      f->addFnAttr(llvm::Attribute::NonLazyBind);
1769db99e8b72ae2b66d3c2f8d896f49513b8d8e2633Michael Gottesman    }
1770554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman  }
1771f85e193739c953358c865005855253af4f68a497John McCall
1772f85e193739c953358c865005855253af4f68a497John McCall  return fn;
1773f85e193739c953358c865005855253af4f68a497John McCall}
1774f85e193739c953358c865005855253af4f68a497John McCall
1775f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the signature
1776f85e193739c953358c865005855253af4f68a497John McCall///   i8* (i8*)
1777f85e193739c953358c865005855253af4f68a497John McCall/// where a null input causes a no-op and returns null.
1778f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCValueOperation(CodeGenFunction &CGF,
1779f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Value *value,
1780f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Constant *&fn,
1781df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier                                          StringRef fnName,
1782df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier                                          bool isTailCall = false) {
1783f85e193739c953358c865005855253af4f68a497John McCall  if (isa<llvm::ConstantPointerNull>(value)) return value;
1784f85e193739c953358c865005855253af4f68a497John McCall
1785f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
17862acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
178776ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer      llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false);
1788f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1789f85e193739c953358c865005855253af4f68a497John McCall  }
1790f85e193739c953358c865005855253af4f68a497John McCall
1791f85e193739c953358c865005855253af4f68a497John McCall  // Cast the argument to 'id'.
17922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *origType = value->getType();
1793f85e193739c953358c865005855253af4f68a497John McCall  value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
1794f85e193739c953358c865005855253af4f68a497John McCall
1795f85e193739c953358c865005855253af4f68a497John McCall  // Call the function.
1796bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value);
1797df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier  if (isTailCall)
1798df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier    call->setTailCall();
1799f85e193739c953358c865005855253af4f68a497John McCall
1800f85e193739c953358c865005855253af4f68a497John McCall  // Cast the result back to the original type.
1801f85e193739c953358c865005855253af4f68a497John McCall  return CGF.Builder.CreateBitCast(call, origType);
1802f85e193739c953358c865005855253af4f68a497John McCall}
1803f85e193739c953358c865005855253af4f68a497John McCall
1804f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature:
1805f85e193739c953358c865005855253af4f68a497John McCall///   i8* (i8**)
1806f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF,
1807f85e193739c953358c865005855253af4f68a497John McCall                                         llvm::Value *addr,
1808f85e193739c953358c865005855253af4f68a497John McCall                                         llvm::Constant *&fn,
18095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                         StringRef fnName) {
1810f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
18112acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
181276ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer      llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false);
1813f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1814f85e193739c953358c865005855253af4f68a497John McCall  }
1815f85e193739c953358c865005855253af4f68a497John McCall
1816f85e193739c953358c865005855253af4f68a497John McCall  // Cast the argument to 'id*'.
18172acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *origType = addr->getType();
1818f85e193739c953358c865005855253af4f68a497John McCall  addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
1819f85e193739c953358c865005855253af4f68a497John McCall
1820f85e193739c953358c865005855253af4f68a497John McCall  // Call the function.
1821bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr);
1822f85e193739c953358c865005855253af4f68a497John McCall
1823f85e193739c953358c865005855253af4f68a497John McCall  // Cast the result back to a dereference of the original type.
1824f85e193739c953358c865005855253af4f68a497John McCall  if (origType != CGF.Int8PtrPtrTy)
1825f85e193739c953358c865005855253af4f68a497John McCall    result = CGF.Builder.CreateBitCast(result,
1826f85e193739c953358c865005855253af4f68a497John McCall                        cast<llvm::PointerType>(origType)->getElementType());
1827f85e193739c953358c865005855253af4f68a497John McCall
1828f85e193739c953358c865005855253af4f68a497John McCall  return result;
1829f85e193739c953358c865005855253af4f68a497John McCall}
1830f85e193739c953358c865005855253af4f68a497John McCall
1831f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature:
1832f85e193739c953358c865005855253af4f68a497John McCall///   i8* (i8**, i8*)
1833f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF,
1834f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Value *addr,
1835f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Value *value,
1836f85e193739c953358c865005855253af4f68a497John McCall                                          llvm::Constant *&fn,
18375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                          StringRef fnName,
1838f85e193739c953358c865005855253af4f68a497John McCall                                          bool ignored) {
1839f85e193739c953358c865005855253af4f68a497John McCall  assert(cast<llvm::PointerType>(addr->getType())->getElementType()
1840f85e193739c953358c865005855253af4f68a497John McCall           == value->getType());
1841f85e193739c953358c865005855253af4f68a497John McCall
1842f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
18431d236ab930816f5da27bade92904914c44b73b4cBenjamin Kramer    llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy };
1844f85e193739c953358c865005855253af4f68a497John McCall
18452acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType
1846f85e193739c953358c865005855253af4f68a497John McCall      = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false);
1847f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1848f85e193739c953358c865005855253af4f68a497John McCall  }
1849f85e193739c953358c865005855253af4f68a497John McCall
18502acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *origType = value->getType();
1851f85e193739c953358c865005855253af4f68a497John McCall
1852bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *args[] = {
1853bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy),
1854bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy)
1855bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  };
1856bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args);
1857f85e193739c953358c865005855253af4f68a497John McCall
1858f85e193739c953358c865005855253af4f68a497John McCall  if (ignored) return 0;
1859f85e193739c953358c865005855253af4f68a497John McCall
1860f85e193739c953358c865005855253af4f68a497John McCall  return CGF.Builder.CreateBitCast(result, origType);
1861f85e193739c953358c865005855253af4f68a497John McCall}
1862f85e193739c953358c865005855253af4f68a497John McCall
1863f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature:
1864f85e193739c953358c865005855253af4f68a497John McCall///   void (i8**, i8**)
1865f85e193739c953358c865005855253af4f68a497John McCallstatic void emitARCCopyOperation(CodeGenFunction &CGF,
1866f85e193739c953358c865005855253af4f68a497John McCall                                 llvm::Value *dst,
1867f85e193739c953358c865005855253af4f68a497John McCall                                 llvm::Value *src,
1868f85e193739c953358c865005855253af4f68a497John McCall                                 llvm::Constant *&fn,
18695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                 StringRef fnName) {
1870f85e193739c953358c865005855253af4f68a497John McCall  assert(dst->getType() == src->getType());
1871f85e193739c953358c865005855253af4f68a497John McCall
1872f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
187376ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer    llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy };
187476ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer
18752acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType
1876f85e193739c953358c865005855253af4f68a497John McCall      = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false);
1877f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1878f85e193739c953358c865005855253af4f68a497John McCall  }
1879f85e193739c953358c865005855253af4f68a497John McCall
1880bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *args[] = {
1881bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy),
1882bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy)
1883bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  };
1884bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  CGF.EmitNounwindRuntimeCall(fn, args);
1885f85e193739c953358c865005855253af4f68a497John McCall}
1886f85e193739c953358c865005855253af4f68a497John McCall
1887f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a retain.  Based on the type, calls one of:
18889d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_retain(i8* %value)
18899d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_retainBlock(i8* %value)
1890f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
1891f85e193739c953358c865005855253af4f68a497John McCall  if (type->isBlockPointerType())
1892348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    return EmitARCRetainBlock(value, /*mandatory*/ false);
1893f85e193739c953358c865005855253af4f68a497John McCall  else
1894f85e193739c953358c865005855253af4f68a497John McCall    return EmitARCRetainNonBlock(value);
1895f85e193739c953358c865005855253af4f68a497John McCall}
1896f85e193739c953358c865005855253af4f68a497John McCall
1897f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object, with normal retain semantics.
18989d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_retain(i8* %value)
1899f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
1900f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
1901f85e193739c953358c865005855253af4f68a497John McCall                               CGM.getARCEntrypoints().objc_retain,
1902f85e193739c953358c865005855253af4f68a497John McCall                               "objc_retain");
1903f85e193739c953358c865005855253af4f68a497John McCall}
1904f85e193739c953358c865005855253af4f68a497John McCall
1905f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given block, with _Block_copy semantics.
19069d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_retainBlock(i8* %value)
1907348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall///
1908348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// \param mandatory - If false, emit the call with metadata
1909348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// indicating that it's okay for the optimizer to eliminate this call
1910348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// if it can prove that the block never escapes except down the stack.
1911348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value,
1912348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall                                                 bool mandatory) {
1913348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  llvm::Value *result
1914348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    = emitARCValueOperation(*this, value,
1915348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall                            CGM.getARCEntrypoints().objc_retainBlock,
1916348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall                            "objc_retainBlock");
1917348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
1918348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  // If the copy isn't mandatory, add !clang.arc.copy_on_escape to
1919348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  // tell the optimizer that it doesn't need to do this copy if the
1920348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  // block doesn't escape, where being passed as an argument doesn't
1921348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  // count as escaping.
1922348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  if (!mandatory && isa<llvm::Instruction>(result)) {
1923348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    llvm::CallInst *call
1924348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall      = cast<llvm::CallInst>(result->stripPointerCasts());
1925348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock);
1926348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
1927348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    SmallVector<llvm::Value*,1> args;
1928348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    call->setMetadata("clang.arc.copy_on_escape",
1929348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall                      llvm::MDNode::get(Builder.getContext(), args));
1930348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  }
1931348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
1932348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  return result;
1933f85e193739c953358c865005855253af4f68a497John McCall}
1934f85e193739c953358c865005855253af4f68a497John McCall
1935f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object which is the result of a function call.
19369d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_retainAutoreleasedReturnValue(i8* %value)
1937f85e193739c953358c865005855253af4f68a497John McCall///
1938f85e193739c953358c865005855253af4f68a497John McCall/// Yes, this function name is one character away from a different
1939f85e193739c953358c865005855253af4f68a497John McCall/// call with completely different semantics.
1940f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
1941f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) {
1942f85e193739c953358c865005855253af4f68a497John McCall  // Fetch the void(void) inline asm which marks that we're going to
1943f85e193739c953358c865005855253af4f68a497John McCall  // retain the autoreleased return value.
1944f85e193739c953358c865005855253af4f68a497John McCall  llvm::InlineAsm *&marker
1945f85e193739c953358c865005855253af4f68a497John McCall    = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker;
1946f85e193739c953358c865005855253af4f68a497John McCall  if (!marker) {
19475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    StringRef assembly
1948f85e193739c953358c865005855253af4f68a497John McCall      = CGM.getTargetCodeGenInfo()
1949f85e193739c953358c865005855253af4f68a497John McCall           .getARCRetainAutoreleasedReturnValueMarker();
1950f85e193739c953358c865005855253af4f68a497John McCall
1951f85e193739c953358c865005855253af4f68a497John McCall    // If we have an empty assembly string, there's nothing to do.
1952f85e193739c953358c865005855253af4f68a497John McCall    if (assembly.empty()) {
1953f85e193739c953358c865005855253af4f68a497John McCall
1954f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, at -O0, build an inline asm that we're going to call
1955f85e193739c953358c865005855253af4f68a497John McCall    // in a moment.
1956f85e193739c953358c865005855253af4f68a497John McCall    } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1957f85e193739c953358c865005855253af4f68a497John McCall      llvm::FunctionType *type =
19588b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        llvm::FunctionType::get(VoidTy, /*variadic*/false);
1959f85e193739c953358c865005855253af4f68a497John McCall
1960f85e193739c953358c865005855253af4f68a497John McCall      marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
1961f85e193739c953358c865005855253af4f68a497John McCall
1962f85e193739c953358c865005855253af4f68a497John McCall    // If we're at -O1 and above, we don't want to litter the code
1963f85e193739c953358c865005855253af4f68a497John McCall    // with this marker yet, so leave a breadcrumb for the ARC
1964f85e193739c953358c865005855253af4f68a497John McCall    // optimizer to pick up.
1965f85e193739c953358c865005855253af4f68a497John McCall    } else {
1966f85e193739c953358c865005855253af4f68a497John McCall      llvm::NamedMDNode *metadata =
1967f85e193739c953358c865005855253af4f68a497John McCall        CGM.getModule().getOrInsertNamedMetadata(
1968f85e193739c953358c865005855253af4f68a497John McCall                            "clang.arc.retainAutoreleasedReturnValueMarker");
1969f85e193739c953358c865005855253af4f68a497John McCall      assert(metadata->getNumOperands() <= 1);
1970f85e193739c953358c865005855253af4f68a497John McCall      if (metadata->getNumOperands() == 0) {
1971f85e193739c953358c865005855253af4f68a497John McCall        llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly);
1972da549e8995c447542d5631b8b67fcc3a9582797aJay Foad        metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string));
1973f85e193739c953358c865005855253af4f68a497John McCall      }
1974f85e193739c953358c865005855253af4f68a497John McCall    }
1975f85e193739c953358c865005855253af4f68a497John McCall  }
1976f85e193739c953358c865005855253af4f68a497John McCall
1977f85e193739c953358c865005855253af4f68a497John McCall  // Call the marker asm if we made one, which we do only at -O0.
1978f85e193739c953358c865005855253af4f68a497John McCall  if (marker) Builder.CreateCall(marker);
1979f85e193739c953358c865005855253af4f68a497John McCall
1980f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
1981f85e193739c953358c865005855253af4f68a497John McCall                     CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue,
1982f85e193739c953358c865005855253af4f68a497John McCall                               "objc_retainAutoreleasedReturnValue");
1983f85e193739c953358c865005855253af4f68a497John McCall}
1984f85e193739c953358c865005855253af4f68a497John McCall
1985f85e193739c953358c865005855253af4f68a497John McCall/// Release the given object.
19869d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call void \@objc_release(i8* %value)
19875b07e8077a20b80fee90bd76c43c6150c676e4a8John McCallvoid CodeGenFunction::EmitARCRelease(llvm::Value *value,
19885b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall                                     ARCPreciseLifetime_t precise) {
1989f85e193739c953358c865005855253af4f68a497John McCall  if (isa<llvm::ConstantPointerNull>(value)) return;
1990f85e193739c953358c865005855253af4f68a497John McCall
1991f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release;
1992f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
19932acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
199476ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer      llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
1995f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_release");
1996f85e193739c953358c865005855253af4f68a497John McCall  }
1997f85e193739c953358c865005855253af4f68a497John McCall
1998f85e193739c953358c865005855253af4f68a497John McCall  // Cast the argument to 'id'.
1999f85e193739c953358c865005855253af4f68a497John McCall  value = Builder.CreateBitCast(value, Int8PtrTy);
2000f85e193739c953358c865005855253af4f68a497John McCall
2001f85e193739c953358c865005855253af4f68a497John McCall  // Call objc_release.
2002bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value);
2003f85e193739c953358c865005855253af4f68a497John McCall
20045b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall  if (precise == ARCImpreciseLifetime) {
20055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<llvm::Value*,1> args;
2006f85e193739c953358c865005855253af4f68a497John McCall    call->setMetadata("clang.imprecise_release",
2007f85e193739c953358c865005855253af4f68a497John McCall                      llvm::MDNode::get(Builder.getContext(), args));
2008f85e193739c953358c865005855253af4f68a497John McCall  }
2009f85e193739c953358c865005855253af4f68a497John McCall}
2010f85e193739c953358c865005855253af4f68a497John McCall
2011015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// Destroy a __strong variable.
2012015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall///
2013015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O0, emit a call to store 'null' into the address;
2014015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// instrumenting tools prefer this because the address is exposed,
2015015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// but it's relatively cumbersome to optimize.
2016015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall///
2017015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O1 and above, just load and call objc_release.
2018015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall///
2019015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall///   call void \@objc_storeStrong(i8** %addr, i8* null)
20205b07e8077a20b80fee90bd76c43c6150c676e4a8John McCallvoid CodeGenFunction::EmitARCDestroyStrong(llvm::Value *addr,
20215b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall                                           ARCPreciseLifetime_t precise) {
2022015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2023015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall    llvm::PointerType *addrTy = cast<llvm::PointerType>(addr->getType());
2024015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall    llvm::Value *null = llvm::ConstantPointerNull::get(
2025015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall                          cast<llvm::PointerType>(addrTy->getElementType()));
2026015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall    EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
2027015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall    return;
2028015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall  }
2029015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall
2030015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall  llvm::Value *value = Builder.CreateLoad(addr);
2031015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall  EmitARCRelease(value, precise);
2032015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall}
2033015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall
2034f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object.  Always calls this:
20359d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call void \@objc_storeStrong(i8** %addr, i8* %value)
2036f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr,
2037f85e193739c953358c865005855253af4f68a497John McCall                                                     llvm::Value *value,
2038f85e193739c953358c865005855253af4f68a497John McCall                                                     bool ignored) {
2039f85e193739c953358c865005855253af4f68a497John McCall  assert(cast<llvm::PointerType>(addr->getType())->getElementType()
2040f85e193739c953358c865005855253af4f68a497John McCall           == value->getType());
2041f85e193739c953358c865005855253af4f68a497John McCall
2042f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong;
2043f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
20449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy };
20452acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType
2046f85e193739c953358c865005855253af4f68a497John McCall      = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false);
2047f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong");
2048f85e193739c953358c865005855253af4f68a497John McCall  }
2049f85e193739c953358c865005855253af4f68a497John McCall
2050bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *args[] = {
2051bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    Builder.CreateBitCast(addr, Int8PtrPtrTy),
2052bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    Builder.CreateBitCast(value, Int8PtrTy)
2053bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  };
2054bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  EmitNounwindRuntimeCall(fn, args);
2055f85e193739c953358c865005855253af4f68a497John McCall
2056f85e193739c953358c865005855253af4f68a497John McCall  if (ignored) return 0;
2057f85e193739c953358c865005855253af4f68a497John McCall  return value;
2058f85e193739c953358c865005855253af4f68a497John McCall}
2059f85e193739c953358c865005855253af4f68a497John McCall
2060f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object.  Sometimes calls this:
20619d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call void \@objc_storeStrong(i8** %addr, i8* %value)
2062f85e193739c953358c865005855253af4f68a497John McCall/// Other times, breaks it down into components.
2063545d996ec5a3113f046944f11b27cc2d6cb055b4John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst,
2064f85e193739c953358c865005855253af4f68a497John McCall                                                 llvm::Value *newValue,
2065f85e193739c953358c865005855253af4f68a497John McCall                                                 bool ignored) {
2066545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  QualType type = dst.getType();
2067f85e193739c953358c865005855253af4f68a497John McCall  bool isBlock = type->isBlockPointerType();
2068f85e193739c953358c865005855253af4f68a497John McCall
2069f85e193739c953358c865005855253af4f68a497John McCall  // Use a store barrier at -O0 unless this is a block type or the
2070f85e193739c953358c865005855253af4f68a497John McCall  // lvalue is inadequately aligned.
2071f85e193739c953358c865005855253af4f68a497John McCall  if (shouldUseFusedARCCalls() &&
2072f85e193739c953358c865005855253af4f68a497John McCall      !isBlock &&
20736da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman      (dst.getAlignment().isZero() ||
20746da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman       dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) {
2075f85e193739c953358c865005855253af4f68a497John McCall    return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored);
2076f85e193739c953358c865005855253af4f68a497John McCall  }
2077f85e193739c953358c865005855253af4f68a497John McCall
2078f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, split it out.
2079f85e193739c953358c865005855253af4f68a497John McCall
2080f85e193739c953358c865005855253af4f68a497John McCall  // Retain the new value.
2081f85e193739c953358c865005855253af4f68a497John McCall  newValue = EmitARCRetain(type, newValue);
2082f85e193739c953358c865005855253af4f68a497John McCall
2083f85e193739c953358c865005855253af4f68a497John McCall  // Read the old value.
2084545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  llvm::Value *oldValue = EmitLoadOfScalar(dst);
2085f85e193739c953358c865005855253af4f68a497John McCall
2086f85e193739c953358c865005855253af4f68a497John McCall  // Store.  We do this before the release so that any deallocs won't
2087f85e193739c953358c865005855253af4f68a497John McCall  // see the old value.
2088545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  EmitStoreOfScalar(newValue, dst);
2089f85e193739c953358c865005855253af4f68a497John McCall
2090f85e193739c953358c865005855253af4f68a497John McCall  // Finally, release the old value.
20915b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall  EmitARCRelease(oldValue, dst.isARCPreciseLifetime());
2092f85e193739c953358c865005855253af4f68a497John McCall
2093f85e193739c953358c865005855253af4f68a497John McCall  return newValue;
2094f85e193739c953358c865005855253af4f68a497John McCall}
2095f85e193739c953358c865005855253af4f68a497John McCall
2096f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object.
20979d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_autorelease(i8* %value)
2098f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
2099f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
2100f85e193739c953358c865005855253af4f68a497John McCall                               CGM.getARCEntrypoints().objc_autorelease,
2101f85e193739c953358c865005855253af4f68a497John McCall                               "objc_autorelease");
2102f85e193739c953358c865005855253af4f68a497John McCall}
2103f85e193739c953358c865005855253af4f68a497John McCall
2104f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object.
21059d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_autoreleaseReturnValue(i8* %value)
2106f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
2107f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) {
2108f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
2109f85e193739c953358c865005855253af4f68a497John McCall                            CGM.getARCEntrypoints().objc_autoreleaseReturnValue,
2110df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier                               "objc_autoreleaseReturnValue",
2111df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier                               /*isTailCall*/ true);
2112f85e193739c953358c865005855253af4f68a497John McCall}
2113f85e193739c953358c865005855253af4f68a497John McCall
2114f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object.
21159d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_retainAutoreleaseReturnValue(i8* %value)
2116f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
2117f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) {
2118f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
2119f85e193739c953358c865005855253af4f68a497John McCall                     CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue,
2120df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier                               "objc_retainAutoreleaseReturnValue",
2121df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier                               /*isTailCall*/ true);
2122f85e193739c953358c865005855253af4f68a497John McCall}
2123f85e193739c953358c865005855253af4f68a497John McCall
2124f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object.
21259d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_retainAutorelease(i8* %value)
2126f85e193739c953358c865005855253af4f68a497John McCall/// or
21279d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   %retain = call i8* \@objc_retainBlock(i8* %value)
21289d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_autorelease(i8* %retain)
2129f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type,
2130f85e193739c953358c865005855253af4f68a497John McCall                                                       llvm::Value *value) {
2131f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isBlockPointerType())
2132f85e193739c953358c865005855253af4f68a497John McCall    return EmitARCRetainAutoreleaseNonBlock(value);
2133f85e193739c953358c865005855253af4f68a497John McCall
2134f85e193739c953358c865005855253af4f68a497John McCall  if (isa<llvm::ConstantPointerNull>(value)) return value;
2135f85e193739c953358c865005855253af4f68a497John McCall
21362acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *origType = value->getType();
2137f85e193739c953358c865005855253af4f68a497John McCall  value = Builder.CreateBitCast(value, Int8PtrTy);
2138348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  value = EmitARCRetainBlock(value, /*mandatory*/ true);
2139f85e193739c953358c865005855253af4f68a497John McCall  value = EmitARCAutorelease(value);
2140f85e193739c953358c865005855253af4f68a497John McCall  return Builder.CreateBitCast(value, origType);
2141f85e193739c953358c865005855253af4f68a497John McCall}
2142f85e193739c953358c865005855253af4f68a497John McCall
2143f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object.
21449d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_retainAutorelease(i8* %value)
2145f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
2146f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) {
2147f85e193739c953358c865005855253af4f68a497John McCall  return emitARCValueOperation(*this, value,
2148f85e193739c953358c865005855253af4f68a497John McCall                               CGM.getARCEntrypoints().objc_retainAutorelease,
2149f85e193739c953358c865005855253af4f68a497John McCall                               "objc_retainAutorelease");
2150f85e193739c953358c865005855253af4f68a497John McCall}
2151f85e193739c953358c865005855253af4f68a497John McCall
21529d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeak(i8** %addr)
2153f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
2154f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) {
2155f85e193739c953358c865005855253af4f68a497John McCall  return emitARCLoadOperation(*this, addr,
2156f85e193739c953358c865005855253af4f68a497John McCall                              CGM.getARCEntrypoints().objc_loadWeak,
2157f85e193739c953358c865005855253af4f68a497John McCall                              "objc_loadWeak");
2158f85e193739c953358c865005855253af4f68a497John McCall}
2159f85e193739c953358c865005855253af4f68a497John McCall
21609d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeakRetained(i8** %addr)
2161f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) {
2162f85e193739c953358c865005855253af4f68a497John McCall  return emitARCLoadOperation(*this, addr,
2163f85e193739c953358c865005855253af4f68a497John McCall                              CGM.getARCEntrypoints().objc_loadWeakRetained,
2164f85e193739c953358c865005855253af4f68a497John McCall                              "objc_loadWeakRetained");
2165f85e193739c953358c865005855253af4f68a497John McCall}
2166f85e193739c953358c865005855253af4f68a497John McCall
21679d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_storeWeak(i8** %addr, i8* %value)
2168f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value.
2169f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr,
2170f85e193739c953358c865005855253af4f68a497John McCall                                               llvm::Value *value,
2171f85e193739c953358c865005855253af4f68a497John McCall                                               bool ignored) {
2172f85e193739c953358c865005855253af4f68a497John McCall  return emitARCStoreOperation(*this, addr, value,
2173f85e193739c953358c865005855253af4f68a497John McCall                               CGM.getARCEntrypoints().objc_storeWeak,
2174f85e193739c953358c865005855253af4f68a497John McCall                               "objc_storeWeak", ignored);
2175f85e193739c953358c865005855253af4f68a497John McCall}
2176f85e193739c953358c865005855253af4f68a497John McCall
21779d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_initWeak(i8** %addr, i8* %value)
2178f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value.  %addr is known to not have a current weak entry.
2179f85e193739c953358c865005855253af4f68a497John McCall/// Essentially equivalent to:
2180f85e193739c953358c865005855253af4f68a497John McCall///   *addr = nil; objc_storeWeak(addr, value);
2181f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) {
2182f85e193739c953358c865005855253af4f68a497John McCall  // If we're initializing to null, just write null to memory; no need
2183f85e193739c953358c865005855253af4f68a497John McCall  // to get the runtime involved.  But don't do this if optimization
2184f85e193739c953358c865005855253af4f68a497John McCall  // is enabled, because accounting for this would make the optimizer
2185f85e193739c953358c865005855253af4f68a497John McCall  // much more complicated.
2186f85e193739c953358c865005855253af4f68a497John McCall  if (isa<llvm::ConstantPointerNull>(value) &&
2187f85e193739c953358c865005855253af4f68a497John McCall      CGM.getCodeGenOpts().OptimizationLevel == 0) {
2188f85e193739c953358c865005855253af4f68a497John McCall    Builder.CreateStore(value, addr);
2189f85e193739c953358c865005855253af4f68a497John McCall    return;
2190f85e193739c953358c865005855253af4f68a497John McCall  }
2191f85e193739c953358c865005855253af4f68a497John McCall
2192f85e193739c953358c865005855253af4f68a497John McCall  emitARCStoreOperation(*this, addr, value,
2193f85e193739c953358c865005855253af4f68a497John McCall                        CGM.getARCEntrypoints().objc_initWeak,
2194f85e193739c953358c865005855253af4f68a497John McCall                        "objc_initWeak", /*ignored*/ true);
2195f85e193739c953358c865005855253af4f68a497John McCall}
2196f85e193739c953358c865005855253af4f68a497John McCall
21979d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_destroyWeak(i8** %addr)
2198f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_storeWeak(addr, nil).
2199f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) {
2200f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak;
2201f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
22022acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
220376ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer      llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false);
2204f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak");
2205f85e193739c953358c865005855253af4f68a497John McCall  }
2206f85e193739c953358c865005855253af4f68a497John McCall
2207f85e193739c953358c865005855253af4f68a497John McCall  // Cast the argument to 'id*'.
2208f85e193739c953358c865005855253af4f68a497John McCall  addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
2209f85e193739c953358c865005855253af4f68a497John McCall
2210bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  EmitNounwindRuntimeCall(fn, addr);
2211f85e193739c953358c865005855253af4f68a497John McCall}
2212f85e193739c953358c865005855253af4f68a497John McCall
22139d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_moveWeak(i8** %dest, i8** %src)
2214f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest.  Leaves %src pointing to nothing.
2215f85e193739c953358c865005855253af4f68a497John McCall/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
2216f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) {
2217f85e193739c953358c865005855253af4f68a497John McCall  emitARCCopyOperation(*this, dst, src,
2218f85e193739c953358c865005855253af4f68a497John McCall                       CGM.getARCEntrypoints().objc_moveWeak,
2219f85e193739c953358c865005855253af4f68a497John McCall                       "objc_moveWeak");
2220f85e193739c953358c865005855253af4f68a497John McCall}
2221f85e193739c953358c865005855253af4f68a497John McCall
22229d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_copyWeak(i8** %dest, i8** %src)
2223f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest.  Essentially
2224f85e193739c953358c865005855253af4f68a497John McCall///   objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
2225f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) {
2226f85e193739c953358c865005855253af4f68a497John McCall  emitARCCopyOperation(*this, dst, src,
2227f85e193739c953358c865005855253af4f68a497John McCall                       CGM.getARCEntrypoints().objc_copyWeak,
2228f85e193739c953358c865005855253af4f68a497John McCall                       "objc_copyWeak");
2229f85e193739c953358c865005855253af4f68a497John McCall}
2230f85e193739c953358c865005855253af4f68a497John McCall
2231f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a objc_autoreleasepool_push.
22329d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call i8* \@objc_autoreleasePoolPush(void)
2233f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() {
2234f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush;
2235f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
22362acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
2237f85e193739c953358c865005855253af4f68a497John McCall      llvm::FunctionType::get(Int8PtrTy, false);
2238f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush");
2239f85e193739c953358c865005855253af4f68a497John McCall  }
2240f85e193739c953358c865005855253af4f68a497John McCall
2241bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return EmitNounwindRuntimeCall(fn);
2242f85e193739c953358c865005855253af4f68a497John McCall}
2243f85e193739c953358c865005855253af4f68a497John McCall
2244f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release.
22459d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett///   call void \@objc_autoreleasePoolPop(i8* %ptr)
2246f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) {
2247f85e193739c953358c865005855253af4f68a497John McCall  assert(value->getType() == Int8PtrTy);
2248f85e193739c953358c865005855253af4f68a497John McCall
2249f85e193739c953358c865005855253af4f68a497John McCall  llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop;
2250f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) {
22512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::FunctionType *fnType =
225276ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer      llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2253f85e193739c953358c865005855253af4f68a497John McCall
2254f85e193739c953358c865005855253af4f68a497John McCall    // We don't want to use a weak import here; instead we should not
2255f85e193739c953358c865005855253af4f68a497John McCall    // fall into this path.
2256f85e193739c953358c865005855253af4f68a497John McCall    fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop");
2257f85e193739c953358c865005855253af4f68a497John McCall  }
2258f85e193739c953358c865005855253af4f68a497John McCall
2259bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  EmitNounwindRuntimeCall(fn, value);
2260f85e193739c953358c865005855253af4f68a497John McCall}
2261f85e193739c953358c865005855253af4f68a497John McCall
2262f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do an MRR version objc_autoreleasepool_push.
2263f85e193739c953358c865005855253af4f68a497John McCall/// Which is: [[NSAutoreleasePool alloc] init];
2264f85e193739c953358c865005855253af4f68a497John McCall/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
2265f85e193739c953358c865005855253af4f68a497John McCall/// init is declared as: - (id) init; in its NSObject super class.
2266f85e193739c953358c865005855253af4f68a497John McCall///
2267f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() {
2268f85e193739c953358c865005855253af4f68a497John McCall  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
2269bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this);
2270f85e193739c953358c865005855253af4f68a497John McCall  // [NSAutoreleasePool alloc]
2271f85e193739c953358c865005855253af4f68a497John McCall  IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
2272f85e193739c953358c865005855253af4f68a497John McCall  Selector AllocSel = getContext().Selectors.getSelector(0, &II);
2273f85e193739c953358c865005855253af4f68a497John McCall  CallArgList Args;
2274f85e193739c953358c865005855253af4f68a497John McCall  RValue AllocRV =
2275f85e193739c953358c865005855253af4f68a497John McCall    Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2276f85e193739c953358c865005855253af4f68a497John McCall                                getContext().getObjCIdType(),
2277f85e193739c953358c865005855253af4f68a497John McCall                                AllocSel, Receiver, Args);
2278f85e193739c953358c865005855253af4f68a497John McCall
2279f85e193739c953358c865005855253af4f68a497John McCall  // [Receiver init]
2280f85e193739c953358c865005855253af4f68a497John McCall  Receiver = AllocRV.getScalarVal();
2281f85e193739c953358c865005855253af4f68a497John McCall  II = &CGM.getContext().Idents.get("init");
2282f85e193739c953358c865005855253af4f68a497John McCall  Selector InitSel = getContext().Selectors.getSelector(0, &II);
2283f85e193739c953358c865005855253af4f68a497John McCall  RValue InitRV =
2284f85e193739c953358c865005855253af4f68a497John McCall    Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2285f85e193739c953358c865005855253af4f68a497John McCall                                getContext().getObjCIdType(),
2286f85e193739c953358c865005855253af4f68a497John McCall                                InitSel, Receiver, Args);
2287f85e193739c953358c865005855253af4f68a497John McCall  return InitRV.getScalarVal();
2288f85e193739c953358c865005855253af4f68a497John McCall}
2289f85e193739c953358c865005855253af4f68a497John McCall
2290f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release.
2291f85e193739c953358c865005855253af4f68a497John McCall/// [tmp drain];
2292f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) {
2293f85e193739c953358c865005855253af4f68a497John McCall  IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
2294f85e193739c953358c865005855253af4f68a497John McCall  Selector DrainSel = getContext().Selectors.getSelector(0, &II);
2295f85e193739c953358c865005855253af4f68a497John McCall  CallArgList Args;
2296f85e193739c953358c865005855253af4f68a497John McCall  CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2297f85e193739c953358c865005855253af4f68a497John McCall                              getContext().VoidTy, DrainSel, Arg, Args);
2298f85e193739c953358c865005855253af4f68a497John McCall}
2299f85e193739c953358c865005855253af4f68a497John McCall
2300bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF,
2301bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              llvm::Value *addr,
2302bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              QualType type) {
23035b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall  CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime);
2304bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
2305bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
2306bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF,
2307bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                                llvm::Value *addr,
2308bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                                QualType type) {
23095b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall  CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime);
2310bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
2311bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
2312bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF,
2313bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                     llvm::Value *addr,
2314bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                     QualType type) {
2315bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  CGF.EmitARCDestroyWeak(addr);
2316bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
2317bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
2318f85e193739c953358c865005855253af4f68a497John McCallnamespace {
2319f85e193739c953358c865005855253af4f68a497John McCall  struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup {
2320f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *Token;
2321f85e193739c953358c865005855253af4f68a497John McCall
2322f85e193739c953358c865005855253af4f68a497John McCall    CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2323f85e193739c953358c865005855253af4f68a497John McCall
2324ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
2325f85e193739c953358c865005855253af4f68a497John McCall      CGF.EmitObjCAutoreleasePoolPop(Token);
2326f85e193739c953358c865005855253af4f68a497John McCall    }
2327f85e193739c953358c865005855253af4f68a497John McCall  };
2328f85e193739c953358c865005855253af4f68a497John McCall  struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup {
2329f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *Token;
2330f85e193739c953358c865005855253af4f68a497John McCall
2331f85e193739c953358c865005855253af4f68a497John McCall    CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2332f85e193739c953358c865005855253af4f68a497John McCall
2333ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
2334f85e193739c953358c865005855253af4f68a497John McCall      CGF.EmitObjCMRRAutoreleasePoolPop(Token);
2335f85e193739c953358c865005855253af4f68a497John McCall    }
2336f85e193739c953358c865005855253af4f68a497John McCall  };
2337f85e193739c953358c865005855253af4f68a497John McCall}
2338f85e193739c953358c865005855253af4f68a497John McCall
2339f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) {
23404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().ObjCAutoRefCount)
2341f85e193739c953358c865005855253af4f68a497John McCall    EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
2342f85e193739c953358c865005855253af4f68a497John McCall  else
2343f85e193739c953358c865005855253af4f68a497John McCall    EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
2344f85e193739c953358c865005855253af4f68a497John McCall}
2345f85e193739c953358c865005855253af4f68a497John McCall
2346f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2347f85e193739c953358c865005855253af4f68a497John McCall                                                  LValue lvalue,
2348f85e193739c953358c865005855253af4f68a497John McCall                                                  QualType type) {
2349f85e193739c953358c865005855253af4f68a497John McCall  switch (type.getObjCLifetime()) {
2350f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_None:
2351f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_ExplicitNone:
2352f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Strong:
2353f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Autoreleasing:
2354545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(),
2355f85e193739c953358c865005855253af4f68a497John McCall                         false);
2356f85e193739c953358c865005855253af4f68a497John McCall
2357f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Weak:
2358f85e193739c953358c865005855253af4f68a497John McCall    return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()),
2359f85e193739c953358c865005855253af4f68a497John McCall                         true);
2360f85e193739c953358c865005855253af4f68a497John McCall  }
2361f85e193739c953358c865005855253af4f68a497John McCall
2362f85e193739c953358c865005855253af4f68a497John McCall  llvm_unreachable("impossible lifetime!");
2363f85e193739c953358c865005855253af4f68a497John McCall}
2364f85e193739c953358c865005855253af4f68a497John McCall
2365f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2366f85e193739c953358c865005855253af4f68a497John McCall                                                  const Expr *e) {
2367f85e193739c953358c865005855253af4f68a497John McCall  e = e->IgnoreParens();
2368f85e193739c953358c865005855253af4f68a497John McCall  QualType type = e->getType();
2369f85e193739c953358c865005855253af4f68a497John McCall
23702148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  // If we're loading retained from a __strong xvalue, we can avoid
23712148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  // an extra retain/release pair by zeroing out the source of this
23722148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  // "move" operation.
23732148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  if (e->isXValue() &&
23742148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall      !type.isConstQualified() &&
23752148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall      type.getObjCLifetime() == Qualifiers::OCL_Strong) {
23762148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    // Emit the lvalue.
23772148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    LValue lv = CGF.EmitLValue(e);
23782148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall
23792148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    // Load the object pointer.
23802148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal();
23812148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall
23822148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    // Set the source pointer to NULL.
23832148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv);
23842148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall
23852148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall    return TryEmitResult(result, true);
23862148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall  }
23872148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall
2388f85e193739c953358c865005855253af4f68a497John McCall  // As a very special optimization, in ARC++, if the l-value is the
2389f85e193739c953358c865005855253af4f68a497John McCall  // result of a non-volatile assignment, do a simple retain of the
2390f85e193739c953358c865005855253af4f68a497John McCall  // result of the call to objc_storeWeak instead of reloading.
23914e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGF.getLangOpts().CPlusPlus &&
2392f85e193739c953358c865005855253af4f68a497John McCall      !type.isVolatileQualified() &&
2393f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() == Qualifiers::OCL_Weak &&
2394f85e193739c953358c865005855253af4f68a497John McCall      isa<BinaryOperator>(e) &&
2395f85e193739c953358c865005855253af4f68a497John McCall      cast<BinaryOperator>(e)->getOpcode() == BO_Assign)
2396f85e193739c953358c865005855253af4f68a497John McCall    return TryEmitResult(CGF.EmitScalarExpr(e), false);
2397f85e193739c953358c865005855253af4f68a497John McCall
2398f85e193739c953358c865005855253af4f68a497John McCall  return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
2399f85e193739c953358c865005855253af4f68a497John McCall}
2400f85e193739c953358c865005855253af4f68a497John McCall
2401f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
2402f85e193739c953358c865005855253af4f68a497John McCall                                           llvm::Value *value);
2403f85e193739c953358c865005855253af4f68a497John McCall
2404f85e193739c953358c865005855253af4f68a497John McCall/// Given that the given expression is some sort of call (which does
2405f85e193739c953358c865005855253af4f68a497John McCall/// not return retained), emit a retain following it.
2406f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) {
2407f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = CGF.EmitScalarExpr(e);
2408f85e193739c953358c865005855253af4f68a497John McCall  return emitARCRetainAfterCall(CGF, value);
2409f85e193739c953358c865005855253af4f68a497John McCall}
2410f85e193739c953358c865005855253af4f68a497John McCall
2411f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
2412f85e193739c953358c865005855253af4f68a497John McCall                                           llvm::Value *value) {
2413f85e193739c953358c865005855253af4f68a497John McCall  if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
2414f85e193739c953358c865005855253af4f68a497John McCall    CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2415f85e193739c953358c865005855253af4f68a497John McCall
2416f85e193739c953358c865005855253af4f68a497John McCall    // Place the retain immediately following the call.
2417f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.SetInsertPoint(call->getParent(),
2418f85e193739c953358c865005855253af4f68a497John McCall                               ++llvm::BasicBlock::iterator(call));
2419f85e193739c953358c865005855253af4f68a497John McCall    value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
2420f85e193739c953358c865005855253af4f68a497John McCall
2421f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.restoreIP(ip);
2422f85e193739c953358c865005855253af4f68a497John McCall    return value;
2423f85e193739c953358c865005855253af4f68a497John McCall  } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
2424f85e193739c953358c865005855253af4f68a497John McCall    CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2425f85e193739c953358c865005855253af4f68a497John McCall
2426f85e193739c953358c865005855253af4f68a497John McCall    // Place the retain at the beginning of the normal destination block.
2427f85e193739c953358c865005855253af4f68a497John McCall    llvm::BasicBlock *BB = invoke->getNormalDest();
2428f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.SetInsertPoint(BB, BB->begin());
2429f85e193739c953358c865005855253af4f68a497John McCall    value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
2430f85e193739c953358c865005855253af4f68a497John McCall
2431f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.restoreIP(ip);
2432f85e193739c953358c865005855253af4f68a497John McCall    return value;
2433f85e193739c953358c865005855253af4f68a497John McCall
2434f85e193739c953358c865005855253af4f68a497John McCall  // Bitcasts can arise because of related-result returns.  Rewrite
2435f85e193739c953358c865005855253af4f68a497John McCall  // the operand.
2436f85e193739c953358c865005855253af4f68a497John McCall  } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
2437f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *operand = bitcast->getOperand(0);
2438f85e193739c953358c865005855253af4f68a497John McCall    operand = emitARCRetainAfterCall(CGF, operand);
2439f85e193739c953358c865005855253af4f68a497John McCall    bitcast->setOperand(0, operand);
2440f85e193739c953358c865005855253af4f68a497John McCall    return bitcast;
2441f85e193739c953358c865005855253af4f68a497John McCall
2442f85e193739c953358c865005855253af4f68a497John McCall  // Generic fall-back case.
2443f85e193739c953358c865005855253af4f68a497John McCall  } else {
2444f85e193739c953358c865005855253af4f68a497John McCall    // Retain using the non-block variant: we never need to do a copy
2445f85e193739c953358c865005855253af4f68a497John McCall    // of a block that's been returned to us.
2446f85e193739c953358c865005855253af4f68a497John McCall    return CGF.EmitARCRetainNonBlock(value);
2447f85e193739c953358c865005855253af4f68a497John McCall  }
2448f85e193739c953358c865005855253af4f68a497John McCall}
2449f85e193739c953358c865005855253af4f68a497John McCall
2450dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// Determine whether it might be important to emit a separate
2451dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// objc_retain_block on the result of the given expression, or
2452dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// whether it's okay to just emit it in a +1 context.
2453dc05b11c67331016473fbc7909827b1b89c9616bJohn McCallstatic bool shouldEmitSeparateBlockRetain(const Expr *e) {
2454dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  assert(e->getType()->isBlockPointerType());
2455dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  e = e->IgnoreParens();
2456dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2457dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  // For future goodness, emit block expressions directly in +1
2458dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  // contexts if we can.
2459dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  if (isa<BlockExpr>(e))
2460dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    return false;
2461dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2462dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  if (const CastExpr *cast = dyn_cast<CastExpr>(e)) {
2463dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    switch (cast->getCastKind()) {
2464dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    // Emitting these operations in +1 contexts is goodness.
2465dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    case CK_LValueToRValue:
246633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    case CK_ARCReclaimReturnedObject:
246733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    case CK_ARCConsumeObject:
246833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    case CK_ARCProduceObject:
2469dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      return false;
2470dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2471dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    // These operations preserve a block type.
2472dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    case CK_NoOp:
2473dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    case CK_BitCast:
2474dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      return shouldEmitSeparateBlockRetain(cast->getSubExpr());
2475dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2476dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    // These operations are known to be bad (or haven't been considered).
2477dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    case CK_AnyPointerToBlockPointerCast:
2478dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    default:
2479dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      return true;
2480dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    }
2481dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  }
2482dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2483dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall  return true;
2484dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall}
2485dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
24864b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// Try to emit a PseudoObjectExpr at +1.
24874b9c2d235fb9449e249d74f48ecfec601650de93John McCall///
24884b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// This massively duplicates emitPseudoObjectRValue.
24894b9c2d235fb9449e249d74f48ecfec601650de93John McCallstatic TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF,
24904b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                                  const PseudoObjectExpr *E) {
2491cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
24924b9c2d235fb9449e249d74f48ecfec601650de93John McCall
24934b9c2d235fb9449e249d74f48ecfec601650de93John McCall  // Find the result expression.
24944b9c2d235fb9449e249d74f48ecfec601650de93John McCall  const Expr *resultExpr = E->getResultExpr();
24954b9c2d235fb9449e249d74f48ecfec601650de93John McCall  assert(resultExpr);
24964b9c2d235fb9449e249d74f48ecfec601650de93John McCall  TryEmitResult result;
24974b9c2d235fb9449e249d74f48ecfec601650de93John McCall
24984b9c2d235fb9449e249d74f48ecfec601650de93John McCall  for (PseudoObjectExpr::const_semantics_iterator
24994b9c2d235fb9449e249d74f48ecfec601650de93John McCall         i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
25004b9c2d235fb9449e249d74f48ecfec601650de93John McCall    const Expr *semantic = *i;
25014b9c2d235fb9449e249d74f48ecfec601650de93John McCall
25024b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // If this semantic expression is an opaque value, bind it
25034b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // to the result of its source expression.
25044b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
25054b9c2d235fb9449e249d74f48ecfec601650de93John McCall      typedef CodeGenFunction::OpaqueValueMappingData OVMA;
25064b9c2d235fb9449e249d74f48ecfec601650de93John McCall      OVMA opaqueData;
25074b9c2d235fb9449e249d74f48ecfec601650de93John McCall
25084b9c2d235fb9449e249d74f48ecfec601650de93John McCall      // If this semantic is the result of the pseudo-object
25094b9c2d235fb9449e249d74f48ecfec601650de93John McCall      // expression, try to evaluate the source as +1.
25104b9c2d235fb9449e249d74f48ecfec601650de93John McCall      if (ov == resultExpr) {
25114b9c2d235fb9449e249d74f48ecfec601650de93John McCall        assert(!OVMA::shouldBindAsLValue(ov));
25124b9c2d235fb9449e249d74f48ecfec601650de93John McCall        result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr());
25134b9c2d235fb9449e249d74f48ecfec601650de93John McCall        opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer()));
25144b9c2d235fb9449e249d74f48ecfec601650de93John McCall
25154b9c2d235fb9449e249d74f48ecfec601650de93John McCall      // Otherwise, just bind it.
25164b9c2d235fb9449e249d74f48ecfec601650de93John McCall      } else {
25174b9c2d235fb9449e249d74f48ecfec601650de93John McCall        opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
25184b9c2d235fb9449e249d74f48ecfec601650de93John McCall      }
25194b9c2d235fb9449e249d74f48ecfec601650de93John McCall      opaques.push_back(opaqueData);
25204b9c2d235fb9449e249d74f48ecfec601650de93John McCall
25214b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // Otherwise, if the expression is the result, evaluate it
25224b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // and remember the result.
25234b9c2d235fb9449e249d74f48ecfec601650de93John McCall    } else if (semantic == resultExpr) {
25244b9c2d235fb9449e249d74f48ecfec601650de93John McCall      result = tryEmitARCRetainScalarExpr(CGF, semantic);
25254b9c2d235fb9449e249d74f48ecfec601650de93John McCall
25264b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // Otherwise, evaluate the expression in an ignored context.
25274b9c2d235fb9449e249d74f48ecfec601650de93John McCall    } else {
25284b9c2d235fb9449e249d74f48ecfec601650de93John McCall      CGF.EmitIgnoredExpr(semantic);
25294b9c2d235fb9449e249d74f48ecfec601650de93John McCall    }
25304b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
25314b9c2d235fb9449e249d74f48ecfec601650de93John McCall
25324b9c2d235fb9449e249d74f48ecfec601650de93John McCall  // Unbind all the opaques now.
25334b9c2d235fb9449e249d74f48ecfec601650de93John McCall  for (unsigned i = 0, e = opaques.size(); i != e; ++i)
25344b9c2d235fb9449e249d74f48ecfec601650de93John McCall    opaques[i].unbind(CGF);
25354b9c2d235fb9449e249d74f48ecfec601650de93John McCall
25364b9c2d235fb9449e249d74f48ecfec601650de93John McCall  return result;
25374b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
25384b9c2d235fb9449e249d74f48ecfec601650de93John McCall
2539f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult
2540f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) {
254172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  // We should *never* see a nested full-expression here, because if
254272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  // we fail to emit at +1, our caller must not retain after we close
254372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  // out the full-expression.
254472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  assert(!isa<ExprWithCleanups>(e));
2545990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall
2546f85e193739c953358c865005855253af4f68a497John McCall  // The desired result type, if it differs from the type of the
2547f85e193739c953358c865005855253af4f68a497John McCall  // ultimate opaque expression.
25482acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *resultType = 0;
2549f85e193739c953358c865005855253af4f68a497John McCall
2550f85e193739c953358c865005855253af4f68a497John McCall  while (true) {
2551f85e193739c953358c865005855253af4f68a497John McCall    e = e->IgnoreParens();
2552f85e193739c953358c865005855253af4f68a497John McCall
2553f85e193739c953358c865005855253af4f68a497John McCall    // There's a break at the end of this if-chain;  anything
2554f85e193739c953358c865005855253af4f68a497John McCall    // that wants to keep looping has to explicitly continue.
2555f85e193739c953358c865005855253af4f68a497John McCall    if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
2556f85e193739c953358c865005855253af4f68a497John McCall      switch (ce->getCastKind()) {
2557f85e193739c953358c865005855253af4f68a497John McCall      // No-op casts don't change the type, so we just ignore them.
2558f85e193739c953358c865005855253af4f68a497John McCall      case CK_NoOp:
2559f85e193739c953358c865005855253af4f68a497John McCall        e = ce->getSubExpr();
2560f85e193739c953358c865005855253af4f68a497John McCall        continue;
2561f85e193739c953358c865005855253af4f68a497John McCall
2562f85e193739c953358c865005855253af4f68a497John McCall      case CK_LValueToRValue: {
2563f85e193739c953358c865005855253af4f68a497John McCall        TryEmitResult loadResult
2564f85e193739c953358c865005855253af4f68a497John McCall          = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr());
2565f85e193739c953358c865005855253af4f68a497John McCall        if (resultType) {
2566f85e193739c953358c865005855253af4f68a497John McCall          llvm::Value *value = loadResult.getPointer();
2567f85e193739c953358c865005855253af4f68a497John McCall          value = CGF.Builder.CreateBitCast(value, resultType);
2568f85e193739c953358c865005855253af4f68a497John McCall          loadResult.setPointer(value);
2569f85e193739c953358c865005855253af4f68a497John McCall        }
2570f85e193739c953358c865005855253af4f68a497John McCall        return loadResult;
2571f85e193739c953358c865005855253af4f68a497John McCall      }
2572f85e193739c953358c865005855253af4f68a497John McCall
2573f85e193739c953358c865005855253af4f68a497John McCall      // These casts can change the type, so remember that and
2574f85e193739c953358c865005855253af4f68a497John McCall      // soldier on.  We only need to remember the outermost such
2575f85e193739c953358c865005855253af4f68a497John McCall      // cast, though.
25761d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      case CK_CPointerToObjCPointerCast:
25771d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      case CK_BlockPointerToObjCPointerCast:
2578f85e193739c953358c865005855253af4f68a497John McCall      case CK_AnyPointerToBlockPointerCast:
2579f85e193739c953358c865005855253af4f68a497John McCall      case CK_BitCast:
2580f85e193739c953358c865005855253af4f68a497John McCall        if (!resultType)
2581f85e193739c953358c865005855253af4f68a497John McCall          resultType = CGF.ConvertType(ce->getType());
2582f85e193739c953358c865005855253af4f68a497John McCall        e = ce->getSubExpr();
2583f85e193739c953358c865005855253af4f68a497John McCall        assert(e->getType()->hasPointerRepresentation());
2584f85e193739c953358c865005855253af4f68a497John McCall        continue;
2585f85e193739c953358c865005855253af4f68a497John McCall
2586f85e193739c953358c865005855253af4f68a497John McCall      // For consumptions, just emit the subexpression and thus elide
2587f85e193739c953358c865005855253af4f68a497John McCall      // the retain/release pair.
258833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall      case CK_ARCConsumeObject: {
2589f85e193739c953358c865005855253af4f68a497John McCall        llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr());
2590f85e193739c953358c865005855253af4f68a497John McCall        if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2591f85e193739c953358c865005855253af4f68a497John McCall        return TryEmitResult(result, true);
2592f85e193739c953358c865005855253af4f68a497John McCall      }
2593f85e193739c953358c865005855253af4f68a497John McCall
2594dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      // Block extends are net +0.  Naively, we could just recurse on
2595dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      // the subexpression, but actually we need to ensure that the
2596dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      // value is copied as a block, so there's a little filter here.
259733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall      case CK_ARCExtendBlockObject: {
2598dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        llvm::Value *result; // will be a +0 value
2599dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2600dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        // If we can't safely assume the sub-expression will produce a
2601dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        // block-copied value, emit the sub-expression at +0.
2602dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) {
2603dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          result = CGF.EmitScalarExpr(ce->getSubExpr());
2604dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2605dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        // Otherwise, try to emit the sub-expression at +1 recursively.
2606dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        } else {
2607dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          TryEmitResult subresult
2608dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall            = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr());
2609dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          result = subresult.getPointer();
2610dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2611dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          // If that produced a retained value, just use that,
2612dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          // possibly casting down.
2613dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          if (subresult.getInt()) {
2614dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall            if (resultType)
2615dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall              result = CGF.Builder.CreateBitCast(result, resultType);
2616dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall            return TryEmitResult(result, true);
2617dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          }
2618dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2619dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall          // Otherwise it's +0.
2620dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        }
2621dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2622dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        // Retain the object as a block, then cast down.
2623348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall        result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true);
2624dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2625dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall        return TryEmitResult(result, true);
2626dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      }
2627dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
26287e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall      // For reclaims, emit the subexpression as a retained call and
26297e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall      // skip the consumption.
263033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall      case CK_ARCReclaimReturnedObject: {
26317e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall        llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr());
26327e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall        if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
26337e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall        return TryEmitResult(result, true);
26347e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall      }
26357e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall
2636f85e193739c953358c865005855253af4f68a497John McCall      default:
2637f85e193739c953358c865005855253af4f68a497John McCall        break;
2638f85e193739c953358c865005855253af4f68a497John McCall      }
2639f85e193739c953358c865005855253af4f68a497John McCall
2640f85e193739c953358c865005855253af4f68a497John McCall    // Skip __extension__.
2641f85e193739c953358c865005855253af4f68a497John McCall    } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) {
2642f85e193739c953358c865005855253af4f68a497John McCall      if (op->getOpcode() == UO_Extension) {
2643f85e193739c953358c865005855253af4f68a497John McCall        e = op->getSubExpr();
2644f85e193739c953358c865005855253af4f68a497John McCall        continue;
2645f85e193739c953358c865005855253af4f68a497John McCall      }
2646f85e193739c953358c865005855253af4f68a497John McCall
2647f85e193739c953358c865005855253af4f68a497John McCall    // For calls and message sends, use the retained-call logic.
2648f85e193739c953358c865005855253af4f68a497John McCall    // Delegate inits are a special case in that they're the only
2649f85e193739c953358c865005855253af4f68a497John McCall    // returns-retained expression that *isn't* surrounded by
2650f85e193739c953358c865005855253af4f68a497John McCall    // a consume.
2651f85e193739c953358c865005855253af4f68a497John McCall    } else if (isa<CallExpr>(e) ||
2652f85e193739c953358c865005855253af4f68a497John McCall               (isa<ObjCMessageExpr>(e) &&
2653f85e193739c953358c865005855253af4f68a497John McCall                !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) {
2654f85e193739c953358c865005855253af4f68a497John McCall      llvm::Value *result = emitARCRetainCall(CGF, e);
2655f85e193739c953358c865005855253af4f68a497John McCall      if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2656f85e193739c953358c865005855253af4f68a497John McCall      return TryEmitResult(result, true);
26574b9c2d235fb9449e249d74f48ecfec601650de93John McCall
26584b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // Look through pseudo-object expressions.
26594b9c2d235fb9449e249d74f48ecfec601650de93John McCall    } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
26604b9c2d235fb9449e249d74f48ecfec601650de93John McCall      TryEmitResult result
26614b9c2d235fb9449e249d74f48ecfec601650de93John McCall        = tryEmitARCRetainPseudoObject(CGF, pseudo);
26624b9c2d235fb9449e249d74f48ecfec601650de93John McCall      if (resultType) {
26634b9c2d235fb9449e249d74f48ecfec601650de93John McCall        llvm::Value *value = result.getPointer();
26644b9c2d235fb9449e249d74f48ecfec601650de93John McCall        value = CGF.Builder.CreateBitCast(value, resultType);
26654b9c2d235fb9449e249d74f48ecfec601650de93John McCall        result.setPointer(value);
26664b9c2d235fb9449e249d74f48ecfec601650de93John McCall      }
26674b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return result;
2668f85e193739c953358c865005855253af4f68a497John McCall    }
2669f85e193739c953358c865005855253af4f68a497John McCall
2670f85e193739c953358c865005855253af4f68a497John McCall    // Conservatively halt the search at any other expression kind.
2671f85e193739c953358c865005855253af4f68a497John McCall    break;
2672f85e193739c953358c865005855253af4f68a497John McCall  }
2673f85e193739c953358c865005855253af4f68a497John McCall
2674f85e193739c953358c865005855253af4f68a497John McCall  // We didn't find an obvious production, so emit what we've got and
2675f85e193739c953358c865005855253af4f68a497John McCall  // tell the caller that we didn't manage to retain.
2676f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *result = CGF.EmitScalarExpr(e);
2677f85e193739c953358c865005855253af4f68a497John McCall  if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2678f85e193739c953358c865005855253af4f68a497John McCall  return TryEmitResult(result, false);
2679f85e193739c953358c865005855253af4f68a497John McCall}
2680f85e193739c953358c865005855253af4f68a497John McCall
2681f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2682f85e193739c953358c865005855253af4f68a497John McCall                                                LValue lvalue,
2683f85e193739c953358c865005855253af4f68a497John McCall                                                QualType type) {
2684f85e193739c953358c865005855253af4f68a497John McCall  TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
2685f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = result.getPointer();
2686f85e193739c953358c865005855253af4f68a497John McCall  if (!result.getInt())
2687f85e193739c953358c865005855253af4f68a497John McCall    value = CGF.EmitARCRetain(type, value);
2688f85e193739c953358c865005855253af4f68a497John McCall  return value;
2689f85e193739c953358c865005855253af4f68a497John McCall}
2690f85e193739c953358c865005855253af4f68a497John McCall
2691f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainScalarExpr - Semantically equivalent to
2692f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
2693f85e193739c953358c865005855253af4f68a497John McCall/// best-effort attempt to peephole expressions that naturally produce
2694f85e193739c953358c865005855253af4f68a497John McCall/// retained objects.
2695f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) {
269672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  // The retain needs to happen within the full-expression.
269772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
269872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall    enterFullExpression(cleanups);
269972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall    RunCleanupsScope scope(*this);
270072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall    return EmitARCRetainScalarExpr(cleanups->getSubExpr());
270172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  }
270272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall
2703f85e193739c953358c865005855253af4f68a497John McCall  TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2704f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = result.getPointer();
2705f85e193739c953358c865005855253af4f68a497John McCall  if (!result.getInt())
2706f85e193739c953358c865005855253af4f68a497John McCall    value = EmitARCRetain(e->getType(), value);
2707f85e193739c953358c865005855253af4f68a497John McCall  return value;
2708f85e193739c953358c865005855253af4f68a497John McCall}
2709f85e193739c953358c865005855253af4f68a497John McCall
2710f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *
2711f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) {
271272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  // The retain needs to happen within the full-expression.
271372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
271472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall    enterFullExpression(cleanups);
271572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall    RunCleanupsScope scope(*this);
271672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall    return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr());
271772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall  }
271872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall
2719f85e193739c953358c865005855253af4f68a497John McCall  TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2720f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = result.getPointer();
2721f85e193739c953358c865005855253af4f68a497John McCall  if (result.getInt())
2722f85e193739c953358c865005855253af4f68a497John McCall    value = EmitARCAutorelease(value);
2723f85e193739c953358c865005855253af4f68a497John McCall  else
2724f85e193739c953358c865005855253af4f68a497John McCall    value = EmitARCRetainAutorelease(e->getType(), value);
2725f85e193739c953358c865005855253af4f68a497John McCall  return value;
2726f85e193739c953358c865005855253af4f68a497John McCall}
2727f85e193739c953358c865005855253af4f68a497John McCall
2728348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) {
2729348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  llvm::Value *result;
2730348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  bool doRetain;
2731348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
2732348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  if (shouldEmitSeparateBlockRetain(e)) {
2733348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    result = EmitScalarExpr(e);
2734348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    doRetain = true;
2735348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  } else {
2736348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e);
2737348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    result = subresult.getPointer();
2738348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    doRetain = !subresult.getInt();
2739348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  }
2740348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
2741348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  if (doRetain)
2742348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    result = EmitARCRetainBlock(result, /*mandatory*/ true);
2743348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  return EmitObjCConsumeObject(e->getType(), result);
2744348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall}
2745348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall
27462b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCallllvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) {
27472b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // In ARC, retain and autorelease the expression.
27484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
27492b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    // Do so before running any cleanups for the full-expression.
275072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall    // EmitARCRetainAutoreleaseScalarExpr does this for us.
27512b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    return EmitARCRetainAutoreleaseScalarExpr(expr);
27522b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  }
27532b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall
27542b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // Otherwise, use the normal scalar-expression emission.  The
27552b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // exception machinery doesn't do anything special with the
27562b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // exception like retaining it, so there's no safety associated with
27572b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // only running cleanups after the throw has started, and when it
27582b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  // matters it tends to be substantially inferior code.
27592b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  return EmitScalarExpr(expr);
27602b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall}
27612b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall
2762f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*>
2763f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e,
2764f85e193739c953358c865005855253af4f68a497John McCall                                    bool ignored) {
2765f85e193739c953358c865005855253af4f68a497John McCall  // Evaluate the RHS first.
2766f85e193739c953358c865005855253af4f68a497John McCall  TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
2767f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = result.getPointer();
2768f85e193739c953358c865005855253af4f68a497John McCall
2769fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  bool hasImmediateRetain = result.getInt();
2770fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall
2771fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  // If we didn't emit a retained object, and the l-value is of block
2772fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  // type, then we need to emit the block-retain immediately in case
2773fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  // it invalidates the l-value.
2774fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  if (!hasImmediateRetain && e->getType()->isBlockPointerType()) {
2775348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall    value = EmitARCRetainBlock(value, /*mandatory*/ false);
2776fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall    hasImmediateRetain = true;
2777fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  }
2778fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall
2779f85e193739c953358c865005855253af4f68a497John McCall  LValue lvalue = EmitLValue(e->getLHS());
2780f85e193739c953358c865005855253af4f68a497John McCall
2781f85e193739c953358c865005855253af4f68a497John McCall  // If the RHS was emitted retained, expand this.
2782fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall  if (hasImmediateRetain) {
2783f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *oldValue =
27846da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman      EmitLoadOfScalar(lvalue);
27856da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman    EmitStoreOfScalar(value, lvalue);
27865b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall    EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime());
2787f85e193739c953358c865005855253af4f68a497John McCall  } else {
2788545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    value = EmitARCStoreStrong(lvalue, value, ignored);
2789f85e193739c953358c865005855253af4f68a497John McCall  }
2790f85e193739c953358c865005855253af4f68a497John McCall
2791f85e193739c953358c865005855253af4f68a497John McCall  return std::pair<LValue,llvm::Value*>(lvalue, value);
2792f85e193739c953358c865005855253af4f68a497John McCall}
2793f85e193739c953358c865005855253af4f68a497John McCall
2794f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*>
2795f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) {
2796f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
2797f85e193739c953358c865005855253af4f68a497John McCall  LValue lvalue = EmitLValue(e->getLHS());
2798f85e193739c953358c865005855253af4f68a497John McCall
27996da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman  EmitStoreOfScalar(value, lvalue);
2800f85e193739c953358c865005855253af4f68a497John McCall
2801f85e193739c953358c865005855253af4f68a497John McCall  return std::pair<LValue,llvm::Value*>(lvalue, value);
2802f85e193739c953358c865005855253af4f68a497John McCall}
2803f85e193739c953358c865005855253af4f68a497John McCall
2804f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolStmt(
280516098f366097305c348b356a44638d6c959c6e60Eric Christopher                                          const ObjCAutoreleasePoolStmt &ARPS) {
2806f85e193739c953358c865005855253af4f68a497John McCall  const Stmt *subStmt = ARPS.getSubStmt();
2807f85e193739c953358c865005855253af4f68a497John McCall  const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
2808f85e193739c953358c865005855253af4f68a497John McCall
2809f85e193739c953358c865005855253af4f68a497John McCall  CGDebugInfo *DI = getDebugInfo();
281073fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
281173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockStart(Builder, S.getLBracLoc());
2812f85e193739c953358c865005855253af4f68a497John McCall
2813f85e193739c953358c865005855253af4f68a497John McCall  // Keep track of the current cleanup stack depth.
2814f85e193739c953358c865005855253af4f68a497John McCall  RunCleanupsScope Scope(*this);
28150a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
2816f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *token = EmitObjCAutoreleasePoolPush();
2817f85e193739c953358c865005855253af4f68a497John McCall    EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
2818f85e193739c953358c865005855253af4f68a497John McCall  } else {
2819f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
2820f85e193739c953358c865005855253af4f68a497John McCall    EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
2821f85e193739c953358c865005855253af4f68a497John McCall  }
2822f85e193739c953358c865005855253af4f68a497John McCall
2823f85e193739c953358c865005855253af4f68a497John McCall  for (CompoundStmt::const_body_iterator I = S.body_begin(),
2824f85e193739c953358c865005855253af4f68a497John McCall       E = S.body_end(); I != E; ++I)
2825f85e193739c953358c865005855253af4f68a497John McCall    EmitStmt(*I);
2826f85e193739c953358c865005855253af4f68a497John McCall
282773fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (DI)
282873fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc());
2829f85e193739c953358c865005855253af4f68a497John McCall}
28300c24c808e4dce43e88abf2d5f98546b901bd4315John McCall
28310c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
28320c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// make sure it survives garbage collection until this point.
28330c24c808e4dce43e88abf2d5f98546b901bd4315John McCallvoid CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
28340c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  // We just use an inline assembly.
28350c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  llvm::FunctionType *extenderType
2836de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All);
28370c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  llvm::Value *extender
28380c24c808e4dce43e88abf2d5f98546b901bd4315John McCall    = llvm::InlineAsm::get(extenderType,
28390c24c808e4dce43e88abf2d5f98546b901bd4315John McCall                           /* assembly */ "",
28400c24c808e4dce43e88abf2d5f98546b901bd4315John McCall                           /* constraints */ "r",
28410c24c808e4dce43e88abf2d5f98546b901bd4315John McCall                           /* side effects */ true);
28420c24c808e4dce43e88abf2d5f98546b901bd4315John McCall
28430c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  object = Builder.CreateBitCast(object, VoidPtrTy);
2844bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  EmitNounwindRuntimeCall(extender, object);
28450c24c808e4dce43e88abf2d5f98546b901bd4315John McCall}
28460c24c808e4dce43e88abf2d5f98546b901bd4315John McCall
284720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with
284884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// non-trivial copy assignment function, produce following helper function.
284984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; }
285084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian///
285184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanianllvm::Constant *
285220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction(
285320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                        const ObjCPropertyImplDecl *PID) {
2854260611a32535c851237926bfcf78869b13c07d5bJohn McCall  if (!getLangOpts().CPlusPlus ||
285590f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola      !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
285684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian    return 0;
285784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  QualType Ty = PID->getPropertyIvarDecl()->getType();
285884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  if (!Ty->isRecordType())
285984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian    return 0;
286084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
286120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
286284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian    return 0;
2863b08cfb3c5909752c0e501a4e5c31a507a4cc1f72Fariborz Jahanian  llvm::Constant * HelperFn = 0;
286420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  if (hasTrivialSetExpr(PID))
286520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    return 0;
286620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null");
286720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty)))
286820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    return HelperFn;
286984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
287084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  ASTContext &C = getContext();
287184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  IdentifierInfo *II
287220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    = &CGM.getContext().Idents.get("__assign_helper_atomic_property_");
287384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  FunctionDecl *FD = FunctionDecl::Create(C,
287484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian                                          C.getTranslationUnitDecl(),
287584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian                                          SourceLocation(),
287684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian                                          SourceLocation(), II, C.VoidTy, 0,
287784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian                                          SC_Static,
287884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian                                          false,
2879b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher                                          false);
288084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
288184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  QualType DestTy = C.getPointerType(Ty);
288284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  QualType SrcTy = Ty;
288384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  SrcTy.addConst();
288484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  SrcTy = C.getPointerType(SrcTy);
288584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
288684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  FunctionArgList args;
288784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy);
288884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  args.push_back(&dstDecl);
288984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy);
289084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  args.push_back(&srcDecl);
289184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
289284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  const CGFunctionInfo &FI =
2893de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args,
2894de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                              FunctionType::ExtInfo(),
2895de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                              RequiredArgs::All);
289684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
2897de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
289884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
289984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  llvm::Function *Fn =
290084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian    llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
290116098f366097305c348b356a44638d6c959c6e60Eric Christopher                           "__assign_helper_atomic_property_",
290216098f366097305c348b356a44638d6c959c6e60Eric Christopher                           &CGM.getModule());
290384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
2904a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov  // Initialize debug info if needed.
2905a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov  maybeInitializeDebugInfo();
290684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
290784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
290884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
2909f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  DeclRefExpr DstExpr(&dstDecl, false, DestTy,
2910f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                      VK_RValue, SourceLocation());
2911f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(),
2912f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                    VK_LValue, OK_Ordinary, SourceLocation());
291384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
2914f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  DeclRefExpr SrcExpr(&srcDecl, false, SrcTy,
2915f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                      VK_RValue, SourceLocation());
2916f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
2917f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                    VK_LValue, OK_Ordinary, SourceLocation());
291884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
2919f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  Expr *Args[2] = { &DST, &SRC };
292020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment());
2921f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(),
29223b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                              Args, DestTy->getPointeeType(),
2923be9af1288881110e406b87914162eaa59f1e5918Lang Hames                              VK_LValue, SourceLocation(), false);
2924f4b88a45902af1802a1cb42ba48b1c474474f228John McCall
2925f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  EmitStmt(&TheCall);
292684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
292784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian  FinishFunction();
2928cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
292920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  CGM.setAtomicSetterHelperFnMap(Ty, HelperFn);
2930cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian  return HelperFn;
293120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian}
293220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
293320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianllvm::Constant *
293420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction(
293520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                            const ObjCPropertyImplDecl *PID) {
2936260611a32535c851237926bfcf78869b13c07d5bJohn McCall  if (!getLangOpts().CPlusPlus ||
293790f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola      !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
293820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    return 0;
293920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
294020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  QualType Ty = PD->getType();
294120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  if (!Ty->isRecordType())
294220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    return 0;
294320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
294420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    return 0;
294520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  llvm::Constant * HelperFn = 0;
294620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
294720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  if (hasTrivialGetExpr(PID))
294820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    return 0;
294920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null");
295020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty)))
295120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    return HelperFn;
295220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
295320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
295420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  ASTContext &C = getContext();
295520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  IdentifierInfo *II
295620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  = &CGM.getContext().Idents.get("__copy_helper_atomic_property_");
295720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  FunctionDecl *FD = FunctionDecl::Create(C,
295820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                          C.getTranslationUnitDecl(),
295920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                          SourceLocation(),
296020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                          SourceLocation(), II, C.VoidTy, 0,
296120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                          SC_Static,
296220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                          false,
2963b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher                                          false);
296420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
296520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  QualType DestTy = C.getPointerType(Ty);
296620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  QualType SrcTy = Ty;
296720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  SrcTy.addConst();
296820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  SrcTy = C.getPointerType(SrcTy);
296920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
297020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  FunctionArgList args;
297120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy);
297220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  args.push_back(&dstDecl);
297320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy);
297420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  args.push_back(&srcDecl);
297520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
297620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  const CGFunctionInfo &FI =
2977de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args,
2978de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                            FunctionType::ExtInfo(),
2979de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                            RequiredArgs::All);
298020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
2981de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
298220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
298320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  llvm::Function *Fn =
298420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
298520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                         "__copy_helper_atomic_property_", &CGM.getModule());
298684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
2987a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov  // Initialize debug info if needed.
2988a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov  maybeInitializeDebugInfo();
298920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
299020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
299120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
2992f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  DeclRefExpr SrcExpr(&srcDecl, false, SrcTy,
299320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                      VK_RValue, SourceLocation());
299420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
2995f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
2996f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                    VK_LValue, OK_Ordinary, SourceLocation());
299720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
299820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  CXXConstructExpr *CXXConstExpr =
299920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    cast<CXXConstructExpr>(PID->getGetterCXXConstructor());
300020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
300120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  SmallVector<Expr*, 4> ConstructorArgs;
3002f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  ConstructorArgs.push_back(&SRC);
300320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  CXXConstructExpr::arg_iterator A = CXXConstExpr->arg_begin();
300420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  ++A;
300520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
300620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  for (CXXConstructExpr::arg_iterator AEnd = CXXConstExpr->arg_end();
300720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian       A != AEnd; ++A)
300820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    ConstructorArgs.push_back(*A);
300920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
301020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  CXXConstructExpr *TheCXXConstructExpr =
301120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian    CXXConstructExpr::Create(C, Ty, SourceLocation(),
301220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                             CXXConstExpr->getConstructor(),
301320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                             CXXConstExpr->isElidable(),
30143b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                             ConstructorArgs,
30155b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                             CXXConstExpr->hadMultipleCandidates(),
30165b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                             CXXConstExpr->isListInitialization(),
301720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                             CXXConstExpr->requiresZeroInitialization(),
301816098f366097305c348b356a44638d6c959c6e60Eric Christopher                             CXXConstExpr->getConstructionKind(),
301916098f366097305c348b356a44638d6c959c6e60Eric Christopher                             SourceRange());
302020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
3021f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  DeclRefExpr DstExpr(&dstDecl, false, DestTy,
3022f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                      VK_RValue, SourceLocation());
302320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
3024f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  RValue DV = EmitAnyExpr(&DstExpr);
302516098f366097305c348b356a44638d6c959c6e60Eric Christopher  CharUnits Alignment
302616098f366097305c348b356a44638d6c959c6e60Eric Christopher    = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType());
302720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  EmitAggExpr(TheCXXConstructExpr,
302820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian              AggValueSlot::forAddr(DV.getScalarVal(), Alignment, Qualifiers(),
302920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                    AggValueSlot::IsDestructed,
303020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                    AggValueSlot::DoesNotNeedGCBarriers,
3031649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                    AggValueSlot::IsNotAliased));
303220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian
303320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  FinishFunction();
303420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
303520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  CGM.setAtomicGetterHelperFnMap(Ty, HelperFn);
303620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  return HelperFn;
303784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian}
303884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
3039cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedmanllvm::Value *
3040cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli FriedmanCodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) {
3041cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  // Get selectors for retain/autorelease.
30428c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman  IdentifierInfo *CopyID = &getContext().Idents.get("copy");
30438c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman  Selector CopySelector =
30448c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman      getContext().Selectors.getNullarySelector(CopyID);
3045cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease");
3046cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  Selector AutoreleaseSelector =
3047cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman      getContext().Selectors.getNullarySelector(AutoreleaseID);
3048cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman
3049cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  // Emit calls to retain/autorelease.
3050cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
3051cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  llvm::Value *Val = Block;
3052cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  RValue Result;
3053cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
30548c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman                                       Ty, CopySelector,
3055cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman                                       Val, CallArgList(), 0, 0);
3056cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  Val = Result.getScalarVal();
3057cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3058cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman                                       Ty, AutoreleaseSelector,
3059cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman                                       Val, CallArgList(), 0, 0);
3060cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  Val = Result.getScalarVal();
3061cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  return Val;
3062cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman}
3063cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman
306484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian
30652979ec73b4f974d85f2ce84167712177a44c6f09Ted KremenekCGObjCRuntime::~CGObjCRuntime() {}
3066