| Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1 | //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | // This contains code to emit Objective-C code as LLVM code. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
| Devang Patel | d2d6665 | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" | 
| Ted Kremenek | 43e0633 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 15 | #include "CGObjCRuntime.h" | 
| Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" | 
|  | 17 | #include "CodeGenModule.h" | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 18 | #include "TargetInfo.h" | 
| Daniel Dunbar | 9e22c0d | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" | 
| Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" | 
| Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtObjC.h" | 
| Daniel Dunbar | c5d3304 | 2008-09-03 00:27:26 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" | 
| Anders Carlsson | 2e744e8 | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" | 
| Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 25 | #include "llvm/InlineAsm.h" | 
| Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 26 | using namespace clang; | 
|  | 27 | using namespace CodeGen; | 
|  | 28 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 29 | typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult; | 
|  | 30 | static TryEmitResult | 
|  | 31 | tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e); | 
|  | 32 |  | 
|  | 33 | /// Given the address of a variable of pointer type, find the correct | 
|  | 34 | /// null to store into it. | 
|  | 35 | static llvm::Constant *getNullForVariable(llvm::Value *addr) { | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 36 | llvm::Type *type = | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 37 | cast<llvm::PointerType>(addr->getType())->getElementType(); | 
|  | 38 | return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); | 
|  | 39 | } | 
|  | 40 |  | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 41 | /// Emits an instance of NSConstantString representing the object. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) | 
| Daniel Dunbar | 44b58a2 | 2008-11-25 21:53:21 +0000 | [diff] [blame] | 43 | { | 
| David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 44 | llvm::Constant *C = | 
|  | 45 | CGM.getObjCRuntime().GenerateConstantString(E->getString()); | 
| Daniel Dunbar | 66912a1 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 46 | // FIXME: This bitcast should just be made an invariant on the Runtime. | 
| Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 47 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
|  | 50 | /// Emit a selector. | 
|  | 51 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { | 
|  | 52 | // Untyped selector. | 
|  | 53 | // Note that this implementation allows for non-constant strings to be passed | 
|  | 54 | // as arguments to @selector().  Currently, the only thing preventing this | 
|  | 55 | // behaviour is the type checking in the front end. | 
| Daniel Dunbar | 45858d2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 56 | return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector()); | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Daniel Dunbar | 66912a1 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 59 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { | 
|  | 60 | // FIXME: This should pass the Decl not the name. | 
|  | 61 | return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol()); | 
|  | 62 | } | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 63 |  | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 64 | /// \brief Adjust the type of the result of an Objective-C message send | 
|  | 65 | /// expression when the method has a related result type. | 
|  | 66 | static RValue AdjustRelatedResultType(CodeGenFunction &CGF, | 
|  | 67 | const Expr *E, | 
|  | 68 | const ObjCMethodDecl *Method, | 
|  | 69 | RValue Result) { | 
|  | 70 | if (!Method) | 
|  | 71 | return Result; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 72 |  | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 73 | if (!Method->hasRelatedResultType() || | 
|  | 74 | CGF.getContext().hasSameType(E->getType(), Method->getResultType()) || | 
|  | 75 | !Result.isScalar()) | 
|  | 76 | return Result; | 
|  | 77 |  | 
|  | 78 | // We have applied a related result type. Cast the rvalue appropriately. | 
|  | 79 | return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), | 
|  | 80 | CGF.ConvertType(E->getType()))); | 
|  | 81 | } | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 82 |  | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 83 | /// Decide whether to extend the lifetime of the receiver of a | 
|  | 84 | /// returns-inner-pointer message. | 
|  | 85 | static bool | 
|  | 86 | shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) { | 
|  | 87 | switch (message->getReceiverKind()) { | 
|  | 88 |  | 
|  | 89 | // For a normal instance message, we should extend unless the | 
|  | 90 | // receiver is loaded from a variable with precise lifetime. | 
|  | 91 | case ObjCMessageExpr::Instance: { | 
|  | 92 | const Expr *receiver = message->getInstanceReceiver(); | 
|  | 93 | const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver); | 
|  | 94 | if (!ice || ice->getCastKind() != CK_LValueToRValue) return true; | 
|  | 95 | receiver = ice->getSubExpr()->IgnoreParens(); | 
|  | 96 |  | 
|  | 97 | // Only __strong variables. | 
|  | 98 | if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong) | 
|  | 99 | return true; | 
|  | 100 |  | 
|  | 101 | // All ivars and fields have precise lifetime. | 
|  | 102 | if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver)) | 
|  | 103 | return false; | 
|  | 104 |  | 
|  | 105 | // Otherwise, check for variables. | 
|  | 106 | const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr()); | 
|  | 107 | if (!declRef) return true; | 
|  | 108 | const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl()); | 
|  | 109 | if (!var) return true; | 
|  | 110 |  | 
|  | 111 | // All variables have precise lifetime except local variables with | 
|  | 112 | // automatic storage duration that aren't specially marked. | 
|  | 113 | return (var->hasLocalStorage() && | 
|  | 114 | !var->hasAttr<ObjCPreciseLifetimeAttr>()); | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | case ObjCMessageExpr::Class: | 
|  | 118 | case ObjCMessageExpr::SuperClass: | 
|  | 119 | // It's never necessary for class objects. | 
|  | 120 | return false; | 
|  | 121 |  | 
|  | 122 | case ObjCMessageExpr::SuperInstance: | 
|  | 123 | // We generally assume that 'self' lives throughout a method call. | 
|  | 124 | return false; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | llvm_unreachable("invalid receiver kind"); | 
|  | 128 | } | 
|  | 129 |  | 
| John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 130 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, | 
|  | 131 | ReturnValueSlot Return) { | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 132 | // Only the lookup mechanism and first two arguments of the method | 
|  | 133 | // implementation vary between runtimes.  We can get the receiver and | 
|  | 134 | // arguments in generic code. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 136 | bool isDelegateInit = E->isDelegateInitCall(); | 
|  | 137 |  | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 138 | const ObjCMethodDecl *method = E->getMethodDecl(); | 
|  | 139 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 140 | // We don't retain the receiver in delegate init calls, and this is | 
|  | 141 | // safe because the receiver value is always loaded from 'self', | 
|  | 142 | // which we zero out.  We don't want to Block_copy block receivers, | 
|  | 143 | // though. | 
|  | 144 | bool retainSelf = | 
|  | 145 | (!isDelegateInit && | 
|  | 146 | CGM.getLangOptions().ObjCAutoRefCount && | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 147 | method && | 
|  | 148 | method->hasAttr<NSConsumesSelfAttr>()); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 149 |  | 
| Daniel Dunbar | 8d48059 | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 150 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 151 | bool isSuperMessage = false; | 
| Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 152 | bool isClassMessage = false; | 
| David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 153 | ObjCInterfaceDecl *OID = 0; | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 154 | // Find the receiver | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 155 | QualType ReceiverType; | 
| Daniel Dunbar | b219780 | 2010-04-22 03:17:06 +0000 | [diff] [blame] | 156 | llvm::Value *Receiver = 0; | 
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 157 | switch (E->getReceiverKind()) { | 
|  | 158 | case ObjCMessageExpr::Instance: | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 159 | ReceiverType = E->getInstanceReceiver()->getType(); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 160 | if (retainSelf) { | 
|  | 161 | TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, | 
|  | 162 | E->getInstanceReceiver()); | 
|  | 163 | Receiver = ter.getPointer(); | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 164 | if (ter.getInt()) retainSelf = false; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 165 | } else | 
|  | 166 | Receiver = EmitScalarExpr(E->getInstanceReceiver()); | 
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 167 | break; | 
| Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 168 |  | 
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 169 | case ObjCMessageExpr::Class: { | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 170 | ReceiverType = E->getClassReceiver(); | 
|  | 171 | const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); | 
| John McCall | 3e29492 | 2010-05-17 20:12:43 +0000 | [diff] [blame] | 172 | assert(ObjTy && "Invalid Objective-C class message send"); | 
|  | 173 | OID = ObjTy->getInterface(); | 
|  | 174 | assert(OID && "Invalid Objective-C class message send"); | 
| David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 175 | Receiver = Runtime.GetClass(Builder, OID); | 
| Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 176 | isClassMessage = true; | 
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 177 | break; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | case ObjCMessageExpr::SuperInstance: | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 181 | ReceiverType = E->getSuperType(); | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 182 | Receiver = LoadObjCSelf(); | 
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 183 | isSuperMessage = true; | 
|  | 184 | break; | 
|  | 185 |  | 
|  | 186 | case ObjCMessageExpr::SuperClass: | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 187 | ReceiverType = E->getSuperType(); | 
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 188 | Receiver = LoadObjCSelf(); | 
|  | 189 | isSuperMessage = true; | 
|  | 190 | isClassMessage = true; | 
|  | 191 | break; | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 194 | if (retainSelf) | 
|  | 195 | Receiver = EmitARCRetainNonBlock(Receiver); | 
|  | 196 |  | 
|  | 197 | // In ARC, we sometimes want to "extend the lifetime" | 
|  | 198 | // (i.e. retain+autorelease) of receivers of returns-inner-pointer | 
|  | 199 | // messages. | 
|  | 200 | if (getLangOptions().ObjCAutoRefCount && method && | 
|  | 201 | method->hasAttr<ObjCReturnsInnerPointerAttr>() && | 
|  | 202 | shouldExtendReceiverForInnerPointerMessage(E)) | 
|  | 203 | Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver); | 
|  | 204 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 205 | QualType ResultType = | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 206 | method ? method->getResultType() : E->getType(); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 207 |  | 
| Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 208 | CallArgList Args; | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 209 | EmitCallArgs(Args, method, E->arg_begin(), E->arg_end()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 211 | // For delegate init calls in ARC, do an unsafe store of null into | 
|  | 212 | // self.  This represents the call taking direct ownership of that | 
|  | 213 | // value.  We have to do this after emitting the other call | 
|  | 214 | // arguments because they might also reference self, but we don't | 
|  | 215 | // have to worry about any of them modifying self because that would | 
|  | 216 | // be an undefined read and write of an object in unordered | 
|  | 217 | // expressions. | 
|  | 218 | if (isDelegateInit) { | 
|  | 219 | assert(getLangOptions().ObjCAutoRefCount && | 
|  | 220 | "delegate init calls should only be marked in ARC"); | 
|  | 221 |  | 
|  | 222 | // Do an unsafe store of null into self. | 
|  | 223 | llvm::Value *selfAddr = | 
|  | 224 | LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; | 
|  | 225 | assert(selfAddr && "no self entry for a delegate init call?"); | 
|  | 226 |  | 
|  | 227 | Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); | 
|  | 228 | } | 
| Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 229 |  | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 230 | RValue result; | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 231 | if (isSuperMessage) { | 
| Chris Lattner | 6cfec78 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 232 | // super is only valid in an Objective-C method | 
|  | 233 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); | 
| Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 234 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 235 | result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, | 
|  | 236 | E->getSelector(), | 
|  | 237 | OMD->getClassInterface(), | 
|  | 238 | isCategoryImpl, | 
|  | 239 | Receiver, | 
|  | 240 | isClassMessage, | 
|  | 241 | Args, | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 242 | method); | 
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 243 | } else { | 
|  | 244 | result = Runtime.GenerateMessageSend(*this, Return, ResultType, | 
|  | 245 | E->getSelector(), | 
|  | 246 | Receiver, Args, OID, | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 247 | method); | 
| Chris Lattner | b1d329d | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 248 | } | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 249 |  | 
|  | 250 | // For delegate init calls in ARC, implicitly store the result of | 
|  | 251 | // the call back into self.  This takes ownership of the value. | 
|  | 252 | if (isDelegateInit) { | 
|  | 253 | llvm::Value *selfAddr = | 
|  | 254 | LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; | 
|  | 255 | llvm::Value *newSelf = result.getScalarVal(); | 
|  | 256 |  | 
|  | 257 | // The delegate return type isn't necessarily a matching type; in | 
|  | 258 | // fact, it's quite likely to be 'id'. | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 259 | llvm::Type *selfTy = | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 260 | cast<llvm::PointerType>(selfAddr->getType())->getElementType(); | 
|  | 261 | newSelf = Builder.CreateBitCast(newSelf, selfTy); | 
|  | 262 |  | 
|  | 263 | Builder.CreateStore(newSelf, selfAddr); | 
|  | 264 | } | 
|  | 265 |  | 
| John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 266 | return AdjustRelatedResultType(*this, E, method, result); | 
| Anders Carlsson | 76f4a90 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 267 | } | 
|  | 268 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 269 | namespace { | 
|  | 270 | struct FinishARCDealloc : EHScopeStack::Cleanup { | 
| John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 271 | void Emit(CodeGenFunction &CGF, Flags flags) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 272 | const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); | 
| John McCall | dffafde | 2011-07-13 18:26:47 +0000 | [diff] [blame] | 273 |  | 
|  | 274 | const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 275 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); | 
|  | 276 | if (!iface->getSuperClass()) return; | 
|  | 277 |  | 
| John McCall | dffafde | 2011-07-13 18:26:47 +0000 | [diff] [blame] | 278 | bool isCategory = isa<ObjCCategoryImplDecl>(impl); | 
|  | 279 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 280 | // Call [super dealloc] if we have a superclass. | 
|  | 281 | llvm::Value *self = CGF.LoadObjCSelf(); | 
|  | 282 |  | 
|  | 283 | CallArgList args; | 
|  | 284 | CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), | 
|  | 285 | CGF.getContext().VoidTy, | 
|  | 286 | method->getSelector(), | 
|  | 287 | iface, | 
| John McCall | dffafde | 2011-07-13 18:26:47 +0000 | [diff] [blame] | 288 | isCategory, | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 289 | self, | 
|  | 290 | /*is class msg*/ false, | 
|  | 291 | args, | 
|  | 292 | method); | 
|  | 293 | } | 
|  | 294 | }; | 
|  | 295 | } | 
|  | 296 |  | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 297 | /// StartObjCMethod - Begin emission of an ObjCMethod. This generates | 
|  | 298 | /// the LLVM function and sets the other context used by | 
|  | 299 | /// CodeGenFunction. | 
| Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 300 | void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, | 
| Devang Patel | e7ce540 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 301 | const ObjCContainerDecl *CD, | 
|  | 302 | SourceLocation StartLoc) { | 
| John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 303 | FunctionArgList args; | 
| Devang Patel | a2c048e | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 304 | // Check if we should generate debug info for this method. | 
| Devang Patel | d6ffebb | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 305 | if (CGM.getModuleDebugInfo() && !OMD->hasAttr<NoDebugAttr>()) | 
|  | 306 | DebugInfo = CGM.getModuleDebugInfo(); | 
| Devang Patel | a2c048e | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 307 |  | 
| Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 308 | llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); | 
| Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 309 |  | 
| Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 310 | const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD); | 
|  | 311 | CGM.SetInternalFunctionAttributes(OMD, Fn, FI); | 
| Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 312 |  | 
| John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 313 | args.push_back(OMD->getSelfDecl()); | 
|  | 314 | args.push_back(OMD->getCmdDecl()); | 
| Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 315 |  | 
| Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 316 | for (ObjCMethodDecl::param_const_iterator PI = OMD->param_begin(), | 
| Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 317 | E = OMD->param_end(); PI != E; ++PI) | 
| John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 318 | args.push_back(*PI); | 
| Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 319 |  | 
| Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 320 | CurGD = OMD; | 
|  | 321 |  | 
| Devang Patel | e7ce540 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 322 | StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 323 |  | 
|  | 324 | // In ARC, certain methods get an extra cleanup. | 
|  | 325 | if (CGM.getLangOptions().ObjCAutoRefCount && | 
|  | 326 | OMD->isInstanceMethod() && | 
|  | 327 | OMD->getSelector().isUnarySelector()) { | 
|  | 328 | const IdentifierInfo *ident = | 
|  | 329 | OMD->getSelector().getIdentifierInfoForSlot(0); | 
|  | 330 | if (ident->isStr("dealloc")) | 
|  | 331 | EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); | 
|  | 332 | } | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 333 | } | 
| Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 334 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 335 | static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, | 
|  | 336 | LValue lvalue, QualType type); | 
|  | 337 |  | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 338 | /// Generate an Objective-C method.  An Objective-C method is a C function with | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | /// its pointer, name, and types registered in the class struture. | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 340 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { | 
| Devang Patel | e7ce540 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 341 | StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart()); | 
| Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 342 | EmitStmt(OMD->getBody()); | 
|  | 343 | FinishFunction(OMD->getBodyRBrace()); | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 344 | } | 
|  | 345 |  | 
| John McCall | b923ece | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 346 | /// emitStructGetterCall - Call the runtime function to load a property | 
|  | 347 | /// into the return value slot. | 
|  | 348 | static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, | 
|  | 349 | bool isAtomic, bool hasStrong) { | 
|  | 350 | ASTContext &Context = CGF.getContext(); | 
|  | 351 |  | 
|  | 352 | llvm::Value *src = | 
|  | 353 | CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), | 
|  | 354 | ivar, 0).getAddress(); | 
|  | 355 |  | 
|  | 356 | // objc_copyStruct (ReturnValue, &structIvar, | 
|  | 357 | //                  sizeof (Type of Ivar), isAtomic, false); | 
|  | 358 | CallArgList args; | 
|  | 359 |  | 
|  | 360 | llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy); | 
|  | 361 | args.add(RValue::get(dest), Context.VoidPtrTy); | 
|  | 362 |  | 
|  | 363 | src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy); | 
|  | 364 | args.add(RValue::get(src), Context.VoidPtrTy); | 
|  | 365 |  | 
|  | 366 | CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType()); | 
|  | 367 | args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType()); | 
|  | 368 | args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy); | 
|  | 369 | args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy); | 
|  | 370 |  | 
|  | 371 | llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction(); | 
|  | 372 | CGF.EmitCall(CGF.getTypes().getFunctionInfo(Context.VoidTy, args, | 
|  | 373 | FunctionType::ExtInfo()), | 
|  | 374 | fn, ReturnValueSlot(), args); | 
|  | 375 | } | 
|  | 376 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 377 | /// Determine whether the given architecture supports unaligned atomic | 
|  | 378 | /// accesses.  They don't have to be fast, just faster than a function | 
|  | 379 | /// call and a mutex. | 
|  | 380 | static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) { | 
| Eli Friedman | 5be3e6a | 2011-09-13 20:48:30 +0000 | [diff] [blame] | 381 | // FIXME: Allow unaligned atomic load/store on x86.  (It is not | 
|  | 382 | // currently supported by the backend.) | 
|  | 383 | return 0; | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 384 | } | 
|  | 385 |  | 
|  | 386 | /// Return the maximum size that permits atomic accesses for the given | 
|  | 387 | /// architecture. | 
|  | 388 | static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, | 
|  | 389 | llvm::Triple::ArchType arch) { | 
|  | 390 | // ARM has 8-byte atomic accesses, but it's not clear whether we | 
|  | 391 | // want to rely on them here. | 
|  | 392 |  | 
|  | 393 | // In the default case, just assume that any size up to a pointer is | 
|  | 394 | // fine given adequate alignment. | 
|  | 395 | return CharUnits::fromQuantity(CGM.PointerSizeInBytes); | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | namespace { | 
|  | 399 | class PropertyImplStrategy { | 
|  | 400 | public: | 
|  | 401 | enum StrategyKind { | 
|  | 402 | /// The 'native' strategy is to use the architecture's provided | 
|  | 403 | /// reads and writes. | 
|  | 404 | Native, | 
|  | 405 |  | 
|  | 406 | /// Use objc_setProperty and objc_getProperty. | 
|  | 407 | GetSetProperty, | 
|  | 408 |  | 
|  | 409 | /// Use objc_setProperty for the setter, but use expression | 
|  | 410 | /// evaluation for the getter. | 
|  | 411 | SetPropertyAndExpressionGet, | 
|  | 412 |  | 
|  | 413 | /// Use objc_copyStruct. | 
|  | 414 | CopyStruct, | 
|  | 415 |  | 
|  | 416 | /// The 'expression' strategy is to emit normal assignment or | 
|  | 417 | /// lvalue-to-rvalue expressions. | 
|  | 418 | Expression | 
|  | 419 | }; | 
|  | 420 |  | 
|  | 421 | StrategyKind getKind() const { return StrategyKind(Kind); } | 
|  | 422 |  | 
|  | 423 | bool hasStrongMember() const { return HasStrong; } | 
|  | 424 | bool isAtomic() const { return IsAtomic; } | 
|  | 425 | bool isCopy() const { return IsCopy; } | 
|  | 426 |  | 
|  | 427 | CharUnits getIvarSize() const { return IvarSize; } | 
|  | 428 | CharUnits getIvarAlignment() const { return IvarAlignment; } | 
|  | 429 |  | 
|  | 430 | PropertyImplStrategy(CodeGenModule &CGM, | 
|  | 431 | const ObjCPropertyImplDecl *propImpl); | 
|  | 432 |  | 
|  | 433 | private: | 
|  | 434 | unsigned Kind : 8; | 
|  | 435 | unsigned IsAtomic : 1; | 
|  | 436 | unsigned IsCopy : 1; | 
|  | 437 | unsigned HasStrong : 1; | 
|  | 438 |  | 
|  | 439 | CharUnits IvarSize; | 
|  | 440 | CharUnits IvarAlignment; | 
|  | 441 | }; | 
|  | 442 | } | 
|  | 443 |  | 
|  | 444 | /// Pick an implementation strategy for the the given property synthesis. | 
|  | 445 | PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, | 
|  | 446 | const ObjCPropertyImplDecl *propImpl) { | 
|  | 447 | const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); | 
| John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 448 | ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind(); | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 449 |  | 
| John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 450 | IsCopy = (setterKind == ObjCPropertyDecl::Copy); | 
|  | 451 | IsAtomic = prop->isAtomic(); | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 452 | HasStrong = false; // doesn't matter here. | 
|  | 453 |  | 
|  | 454 | // Evaluate the ivar's size and alignment. | 
|  | 455 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); | 
|  | 456 | QualType ivarType = ivar->getType(); | 
|  | 457 | llvm::tie(IvarSize, IvarAlignment) | 
|  | 458 | = CGM.getContext().getTypeInfoInChars(ivarType); | 
|  | 459 |  | 
|  | 460 | // If we have a copy property, we always have to use getProperty/setProperty. | 
| John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 461 | // TODO: we could actually use setProperty and an expression for non-atomics. | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 462 | if (IsCopy) { | 
|  | 463 | Kind = GetSetProperty; | 
|  | 464 | return; | 
|  | 465 | } | 
|  | 466 |  | 
| John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 467 | // Handle retain. | 
|  | 468 | if (setterKind == ObjCPropertyDecl::Retain) { | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 469 | // In GC-only, there's nothing special that needs to be done. | 
| Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 470 | if (CGM.getLangOptions().getGC() == LangOptions::GCOnly) { | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 471 | // fallthrough | 
|  | 472 |  | 
|  | 473 | // In ARC, if the property is non-atomic, use expression emission, | 
|  | 474 | // which translates to objc_storeStrong.  This isn't required, but | 
|  | 475 | // it's slightly nicer. | 
|  | 476 | } else if (CGM.getLangOptions().ObjCAutoRefCount && !IsAtomic) { | 
|  | 477 | Kind = Expression; | 
|  | 478 | return; | 
|  | 479 |  | 
|  | 480 | // Otherwise, we need to at least use setProperty.  However, if | 
|  | 481 | // the property isn't atomic, we can use normal expression | 
|  | 482 | // emission for the getter. | 
|  | 483 | } else if (!IsAtomic) { | 
|  | 484 | Kind = SetPropertyAndExpressionGet; | 
|  | 485 | return; | 
|  | 486 |  | 
|  | 487 | // Otherwise, we have to use both setProperty and getProperty. | 
|  | 488 | } else { | 
|  | 489 | Kind = GetSetProperty; | 
|  | 490 | return; | 
|  | 491 | } | 
|  | 492 | } | 
|  | 493 |  | 
|  | 494 | // If we're not atomic, just use expression accesses. | 
|  | 495 | if (!IsAtomic) { | 
|  | 496 | Kind = Expression; | 
|  | 497 | return; | 
|  | 498 | } | 
|  | 499 |  | 
| John McCall | 0e5c086 | 2011-09-13 05:36:29 +0000 | [diff] [blame] | 500 | // Properties on bitfield ivars need to be emitted using expression | 
|  | 501 | // accesses even if they're nominally atomic. | 
|  | 502 | if (ivar->isBitField()) { | 
|  | 503 | Kind = Expression; | 
|  | 504 | return; | 
|  | 505 | } | 
|  | 506 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 507 | // GC-qualified or ARC-qualified ivars need to be emitted as | 
|  | 508 | // expressions.  This actually works out to being atomic anyway, | 
|  | 509 | // except for ARC __strong, but that should trigger the above code. | 
|  | 510 | if (ivarType.hasNonTrivialObjCLifetime() || | 
| Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 511 | (CGM.getLangOptions().getGC() && | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 512 | CGM.getContext().getObjCGCAttrKind(ivarType))) { | 
|  | 513 | Kind = Expression; | 
|  | 514 | return; | 
|  | 515 | } | 
|  | 516 |  | 
|  | 517 | // Compute whether the ivar has strong members. | 
| Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 518 | if (CGM.getLangOptions().getGC()) | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 519 | if (const RecordType *recordType = ivarType->getAs<RecordType>()) | 
|  | 520 | HasStrong = recordType->getDecl()->hasObjectMember(); | 
|  | 521 |  | 
|  | 522 | // We can never access structs with object members with a native | 
|  | 523 | // access, because we need to use write barriers.  This is what | 
|  | 524 | // objc_copyStruct is for. | 
|  | 525 | if (HasStrong) { | 
|  | 526 | Kind = CopyStruct; | 
|  | 527 | return; | 
|  | 528 | } | 
|  | 529 |  | 
|  | 530 | // Otherwise, this is target-dependent and based on the size and | 
|  | 531 | // alignment of the ivar. | 
| John McCall | 0bef0ba | 2011-09-13 07:33:34 +0000 | [diff] [blame] | 532 |  | 
|  | 533 | // If the size of the ivar is not a power of two, give up.  We don't | 
|  | 534 | // want to get into the business of doing compare-and-swaps. | 
|  | 535 | if (!IvarSize.isPowerOfTwo()) { | 
|  | 536 | Kind = CopyStruct; | 
|  | 537 | return; | 
|  | 538 | } | 
|  | 539 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 540 | llvm::Triple::ArchType arch = | 
|  | 541 | CGM.getContext().getTargetInfo().getTriple().getArch(); | 
|  | 542 |  | 
|  | 543 | // Most architectures require memory to fit within a single cache | 
|  | 544 | // line, so the alignment has to be at least the size of the access. | 
|  | 545 | // Otherwise we have to grab a lock. | 
|  | 546 | if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) { | 
|  | 547 | Kind = CopyStruct; | 
|  | 548 | return; | 
|  | 549 | } | 
|  | 550 |  | 
|  | 551 | // If the ivar's size exceeds the architecture's maximum atomic | 
|  | 552 | // access size, we have to use CopyStruct. | 
|  | 553 | if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) { | 
|  | 554 | Kind = CopyStruct; | 
|  | 555 | return; | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | // Otherwise, we can use native loads and stores. | 
|  | 559 | Kind = Native; | 
|  | 560 | } | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 561 |  | 
|  | 562 | /// GenerateObjCGetter - Generate an Objective-C property getter | 
| Steve Naroff | 5a7dd78 | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 563 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize | 
|  | 564 | /// is illegal within a category. | 
| Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 565 | void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, | 
|  | 566 | const ObjCPropertyImplDecl *PID) { | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 567 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); | 
|  | 568 | ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); | 
|  | 569 | assert(OMD && "Invalid call to generate getter (empty method)"); | 
| Devang Patel | e7ce540 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 570 | StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 572 | generateObjCGetterBody(IMP, PID); | 
|  | 573 |  | 
|  | 574 | FinishFunction(); | 
|  | 575 | } | 
|  | 576 |  | 
| John McCall | bdd8185 | 2011-09-13 06:00:03 +0000 | [diff] [blame] | 577 | static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) { | 
|  | 578 | const Expr *getter = propImpl->getGetterCXXConstructor(); | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 579 | if (!getter) return true; | 
|  | 580 |  | 
|  | 581 | // Sema only makes only of these when the ivar has a C++ class type, | 
|  | 582 | // so the form is pretty constrained. | 
|  | 583 |  | 
| John McCall | bdd8185 | 2011-09-13 06:00:03 +0000 | [diff] [blame] | 584 | // If the property has a reference type, we might just be binding a | 
|  | 585 | // reference, in which case the result will be a gl-value.  We should | 
|  | 586 | // treat this as a non-trivial operation. | 
|  | 587 | if (getter->isGLValue()) | 
|  | 588 | return false; | 
|  | 589 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 590 | // If we selected a trivial copy-constructor, we're okay. | 
|  | 591 | if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter)) | 
|  | 592 | return (construct->getConstructor()->isTrivial()); | 
|  | 593 |  | 
|  | 594 | // The constructor might require cleanups (in which case it's never | 
|  | 595 | // trivial). | 
|  | 596 | assert(isa<ExprWithCleanups>(getter)); | 
|  | 597 | return false; | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | void | 
|  | 601 | CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, | 
|  | 602 | const ObjCPropertyImplDecl *propImpl) { | 
|  | 603 | // If there's a non-trivial 'get' expression, we just have to emit that. | 
|  | 604 | if (!hasTrivialGetExpr(propImpl)) { | 
|  | 605 | ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(), | 
|  | 606 | /*nrvo*/ 0); | 
|  | 607 | EmitReturnStmt(ret); | 
|  | 608 | return; | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); | 
|  | 612 | QualType propType = prop->getType(); | 
|  | 613 | ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl(); | 
|  | 614 |  | 
|  | 615 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); | 
|  | 616 |  | 
|  | 617 | // Pick an implementation strategy. | 
|  | 618 | PropertyImplStrategy strategy(CGM, propImpl); | 
|  | 619 | switch (strategy.getKind()) { | 
|  | 620 | case PropertyImplStrategy::Native: { | 
|  | 621 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); | 
|  | 622 |  | 
|  | 623 | // Currently, all atomic accesses have to be through integer | 
|  | 624 | // types, so there's no point in trying to pick a prettier type. | 
|  | 625 | llvm::Type *bitcastType = | 
|  | 626 | llvm::Type::getIntNTy(getLLVMContext(), | 
|  | 627 | getContext().toBits(strategy.getIvarSize())); | 
|  | 628 | bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay | 
|  | 629 |  | 
|  | 630 | // Perform an atomic load.  This does not impose ordering constraints. | 
|  | 631 | llvm::Value *ivarAddr = LV.getAddress(); | 
|  | 632 | ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); | 
|  | 633 | llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); | 
|  | 634 | load->setAlignment(strategy.getIvarAlignment().getQuantity()); | 
|  | 635 | load->setAtomic(llvm::Unordered); | 
|  | 636 |  | 
|  | 637 | // Store that value into the return address.  Doing this with a | 
|  | 638 | // bitcast is likely to produce some pretty ugly IR, but it's not | 
|  | 639 | // the *most* terrible thing in the world. | 
|  | 640 | Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType)); | 
|  | 641 |  | 
|  | 642 | // Make sure we don't do an autorelease. | 
|  | 643 | AutoreleaseResult = false; | 
|  | 644 | return; | 
|  | 645 | } | 
|  | 646 |  | 
|  | 647 | case PropertyImplStrategy::GetSetProperty: { | 
|  | 648 | llvm::Value *getPropertyFn = | 
|  | 649 | CGM.getObjCRuntime().GetPropertyGetFunction(); | 
|  | 650 | if (!getPropertyFn) { | 
|  | 651 | CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy"); | 
| Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 652 | return; | 
|  | 653 | } | 
|  | 654 |  | 
|  | 655 | // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). | 
|  | 656 | // FIXME: Can't this be simpler? This might even be worse than the | 
|  | 657 | // corresponding gcc code. | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 658 | llvm::Value *cmd = | 
|  | 659 | Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd"); | 
|  | 660 | llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); | 
|  | 661 | llvm::Value *ivarOffset = | 
|  | 662 | EmitIvarOffset(classImpl->getClassInterface(), ivar); | 
|  | 663 |  | 
|  | 664 | CallArgList args; | 
|  | 665 | args.add(RValue::get(self), getContext().getObjCIdType()); | 
|  | 666 | args.add(RValue::get(cmd), getContext().getObjCSelType()); | 
|  | 667 | args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); | 
| John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 668 | args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), | 
|  | 669 | getContext().BoolTy); | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 670 |  | 
| Daniel Dunbar | 1ef7373 | 2009-02-03 23:43:59 +0000 | [diff] [blame] | 671 | // FIXME: We shouldn't need to get the function info here, the | 
|  | 672 | // runtime already should have computed it to build the function. | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 673 | RValue RV = EmitCall(getTypes().getFunctionInfo(propType, args, | 
| John McCall | b923ece | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 674 | FunctionType::ExtInfo()), | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 675 | getPropertyFn, ReturnValueSlot(), args); | 
|  | 676 |  | 
| Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 677 | // We need to fix the type here. Ivars with copy & retain are | 
|  | 678 | // always objects so we don't need to worry about complex or | 
|  | 679 | // aggregates. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 681 | getTypes().ConvertType(propType))); | 
|  | 682 |  | 
|  | 683 | EmitReturnOfRValue(RV, propType); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 684 |  | 
|  | 685 | // objc_getProperty does an autorelease, so we should suppress ours. | 
|  | 686 | AutoreleaseResult = false; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 687 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 688 | return; | 
|  | 689 | } | 
|  | 690 |  | 
|  | 691 | case PropertyImplStrategy::CopyStruct: | 
|  | 692 | emitStructGetterCall(*this, ivar, strategy.isAtomic(), | 
|  | 693 | strategy.hasStrongMember()); | 
|  | 694 | return; | 
|  | 695 |  | 
|  | 696 | case PropertyImplStrategy::Expression: | 
|  | 697 | case PropertyImplStrategy::SetPropertyAndExpressionGet: { | 
|  | 698 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); | 
|  | 699 |  | 
|  | 700 | QualType ivarType = ivar->getType(); | 
|  | 701 | if (ivarType->isAnyComplexType()) { | 
|  | 702 | ComplexPairTy pair = LoadComplexFromAddr(LV.getAddress(), | 
|  | 703 | LV.isVolatileQualified()); | 
|  | 704 | StoreComplexToAddr(pair, ReturnValue, LV.isVolatileQualified()); | 
|  | 705 | } else if (hasAggregateLLVMType(ivarType)) { | 
|  | 706 | // The return value slot is guaranteed to not be aliased, but | 
|  | 707 | // that's not necessarily the same as "on the stack", so | 
|  | 708 | // we still potentially need objc_memmove_collectable. | 
|  | 709 | EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType); | 
|  | 710 | } else { | 
| John McCall | 24fada1 | 2011-07-22 05:23:13 +0000 | [diff] [blame] | 711 | llvm::Value *value; | 
|  | 712 | if (propType->isReferenceType()) { | 
|  | 713 | value = LV.getAddress(); | 
|  | 714 | } else { | 
|  | 715 | // We want to load and autoreleaseReturnValue ARC __weak ivars. | 
|  | 716 | if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 717 | value = emitARCRetainLoadOfScalar(*this, LV, ivarType); | 
| John McCall | 24fada1 | 2011-07-22 05:23:13 +0000 | [diff] [blame] | 718 |  | 
|  | 719 | // Otherwise we want to do a simple load, suppressing the | 
|  | 720 | // final autorelease. | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 721 | } else { | 
| John McCall | 24fada1 | 2011-07-22 05:23:13 +0000 | [diff] [blame] | 722 | value = EmitLoadOfLValue(LV).getScalarVal(); | 
|  | 723 | AutoreleaseResult = false; | 
| Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 724 | } | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 725 |  | 
| John McCall | 24fada1 | 2011-07-22 05:23:13 +0000 | [diff] [blame] | 726 | value = Builder.CreateBitCast(value, ConvertType(propType)); | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | EmitReturnOfRValue(RValue::get(value), propType); | 
| Fariborz Jahanian | eab5ecd | 2009-03-03 18:49:40 +0000 | [diff] [blame] | 730 | } | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 731 | return; | 
| Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 732 | } | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 733 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 734 | } | 
|  | 735 | llvm_unreachable("bad @property implementation strategy!"); | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 736 | } | 
|  | 737 |  | 
| John McCall | b923ece | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 738 | /// emitStructSetterCall - Call the runtime function to store the value | 
|  | 739 | /// from the first formal parameter into the given ivar. | 
|  | 740 | static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, | 
|  | 741 | ObjCIvarDecl *ivar) { | 
| Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 742 | // objc_copyStruct (&structIvar, &Arg, | 
|  | 743 | //                  sizeof (struct something), true, false); | 
| John McCall | 6acaef9 | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 744 | CallArgList args; | 
|  | 745 |  | 
|  | 746 | // The first argument is the address of the ivar. | 
| John McCall | b923ece | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 747 | llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), | 
|  | 748 | CGF.LoadObjCSelf(), ivar, 0) | 
|  | 749 | .getAddress(); | 
|  | 750 | ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); | 
|  | 751 | args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); | 
| John McCall | 6acaef9 | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 752 |  | 
|  | 753 | // The second argument is the address of the parameter variable. | 
| John McCall | b923ece | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 754 | ParmVarDecl *argVar = *OMD->param_begin(); | 
|  | 755 | DeclRefExpr argRef(argVar, argVar->getType(), VK_LValue, SourceLocation()); | 
|  | 756 | llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); | 
|  | 757 | argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); | 
|  | 758 | args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); | 
| John McCall | 6acaef9 | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 759 |  | 
|  | 760 | // The third argument is the sizeof the type. | 
|  | 761 | llvm::Value *size = | 
| John McCall | b923ece | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 762 | CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); | 
|  | 763 | args.add(RValue::get(size), CGF.getContext().getSizeType()); | 
| John McCall | 6acaef9 | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 764 |  | 
| John McCall | b923ece | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 765 | // The fourth argument is the 'isAtomic' flag. | 
|  | 766 | args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); | 
| John McCall | 6acaef9 | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 767 |  | 
| John McCall | b923ece | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 768 | // The fifth argument is the 'hasStrong' flag. | 
|  | 769 | // FIXME: should this really always be false? | 
|  | 770 | args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); | 
|  | 771 |  | 
|  | 772 | llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); | 
|  | 773 | CGF.EmitCall(CGF.getTypes().getFunctionInfo(CGF.getContext().VoidTy, args, | 
|  | 774 | FunctionType::ExtInfo()), | 
|  | 775 | copyStructFn, ReturnValueSlot(), args); | 
| Fariborz Jahanian | 302a3d4 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 776 | } | 
|  | 777 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 778 | static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { | 
|  | 779 | Expr *setter = PID->getSetterCXXAssignment(); | 
|  | 780 | if (!setter) return true; | 
|  | 781 |  | 
|  | 782 | // Sema only makes only of these when the ivar has a C++ class type, | 
|  | 783 | // so the form is pretty constrained. | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 784 |  | 
|  | 785 | // An operator call is trivial if the function it calls is trivial. | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 786 | // This also implies that there's nothing non-trivial going on with | 
|  | 787 | // the arguments, because operator= can only be trivial if it's a | 
|  | 788 | // synthesized assignment operator and therefore both parameters are | 
|  | 789 | // references. | 
|  | 790 | if (CallExpr *call = dyn_cast<CallExpr>(setter)) { | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 791 | if (const FunctionDecl *callee | 
|  | 792 | = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) | 
|  | 793 | if (callee->isTrivial()) | 
|  | 794 | return true; | 
|  | 795 | return false; | 
| Fariborz Jahanian | 5de5313 | 2011-04-06 16:05:26 +0000 | [diff] [blame] | 796 | } | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 797 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 798 | assert(isa<ExprWithCleanups>(setter)); | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 799 | return false; | 
|  | 800 | } | 
|  | 801 |  | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 802 | void | 
|  | 803 | CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, | 
|  | 804 | const ObjCPropertyImplDecl *propImpl) { | 
|  | 805 | // Just use the setter expression if Sema gave us one and it's | 
|  | 806 | // non-trivial.  There's no way to do this atomically. | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 807 | if (!hasTrivialSetExpr(propImpl)) { | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 808 | EmitStmt(propImpl->getSetterCXXAssignment()); | 
|  | 809 | return; | 
|  | 810 | } | 
|  | 811 |  | 
|  | 812 | const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); | 
|  | 813 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); | 
|  | 814 | ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl(); | 
|  | 815 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 816 | PropertyImplStrategy strategy(CGM, propImpl); | 
|  | 817 | switch (strategy.getKind()) { | 
|  | 818 | case PropertyImplStrategy::Native: { | 
|  | 819 | llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()]; | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 820 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 821 | LValue ivarLValue = | 
|  | 822 | EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0); | 
|  | 823 | llvm::Value *ivarAddr = ivarLValue.getAddress(); | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 824 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 825 | // Currently, all atomic accesses have to be through integer | 
|  | 826 | // types, so there's no point in trying to pick a prettier type. | 
|  | 827 | llvm::Type *bitcastType = | 
|  | 828 | llvm::Type::getIntNTy(getLLVMContext(), | 
|  | 829 | getContext().toBits(strategy.getIvarSize())); | 
|  | 830 | bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay | 
|  | 831 |  | 
|  | 832 | // Cast both arguments to the chosen operation type. | 
|  | 833 | argAddr = Builder.CreateBitCast(argAddr, bitcastType); | 
|  | 834 | ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); | 
|  | 835 |  | 
|  | 836 | // This bitcast load is likely to cause some nasty IR. | 
|  | 837 | llvm::Value *load = Builder.CreateLoad(argAddr); | 
|  | 838 |  | 
|  | 839 | // Perform an atomic store.  There are no memory ordering requirements. | 
|  | 840 | llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); | 
|  | 841 | store->setAlignment(strategy.getIvarAlignment().getQuantity()); | 
|  | 842 | store->setAtomic(llvm::Unordered); | 
|  | 843 | return; | 
|  | 844 | } | 
|  | 845 |  | 
|  | 846 | case PropertyImplStrategy::GetSetProperty: | 
|  | 847 | case PropertyImplStrategy::SetPropertyAndExpressionGet: { | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 848 | llvm::Value *setPropertyFn = | 
|  | 849 | CGM.getObjCRuntime().GetPropertySetFunction(); | 
|  | 850 | if (!setPropertyFn) { | 
|  | 851 | CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); | 
|  | 852 | return; | 
|  | 853 | } | 
|  | 854 |  | 
|  | 855 | // Emit objc_setProperty((id) self, _cmd, offset, arg, | 
|  | 856 | //                       <is-atomic>, <is-copy>). | 
|  | 857 | llvm::Value *cmd = | 
|  | 858 | Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]); | 
|  | 859 | llvm::Value *self = | 
|  | 860 | Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); | 
|  | 861 | llvm::Value *ivarOffset = | 
|  | 862 | EmitIvarOffset(classImpl->getClassInterface(), ivar); | 
|  | 863 | llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()]; | 
|  | 864 | arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy); | 
|  | 865 |  | 
|  | 866 | CallArgList args; | 
|  | 867 | args.add(RValue::get(self), getContext().getObjCIdType()); | 
|  | 868 | args.add(RValue::get(cmd), getContext().getObjCSelType()); | 
|  | 869 | args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); | 
|  | 870 | args.add(RValue::get(arg), getContext().getObjCIdType()); | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 871 | args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), | 
|  | 872 | getContext().BoolTy); | 
|  | 873 | args.add(RValue::get(Builder.getInt1(strategy.isCopy())), | 
|  | 874 | getContext().BoolTy); | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 875 | // FIXME: We shouldn't need to get the function info here, the runtime | 
|  | 876 | // already should have computed it to build the function. | 
|  | 877 | EmitCall(getTypes().getFunctionInfo(getContext().VoidTy, args, | 
|  | 878 | FunctionType::ExtInfo()), | 
|  | 879 | setPropertyFn, ReturnValueSlot(), args); | 
|  | 880 | return; | 
|  | 881 | } | 
|  | 882 |  | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 883 | case PropertyImplStrategy::CopyStruct: | 
| John McCall | b923ece | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 884 | emitStructSetterCall(*this, setterMethod, ivar); | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 885 | return; | 
| John McCall | f4528ae | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 886 |  | 
|  | 887 | case PropertyImplStrategy::Expression: | 
|  | 888 | break; | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 889 | } | 
|  | 890 |  | 
|  | 891 | // Otherwise, fake up some ASTs and emit a normal assignment. | 
|  | 892 | ValueDecl *selfDecl = setterMethod->getSelfDecl(); | 
|  | 893 | DeclRefExpr self(selfDecl, selfDecl->getType(), VK_LValue, SourceLocation()); | 
|  | 894 | ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, | 
|  | 895 | selfDecl->getType(), CK_LValueToRValue, &self, | 
|  | 896 | VK_RValue); | 
|  | 897 | ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), | 
|  | 898 | SourceLocation(), &selfLoad, true, true); | 
|  | 899 |  | 
|  | 900 | ParmVarDecl *argDecl = *setterMethod->param_begin(); | 
|  | 901 | QualType argType = argDecl->getType().getNonReferenceType(); | 
|  | 902 | DeclRefExpr arg(argDecl, argType, VK_LValue, SourceLocation()); | 
|  | 903 | ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, | 
|  | 904 | argType.getUnqualifiedType(), CK_LValueToRValue, | 
|  | 905 | &arg, VK_RValue); | 
|  | 906 |  | 
|  | 907 | // The property type can differ from the ivar type in some situations with | 
|  | 908 | // Objective-C pointer types, we can always bit cast the RHS in these cases. | 
|  | 909 | // The following absurdity is just to ensure well-formed IR. | 
|  | 910 | CastKind argCK = CK_NoOp; | 
|  | 911 | if (ivarRef.getType()->isObjCObjectPointerType()) { | 
|  | 912 | if (argLoad.getType()->isObjCObjectPointerType()) | 
|  | 913 | argCK = CK_BitCast; | 
|  | 914 | else if (argLoad.getType()->isBlockPointerType()) | 
|  | 915 | argCK = CK_BlockPointerToObjCPointerCast; | 
|  | 916 | else | 
|  | 917 | argCK = CK_CPointerToObjCPointerCast; | 
|  | 918 | } else if (ivarRef.getType()->isBlockPointerType()) { | 
|  | 919 | if (argLoad.getType()->isBlockPointerType()) | 
|  | 920 | argCK = CK_BitCast; | 
|  | 921 | else | 
|  | 922 | argCK = CK_AnyPointerToBlockPointerCast; | 
|  | 923 | } else if (ivarRef.getType()->isPointerType()) { | 
|  | 924 | argCK = CK_BitCast; | 
|  | 925 | } | 
|  | 926 | ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, | 
|  | 927 | ivarRef.getType(), argCK, &argLoad, | 
|  | 928 | VK_RValue); | 
|  | 929 | Expr *finalArg = &argLoad; | 
|  | 930 | if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), | 
|  | 931 | argLoad.getType())) | 
|  | 932 | finalArg = &argCast; | 
|  | 933 |  | 
|  | 934 |  | 
|  | 935 | BinaryOperator assign(&ivarRef, finalArg, BO_Assign, | 
|  | 936 | ivarRef.getType(), VK_RValue, OK_Ordinary, | 
|  | 937 | SourceLocation()); | 
|  | 938 | EmitStmt(&assign); | 
| Fariborz Jahanian | 5de5313 | 2011-04-06 16:05:26 +0000 | [diff] [blame] | 939 | } | 
|  | 940 |  | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 941 | /// GenerateObjCSetter - Generate an Objective-C property setter | 
| Steve Naroff | 5a7dd78 | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 942 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize | 
|  | 943 | /// is illegal within a category. | 
| Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 944 | void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, | 
|  | 945 | const ObjCPropertyImplDecl *PID) { | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 946 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); | 
|  | 947 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); | 
|  | 948 | assert(OMD && "Invalid call to generate setter (empty method)"); | 
| Devang Patel | e7ce540 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 949 | StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart()); | 
| Daniel Dunbar | 5449ce5 | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 950 |  | 
| John McCall | 7f16c42 | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 951 | generateObjCSetterBody(IMP, PID); | 
| Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 952 |  | 
|  | 953 | FinishFunction(); | 
| Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 954 | } | 
|  | 955 |  | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 956 | namespace { | 
| John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 957 | struct DestroyIvar : EHScopeStack::Cleanup { | 
|  | 958 | private: | 
|  | 959 | llvm::Value *addr; | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 960 | const ObjCIvarDecl *ivar; | 
| John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 961 | CodeGenFunction::Destroyer &destroyer; | 
|  | 962 | bool useEHCleanupForArray; | 
|  | 963 | public: | 
|  | 964 | DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, | 
|  | 965 | CodeGenFunction::Destroyer *destroyer, | 
|  | 966 | bool useEHCleanupForArray) | 
|  | 967 | : addr(addr), ivar(ivar), destroyer(*destroyer), | 
|  | 968 | useEHCleanupForArray(useEHCleanupForArray) {} | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 969 |  | 
| John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 970 | void Emit(CodeGenFunction &CGF, Flags flags) { | 
| John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 971 | LValue lvalue | 
|  | 972 | = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); | 
|  | 973 | CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer, | 
| John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 974 | flags.isForNormalCleanup() && useEHCleanupForArray); | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 975 | } | 
|  | 976 | }; | 
|  | 977 | } | 
|  | 978 |  | 
| John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 979 | /// Like CodeGenFunction::destroyARCStrong, but do it with a call. | 
|  | 980 | static void destroyARCStrongWithStore(CodeGenFunction &CGF, | 
|  | 981 | llvm::Value *addr, | 
|  | 982 | QualType type) { | 
|  | 983 | llvm::Value *null = getNullForVariable(addr); | 
|  | 984 | CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); | 
|  | 985 | } | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 986 |  | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 987 | static void emitCXXDestructMethod(CodeGenFunction &CGF, | 
|  | 988 | ObjCImplementationDecl *impl) { | 
|  | 989 | CodeGenFunction::RunCleanupsScope scope(CGF); | 
|  | 990 |  | 
|  | 991 | llvm::Value *self = CGF.LoadObjCSelf(); | 
|  | 992 |  | 
| Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 993 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); | 
|  | 994 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 995 | ivar; ivar = ivar->getNextIvar()) { | 
|  | 996 | QualType type = ivar->getType(); | 
|  | 997 |  | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 998 | // Check whether the ivar is a destructible type. | 
| John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 999 | QualType::DestructionKind dtorKind = type.isDestructedType(); | 
|  | 1000 | if (!dtorKind) continue; | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1001 |  | 
| John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1002 | CodeGenFunction::Destroyer *destroyer = 0; | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1003 |  | 
| John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1004 | // Use a call to objc_storeStrong to destroy strong ivars, for the | 
|  | 1005 | // general benefit of the tools. | 
|  | 1006 | if (dtorKind == QualType::DK_objc_strong_lifetime) { | 
|  | 1007 | destroyer = &destroyARCStrongWithStore; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1008 |  | 
| John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1009 | // Otherwise use the default for the destruction kind. | 
|  | 1010 | } else { | 
|  | 1011 | destroyer = &CGF.getDestroyer(dtorKind); | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1012 | } | 
| John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1013 |  | 
|  | 1014 | CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); | 
|  | 1015 |  | 
|  | 1016 | CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, | 
|  | 1017 | cleanupKind & EHCleanup); | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); | 
|  | 1021 | } | 
|  | 1022 |  | 
| Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1023 | void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, | 
|  | 1024 | ObjCMethodDecl *MD, | 
|  | 1025 | bool ctor) { | 
| Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1026 | MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); | 
| Devang Patel | e7ce540 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 1027 | StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart()); | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1028 |  | 
|  | 1029 | // Emit .cxx_construct. | 
| Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1030 | if (ctor) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1031 | // Suppress the final autorelease in ARC. | 
|  | 1032 | AutoreleaseResult = false; | 
|  | 1033 |  | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1034 | SmallVector<CXXCtorInitializer *, 8> IvarInitializers; | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1035 | for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), | 
|  | 1036 | E = IMP->init_end(); B != E; ++B) { | 
|  | 1037 | CXXCtorInitializer *IvarInit = (*B); | 
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1038 | FieldDecl *Field = IvarInit->getAnyMember(); | 
| Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1039 | ObjCIvarDecl  *Ivar = cast<ObjCIvarDecl>(Field); | 
| Fariborz Jahanian | 499b902 | 2010-04-28 22:30:33 +0000 | [diff] [blame] | 1040 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), | 
|  | 1041 | LoadObjCSelf(), Ivar, 0); | 
| John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 1042 | EmitAggExpr(IvarInit->getInit(), | 
|  | 1043 | AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed, | 
| John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 1044 | AggValueSlot::DoesNotNeedGCBarriers, | 
|  | 1045 | AggValueSlot::IsNotAliased)); | 
| Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1046 | } | 
|  | 1047 | // constructor returns 'self'. | 
|  | 1048 | CodeGenTypes &Types = CGM.getTypes(); | 
|  | 1049 | QualType IdTy(CGM.getContext().getObjCIdType()); | 
|  | 1050 | llvm::Value *SelfAsId = | 
|  | 1051 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); | 
|  | 1052 | EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1053 |  | 
|  | 1054 | // Emit .cxx_destruct. | 
| Chandler Carruth | f398365 | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 1055 | } else { | 
| John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1056 | emitCXXDestructMethod(*this, IMP); | 
| Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1057 | } | 
|  | 1058 | FinishFunction(); | 
|  | 1059 | } | 
|  | 1060 |  | 
| Fariborz Jahanian | 08b0f66 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 1061 | bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { | 
|  | 1062 | CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); | 
|  | 1063 | it++; it++; | 
|  | 1064 | const ABIArgInfo &AI = it->info; | 
|  | 1065 | // FIXME. Is this sufficient check? | 
|  | 1066 | return (AI.getKind() == ABIArgInfo::Indirect); | 
|  | 1067 | } | 
|  | 1068 |  | 
| Fariborz Jahanian | 7e9d52a | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 1069 | bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { | 
| Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1070 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC) | 
| Fariborz Jahanian | 7e9d52a | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 1071 | return false; | 
|  | 1072 | if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) | 
|  | 1073 | return FDTTy->getDecl()->hasObjectMember(); | 
|  | 1074 | return false; | 
|  | 1075 | } | 
|  | 1076 |  | 
| Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 1077 | llvm::Value *CodeGenFunction::LoadObjCSelf() { | 
| Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1078 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); | 
|  | 1079 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); | 
| Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1080 | } | 
|  | 1081 |  | 
| Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1082 | QualType CodeGenFunction::TypeOfSelfObject() { | 
|  | 1083 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); | 
|  | 1084 | ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); | 
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1085 | const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( | 
|  | 1086 | getContext().getCanonicalType(selfDecl->getType())); | 
| Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1087 | return PTy->getPointeeType(); | 
|  | 1088 | } | 
|  | 1089 |  | 
| Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1090 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | llvm::Constant *EnumerationMutationFn = | 
| Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 1092 | CGM.getObjCRuntime().EnumerationMutationFunction(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 |  | 
| Daniel Dunbar | a08dff1 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 1094 | if (!EnumerationMutationFn) { | 
|  | 1095 | CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); | 
|  | 1096 | return; | 
|  | 1097 | } | 
|  | 1098 |  | 
| Devang Patel | d2d6665 | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 1099 | CGDebugInfo *DI = getDebugInfo(); | 
| Eric Christopher | 7cdf948 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1100 | if (DI) | 
|  | 1101 | DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); | 
| Devang Patel | d2d6665 | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 1102 |  | 
| Devang Patel | 297207f | 2011-06-13 23:15:32 +0000 | [diff] [blame] | 1103 | // The local variable comes into scope immediately. | 
|  | 1104 | AutoVarEmission variable = AutoVarEmission::invalid(); | 
|  | 1105 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) | 
|  | 1106 | variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); | 
|  | 1107 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1108 | JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 |  | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1110 | // Fast enumeration state. | 
| Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 1111 | QualType StateTy = CGM.getObjCFastEnumerationStateType(); | 
| Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1112 | llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); | 
| Anders Carlsson | c0964b6 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 1113 | EmitNullInitialization(StatePtr, StateTy); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 |  | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1115 | // Number of elements in the items array. | 
| Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1116 | static const unsigned NumItems = 16; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1118 | // Fetch the countByEnumeratingWithState:objects:count: selector. | 
| Benjamin Kramer | 9e649c3 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 1119 | IdentifierInfo *II[] = { | 
|  | 1120 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), | 
|  | 1121 | &CGM.getContext().Idents.get("objects"), | 
|  | 1122 | &CGM.getContext().Idents.get("count") | 
|  | 1123 | }; | 
|  | 1124 | Selector FastEnumSel = | 
|  | 1125 | CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1126 |  | 
|  | 1127 | QualType ItemsTy = | 
|  | 1128 | getContext().getConstantArrayType(getContext().getObjCIdType(), | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | llvm::APInt(32, NumItems), | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1130 | ArrayType::Normal, 0); | 
| Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1131 | llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 |  | 
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1133 | // Emit the collection pointer.  In ARC, we do a retain. | 
|  | 1134 | llvm::Value *Collection; | 
|  | 1135 | if (getLangOptions().ObjCAutoRefCount) { | 
|  | 1136 | Collection = EmitARCRetainScalarExpr(S.getCollection()); | 
|  | 1137 |  | 
|  | 1138 | // Enter a cleanup to do the release. | 
|  | 1139 | EmitObjCConsumeObject(S.getCollection()->getType(), Collection); | 
|  | 1140 | } else { | 
|  | 1141 | Collection = EmitScalarExpr(S.getCollection()); | 
|  | 1142 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 |  | 
| John McCall | 91e82dd | 2011-08-05 00:14:38 +0000 | [diff] [blame] | 1144 | // The 'continue' label needs to appear within the cleanup for the | 
|  | 1145 | // collection object. | 
|  | 1146 | JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); | 
|  | 1147 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1148 | // Send it our message: | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1149 | CallArgList Args; | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1150 |  | 
|  | 1151 | // The first argument is a temporary of the enumeration-state type. | 
| Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1152 | Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy)); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1154 | // The second argument is a temporary array with space for NumItems | 
|  | 1155 | // pointers.  We'll actually be loading elements from the array | 
|  | 1156 | // pointer written into the control state; this buffer is so that | 
|  | 1157 | // collections that *aren't* backed by arrays can still queue up | 
|  | 1158 | // batches of elements. | 
| Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1159 | Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy)); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1161 | // The third argument is the capacity of that temporary array. | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1162 | llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); | 
| Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1163 | llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); | 
| Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1164 | Args.add(RValue::get(Count), getContext().UnsignedLongTy); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1166 | // Start the enumeration. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1167 | RValue CountRV = | 
| John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1168 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1169 | getContext().UnsignedLongTy, | 
|  | 1170 | FastEnumSel, | 
| David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1171 | Collection, Args); | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1172 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1173 | // The initial number of objects that were returned in the buffer. | 
|  | 1174 | llvm::Value *initialBufferLimit = CountRV.getScalarVal(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1176 | llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); | 
|  | 1177 | llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1179 | llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1180 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1181 | // If the limit pointer was zero to begin with, the collection is | 
|  | 1182 | // empty; skip all this. | 
|  | 1183 | Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), | 
|  | 1184 | EmptyBB, LoopInitBB); | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1185 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1186 | // Otherwise, initialize the loop. | 
|  | 1187 | EmitBlock(LoopInitBB); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1189 | // Save the initial mutations value.  This is the value at an | 
|  | 1190 | // address that was written into the state object by | 
|  | 1191 | // countByEnumeratingWithState:objects:count:. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | llvm::Value *StateMutationsPtrPtr = | 
| Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1193 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, | 
| Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1195 | "mutationsptr"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1197 | llvm::Value *initialMutations = | 
|  | 1198 | Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1200 | // Start looping.  This is the point we return to whenever we have a | 
|  | 1201 | // fresh, non-empty batch of objects. | 
|  | 1202 | llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); | 
|  | 1203 | EmitBlock(LoopBodyBB); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1205 | // The current index into the buffer. | 
| Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1206 | llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index"); | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1207 | index->addIncoming(zero, LoopInitBB); | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1208 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1209 | // The current buffer size. | 
| Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1210 | llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count"); | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1211 | count->addIncoming(initialBufferLimit, LoopInitBB); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1213 | // Check whether the mutations value has changed from where it was | 
|  | 1214 | // at start.  StateMutationsPtr should actually be invariant between | 
|  | 1215 | // refreshes. | 
| Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1216 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1217 | llvm::Value *currentMutations | 
|  | 1218 | = Builder.CreateLoad(StateMutationsPtr, "statemutations"); | 
| Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1219 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1220 | llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); | 
| Dan Gohman | 6ca9982 | 2011-03-02 22:39:34 +0000 | [diff] [blame] | 1221 | llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1223 | Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), | 
|  | 1224 | WasNotMutatedBB, WasMutatedBB); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1226 | // If so, call the enumeration-mutation function. | 
|  | 1227 | EmitBlock(WasMutatedBB); | 
| Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1228 | llvm::Value *V = | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | Builder.CreateBitCast(Collection, | 
| Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1230 | ConvertType(getContext().getObjCIdType())); | 
| Daniel Dunbar | 84388bf | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 1231 | CallArgList Args2; | 
| Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1232 | Args2.add(RValue::get(V), getContext().getObjCIdType()); | 
| Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1233 | // FIXME: We shouldn't need to get the function info here, the runtime already | 
|  | 1234 | // should have computed it to build the function. | 
| John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1235 | EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2, | 
| Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1236 | FunctionType::ExtInfo()), | 
| Anders Carlsson | 61a401c | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 1237 | EnumerationMutationFn, ReturnValueSlot(), Args2); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1239 | // Otherwise, or if the mutation function returns, just continue. | 
|  | 1240 | EmitBlock(WasNotMutatedBB); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1242 | // Initialize the element variable. | 
|  | 1243 | RunCleanupsScope elementVariableScope(*this); | 
| John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1244 | bool elementIsVariable; | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1245 | LValue elementLValue; | 
|  | 1246 | QualType elementType; | 
|  | 1247 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { | 
| John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1248 | // Initialize the variable, in case it's a __block variable or something. | 
|  | 1249 | EmitAutoVarInit(variable); | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1250 |  | 
| John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1251 | const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1252 | DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(), | 
|  | 1253 | VK_LValue, SourceLocation()); | 
|  | 1254 | elementLValue = EmitLValue(&tempDRE); | 
|  | 1255 | elementType = D->getType(); | 
| John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1256 | elementIsVariable = true; | 
| John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1257 |  | 
|  | 1258 | if (D->isARCPseudoStrong()) | 
|  | 1259 | elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1260 | } else { | 
|  | 1261 | elementLValue = LValue(); // suppress warning | 
|  | 1262 | elementType = cast<Expr>(S.getElement())->getType(); | 
| John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1263 | elementIsVariable = false; | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1264 | } | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1265 | llvm::Type *convertedElementType = ConvertType(elementType); | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1266 |  | 
|  | 1267 | // Fetch the buffer out of the enumeration state. | 
|  | 1268 | // TODO: this pointer should actually be invariant between | 
|  | 1269 | // refreshes, which would help us do certain loop optimizations. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | llvm::Value *StateItemsPtr = | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1271 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1272 | llvm::Value *EnumStateItems = | 
|  | 1273 | Builder.CreateLoad(StateItemsPtr, "stateitems"); | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1274 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1275 | // Fetch the value at the current index from the buffer. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | llvm::Value *CurrentItemPtr = | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1277 | Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); | 
|  | 1278 | llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1280 | // Cast that value to the right type. | 
|  | 1281 | CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, | 
|  | 1282 | "currentitem"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1284 | // Make sure we have an l-value.  Yes, this gets evaluated every | 
|  | 1285 | // time through the loop. | 
| John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1286 | if (!elementIsVariable) { | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1287 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); | 
| John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1288 | EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); | 
| John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1289 | } else { | 
|  | 1290 | EmitScalarInit(CurrentItem, elementLValue); | 
|  | 1291 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1292 |  | 
| John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1293 | // If we do have an element variable, this assignment is the end of | 
|  | 1294 | // its initialization. | 
|  | 1295 | if (elementIsVariable) | 
|  | 1296 | EmitAutoVarCleanups(variable); | 
|  | 1297 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1298 | // Perform the loop body, setting up break and continue labels. | 
| Anders Carlsson | 33747b6 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 1299 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1300 | { | 
|  | 1301 | RunCleanupsScope Scope(*this); | 
|  | 1302 | EmitStmt(S.getBody()); | 
|  | 1303 | } | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1304 | BreakContinueStack.pop_back(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1306 | // Destroy the element variable now. | 
|  | 1307 | elementVariableScope.ForceCleanup(); | 
|  | 1308 |  | 
|  | 1309 | // Check whether there are more elements. | 
| John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1310 | EmitBlock(AfterBody.getBlock()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1312 | llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); | 
| Fariborz Jahanian | 6e7ecc8 | 2009-01-06 18:56:31 +0000 | [diff] [blame] | 1313 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1314 | // First we check in the local buffer. | 
|  | 1315 | llvm::Value *indexPlusOne | 
|  | 1316 | = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1317 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1318 | // If we haven't overrun the buffer yet, we can continue. | 
|  | 1319 | Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), | 
|  | 1320 | LoopBodyBB, FetchMoreBB); | 
|  | 1321 |  | 
|  | 1322 | index->addIncoming(indexPlusOne, AfterBody.getBlock()); | 
|  | 1323 | count->addIncoming(count, AfterBody.getBlock()); | 
|  | 1324 |  | 
|  | 1325 | // Otherwise, we have to fetch more elements. | 
|  | 1326 | EmitBlock(FetchMoreBB); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 |  | 
|  | 1328 | CountRV = | 
| John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1329 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1330 | getContext().UnsignedLongTy, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | FastEnumSel, | 
| David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1332 | Collection, Args); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1334 | // If we got a zero count, we're done. | 
|  | 1335 | llvm::Value *refetchCount = CountRV.getScalarVal(); | 
|  | 1336 |  | 
|  | 1337 | // (note that the message send might split FetchMoreBB) | 
|  | 1338 | index->addIncoming(zero, Builder.GetInsertBlock()); | 
|  | 1339 | count->addIncoming(refetchCount, Builder.GetInsertBlock()); | 
|  | 1340 |  | 
|  | 1341 | Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), | 
|  | 1342 | EmptyBB, LoopBodyBB); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 |  | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1344 | // No more elements. | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1345 | EmitBlock(EmptyBB); | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1346 |  | 
| John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1347 | if (!elementIsVariable) { | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1348 | // If the element was not a declaration, set it to be null. | 
|  | 1349 |  | 
| John McCall | 1c926b7 | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1350 | llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); | 
|  | 1351 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); | 
| John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1352 | EmitStoreThroughLValue(RValue::get(null), elementLValue); | 
| Anders Carlsson | 7565859 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1353 | } | 
|  | 1354 |  | 
| Eric Christopher | 7cdf948 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1355 | if (DI) | 
|  | 1356 | DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); | 
| Devang Patel | d2d6665 | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 1357 |  | 
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1358 | // Leave the cleanup we entered in ARC. | 
|  | 1359 | if (getLangOptions().ObjCAutoRefCount) | 
|  | 1360 | PopCleanupBlock(); | 
|  | 1361 |  | 
| John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1362 | EmitBlock(LoopEnd.getBlock()); | 
| Anders Carlsson | 2e744e8 | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 1363 | } | 
|  | 1364 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { | 
| John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1366 | CGM.getObjCRuntime().EmitTryStmt(*this, S); | 
| Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1367 | } | 
|  | 1368 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { | 
| Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1370 | CGM.getObjCRuntime().EmitThrowStmt(*this, S); | 
|  | 1371 | } | 
|  | 1372 |  | 
| Chris Lattner | e132e24 | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 1373 | void CodeGenFunction::EmitObjCAtSynchronizedStmt( | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | const ObjCAtSynchronizedStmt &S) { | 
| John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1375 | CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); | 
| Chris Lattner | e132e24 | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 1376 | } | 
|  | 1377 |  | 
| John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 1378 | /// Produce the code for a CK_ARCProduceObject.  Just does a | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1379 | /// primitive retain. | 
|  | 1380 | llvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type, | 
|  | 1381 | llvm::Value *value) { | 
|  | 1382 | return EmitARCRetain(type, value); | 
|  | 1383 | } | 
|  | 1384 |  | 
|  | 1385 | namespace { | 
|  | 1386 | struct CallObjCRelease : EHScopeStack::Cleanup { | 
| John McCall | 9c8e1c9 | 2011-08-03 22:24:24 +0000 | [diff] [blame] | 1387 | CallObjCRelease(llvm::Value *object) : object(object) {} | 
|  | 1388 | llvm::Value *object; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1389 |  | 
| John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1390 | void Emit(CodeGenFunction &CGF, Flags flags) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1391 | CGF.EmitARCRelease(object, /*precise*/ true); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1392 | } | 
|  | 1393 | }; | 
|  | 1394 | } | 
|  | 1395 |  | 
| John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 1396 | /// Produce the code for a CK_ARCConsumeObject.  Does a primitive | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1397 | /// release at the end of the full-expression. | 
|  | 1398 | llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, | 
|  | 1399 | llvm::Value *object) { | 
|  | 1400 | // If we're in a conditional branch, we need to make the cleanup | 
| John McCall | 9c8e1c9 | 2011-08-03 22:24:24 +0000 | [diff] [blame] | 1401 | // conditional. | 
|  | 1402 | pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1403 | return object; | 
|  | 1404 | } | 
|  | 1405 |  | 
|  | 1406 | llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, | 
|  | 1407 | llvm::Value *value) { | 
|  | 1408 | return EmitARCRetainAutorelease(type, value); | 
|  | 1409 | } | 
|  | 1410 |  | 
|  | 1411 |  | 
|  | 1412 | static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM, | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1413 | llvm::FunctionType *type, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1414 | StringRef fnName) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1415 | llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName); | 
|  | 1416 |  | 
|  | 1417 | // In -fobjc-no-arc-runtime, emit weak references to the runtime | 
|  | 1418 | // support library. | 
| John McCall | 24fc0de | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 1419 | if (!CGM.getCodeGenOpts().ObjCRuntimeHasARC) | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1420 | if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) | 
|  | 1421 | f->setLinkage(llvm::Function::ExternalWeakLinkage); | 
|  | 1422 |  | 
|  | 1423 | return fn; | 
|  | 1424 | } | 
|  | 1425 |  | 
|  | 1426 | /// Perform an operation having the signature | 
|  | 1427 | ///   i8* (i8*) | 
|  | 1428 | /// where a null input causes a no-op and returns null. | 
|  | 1429 | static llvm::Value *emitARCValueOperation(CodeGenFunction &CGF, | 
|  | 1430 | llvm::Value *value, | 
|  | 1431 | llvm::Constant *&fn, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1432 | StringRef fnName) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1433 | if (isa<llvm::ConstantPointerNull>(value)) return value; | 
|  | 1434 |  | 
|  | 1435 | if (!fn) { | 
| Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1436 | std::vector<llvm::Type*> args(1, CGF.Int8PtrTy); | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1437 | llvm::FunctionType *fnType = | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1438 | llvm::FunctionType::get(CGF.Int8PtrTy, args, false); | 
|  | 1439 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); | 
|  | 1440 | } | 
|  | 1441 |  | 
|  | 1442 | // Cast the argument to 'id'. | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1443 | llvm::Type *origType = value->getType(); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1444 | value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); | 
|  | 1445 |  | 
|  | 1446 | // Call the function. | 
|  | 1447 | llvm::CallInst *call = CGF.Builder.CreateCall(fn, value); | 
|  | 1448 | call->setDoesNotThrow(); | 
|  | 1449 |  | 
|  | 1450 | // Cast the result back to the original type. | 
|  | 1451 | return CGF.Builder.CreateBitCast(call, origType); | 
|  | 1452 | } | 
|  | 1453 |  | 
|  | 1454 | /// Perform an operation having the following signature: | 
|  | 1455 | ///   i8* (i8**) | 
|  | 1456 | static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, | 
|  | 1457 | llvm::Value *addr, | 
|  | 1458 | llvm::Constant *&fn, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1459 | StringRef fnName) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1460 | if (!fn) { | 
| Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1461 | std::vector<llvm::Type*> args(1, CGF.Int8PtrPtrTy); | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1462 | llvm::FunctionType *fnType = | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1463 | llvm::FunctionType::get(CGF.Int8PtrTy, args, false); | 
|  | 1464 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); | 
|  | 1465 | } | 
|  | 1466 |  | 
|  | 1467 | // Cast the argument to 'id*'. | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1468 | llvm::Type *origType = addr->getType(); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1469 | addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); | 
|  | 1470 |  | 
|  | 1471 | // Call the function. | 
|  | 1472 | llvm::CallInst *call = CGF.Builder.CreateCall(fn, addr); | 
|  | 1473 | call->setDoesNotThrow(); | 
|  | 1474 |  | 
|  | 1475 | // Cast the result back to a dereference of the original type. | 
|  | 1476 | llvm::Value *result = call; | 
|  | 1477 | if (origType != CGF.Int8PtrPtrTy) | 
|  | 1478 | result = CGF.Builder.CreateBitCast(result, | 
|  | 1479 | cast<llvm::PointerType>(origType)->getElementType()); | 
|  | 1480 |  | 
|  | 1481 | return result; | 
|  | 1482 | } | 
|  | 1483 |  | 
|  | 1484 | /// Perform an operation having the following signature: | 
|  | 1485 | ///   i8* (i8**, i8*) | 
|  | 1486 | static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, | 
|  | 1487 | llvm::Value *addr, | 
|  | 1488 | llvm::Value *value, | 
|  | 1489 | llvm::Constant *&fn, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1490 | StringRef fnName, | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1491 | bool ignored) { | 
|  | 1492 | assert(cast<llvm::PointerType>(addr->getType())->getElementType() | 
|  | 1493 | == value->getType()); | 
|  | 1494 |  | 
|  | 1495 | if (!fn) { | 
| Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1496 | llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy }; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1497 |  | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1498 | llvm::FunctionType *fnType | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1499 | = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false); | 
|  | 1500 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); | 
|  | 1501 | } | 
|  | 1502 |  | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1503 | llvm::Type *origType = value->getType(); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1504 |  | 
|  | 1505 | addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); | 
|  | 1506 | value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); | 
|  | 1507 |  | 
|  | 1508 | llvm::CallInst *result = CGF.Builder.CreateCall2(fn, addr, value); | 
|  | 1509 | result->setDoesNotThrow(); | 
|  | 1510 |  | 
|  | 1511 | if (ignored) return 0; | 
|  | 1512 |  | 
|  | 1513 | return CGF.Builder.CreateBitCast(result, origType); | 
|  | 1514 | } | 
|  | 1515 |  | 
|  | 1516 | /// Perform an operation having the following signature: | 
|  | 1517 | ///   void (i8**, i8**) | 
|  | 1518 | static void emitARCCopyOperation(CodeGenFunction &CGF, | 
|  | 1519 | llvm::Value *dst, | 
|  | 1520 | llvm::Value *src, | 
|  | 1521 | llvm::Constant *&fn, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1522 | StringRef fnName) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1523 | assert(dst->getType() == src->getType()); | 
|  | 1524 |  | 
|  | 1525 | if (!fn) { | 
| Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1526 | std::vector<llvm::Type*> argTypes(2, CGF.Int8PtrPtrTy); | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1527 | llvm::FunctionType *fnType | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1528 | = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false); | 
|  | 1529 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); | 
|  | 1530 | } | 
|  | 1531 |  | 
|  | 1532 | dst = CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy); | 
|  | 1533 | src = CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy); | 
|  | 1534 |  | 
|  | 1535 | llvm::CallInst *result = CGF.Builder.CreateCall2(fn, dst, src); | 
|  | 1536 | result->setDoesNotThrow(); | 
|  | 1537 | } | 
|  | 1538 |  | 
|  | 1539 | /// Produce the code to do a retain.  Based on the type, calls one of: | 
|  | 1540 | ///   call i8* @objc_retain(i8* %value) | 
|  | 1541 | ///   call i8* @objc_retainBlock(i8* %value) | 
|  | 1542 | llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { | 
|  | 1543 | if (type->isBlockPointerType()) | 
| John McCall | ff61303 | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 1544 | return EmitARCRetainBlock(value, /*mandatory*/ false); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1545 | else | 
|  | 1546 | return EmitARCRetainNonBlock(value); | 
|  | 1547 | } | 
|  | 1548 |  | 
|  | 1549 | /// Retain the given object, with normal retain semantics. | 
|  | 1550 | ///   call i8* @objc_retain(i8* %value) | 
|  | 1551 | llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { | 
|  | 1552 | return emitARCValueOperation(*this, value, | 
|  | 1553 | CGM.getARCEntrypoints().objc_retain, | 
|  | 1554 | "objc_retain"); | 
|  | 1555 | } | 
|  | 1556 |  | 
|  | 1557 | /// Retain the given block, with _Block_copy semantics. | 
|  | 1558 | ///   call i8* @objc_retainBlock(i8* %value) | 
| John McCall | ff61303 | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 1559 | /// | 
|  | 1560 | /// \param mandatory - If false, emit the call with metadata | 
|  | 1561 | /// indicating that it's okay for the optimizer to eliminate this call | 
|  | 1562 | /// if it can prove that the block never escapes except down the stack. | 
|  | 1563 | llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, | 
|  | 1564 | bool mandatory) { | 
|  | 1565 | llvm::Value *result | 
|  | 1566 | = emitARCValueOperation(*this, value, | 
|  | 1567 | CGM.getARCEntrypoints().objc_retainBlock, | 
|  | 1568 | "objc_retainBlock"); | 
|  | 1569 |  | 
|  | 1570 | // If the copy isn't mandatory, add !clang.arc.copy_on_escape to | 
|  | 1571 | // tell the optimizer that it doesn't need to do this copy if the | 
|  | 1572 | // block doesn't escape, where being passed as an argument doesn't | 
|  | 1573 | // count as escaping. | 
|  | 1574 | if (!mandatory && isa<llvm::Instruction>(result)) { | 
|  | 1575 | llvm::CallInst *call | 
|  | 1576 | = cast<llvm::CallInst>(result->stripPointerCasts()); | 
|  | 1577 | assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock); | 
|  | 1578 |  | 
|  | 1579 | SmallVector<llvm::Value*,1> args; | 
|  | 1580 | call->setMetadata("clang.arc.copy_on_escape", | 
|  | 1581 | llvm::MDNode::get(Builder.getContext(), args)); | 
|  | 1582 | } | 
|  | 1583 |  | 
|  | 1584 | return result; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1585 | } | 
|  | 1586 |  | 
|  | 1587 | /// Retain the given object which is the result of a function call. | 
|  | 1588 | ///   call i8* @objc_retainAutoreleasedReturnValue(i8* %value) | 
|  | 1589 | /// | 
|  | 1590 | /// Yes, this function name is one character away from a different | 
|  | 1591 | /// call with completely different semantics. | 
|  | 1592 | llvm::Value * | 
|  | 1593 | CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { | 
|  | 1594 | // Fetch the void(void) inline asm which marks that we're going to | 
|  | 1595 | // retain the autoreleased return value. | 
|  | 1596 | llvm::InlineAsm *&marker | 
|  | 1597 | = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker; | 
|  | 1598 | if (!marker) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1599 | StringRef assembly | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1600 | = CGM.getTargetCodeGenInfo() | 
|  | 1601 | .getARCRetainAutoreleasedReturnValueMarker(); | 
|  | 1602 |  | 
|  | 1603 | // If we have an empty assembly string, there's nothing to do. | 
|  | 1604 | if (assembly.empty()) { | 
|  | 1605 |  | 
|  | 1606 | // Otherwise, at -O0, build an inline asm that we're going to call | 
|  | 1607 | // in a moment. | 
|  | 1608 | } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) { | 
|  | 1609 | llvm::FunctionType *type = | 
|  | 1610 | llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), | 
|  | 1611 | /*variadic*/ false); | 
|  | 1612 |  | 
|  | 1613 | marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); | 
|  | 1614 |  | 
|  | 1615 | // If we're at -O1 and above, we don't want to litter the code | 
|  | 1616 | // with this marker yet, so leave a breadcrumb for the ARC | 
|  | 1617 | // optimizer to pick up. | 
|  | 1618 | } else { | 
|  | 1619 | llvm::NamedMDNode *metadata = | 
|  | 1620 | CGM.getModule().getOrInsertNamedMetadata( | 
|  | 1621 | "clang.arc.retainAutoreleasedReturnValueMarker"); | 
|  | 1622 | assert(metadata->getNumOperands() <= 1); | 
|  | 1623 | if (metadata->getNumOperands() == 0) { | 
|  | 1624 | llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly); | 
| Jay Foad | 5709f7c | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1625 | metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string)); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1626 | } | 
|  | 1627 | } | 
|  | 1628 | } | 
|  | 1629 |  | 
|  | 1630 | // Call the marker asm if we made one, which we do only at -O0. | 
|  | 1631 | if (marker) Builder.CreateCall(marker); | 
|  | 1632 |  | 
|  | 1633 | return emitARCValueOperation(*this, value, | 
|  | 1634 | CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue, | 
|  | 1635 | "objc_retainAutoreleasedReturnValue"); | 
|  | 1636 | } | 
|  | 1637 |  | 
|  | 1638 | /// Release the given object. | 
|  | 1639 | ///   call void @objc_release(i8* %value) | 
|  | 1640 | void CodeGenFunction::EmitARCRelease(llvm::Value *value, bool precise) { | 
|  | 1641 | if (isa<llvm::ConstantPointerNull>(value)) return; | 
|  | 1642 |  | 
|  | 1643 | llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release; | 
|  | 1644 | if (!fn) { | 
| Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1645 | std::vector<llvm::Type*> args(1, Int8PtrTy); | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1646 | llvm::FunctionType *fnType = | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1647 | llvm::FunctionType::get(Builder.getVoidTy(), args, false); | 
|  | 1648 | fn = createARCRuntimeFunction(CGM, fnType, "objc_release"); | 
|  | 1649 | } | 
|  | 1650 |  | 
|  | 1651 | // Cast the argument to 'id'. | 
|  | 1652 | value = Builder.CreateBitCast(value, Int8PtrTy); | 
|  | 1653 |  | 
|  | 1654 | // Call objc_release. | 
|  | 1655 | llvm::CallInst *call = Builder.CreateCall(fn, value); | 
|  | 1656 | call->setDoesNotThrow(); | 
|  | 1657 |  | 
|  | 1658 | if (!precise) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1659 | SmallVector<llvm::Value*,1> args; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1660 | call->setMetadata("clang.imprecise_release", | 
|  | 1661 | llvm::MDNode::get(Builder.getContext(), args)); | 
|  | 1662 | } | 
|  | 1663 | } | 
|  | 1664 |  | 
|  | 1665 | /// Store into a strong object.  Always calls this: | 
|  | 1666 | ///   call void @objc_storeStrong(i8** %addr, i8* %value) | 
|  | 1667 | llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr, | 
|  | 1668 | llvm::Value *value, | 
|  | 1669 | bool ignored) { | 
|  | 1670 | assert(cast<llvm::PointerType>(addr->getType())->getElementType() | 
|  | 1671 | == value->getType()); | 
|  | 1672 |  | 
|  | 1673 | llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong; | 
|  | 1674 | if (!fn) { | 
| Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1675 | llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy }; | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1676 | llvm::FunctionType *fnType | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1677 | = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false); | 
|  | 1678 | fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong"); | 
|  | 1679 | } | 
|  | 1680 |  | 
|  | 1681 | addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); | 
|  | 1682 | llvm::Value *castValue = Builder.CreateBitCast(value, Int8PtrTy); | 
|  | 1683 |  | 
|  | 1684 | Builder.CreateCall2(fn, addr, castValue)->setDoesNotThrow(); | 
|  | 1685 |  | 
|  | 1686 | if (ignored) return 0; | 
|  | 1687 | return value; | 
|  | 1688 | } | 
|  | 1689 |  | 
|  | 1690 | /// Store into a strong object.  Sometimes calls this: | 
|  | 1691 | ///   call void @objc_storeStrong(i8** %addr, i8* %value) | 
|  | 1692 | /// Other times, breaks it down into components. | 
| John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1693 | llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1694 | llvm::Value *newValue, | 
|  | 1695 | bool ignored) { | 
| John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1696 | QualType type = dst.getType(); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1697 | bool isBlock = type->isBlockPointerType(); | 
|  | 1698 |  | 
|  | 1699 | // Use a store barrier at -O0 unless this is a block type or the | 
|  | 1700 | // lvalue is inadequately aligned. | 
|  | 1701 | if (shouldUseFusedARCCalls() && | 
|  | 1702 | !isBlock && | 
| Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1703 | (dst.getAlignment().isZero() || | 
|  | 1704 | dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1705 | return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); | 
|  | 1706 | } | 
|  | 1707 |  | 
|  | 1708 | // Otherwise, split it out. | 
|  | 1709 |  | 
|  | 1710 | // Retain the new value. | 
|  | 1711 | newValue = EmitARCRetain(type, newValue); | 
|  | 1712 |  | 
|  | 1713 | // Read the old value. | 
| John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1714 | llvm::Value *oldValue = EmitLoadOfScalar(dst); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1715 |  | 
|  | 1716 | // Store.  We do this before the release so that any deallocs won't | 
|  | 1717 | // see the old value. | 
| John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1718 | EmitStoreOfScalar(newValue, dst); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1719 |  | 
|  | 1720 | // Finally, release the old value. | 
|  | 1721 | EmitARCRelease(oldValue, /*precise*/ false); | 
|  | 1722 |  | 
|  | 1723 | return newValue; | 
|  | 1724 | } | 
|  | 1725 |  | 
|  | 1726 | /// Autorelease the given object. | 
|  | 1727 | ///   call i8* @objc_autorelease(i8* %value) | 
|  | 1728 | llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { | 
|  | 1729 | return emitARCValueOperation(*this, value, | 
|  | 1730 | CGM.getARCEntrypoints().objc_autorelease, | 
|  | 1731 | "objc_autorelease"); | 
|  | 1732 | } | 
|  | 1733 |  | 
|  | 1734 | /// Autorelease the given object. | 
|  | 1735 | ///   call i8* @objc_autoreleaseReturnValue(i8* %value) | 
|  | 1736 | llvm::Value * | 
|  | 1737 | CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { | 
|  | 1738 | return emitARCValueOperation(*this, value, | 
|  | 1739 | CGM.getARCEntrypoints().objc_autoreleaseReturnValue, | 
|  | 1740 | "objc_autoreleaseReturnValue"); | 
|  | 1741 | } | 
|  | 1742 |  | 
|  | 1743 | /// Do a fused retain/autorelease of the given object. | 
|  | 1744 | ///   call i8* @objc_retainAutoreleaseReturnValue(i8* %value) | 
|  | 1745 | llvm::Value * | 
|  | 1746 | CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { | 
|  | 1747 | return emitARCValueOperation(*this, value, | 
|  | 1748 | CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue, | 
|  | 1749 | "objc_retainAutoreleaseReturnValue"); | 
|  | 1750 | } | 
|  | 1751 |  | 
|  | 1752 | /// Do a fused retain/autorelease of the given object. | 
|  | 1753 | ///   call i8* @objc_retainAutorelease(i8* %value) | 
|  | 1754 | /// or | 
|  | 1755 | ///   %retain = call i8* @objc_retainBlock(i8* %value) | 
|  | 1756 | ///   call i8* @objc_autorelease(i8* %retain) | 
|  | 1757 | llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, | 
|  | 1758 | llvm::Value *value) { | 
|  | 1759 | if (!type->isBlockPointerType()) | 
|  | 1760 | return EmitARCRetainAutoreleaseNonBlock(value); | 
|  | 1761 |  | 
|  | 1762 | if (isa<llvm::ConstantPointerNull>(value)) return value; | 
|  | 1763 |  | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1764 | llvm::Type *origType = value->getType(); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1765 | value = Builder.CreateBitCast(value, Int8PtrTy); | 
| John McCall | ff61303 | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 1766 | value = EmitARCRetainBlock(value, /*mandatory*/ true); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1767 | value = EmitARCAutorelease(value); | 
|  | 1768 | return Builder.CreateBitCast(value, origType); | 
|  | 1769 | } | 
|  | 1770 |  | 
|  | 1771 | /// Do a fused retain/autorelease of the given object. | 
|  | 1772 | ///   call i8* @objc_retainAutorelease(i8* %value) | 
|  | 1773 | llvm::Value * | 
|  | 1774 | CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { | 
|  | 1775 | return emitARCValueOperation(*this, value, | 
|  | 1776 | CGM.getARCEntrypoints().objc_retainAutorelease, | 
|  | 1777 | "objc_retainAutorelease"); | 
|  | 1778 | } | 
|  | 1779 |  | 
|  | 1780 | /// i8* @objc_loadWeak(i8** %addr) | 
|  | 1781 | /// Essentially objc_autorelease(objc_loadWeakRetained(addr)). | 
|  | 1782 | llvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) { | 
|  | 1783 | return emitARCLoadOperation(*this, addr, | 
|  | 1784 | CGM.getARCEntrypoints().objc_loadWeak, | 
|  | 1785 | "objc_loadWeak"); | 
|  | 1786 | } | 
|  | 1787 |  | 
|  | 1788 | /// i8* @objc_loadWeakRetained(i8** %addr) | 
|  | 1789 | llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) { | 
|  | 1790 | return emitARCLoadOperation(*this, addr, | 
|  | 1791 | CGM.getARCEntrypoints().objc_loadWeakRetained, | 
|  | 1792 | "objc_loadWeakRetained"); | 
|  | 1793 | } | 
|  | 1794 |  | 
|  | 1795 | /// i8* @objc_storeWeak(i8** %addr, i8* %value) | 
|  | 1796 | /// Returns %value. | 
|  | 1797 | llvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr, | 
|  | 1798 | llvm::Value *value, | 
|  | 1799 | bool ignored) { | 
|  | 1800 | return emitARCStoreOperation(*this, addr, value, | 
|  | 1801 | CGM.getARCEntrypoints().objc_storeWeak, | 
|  | 1802 | "objc_storeWeak", ignored); | 
|  | 1803 | } | 
|  | 1804 |  | 
|  | 1805 | /// i8* @objc_initWeak(i8** %addr, i8* %value) | 
|  | 1806 | /// Returns %value.  %addr is known to not have a current weak entry. | 
|  | 1807 | /// Essentially equivalent to: | 
|  | 1808 | ///   *addr = nil; objc_storeWeak(addr, value); | 
|  | 1809 | void CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) { | 
|  | 1810 | // If we're initializing to null, just write null to memory; no need | 
|  | 1811 | // to get the runtime involved.  But don't do this if optimization | 
|  | 1812 | // is enabled, because accounting for this would make the optimizer | 
|  | 1813 | // much more complicated. | 
|  | 1814 | if (isa<llvm::ConstantPointerNull>(value) && | 
|  | 1815 | CGM.getCodeGenOpts().OptimizationLevel == 0) { | 
|  | 1816 | Builder.CreateStore(value, addr); | 
|  | 1817 | return; | 
|  | 1818 | } | 
|  | 1819 |  | 
|  | 1820 | emitARCStoreOperation(*this, addr, value, | 
|  | 1821 | CGM.getARCEntrypoints().objc_initWeak, | 
|  | 1822 | "objc_initWeak", /*ignored*/ true); | 
|  | 1823 | } | 
|  | 1824 |  | 
|  | 1825 | /// void @objc_destroyWeak(i8** %addr) | 
|  | 1826 | /// Essentially objc_storeWeak(addr, nil). | 
|  | 1827 | void CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) { | 
|  | 1828 | llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak; | 
|  | 1829 | if (!fn) { | 
| Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1830 | std::vector<llvm::Type*> args(1, Int8PtrPtrTy); | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1831 | llvm::FunctionType *fnType = | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1832 | llvm::FunctionType::get(Builder.getVoidTy(), args, false); | 
|  | 1833 | fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak"); | 
|  | 1834 | } | 
|  | 1835 |  | 
|  | 1836 | // Cast the argument to 'id*'. | 
|  | 1837 | addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); | 
|  | 1838 |  | 
|  | 1839 | llvm::CallInst *call = Builder.CreateCall(fn, addr); | 
|  | 1840 | call->setDoesNotThrow(); | 
|  | 1841 | } | 
|  | 1842 |  | 
|  | 1843 | /// void @objc_moveWeak(i8** %dest, i8** %src) | 
|  | 1844 | /// Disregards the current value in %dest.  Leaves %src pointing to nothing. | 
|  | 1845 | /// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). | 
|  | 1846 | void CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) { | 
|  | 1847 | emitARCCopyOperation(*this, dst, src, | 
|  | 1848 | CGM.getARCEntrypoints().objc_moveWeak, | 
|  | 1849 | "objc_moveWeak"); | 
|  | 1850 | } | 
|  | 1851 |  | 
|  | 1852 | /// void @objc_copyWeak(i8** %dest, i8** %src) | 
|  | 1853 | /// Disregards the current value in %dest.  Essentially | 
|  | 1854 | ///   objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) | 
|  | 1855 | void CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) { | 
|  | 1856 | emitARCCopyOperation(*this, dst, src, | 
|  | 1857 | CGM.getARCEntrypoints().objc_copyWeak, | 
|  | 1858 | "objc_copyWeak"); | 
|  | 1859 | } | 
|  | 1860 |  | 
|  | 1861 | /// Produce the code to do a objc_autoreleasepool_push. | 
|  | 1862 | ///   call i8* @objc_autoreleasePoolPush(void) | 
|  | 1863 | llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { | 
|  | 1864 | llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush; | 
|  | 1865 | if (!fn) { | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1866 | llvm::FunctionType *fnType = | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1867 | llvm::FunctionType::get(Int8PtrTy, false); | 
|  | 1868 | fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush"); | 
|  | 1869 | } | 
|  | 1870 |  | 
|  | 1871 | llvm::CallInst *call = Builder.CreateCall(fn); | 
|  | 1872 | call->setDoesNotThrow(); | 
|  | 1873 |  | 
|  | 1874 | return call; | 
|  | 1875 | } | 
|  | 1876 |  | 
|  | 1877 | /// Produce the code to do a primitive release. | 
|  | 1878 | ///   call void @objc_autoreleasePoolPop(i8* %ptr) | 
|  | 1879 | void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { | 
|  | 1880 | assert(value->getType() == Int8PtrTy); | 
|  | 1881 |  | 
|  | 1882 | llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop; | 
|  | 1883 | if (!fn) { | 
| Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1884 | std::vector<llvm::Type*> args(1, Int8PtrTy); | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1885 | llvm::FunctionType *fnType = | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1886 | llvm::FunctionType::get(Builder.getVoidTy(), args, false); | 
|  | 1887 |  | 
|  | 1888 | // We don't want to use a weak import here; instead we should not | 
|  | 1889 | // fall into this path. | 
|  | 1890 | fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop"); | 
|  | 1891 | } | 
|  | 1892 |  | 
|  | 1893 | llvm::CallInst *call = Builder.CreateCall(fn, value); | 
|  | 1894 | call->setDoesNotThrow(); | 
|  | 1895 | } | 
|  | 1896 |  | 
|  | 1897 | /// Produce the code to do an MRR version objc_autoreleasepool_push. | 
|  | 1898 | /// Which is: [[NSAutoreleasePool alloc] init]; | 
|  | 1899 | /// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. | 
|  | 1900 | /// init is declared as: - (id) init; in its NSObject super class. | 
|  | 1901 | /// | 
|  | 1902 | llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { | 
|  | 1903 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); | 
|  | 1904 | llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(Builder); | 
|  | 1905 | // [NSAutoreleasePool alloc] | 
|  | 1906 | IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); | 
|  | 1907 | Selector AllocSel = getContext().Selectors.getSelector(0, &II); | 
|  | 1908 | CallArgList Args; | 
|  | 1909 | RValue AllocRV = | 
|  | 1910 | Runtime.GenerateMessageSend(*this, ReturnValueSlot(), | 
|  | 1911 | getContext().getObjCIdType(), | 
|  | 1912 | AllocSel, Receiver, Args); | 
|  | 1913 |  | 
|  | 1914 | // [Receiver init] | 
|  | 1915 | Receiver = AllocRV.getScalarVal(); | 
|  | 1916 | II = &CGM.getContext().Idents.get("init"); | 
|  | 1917 | Selector InitSel = getContext().Selectors.getSelector(0, &II); | 
|  | 1918 | RValue InitRV = | 
|  | 1919 | Runtime.GenerateMessageSend(*this, ReturnValueSlot(), | 
|  | 1920 | getContext().getObjCIdType(), | 
|  | 1921 | InitSel, Receiver, Args); | 
|  | 1922 | return InitRV.getScalarVal(); | 
|  | 1923 | } | 
|  | 1924 |  | 
|  | 1925 | /// Produce the code to do a primitive release. | 
|  | 1926 | /// [tmp drain]; | 
|  | 1927 | void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { | 
|  | 1928 | IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); | 
|  | 1929 | Selector DrainSel = getContext().Selectors.getSelector(0, &II); | 
|  | 1930 | CallArgList Args; | 
|  | 1931 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), | 
|  | 1932 | getContext().VoidTy, DrainSel, Arg, Args); | 
|  | 1933 | } | 
|  | 1934 |  | 
| John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1935 | void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, | 
|  | 1936 | llvm::Value *addr, | 
|  | 1937 | QualType type) { | 
|  | 1938 | llvm::Value *ptr = CGF.Builder.CreateLoad(addr, "strongdestroy"); | 
|  | 1939 | CGF.EmitARCRelease(ptr, /*precise*/ true); | 
|  | 1940 | } | 
|  | 1941 |  | 
|  | 1942 | void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, | 
|  | 1943 | llvm::Value *addr, | 
|  | 1944 | QualType type) { | 
|  | 1945 | llvm::Value *ptr = CGF.Builder.CreateLoad(addr, "strongdestroy"); | 
|  | 1946 | CGF.EmitARCRelease(ptr, /*precise*/ false); | 
|  | 1947 | } | 
|  | 1948 |  | 
|  | 1949 | void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, | 
|  | 1950 | llvm::Value *addr, | 
|  | 1951 | QualType type) { | 
|  | 1952 | CGF.EmitARCDestroyWeak(addr); | 
|  | 1953 | } | 
|  | 1954 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1955 | namespace { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1956 | struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup { | 
|  | 1957 | llvm::Value *Token; | 
|  | 1958 |  | 
|  | 1959 | CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} | 
|  | 1960 |  | 
| John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1961 | void Emit(CodeGenFunction &CGF, Flags flags) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1962 | CGF.EmitObjCAutoreleasePoolPop(Token); | 
|  | 1963 | } | 
|  | 1964 | }; | 
|  | 1965 | struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup { | 
|  | 1966 | llvm::Value *Token; | 
|  | 1967 |  | 
|  | 1968 | CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} | 
|  | 1969 |  | 
| John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1970 | void Emit(CodeGenFunction &CGF, Flags flags) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1971 | CGF.EmitObjCMRRAutoreleasePoolPop(Token); | 
|  | 1972 | } | 
|  | 1973 | }; | 
|  | 1974 | } | 
|  | 1975 |  | 
|  | 1976 | void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { | 
|  | 1977 | if (CGM.getLangOptions().ObjCAutoRefCount) | 
|  | 1978 | EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); | 
|  | 1979 | else | 
|  | 1980 | EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); | 
|  | 1981 | } | 
|  | 1982 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1983 | static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, | 
|  | 1984 | LValue lvalue, | 
|  | 1985 | QualType type) { | 
|  | 1986 | switch (type.getObjCLifetime()) { | 
|  | 1987 | case Qualifiers::OCL_None: | 
|  | 1988 | case Qualifiers::OCL_ExplicitNone: | 
|  | 1989 | case Qualifiers::OCL_Strong: | 
|  | 1990 | case Qualifiers::OCL_Autoreleasing: | 
| John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1991 | return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(), | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1992 | false); | 
|  | 1993 |  | 
|  | 1994 | case Qualifiers::OCL_Weak: | 
|  | 1995 | return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()), | 
|  | 1996 | true); | 
|  | 1997 | } | 
|  | 1998 |  | 
|  | 1999 | llvm_unreachable("impossible lifetime!"); | 
|  | 2000 | return TryEmitResult(); | 
|  | 2001 | } | 
|  | 2002 |  | 
|  | 2003 | static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, | 
|  | 2004 | const Expr *e) { | 
|  | 2005 | e = e->IgnoreParens(); | 
|  | 2006 | QualType type = e->getType(); | 
|  | 2007 |  | 
| John McCall | 154a2fd | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 2008 | // If we're loading retained from a __strong xvalue, we can avoid | 
|  | 2009 | // an extra retain/release pair by zeroing out the source of this | 
|  | 2010 | // "move" operation. | 
|  | 2011 | if (e->isXValue() && | 
|  | 2012 | !type.isConstQualified() && | 
|  | 2013 | type.getObjCLifetime() == Qualifiers::OCL_Strong) { | 
|  | 2014 | // Emit the lvalue. | 
|  | 2015 | LValue lv = CGF.EmitLValue(e); | 
|  | 2016 |  | 
|  | 2017 | // Load the object pointer. | 
|  | 2018 | llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal(); | 
|  | 2019 |  | 
|  | 2020 | // Set the source pointer to NULL. | 
|  | 2021 | CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv); | 
|  | 2022 |  | 
|  | 2023 | return TryEmitResult(result, true); | 
|  | 2024 | } | 
|  | 2025 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2026 | // As a very special optimization, in ARC++, if the l-value is the | 
|  | 2027 | // result of a non-volatile assignment, do a simple retain of the | 
|  | 2028 | // result of the call to objc_storeWeak instead of reloading. | 
|  | 2029 | if (CGF.getLangOptions().CPlusPlus && | 
|  | 2030 | !type.isVolatileQualified() && | 
|  | 2031 | type.getObjCLifetime() == Qualifiers::OCL_Weak && | 
|  | 2032 | isa<BinaryOperator>(e) && | 
|  | 2033 | cast<BinaryOperator>(e)->getOpcode() == BO_Assign) | 
|  | 2034 | return TryEmitResult(CGF.EmitScalarExpr(e), false); | 
|  | 2035 |  | 
|  | 2036 | return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); | 
|  | 2037 | } | 
|  | 2038 |  | 
|  | 2039 | static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, | 
|  | 2040 | llvm::Value *value); | 
|  | 2041 |  | 
|  | 2042 | /// Given that the given expression is some sort of call (which does | 
|  | 2043 | /// not return retained), emit a retain following it. | 
|  | 2044 | static llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) { | 
|  | 2045 | llvm::Value *value = CGF.EmitScalarExpr(e); | 
|  | 2046 | return emitARCRetainAfterCall(CGF, value); | 
|  | 2047 | } | 
|  | 2048 |  | 
|  | 2049 | static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, | 
|  | 2050 | llvm::Value *value) { | 
|  | 2051 | if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { | 
|  | 2052 | CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); | 
|  | 2053 |  | 
|  | 2054 | // Place the retain immediately following the call. | 
|  | 2055 | CGF.Builder.SetInsertPoint(call->getParent(), | 
|  | 2056 | ++llvm::BasicBlock::iterator(call)); | 
|  | 2057 | value = CGF.EmitARCRetainAutoreleasedReturnValue(value); | 
|  | 2058 |  | 
|  | 2059 | CGF.Builder.restoreIP(ip); | 
|  | 2060 | return value; | 
|  | 2061 | } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { | 
|  | 2062 | CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); | 
|  | 2063 |  | 
|  | 2064 | // Place the retain at the beginning of the normal destination block. | 
|  | 2065 | llvm::BasicBlock *BB = invoke->getNormalDest(); | 
|  | 2066 | CGF.Builder.SetInsertPoint(BB, BB->begin()); | 
|  | 2067 | value = CGF.EmitARCRetainAutoreleasedReturnValue(value); | 
|  | 2068 |  | 
|  | 2069 | CGF.Builder.restoreIP(ip); | 
|  | 2070 | return value; | 
|  | 2071 |  | 
|  | 2072 | // Bitcasts can arise because of related-result returns.  Rewrite | 
|  | 2073 | // the operand. | 
|  | 2074 | } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { | 
|  | 2075 | llvm::Value *operand = bitcast->getOperand(0); | 
|  | 2076 | operand = emitARCRetainAfterCall(CGF, operand); | 
|  | 2077 | bitcast->setOperand(0, operand); | 
|  | 2078 | return bitcast; | 
|  | 2079 |  | 
|  | 2080 | // Generic fall-back case. | 
|  | 2081 | } else { | 
|  | 2082 | // Retain using the non-block variant: we never need to do a copy | 
|  | 2083 | // of a block that's been returned to us. | 
|  | 2084 | return CGF.EmitARCRetainNonBlock(value); | 
|  | 2085 | } | 
|  | 2086 | } | 
|  | 2087 |  | 
| John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2088 | /// Determine whether it might be important to emit a separate | 
|  | 2089 | /// objc_retain_block on the result of the given expression, or | 
|  | 2090 | /// whether it's okay to just emit it in a +1 context. | 
|  | 2091 | static bool shouldEmitSeparateBlockRetain(const Expr *e) { | 
|  | 2092 | assert(e->getType()->isBlockPointerType()); | 
|  | 2093 | e = e->IgnoreParens(); | 
|  | 2094 |  | 
|  | 2095 | // For future goodness, emit block expressions directly in +1 | 
|  | 2096 | // contexts if we can. | 
|  | 2097 | if (isa<BlockExpr>(e)) | 
|  | 2098 | return false; | 
|  | 2099 |  | 
|  | 2100 | if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { | 
|  | 2101 | switch (cast->getCastKind()) { | 
|  | 2102 | // Emitting these operations in +1 contexts is goodness. | 
|  | 2103 | case CK_LValueToRValue: | 
| John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2104 | case CK_ARCReclaimReturnedObject: | 
|  | 2105 | case CK_ARCConsumeObject: | 
|  | 2106 | case CK_ARCProduceObject: | 
| John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2107 | return false; | 
|  | 2108 |  | 
|  | 2109 | // These operations preserve a block type. | 
|  | 2110 | case CK_NoOp: | 
|  | 2111 | case CK_BitCast: | 
|  | 2112 | return shouldEmitSeparateBlockRetain(cast->getSubExpr()); | 
|  | 2113 |  | 
|  | 2114 | // These operations are known to be bad (or haven't been considered). | 
|  | 2115 | case CK_AnyPointerToBlockPointerCast: | 
|  | 2116 | default: | 
|  | 2117 | return true; | 
|  | 2118 | } | 
|  | 2119 | } | 
|  | 2120 |  | 
|  | 2121 | return true; | 
|  | 2122 | } | 
|  | 2123 |  | 
| John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2124 | /// Try to emit a PseudoObjectExpr at +1. | 
|  | 2125 | /// | 
|  | 2126 | /// This massively duplicates emitPseudoObjectRValue. | 
|  | 2127 | static TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF, | 
|  | 2128 | const PseudoObjectExpr *E) { | 
|  | 2129 | llvm::SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; | 
|  | 2130 |  | 
|  | 2131 | // Find the result expression. | 
|  | 2132 | const Expr *resultExpr = E->getResultExpr(); | 
|  | 2133 | assert(resultExpr); | 
|  | 2134 | TryEmitResult result; | 
|  | 2135 |  | 
|  | 2136 | for (PseudoObjectExpr::const_semantics_iterator | 
|  | 2137 | i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { | 
|  | 2138 | const Expr *semantic = *i; | 
|  | 2139 |  | 
|  | 2140 | // If this semantic expression is an opaque value, bind it | 
|  | 2141 | // to the result of its source expression. | 
|  | 2142 | if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { | 
|  | 2143 | typedef CodeGenFunction::OpaqueValueMappingData OVMA; | 
|  | 2144 | OVMA opaqueData; | 
|  | 2145 |  | 
|  | 2146 | // If this semantic is the result of the pseudo-object | 
|  | 2147 | // expression, try to evaluate the source as +1. | 
|  | 2148 | if (ov == resultExpr) { | 
|  | 2149 | assert(!OVMA::shouldBindAsLValue(ov)); | 
|  | 2150 | result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr()); | 
|  | 2151 | opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer())); | 
|  | 2152 |  | 
|  | 2153 | // Otherwise, just bind it. | 
|  | 2154 | } else { | 
|  | 2155 | opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); | 
|  | 2156 | } | 
|  | 2157 | opaques.push_back(opaqueData); | 
|  | 2158 |  | 
|  | 2159 | // Otherwise, if the expression is the result, evaluate it | 
|  | 2160 | // and remember the result. | 
|  | 2161 | } else if (semantic == resultExpr) { | 
|  | 2162 | result = tryEmitARCRetainScalarExpr(CGF, semantic); | 
|  | 2163 |  | 
|  | 2164 | // Otherwise, evaluate the expression in an ignored context. | 
|  | 2165 | } else { | 
|  | 2166 | CGF.EmitIgnoredExpr(semantic); | 
|  | 2167 | } | 
|  | 2168 | } | 
|  | 2169 |  | 
|  | 2170 | // Unbind all the opaques now. | 
|  | 2171 | for (unsigned i = 0, e = opaques.size(); i != e; ++i) | 
|  | 2172 | opaques[i].unbind(CGF); | 
|  | 2173 |  | 
|  | 2174 | return result; | 
|  | 2175 | } | 
|  | 2176 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2177 | static TryEmitResult | 
|  | 2178 | tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { | 
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 2179 | // Look through cleanups. | 
|  | 2180 | if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { | 
| John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 2181 | CGF.enterFullExpression(cleanups); | 
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 2182 | CodeGenFunction::RunCleanupsScope scope(CGF); | 
|  | 2183 | return tryEmitARCRetainScalarExpr(CGF, cleanups->getSubExpr()); | 
|  | 2184 | } | 
|  | 2185 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2186 | // The desired result type, if it differs from the type of the | 
|  | 2187 | // ultimate opaque expression. | 
| Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2188 | llvm::Type *resultType = 0; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2189 |  | 
|  | 2190 | while (true) { | 
|  | 2191 | e = e->IgnoreParens(); | 
|  | 2192 |  | 
|  | 2193 | // There's a break at the end of this if-chain;  anything | 
|  | 2194 | // that wants to keep looping has to explicitly continue. | 
|  | 2195 | if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { | 
|  | 2196 | switch (ce->getCastKind()) { | 
|  | 2197 | // No-op casts don't change the type, so we just ignore them. | 
|  | 2198 | case CK_NoOp: | 
|  | 2199 | e = ce->getSubExpr(); | 
|  | 2200 | continue; | 
|  | 2201 |  | 
|  | 2202 | case CK_LValueToRValue: { | 
|  | 2203 | TryEmitResult loadResult | 
|  | 2204 | = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr()); | 
|  | 2205 | if (resultType) { | 
|  | 2206 | llvm::Value *value = loadResult.getPointer(); | 
|  | 2207 | value = CGF.Builder.CreateBitCast(value, resultType); | 
|  | 2208 | loadResult.setPointer(value); | 
|  | 2209 | } | 
|  | 2210 | return loadResult; | 
|  | 2211 | } | 
|  | 2212 |  | 
|  | 2213 | // These casts can change the type, so remember that and | 
|  | 2214 | // soldier on.  We only need to remember the outermost such | 
|  | 2215 | // cast, though. | 
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2216 | case CK_CPointerToObjCPointerCast: | 
|  | 2217 | case CK_BlockPointerToObjCPointerCast: | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2218 | case CK_AnyPointerToBlockPointerCast: | 
|  | 2219 | case CK_BitCast: | 
|  | 2220 | if (!resultType) | 
|  | 2221 | resultType = CGF.ConvertType(ce->getType()); | 
|  | 2222 | e = ce->getSubExpr(); | 
|  | 2223 | assert(e->getType()->hasPointerRepresentation()); | 
|  | 2224 | continue; | 
|  | 2225 |  | 
|  | 2226 | // For consumptions, just emit the subexpression and thus elide | 
|  | 2227 | // the retain/release pair. | 
| John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2228 | case CK_ARCConsumeObject: { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2229 | llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr()); | 
|  | 2230 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); | 
|  | 2231 | return TryEmitResult(result, true); | 
|  | 2232 | } | 
|  | 2233 |  | 
| John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2234 | // Block extends are net +0.  Naively, we could just recurse on | 
|  | 2235 | // the subexpression, but actually we need to ensure that the | 
|  | 2236 | // value is copied as a block, so there's a little filter here. | 
| John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2237 | case CK_ARCExtendBlockObject: { | 
| John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2238 | llvm::Value *result; // will be a +0 value | 
|  | 2239 |  | 
|  | 2240 | // If we can't safely assume the sub-expression will produce a | 
|  | 2241 | // block-copied value, emit the sub-expression at +0. | 
|  | 2242 | if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) { | 
|  | 2243 | result = CGF.EmitScalarExpr(ce->getSubExpr()); | 
|  | 2244 |  | 
|  | 2245 | // Otherwise, try to emit the sub-expression at +1 recursively. | 
|  | 2246 | } else { | 
|  | 2247 | TryEmitResult subresult | 
|  | 2248 | = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr()); | 
|  | 2249 | result = subresult.getPointer(); | 
|  | 2250 |  | 
|  | 2251 | // If that produced a retained value, just use that, | 
|  | 2252 | // possibly casting down. | 
|  | 2253 | if (subresult.getInt()) { | 
|  | 2254 | if (resultType) | 
|  | 2255 | result = CGF.Builder.CreateBitCast(result, resultType); | 
|  | 2256 | return TryEmitResult(result, true); | 
|  | 2257 | } | 
|  | 2258 |  | 
|  | 2259 | // Otherwise it's +0. | 
|  | 2260 | } | 
|  | 2261 |  | 
|  | 2262 | // Retain the object as a block, then cast down. | 
| John McCall | ff61303 | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 2263 | result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true); | 
| John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2264 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); | 
|  | 2265 | return TryEmitResult(result, true); | 
|  | 2266 | } | 
|  | 2267 |  | 
| John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 2268 | // For reclaims, emit the subexpression as a retained call and | 
|  | 2269 | // skip the consumption. | 
| John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2270 | case CK_ARCReclaimReturnedObject: { | 
| John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 2271 | llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr()); | 
|  | 2272 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); | 
|  | 2273 | return TryEmitResult(result, true); | 
|  | 2274 | } | 
|  | 2275 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2276 | default: | 
|  | 2277 | break; | 
|  | 2278 | } | 
|  | 2279 |  | 
|  | 2280 | // Skip __extension__. | 
|  | 2281 | } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { | 
|  | 2282 | if (op->getOpcode() == UO_Extension) { | 
|  | 2283 | e = op->getSubExpr(); | 
|  | 2284 | continue; | 
|  | 2285 | } | 
|  | 2286 |  | 
|  | 2287 | // For calls and message sends, use the retained-call logic. | 
|  | 2288 | // Delegate inits are a special case in that they're the only | 
|  | 2289 | // returns-retained expression that *isn't* surrounded by | 
|  | 2290 | // a consume. | 
|  | 2291 | } else if (isa<CallExpr>(e) || | 
|  | 2292 | (isa<ObjCMessageExpr>(e) && | 
|  | 2293 | !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { | 
|  | 2294 | llvm::Value *result = emitARCRetainCall(CGF, e); | 
|  | 2295 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); | 
|  | 2296 | return TryEmitResult(result, true); | 
| John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2297 |  | 
|  | 2298 | // Look through pseudo-object expressions. | 
|  | 2299 | } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { | 
|  | 2300 | TryEmitResult result | 
|  | 2301 | = tryEmitARCRetainPseudoObject(CGF, pseudo); | 
|  | 2302 | if (resultType) { | 
|  | 2303 | llvm::Value *value = result.getPointer(); | 
|  | 2304 | value = CGF.Builder.CreateBitCast(value, resultType); | 
|  | 2305 | result.setPointer(value); | 
|  | 2306 | } | 
|  | 2307 | return result; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2308 | } | 
|  | 2309 |  | 
|  | 2310 | // Conservatively halt the search at any other expression kind. | 
|  | 2311 | break; | 
|  | 2312 | } | 
|  | 2313 |  | 
|  | 2314 | // We didn't find an obvious production, so emit what we've got and | 
|  | 2315 | // tell the caller that we didn't manage to retain. | 
|  | 2316 | llvm::Value *result = CGF.EmitScalarExpr(e); | 
|  | 2317 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); | 
|  | 2318 | return TryEmitResult(result, false); | 
|  | 2319 | } | 
|  | 2320 |  | 
|  | 2321 | static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, | 
|  | 2322 | LValue lvalue, | 
|  | 2323 | QualType type) { | 
|  | 2324 | TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); | 
|  | 2325 | llvm::Value *value = result.getPointer(); | 
|  | 2326 | if (!result.getInt()) | 
|  | 2327 | value = CGF.EmitARCRetain(type, value); | 
|  | 2328 | return value; | 
|  | 2329 | } | 
|  | 2330 |  | 
|  | 2331 | /// EmitARCRetainScalarExpr - Semantically equivalent to | 
|  | 2332 | /// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a | 
|  | 2333 | /// best-effort attempt to peephole expressions that naturally produce | 
|  | 2334 | /// retained objects. | 
|  | 2335 | llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { | 
|  | 2336 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); | 
|  | 2337 | llvm::Value *value = result.getPointer(); | 
|  | 2338 | if (!result.getInt()) | 
|  | 2339 | value = EmitARCRetain(e->getType(), value); | 
|  | 2340 | return value; | 
|  | 2341 | } | 
|  | 2342 |  | 
|  | 2343 | llvm::Value * | 
|  | 2344 | CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { | 
|  | 2345 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); | 
|  | 2346 | llvm::Value *value = result.getPointer(); | 
|  | 2347 | if (result.getInt()) | 
|  | 2348 | value = EmitARCAutorelease(value); | 
|  | 2349 | else | 
|  | 2350 | value = EmitARCRetainAutorelease(e->getType(), value); | 
|  | 2351 | return value; | 
|  | 2352 | } | 
|  | 2353 |  | 
| John McCall | ff61303 | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 2354 | llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { | 
|  | 2355 | llvm::Value *result; | 
|  | 2356 | bool doRetain; | 
|  | 2357 |  | 
|  | 2358 | if (shouldEmitSeparateBlockRetain(e)) { | 
|  | 2359 | result = EmitScalarExpr(e); | 
|  | 2360 | doRetain = true; | 
|  | 2361 | } else { | 
|  | 2362 | TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); | 
|  | 2363 | result = subresult.getPointer(); | 
|  | 2364 | doRetain = !subresult.getInt(); | 
|  | 2365 | } | 
|  | 2366 |  | 
|  | 2367 | if (doRetain) | 
|  | 2368 | result = EmitARCRetainBlock(result, /*mandatory*/ true); | 
|  | 2369 | return EmitObjCConsumeObject(e->getType(), result); | 
|  | 2370 | } | 
|  | 2371 |  | 
| John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2372 | llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { | 
|  | 2373 | // In ARC, retain and autorelease the expression. | 
|  | 2374 | if (getLangOptions().ObjCAutoRefCount) { | 
|  | 2375 | // Do so before running any cleanups for the full-expression. | 
|  | 2376 | // tryEmitARCRetainScalarExpr does make an effort to do things | 
|  | 2377 | // inside cleanups, but there are crazy cases like | 
|  | 2378 | //   @throw A().foo; | 
|  | 2379 | // where a full retain+autorelease is required and would | 
|  | 2380 | // otherwise happen after the destructor for the temporary. | 
| John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 2381 | if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(expr)) { | 
|  | 2382 | enterFullExpression(ewc); | 
| John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2383 | expr = ewc->getSubExpr(); | 
| John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 2384 | } | 
| John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2385 |  | 
| John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 2386 | CodeGenFunction::RunCleanupsScope cleanups(*this); | 
| John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2387 | return EmitARCRetainAutoreleaseScalarExpr(expr); | 
|  | 2388 | } | 
|  | 2389 |  | 
|  | 2390 | // Otherwise, use the normal scalar-expression emission.  The | 
|  | 2391 | // exception machinery doesn't do anything special with the | 
|  | 2392 | // exception like retaining it, so there's no safety associated with | 
|  | 2393 | // only running cleanups after the throw has started, and when it | 
|  | 2394 | // matters it tends to be substantially inferior code. | 
|  | 2395 | return EmitScalarExpr(expr); | 
|  | 2396 | } | 
|  | 2397 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2398 | std::pair<LValue,llvm::Value*> | 
|  | 2399 | CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, | 
|  | 2400 | bool ignored) { | 
|  | 2401 | // Evaluate the RHS first. | 
|  | 2402 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); | 
|  | 2403 | llvm::Value *value = result.getPointer(); | 
|  | 2404 |  | 
| John McCall | b726a55 | 2011-07-28 07:23:35 +0000 | [diff] [blame] | 2405 | bool hasImmediateRetain = result.getInt(); | 
|  | 2406 |  | 
|  | 2407 | // If we didn't emit a retained object, and the l-value is of block | 
|  | 2408 | // type, then we need to emit the block-retain immediately in case | 
|  | 2409 | // it invalidates the l-value. | 
|  | 2410 | if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { | 
| John McCall | ff61303 | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 2411 | value = EmitARCRetainBlock(value, /*mandatory*/ false); | 
| John McCall | b726a55 | 2011-07-28 07:23:35 +0000 | [diff] [blame] | 2412 | hasImmediateRetain = true; | 
|  | 2413 | } | 
|  | 2414 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2415 | LValue lvalue = EmitLValue(e->getLHS()); | 
|  | 2416 |  | 
|  | 2417 | // If the RHS was emitted retained, expand this. | 
| John McCall | b726a55 | 2011-07-28 07:23:35 +0000 | [diff] [blame] | 2418 | if (hasImmediateRetain) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2419 | llvm::Value *oldValue = | 
| Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2420 | EmitLoadOfScalar(lvalue); | 
|  | 2421 | EmitStoreOfScalar(value, lvalue); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2422 | EmitARCRelease(oldValue, /*precise*/ false); | 
|  | 2423 | } else { | 
| John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2424 | value = EmitARCStoreStrong(lvalue, value, ignored); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2425 | } | 
|  | 2426 |  | 
|  | 2427 | return std::pair<LValue,llvm::Value*>(lvalue, value); | 
|  | 2428 | } | 
|  | 2429 |  | 
|  | 2430 | std::pair<LValue,llvm::Value*> | 
|  | 2431 | CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { | 
|  | 2432 | llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); | 
|  | 2433 | LValue lvalue = EmitLValue(e->getLHS()); | 
|  | 2434 |  | 
| Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2435 | EmitStoreOfScalar(value, lvalue); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2436 |  | 
|  | 2437 | return std::pair<LValue,llvm::Value*>(lvalue, value); | 
|  | 2438 | } | 
|  | 2439 |  | 
|  | 2440 | void CodeGenFunction::EmitObjCAutoreleasePoolStmt( | 
|  | 2441 | const ObjCAutoreleasePoolStmt &ARPS) { | 
|  | 2442 | const Stmt *subStmt = ARPS.getSubStmt(); | 
|  | 2443 | const CompoundStmt &S = cast<CompoundStmt>(*subStmt); | 
|  | 2444 |  | 
|  | 2445 | CGDebugInfo *DI = getDebugInfo(); | 
| Eric Christopher | 7cdf948 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2446 | if (DI) | 
|  | 2447 | DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2448 |  | 
|  | 2449 | // Keep track of the current cleanup stack depth. | 
|  | 2450 | RunCleanupsScope Scope(*this); | 
| John McCall | 24fc0de | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 2451 | if (CGM.getCodeGenOpts().ObjCRuntimeHasARC) { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2452 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); | 
|  | 2453 | EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); | 
|  | 2454 | } else { | 
|  | 2455 | llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); | 
|  | 2456 | EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); | 
|  | 2457 | } | 
|  | 2458 |  | 
|  | 2459 | for (CompoundStmt::const_body_iterator I = S.body_begin(), | 
|  | 2460 | E = S.body_end(); I != E; ++I) | 
|  | 2461 | EmitStmt(*I); | 
|  | 2462 |  | 
| Eric Christopher | 7cdf948 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2463 | if (DI) | 
|  | 2464 | DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2465 | } | 
| John McCall | 1bd2556 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2466 |  | 
|  | 2467 | /// EmitExtendGCLifetime - Given a pointer to an Objective-C object, | 
|  | 2468 | /// make sure it survives garbage collection until this point. | 
|  | 2469 | void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { | 
|  | 2470 | // We just use an inline assembly. | 
| John McCall | 1bd2556 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2471 | llvm::FunctionType *extenderType | 
| Jay Foad | 5709f7c | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 2472 | = llvm::FunctionType::get(VoidTy, VoidPtrTy, /*variadic*/ false); | 
| John McCall | 1bd2556 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2473 | llvm::Value *extender | 
|  | 2474 | = llvm::InlineAsm::get(extenderType, | 
|  | 2475 | /* assembly */ "", | 
|  | 2476 | /* constraints */ "r", | 
|  | 2477 | /* side effects */ true); | 
|  | 2478 |  | 
|  | 2479 | object = Builder.CreateBitCast(object, VoidPtrTy); | 
|  | 2480 | Builder.CreateCall(extender, object)->setDoesNotThrow(); | 
|  | 2481 | } | 
|  | 2482 |  | 
| Ted Kremenek | 43e0633 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 2483 | CGObjCRuntime::~CGObjCRuntime() {} |