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