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" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/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); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 32 | static RValue AdjustRelatedResultType(CodeGenFunction &CGF, |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 33 | QualType ET, |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 34 | const ObjCMethodDecl *Method, |
| 35 | RValue Result); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 36 | |
| 37 | /// Given the address of a variable of pointer type, find the correct |
| 38 | /// null to store into it. |
| 39 | static llvm::Constant *getNullForVariable(llvm::Value *addr) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 40 | llvm::Type *type = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 41 | cast<llvm::PointerType>(addr->getType())->getElementType(); |
| 42 | return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); |
| 43 | } |
| 44 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 45 | /// Emits an instance of NSConstantString representing the object. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) |
Daniel Dunbar | 71fcec9 | 2008-11-25 21:53:21 +0000 | [diff] [blame] | 47 | { |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 48 | llvm::Constant *C = |
| 49 | CGM.getObjCRuntime().GenerateConstantString(E->getString()); |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 50 | // FIXME: This bitcast should just be made an invariant on the Runtime. |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 51 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 54 | /// EmitObjCBoxedExpr - This routine generates code to call |
| 55 | /// the appropriate expression boxing method. This will either be |
| 56 | /// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:]. |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 57 | /// |
Eric Christopher | 16098f3 | 2012-03-29 17:31:31 +0000 | [diff] [blame] | 58 | llvm::Value * |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 59 | CodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 60 | // Generate the correct selector for this literal's concrete type. |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 61 | const Expr *SubExpr = E->getSubExpr(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 62 | // Get the method. |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 63 | const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod(); |
| 64 | assert(BoxingMethod && "BoxingMethod is null"); |
| 65 | assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method"); |
| 66 | Selector Sel = BoxingMethod->getSelector(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 67 | |
| 68 | // Generate a reference to the class pointer, which will be the receiver. |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 69 | // Assumes that the method was introduced in the class that should be |
| 70 | // messaged (avoids pulling it out of the result type). |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 71 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 72 | const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface(); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 73 | llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 74 | |
| 75 | const ParmVarDecl *argDecl = *BoxingMethod->param_begin(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 76 | QualType ArgQT = argDecl->getType().getUnqualifiedType(); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 77 | RValue RV = EmitAnyExpr(SubExpr); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 78 | CallArgList Args; |
| 79 | Args.add(RV, ArgQT); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 81 | RValue result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 82 | BoxingMethod->getResultType(), Sel, Receiver, Args, |
| 83 | ClassDecl, BoxingMethod); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 84 | return Builder.CreateBitCast(result.getScalarVal(), |
| 85 | ConvertType(E->getType())); |
| 86 | } |
| 87 | |
| 88 | llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, |
| 89 | const ObjCMethodDecl *MethodWithObjects) { |
| 90 | ASTContext &Context = CGM.getContext(); |
| 91 | const ObjCDictionaryLiteral *DLE = 0; |
| 92 | const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E); |
| 93 | if (!ALE) |
| 94 | DLE = cast<ObjCDictionaryLiteral>(E); |
| 95 | |
| 96 | // Compute the type of the array we're initializing. |
| 97 | uint64_t NumElements = |
| 98 | ALE ? ALE->getNumElements() : DLE->getNumElements(); |
| 99 | llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()), |
| 100 | NumElements); |
| 101 | QualType ElementType = Context.getObjCIdType().withConst(); |
| 102 | QualType ElementArrayType |
| 103 | = Context.getConstantArrayType(ElementType, APNumElements, |
| 104 | ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 105 | |
| 106 | // Allocate the temporary array(s). |
| 107 | llvm::Value *Objects = CreateMemTemp(ElementArrayType, "objects"); |
| 108 | llvm::Value *Keys = 0; |
| 109 | if (DLE) |
| 110 | Keys = CreateMemTemp(ElementArrayType, "keys"); |
| 111 | |
| 112 | // Perform the actual initialialization of the array(s). |
| 113 | for (uint64_t i = 0; i < NumElements; i++) { |
| 114 | if (ALE) { |
| 115 | // Emit the initializer. |
| 116 | const Expr *Rhs = ALE->getElement(i); |
| 117 | LValue LV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), |
| 118 | ElementType, |
| 119 | Context.getTypeAlignInChars(Rhs->getType()), |
| 120 | Context); |
| 121 | EmitScalarInit(Rhs, /*D=*/0, LV, /*capturedByInit=*/false); |
| 122 | } else { |
| 123 | // Emit the key initializer. |
| 124 | const Expr *Key = DLE->getKeyValueElement(i).Key; |
| 125 | LValue KeyLV = LValue::MakeAddr(Builder.CreateStructGEP(Keys, i), |
| 126 | ElementType, |
| 127 | Context.getTypeAlignInChars(Key->getType()), |
| 128 | Context); |
| 129 | EmitScalarInit(Key, /*D=*/0, KeyLV, /*capturedByInit=*/false); |
| 130 | |
| 131 | // Emit the value initializer. |
| 132 | const Expr *Value = DLE->getKeyValueElement(i).Value; |
| 133 | LValue ValueLV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), |
| 134 | ElementType, |
| 135 | Context.getTypeAlignInChars(Value->getType()), |
| 136 | Context); |
| 137 | EmitScalarInit(Value, /*D=*/0, ValueLV, /*capturedByInit=*/false); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Generate the argument list. |
| 142 | CallArgList Args; |
| 143 | ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin(); |
| 144 | const ParmVarDecl *argDecl = *PI++; |
| 145 | QualType ArgQT = argDecl->getType().getUnqualifiedType(); |
| 146 | Args.add(RValue::get(Objects), ArgQT); |
| 147 | if (DLE) { |
| 148 | argDecl = *PI++; |
| 149 | ArgQT = argDecl->getType().getUnqualifiedType(); |
| 150 | Args.add(RValue::get(Keys), ArgQT); |
| 151 | } |
| 152 | argDecl = *PI; |
| 153 | ArgQT = argDecl->getType().getUnqualifiedType(); |
| 154 | llvm::Value *Count = |
| 155 | llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements); |
| 156 | Args.add(RValue::get(Count), ArgQT); |
| 157 | |
| 158 | // Generate a reference to the class pointer, which will be the receiver. |
| 159 | Selector Sel = MethodWithObjects->getSelector(); |
| 160 | QualType ResultType = E->getType(); |
| 161 | const ObjCObjectPointerType *InterfacePointerType |
| 162 | = ResultType->getAsObjCInterfacePointerType(); |
| 163 | ObjCInterfaceDecl *Class |
| 164 | = InterfacePointerType->getObjectType()->getInterface(); |
| 165 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 166 | llvm::Value *Receiver = Runtime.GetClass(*this, Class); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 167 | |
| 168 | // Generate the message send. |
Eric Christopher | 16098f3 | 2012-03-29 17:31:31 +0000 | [diff] [blame] | 169 | RValue result |
| 170 | = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
| 171 | MethodWithObjects->getResultType(), |
| 172 | Sel, |
| 173 | Receiver, Args, Class, |
| 174 | MethodWithObjects); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 175 | return Builder.CreateBitCast(result.getScalarVal(), |
| 176 | ConvertType(E->getType())); |
| 177 | } |
| 178 | |
| 179 | llvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) { |
| 180 | return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod()); |
| 181 | } |
| 182 | |
| 183 | llvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral( |
| 184 | const ObjCDictionaryLiteral *E) { |
| 185 | return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod()); |
| 186 | } |
| 187 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 188 | /// Emit a selector. |
| 189 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { |
| 190 | // Untyped selector. |
| 191 | // Note that this implementation allows for non-constant strings to be passed |
| 192 | // as arguments to @selector(). Currently, the only thing preventing this |
| 193 | // behaviour is the type checking in the front end. |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 194 | return CGM.getObjCRuntime().GetSelector(*this, E->getSelector()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 197 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { |
| 198 | // FIXME: This should pass the Decl not the name. |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 199 | return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol()); |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 200 | } |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 201 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 202 | /// \brief Adjust the type of the result of an Objective-C message send |
| 203 | /// expression when the method has a related result type. |
| 204 | static RValue AdjustRelatedResultType(CodeGenFunction &CGF, |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 205 | QualType ExpT, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 206 | const ObjCMethodDecl *Method, |
| 207 | RValue Result) { |
| 208 | if (!Method) |
| 209 | return Result; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 211 | if (!Method->hasRelatedResultType() || |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 212 | CGF.getContext().hasSameType(ExpT, Method->getResultType()) || |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 213 | !Result.isScalar()) |
| 214 | return Result; |
| 215 | |
| 216 | // We have applied a related result type. Cast the rvalue appropriately. |
| 217 | return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 218 | CGF.ConvertType(ExpT))); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 219 | } |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 220 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 221 | /// Decide whether to extend the lifetime of the receiver of a |
| 222 | /// returns-inner-pointer message. |
| 223 | static bool |
| 224 | shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) { |
| 225 | switch (message->getReceiverKind()) { |
| 226 | |
| 227 | // For a normal instance message, we should extend unless the |
| 228 | // receiver is loaded from a variable with precise lifetime. |
| 229 | case ObjCMessageExpr::Instance: { |
| 230 | const Expr *receiver = message->getInstanceReceiver(); |
| 231 | const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver); |
| 232 | if (!ice || ice->getCastKind() != CK_LValueToRValue) return true; |
| 233 | receiver = ice->getSubExpr()->IgnoreParens(); |
| 234 | |
| 235 | // Only __strong variables. |
| 236 | if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 237 | return true; |
| 238 | |
| 239 | // All ivars and fields have precise lifetime. |
| 240 | if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver)) |
| 241 | return false; |
| 242 | |
| 243 | // Otherwise, check for variables. |
| 244 | const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr()); |
| 245 | if (!declRef) return true; |
| 246 | const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl()); |
| 247 | if (!var) return true; |
| 248 | |
| 249 | // All variables have precise lifetime except local variables with |
| 250 | // automatic storage duration that aren't specially marked. |
| 251 | return (var->hasLocalStorage() && |
| 252 | !var->hasAttr<ObjCPreciseLifetimeAttr>()); |
| 253 | } |
| 254 | |
| 255 | case ObjCMessageExpr::Class: |
| 256 | case ObjCMessageExpr::SuperClass: |
| 257 | // It's never necessary for class objects. |
| 258 | return false; |
| 259 | |
| 260 | case ObjCMessageExpr::SuperInstance: |
| 261 | // We generally assume that 'self' lives throughout a method call. |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | llvm_unreachable("invalid receiver kind"); |
| 266 | } |
| 267 | |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 268 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, |
| 269 | ReturnValueSlot Return) { |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 270 | // Only the lookup mechanism and first two arguments of the method |
| 271 | // implementation vary between runtimes. We can get the receiver and |
| 272 | // arguments in generic code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 274 | bool isDelegateInit = E->isDelegateInitCall(); |
| 275 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 276 | const ObjCMethodDecl *method = E->getMethodDecl(); |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 277 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 278 | // We don't retain the receiver in delegate init calls, and this is |
| 279 | // safe because the receiver value is always loaded from 'self', |
| 280 | // which we zero out. We don't want to Block_copy block receivers, |
| 281 | // though. |
| 282 | bool retainSelf = |
| 283 | (!isDelegateInit && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 284 | CGM.getLangOpts().ObjCAutoRefCount && |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 285 | method && |
| 286 | method->hasAttr<NSConsumesSelfAttr>()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 287 | |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 288 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 289 | bool isSuperMessage = false; |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 290 | bool isClassMessage = false; |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 291 | ObjCInterfaceDecl *OID = 0; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 292 | // Find the receiver |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 293 | QualType ReceiverType; |
Daniel Dunbar | 0b647a6 | 2010-04-22 03:17:06 +0000 | [diff] [blame] | 294 | llvm::Value *Receiver = 0; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 295 | switch (E->getReceiverKind()) { |
| 296 | case ObjCMessageExpr::Instance: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 297 | ReceiverType = E->getInstanceReceiver()->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 298 | if (retainSelf) { |
| 299 | TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, |
| 300 | E->getInstanceReceiver()); |
| 301 | Receiver = ter.getPointer(); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 302 | if (ter.getInt()) retainSelf = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 303 | } else |
| 304 | Receiver = EmitScalarExpr(E->getInstanceReceiver()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 305 | break; |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 307 | case ObjCMessageExpr::Class: { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 308 | ReceiverType = E->getClassReceiver(); |
| 309 | const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); |
John McCall | 3031c63 | 2010-05-17 20:12:43 +0000 | [diff] [blame] | 310 | assert(ObjTy && "Invalid Objective-C class message send"); |
| 311 | OID = ObjTy->getInterface(); |
| 312 | assert(OID && "Invalid Objective-C class message send"); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 313 | Receiver = Runtime.GetClass(*this, OID); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 314 | isClassMessage = true; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | |
| 318 | case ObjCMessageExpr::SuperInstance: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 319 | ReceiverType = E->getSuperType(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 320 | Receiver = LoadObjCSelf(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 321 | isSuperMessage = true; |
| 322 | break; |
| 323 | |
| 324 | case ObjCMessageExpr::SuperClass: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 325 | ReceiverType = E->getSuperType(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 326 | Receiver = LoadObjCSelf(); |
| 327 | isSuperMessage = true; |
| 328 | isClassMessage = true; |
| 329 | break; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 330 | } |
| 331 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 332 | if (retainSelf) |
| 333 | Receiver = EmitARCRetainNonBlock(Receiver); |
| 334 | |
| 335 | // In ARC, we sometimes want to "extend the lifetime" |
| 336 | // (i.e. retain+autorelease) of receivers of returns-inner-pointer |
| 337 | // messages. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 338 | if (getLangOpts().ObjCAutoRefCount && method && |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 339 | method->hasAttr<ObjCReturnsInnerPointerAttr>() && |
| 340 | shouldExtendReceiverForInnerPointerMessage(E)) |
| 341 | Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver); |
| 342 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 343 | QualType ResultType = |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 344 | method ? method->getResultType() : E->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 345 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 346 | CallArgList Args; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 347 | EmitCallArgs(Args, method, E->arg_begin(), E->arg_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 349 | // For delegate init calls in ARC, do an unsafe store of null into |
| 350 | // self. This represents the call taking direct ownership of that |
| 351 | // value. We have to do this after emitting the other call |
| 352 | // arguments because they might also reference self, but we don't |
| 353 | // have to worry about any of them modifying self because that would |
| 354 | // be an undefined read and write of an object in unordered |
| 355 | // expressions. |
| 356 | if (isDelegateInit) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 357 | assert(getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 358 | "delegate init calls should only be marked in ARC"); |
| 359 | |
| 360 | // Do an unsafe store of null into self. |
| 361 | llvm::Value *selfAddr = |
| 362 | LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; |
| 363 | assert(selfAddr && "no self entry for a delegate init call?"); |
| 364 | |
| 365 | Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); |
| 366 | } |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 368 | RValue result; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 369 | if (isSuperMessage) { |
Chris Lattner | 9384c76 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 370 | // super is only valid in an Objective-C method |
| 371 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 372 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 373 | result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, |
| 374 | E->getSelector(), |
| 375 | OMD->getClassInterface(), |
| 376 | isCategoryImpl, |
| 377 | Receiver, |
| 378 | isClassMessage, |
| 379 | Args, |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 380 | method); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 381 | } else { |
| 382 | result = Runtime.GenerateMessageSend(*this, Return, ResultType, |
| 383 | E->getSelector(), |
| 384 | Receiver, Args, OID, |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 385 | method); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 386 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 387 | |
| 388 | // For delegate init calls in ARC, implicitly store the result of |
| 389 | // the call back into self. This takes ownership of the value. |
| 390 | if (isDelegateInit) { |
| 391 | llvm::Value *selfAddr = |
| 392 | LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; |
| 393 | llvm::Value *newSelf = result.getScalarVal(); |
| 394 | |
| 395 | // The delegate return type isn't necessarily a matching type; in |
| 396 | // fact, it's quite likely to be 'id'. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 397 | llvm::Type *selfTy = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 398 | cast<llvm::PointerType>(selfAddr->getType())->getElementType(); |
| 399 | newSelf = Builder.CreateBitCast(newSelf, selfTy); |
| 400 | |
| 401 | Builder.CreateStore(newSelf, selfAddr); |
| 402 | } |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 403 | |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 404 | return AdjustRelatedResultType(*this, E->getType(), method, result); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 405 | } |
| 406 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 407 | namespace { |
| 408 | struct FinishARCDealloc : EHScopeStack::Cleanup { |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 409 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 410 | const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); |
John McCall | 799d34e | 2011-07-13 18:26:47 +0000 | [diff] [blame] | 411 | |
| 412 | const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 413 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 414 | if (!iface->getSuperClass()) return; |
| 415 | |
John McCall | 799d34e | 2011-07-13 18:26:47 +0000 | [diff] [blame] | 416 | bool isCategory = isa<ObjCCategoryImplDecl>(impl); |
| 417 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 418 | // Call [super dealloc] if we have a superclass. |
| 419 | llvm::Value *self = CGF.LoadObjCSelf(); |
| 420 | |
| 421 | CallArgList args; |
| 422 | CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), |
| 423 | CGF.getContext().VoidTy, |
| 424 | method->getSelector(), |
| 425 | iface, |
John McCall | 799d34e | 2011-07-13 18:26:47 +0000 | [diff] [blame] | 426 | isCategory, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 427 | self, |
| 428 | /*is class msg*/ false, |
| 429 | args, |
| 430 | method); |
| 431 | } |
| 432 | }; |
| 433 | } |
| 434 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 435 | /// StartObjCMethod - Begin emission of an ObjCMethod. This generates |
| 436 | /// the LLVM function and sets the other context used by |
| 437 | /// CodeGenFunction. |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 438 | void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 439 | const ObjCContainerDecl *CD, |
| 440 | SourceLocation StartLoc) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 441 | FunctionArgList args; |
Devang Patel | 4800ea6 | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 442 | // Check if we should generate debug info for this method. |
Alexey Samsonov | a240df2 | 2012-10-16 07:22:28 +0000 | [diff] [blame] | 443 | if (!OMD->hasAttr<NoDebugAttr>()) |
| 444 | maybeInitializeDebugInfo(); |
Devang Patel | 4800ea6 | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 445 | |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 446 | llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 447 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 448 | const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD); |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 449 | CGM.SetInternalFunctionAttributes(OMD, Fn, FI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 450 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 451 | args.push_back(OMD->getSelfDecl()); |
| 452 | args.push_back(OMD->getCmdDecl()); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 453 | |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 454 | for (ObjCMethodDecl::param_const_iterator PI = OMD->param_begin(), |
Eric Christopher | 16098f3 | 2012-03-29 17:31:31 +0000 | [diff] [blame] | 455 | E = OMD->param_end(); PI != E; ++PI) |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 456 | args.push_back(*PI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 457 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 458 | CurGD = OMD; |
| 459 | |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 460 | StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 461 | |
| 462 | // In ARC, certain methods get an extra cleanup. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 463 | if (CGM.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 464 | OMD->isInstanceMethod() && |
| 465 | OMD->getSelector().isUnarySelector()) { |
| 466 | const IdentifierInfo *ident = |
| 467 | OMD->getSelector().getIdentifierInfoForSlot(0); |
| 468 | if (ident->isStr("dealloc")) |
| 469 | EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); |
| 470 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 471 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 472 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 473 | static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
| 474 | LValue lvalue, QualType type); |
| 475 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 476 | /// 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] | 477 | /// its pointer, name, and types registered in the class struture. |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 478 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 479 | StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart()); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 480 | EmitStmt(OMD->getBody()); |
| 481 | FinishFunction(OMD->getBodyRBrace()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 482 | } |
| 483 | |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 484 | /// emitStructGetterCall - Call the runtime function to load a property |
| 485 | /// into the return value slot. |
| 486 | static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, |
| 487 | bool isAtomic, bool hasStrong) { |
| 488 | ASTContext &Context = CGF.getContext(); |
| 489 | |
| 490 | llvm::Value *src = |
| 491 | CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), |
| 492 | ivar, 0).getAddress(); |
| 493 | |
| 494 | // objc_copyStruct (ReturnValue, &structIvar, |
| 495 | // sizeof (Type of Ivar), isAtomic, false); |
| 496 | CallArgList args; |
| 497 | |
| 498 | llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy); |
| 499 | args.add(RValue::get(dest), Context.VoidPtrTy); |
| 500 | |
| 501 | src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy); |
| 502 | args.add(RValue::get(src), Context.VoidPtrTy); |
| 503 | |
| 504 | CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType()); |
| 505 | args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType()); |
| 506 | args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy); |
| 507 | args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy); |
| 508 | |
| 509 | llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction(); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 510 | CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Context.VoidTy, args, |
| 511 | FunctionType::ExtInfo(), |
| 512 | RequiredArgs::All), |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 513 | fn, ReturnValueSlot(), args); |
| 514 | } |
| 515 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 516 | /// Determine whether the given architecture supports unaligned atomic |
| 517 | /// accesses. They don't have to be fast, just faster than a function |
| 518 | /// call and a mutex. |
| 519 | static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) { |
Eli Friedman | de24d44 | 2011-09-13 20:48:30 +0000 | [diff] [blame] | 520 | // FIXME: Allow unaligned atomic load/store on x86. (It is not |
| 521 | // currently supported by the backend.) |
| 522 | return 0; |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /// Return the maximum size that permits atomic accesses for the given |
| 526 | /// architecture. |
| 527 | static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, |
| 528 | llvm::Triple::ArchType arch) { |
| 529 | // ARM has 8-byte atomic accesses, but it's not clear whether we |
| 530 | // want to rely on them here. |
| 531 | |
| 532 | // In the default case, just assume that any size up to a pointer is |
| 533 | // fine given adequate alignment. |
| 534 | return CharUnits::fromQuantity(CGM.PointerSizeInBytes); |
| 535 | } |
| 536 | |
| 537 | namespace { |
| 538 | class PropertyImplStrategy { |
| 539 | public: |
| 540 | enum StrategyKind { |
| 541 | /// The 'native' strategy is to use the architecture's provided |
| 542 | /// reads and writes. |
| 543 | Native, |
| 544 | |
| 545 | /// Use objc_setProperty and objc_getProperty. |
| 546 | GetSetProperty, |
| 547 | |
| 548 | /// Use objc_setProperty for the setter, but use expression |
| 549 | /// evaluation for the getter. |
| 550 | SetPropertyAndExpressionGet, |
| 551 | |
| 552 | /// Use objc_copyStruct. |
| 553 | CopyStruct, |
| 554 | |
| 555 | /// The 'expression' strategy is to emit normal assignment or |
| 556 | /// lvalue-to-rvalue expressions. |
| 557 | Expression |
| 558 | }; |
| 559 | |
| 560 | StrategyKind getKind() const { return StrategyKind(Kind); } |
| 561 | |
| 562 | bool hasStrongMember() const { return HasStrong; } |
| 563 | bool isAtomic() const { return IsAtomic; } |
| 564 | bool isCopy() const { return IsCopy; } |
| 565 | |
| 566 | CharUnits getIvarSize() const { return IvarSize; } |
| 567 | CharUnits getIvarAlignment() const { return IvarAlignment; } |
| 568 | |
| 569 | PropertyImplStrategy(CodeGenModule &CGM, |
| 570 | const ObjCPropertyImplDecl *propImpl); |
| 571 | |
| 572 | private: |
| 573 | unsigned Kind : 8; |
| 574 | unsigned IsAtomic : 1; |
| 575 | unsigned IsCopy : 1; |
| 576 | unsigned HasStrong : 1; |
| 577 | |
| 578 | CharUnits IvarSize; |
| 579 | CharUnits IvarAlignment; |
| 580 | }; |
| 581 | } |
| 582 | |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 583 | /// Pick an implementation strategy for the given property synthesis. |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 584 | PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, |
| 585 | const ObjCPropertyImplDecl *propImpl) { |
| 586 | const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 587 | ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind(); |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 588 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 589 | IsCopy = (setterKind == ObjCPropertyDecl::Copy); |
| 590 | IsAtomic = prop->isAtomic(); |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 591 | HasStrong = false; // doesn't matter here. |
| 592 | |
| 593 | // Evaluate the ivar's size and alignment. |
| 594 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); |
| 595 | QualType ivarType = ivar->getType(); |
| 596 | llvm::tie(IvarSize, IvarAlignment) |
| 597 | = CGM.getContext().getTypeInfoInChars(ivarType); |
| 598 | |
| 599 | // If we have a copy property, we always have to use getProperty/setProperty. |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 600 | // TODO: we could actually use setProperty and an expression for non-atomics. |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 601 | if (IsCopy) { |
| 602 | Kind = GetSetProperty; |
| 603 | return; |
| 604 | } |
| 605 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 606 | // Handle retain. |
| 607 | if (setterKind == ObjCPropertyDecl::Retain) { |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 608 | // In GC-only, there's nothing special that needs to be done. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 609 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 610 | // fallthrough |
| 611 | |
| 612 | // In ARC, if the property is non-atomic, use expression emission, |
| 613 | // which translates to objc_storeStrong. This isn't required, but |
| 614 | // it's slightly nicer. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 615 | } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) { |
John McCall | d64c2eb | 2012-08-20 23:36:59 +0000 | [diff] [blame] | 616 | // Using standard expression emission for the setter is only |
| 617 | // acceptable if the ivar is __strong, which won't be true if |
| 618 | // the property is annotated with __attribute__((NSObject)). |
| 619 | // TODO: falling all the way back to objc_setProperty here is |
| 620 | // just laziness, though; we could still use objc_storeStrong |
| 621 | // if we hacked it right. |
| 622 | if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong) |
| 623 | Kind = Expression; |
| 624 | else |
| 625 | Kind = SetPropertyAndExpressionGet; |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 626 | return; |
| 627 | |
| 628 | // Otherwise, we need to at least use setProperty. However, if |
| 629 | // the property isn't atomic, we can use normal expression |
| 630 | // emission for the getter. |
| 631 | } else if (!IsAtomic) { |
| 632 | Kind = SetPropertyAndExpressionGet; |
| 633 | return; |
| 634 | |
| 635 | // Otherwise, we have to use both setProperty and getProperty. |
| 636 | } else { |
| 637 | Kind = GetSetProperty; |
| 638 | return; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | // If we're not atomic, just use expression accesses. |
| 643 | if (!IsAtomic) { |
| 644 | Kind = Expression; |
| 645 | return; |
| 646 | } |
| 647 | |
John McCall | 5889c60 | 2011-09-13 05:36:29 +0000 | [diff] [blame] | 648 | // Properties on bitfield ivars need to be emitted using expression |
| 649 | // accesses even if they're nominally atomic. |
| 650 | if (ivar->isBitField()) { |
| 651 | Kind = Expression; |
| 652 | return; |
| 653 | } |
| 654 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 655 | // GC-qualified or ARC-qualified ivars need to be emitted as |
| 656 | // expressions. This actually works out to being atomic anyway, |
| 657 | // except for ARC __strong, but that should trigger the above code. |
| 658 | if (ivarType.hasNonTrivialObjCLifetime() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 659 | (CGM.getLangOpts().getGC() && |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 660 | CGM.getContext().getObjCGCAttrKind(ivarType))) { |
| 661 | Kind = Expression; |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | // Compute whether the ivar has strong members. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 666 | if (CGM.getLangOpts().getGC()) |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 667 | if (const RecordType *recordType = ivarType->getAs<RecordType>()) |
| 668 | HasStrong = recordType->getDecl()->hasObjectMember(); |
| 669 | |
| 670 | // We can never access structs with object members with a native |
| 671 | // access, because we need to use write barriers. This is what |
| 672 | // objc_copyStruct is for. |
| 673 | if (HasStrong) { |
| 674 | Kind = CopyStruct; |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | // Otherwise, this is target-dependent and based on the size and |
| 679 | // alignment of the ivar. |
John McCall | c5d9a90 | 2011-09-13 07:33:34 +0000 | [diff] [blame] | 680 | |
| 681 | // If the size of the ivar is not a power of two, give up. We don't |
| 682 | // want to get into the business of doing compare-and-swaps. |
| 683 | if (!IvarSize.isPowerOfTwo()) { |
| 684 | Kind = CopyStruct; |
| 685 | return; |
| 686 | } |
| 687 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 688 | llvm::Triple::ArchType arch = |
| 689 | CGM.getContext().getTargetInfo().getTriple().getArch(); |
| 690 | |
| 691 | // Most architectures require memory to fit within a single cache |
| 692 | // line, so the alignment has to be at least the size of the access. |
| 693 | // Otherwise we have to grab a lock. |
| 694 | if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) { |
| 695 | Kind = CopyStruct; |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | // If the ivar's size exceeds the architecture's maximum atomic |
| 700 | // access size, we have to use CopyStruct. |
| 701 | if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) { |
| 702 | Kind = CopyStruct; |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | // Otherwise, we can use native loads and stores. |
| 707 | Kind = Native; |
| 708 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 709 | |
James Dennett | 2ee5ba3 | 2012-06-15 22:10:14 +0000 | [diff] [blame] | 710 | /// \brief Generate an Objective-C property getter function. |
| 711 | /// |
| 712 | /// The given Decl must be an ObjCImplementationDecl. \@synthesize |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 713 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 714 | void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, |
| 715 | const ObjCPropertyImplDecl *PID) { |
Fariborz Jahanian | b6e5fe3 | 2012-01-07 18:56:22 +0000 | [diff] [blame] | 716 | llvm::Constant *AtomicHelperFn = |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 717 | GenerateObjCAtomicGetterCopyHelperFunction(PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 718 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 719 | ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); |
| 720 | assert(OMD && "Invalid call to generate getter (empty method)"); |
Eric Christopher | ea32047 | 2012-04-03 00:44:15 +0000 | [diff] [blame] | 721 | StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 723 | generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn); |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 724 | |
| 725 | FinishFunction(); |
| 726 | } |
| 727 | |
John McCall | 6c11f0b | 2011-09-13 06:00:03 +0000 | [diff] [blame] | 728 | static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) { |
| 729 | const Expr *getter = propImpl->getGetterCXXConstructor(); |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 730 | if (!getter) return true; |
| 731 | |
| 732 | // Sema only makes only of these when the ivar has a C++ class type, |
| 733 | // so the form is pretty constrained. |
| 734 | |
John McCall | 6c11f0b | 2011-09-13 06:00:03 +0000 | [diff] [blame] | 735 | // If the property has a reference type, we might just be binding a |
| 736 | // reference, in which case the result will be a gl-value. We should |
| 737 | // treat this as a non-trivial operation. |
| 738 | if (getter->isGLValue()) |
| 739 | return false; |
| 740 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 741 | // If we selected a trivial copy-constructor, we're okay. |
| 742 | if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter)) |
| 743 | return (construct->getConstructor()->isTrivial()); |
| 744 | |
| 745 | // The constructor might require cleanups (in which case it's never |
| 746 | // trivial). |
| 747 | assert(isa<ExprWithCleanups>(getter)); |
| 748 | return false; |
| 749 | } |
| 750 | |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 751 | /// emitCPPObjectAtomicGetterCall - Call the runtime function to |
| 752 | /// copy the ivar into the resturn slot. |
| 753 | static void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, |
| 754 | llvm::Value *returnAddr, |
| 755 | ObjCIvarDecl *ivar, |
| 756 | llvm::Constant *AtomicHelperFn) { |
| 757 | // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar, |
| 758 | // AtomicHelperFn); |
| 759 | CallArgList args; |
| 760 | |
| 761 | // The 1st argument is the return Slot. |
| 762 | args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy); |
| 763 | |
| 764 | // The 2nd argument is the address of the ivar. |
| 765 | llvm::Value *ivarAddr = |
| 766 | CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), |
| 767 | CGF.LoadObjCSelf(), ivar, 0).getAddress(); |
| 768 | ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); |
| 769 | args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); |
| 770 | |
| 771 | // Third argument is the helper function. |
| 772 | args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); |
| 773 | |
| 774 | llvm::Value *copyCppAtomicObjectFn = |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 775 | CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction(); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 776 | CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, |
| 777 | args, |
| 778 | FunctionType::ExtInfo(), |
| 779 | RequiredArgs::All), |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 780 | copyCppAtomicObjectFn, ReturnValueSlot(), args); |
| 781 | } |
| 782 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 783 | void |
| 784 | CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, |
Fariborz Jahanian | b6e5fe3 | 2012-01-07 18:56:22 +0000 | [diff] [blame] | 785 | const ObjCPropertyImplDecl *propImpl, |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 786 | const ObjCMethodDecl *GetterMethodDecl, |
Fariborz Jahanian | b6e5fe3 | 2012-01-07 18:56:22 +0000 | [diff] [blame] | 787 | llvm::Constant *AtomicHelperFn) { |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 788 | // If there's a non-trivial 'get' expression, we just have to emit that. |
| 789 | if (!hasTrivialGetExpr(propImpl)) { |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 790 | if (!AtomicHelperFn) { |
| 791 | ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(), |
| 792 | /*nrvo*/ 0); |
| 793 | EmitReturnStmt(ret); |
| 794 | } |
| 795 | else { |
| 796 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); |
| 797 | emitCPPObjectAtomicGetterCall(*this, ReturnValue, |
| 798 | ivar, AtomicHelperFn); |
| 799 | } |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 800 | return; |
| 801 | } |
| 802 | |
| 803 | const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); |
| 804 | QualType propType = prop->getType(); |
| 805 | ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl(); |
| 806 | |
| 807 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); |
| 808 | |
| 809 | // Pick an implementation strategy. |
| 810 | PropertyImplStrategy strategy(CGM, propImpl); |
| 811 | switch (strategy.getKind()) { |
| 812 | case PropertyImplStrategy::Native: { |
Eli Friedman | aa01466 | 2012-10-26 22:38:05 +0000 | [diff] [blame] | 813 | // We don't need to do anything for a zero-size struct. |
| 814 | if (strategy.getIvarSize().isZero()) |
| 815 | return; |
| 816 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 817 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); |
| 818 | |
| 819 | // Currently, all atomic accesses have to be through integer |
| 820 | // types, so there's no point in trying to pick a prettier type. |
| 821 | llvm::Type *bitcastType = |
| 822 | llvm::Type::getIntNTy(getLLVMContext(), |
| 823 | getContext().toBits(strategy.getIvarSize())); |
| 824 | bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay |
| 825 | |
| 826 | // Perform an atomic load. This does not impose ordering constraints. |
| 827 | llvm::Value *ivarAddr = LV.getAddress(); |
| 828 | ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); |
| 829 | llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); |
| 830 | load->setAlignment(strategy.getIvarAlignment().getQuantity()); |
| 831 | load->setAtomic(llvm::Unordered); |
| 832 | |
| 833 | // Store that value into the return address. Doing this with a |
| 834 | // bitcast is likely to produce some pretty ugly IR, but it's not |
| 835 | // the *most* terrible thing in the world. |
| 836 | Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType)); |
| 837 | |
| 838 | // Make sure we don't do an autorelease. |
| 839 | AutoreleaseResult = false; |
| 840 | return; |
| 841 | } |
| 842 | |
| 843 | case PropertyImplStrategy::GetSetProperty: { |
| 844 | llvm::Value *getPropertyFn = |
| 845 | CGM.getObjCRuntime().GetPropertyGetFunction(); |
| 846 | if (!getPropertyFn) { |
| 847 | CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy"); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 848 | return; |
| 849 | } |
| 850 | |
| 851 | // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). |
| 852 | // FIXME: Can't this be simpler? This might even be worse than the |
| 853 | // corresponding gcc code. |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 854 | llvm::Value *cmd = |
| 855 | Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd"); |
| 856 | llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); |
| 857 | llvm::Value *ivarOffset = |
| 858 | EmitIvarOffset(classImpl->getClassInterface(), ivar); |
| 859 | |
| 860 | CallArgList args; |
| 861 | args.add(RValue::get(self), getContext().getObjCIdType()); |
| 862 | args.add(RValue::get(cmd), getContext().getObjCSelType()); |
| 863 | args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 864 | args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), |
| 865 | getContext().BoolTy); |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 866 | |
Daniel Dunbar | e4be5a6 | 2009-02-03 23:43:59 +0000 | [diff] [blame] | 867 | // FIXME: We shouldn't need to get the function info here, the |
| 868 | // runtime already should have computed it to build the function. |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 869 | RValue RV = EmitCall(getTypes().arrangeFreeFunctionCall(propType, args, |
| 870 | FunctionType::ExtInfo(), |
| 871 | RequiredArgs::All), |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 872 | getPropertyFn, ReturnValueSlot(), args); |
| 873 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 874 | // We need to fix the type here. Ivars with copy & retain are |
| 875 | // always objects so we don't need to worry about complex or |
| 876 | // aggregates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
Fariborz Jahanian | 52c18b0 | 2012-04-26 21:33:14 +0000 | [diff] [blame] | 878 | getTypes().ConvertType(getterMethod->getResultType()))); |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 879 | |
| 880 | EmitReturnOfRValue(RV, propType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 881 | |
| 882 | // objc_getProperty does an autorelease, so we should suppress ours. |
| 883 | AutoreleaseResult = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 884 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 885 | return; |
| 886 | } |
| 887 | |
| 888 | case PropertyImplStrategy::CopyStruct: |
| 889 | emitStructGetterCall(*this, ivar, strategy.isAtomic(), |
| 890 | strategy.hasStrongMember()); |
| 891 | return; |
| 892 | |
| 893 | case PropertyImplStrategy::Expression: |
| 894 | case PropertyImplStrategy::SetPropertyAndExpressionGet: { |
| 895 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); |
| 896 | |
| 897 | QualType ivarType = ivar->getType(); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 898 | switch (getEvaluationKind(ivarType)) { |
| 899 | case TEK_Complex: { |
| 900 | ComplexPairTy pair = EmitLoadOfComplex(LV); |
| 901 | EmitStoreOfComplex(pair, |
| 902 | MakeNaturalAlignAddrLValue(ReturnValue, ivarType), |
| 903 | /*init*/ true); |
| 904 | return; |
| 905 | } |
| 906 | case TEK_Aggregate: |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 907 | // The return value slot is guaranteed to not be aliased, but |
| 908 | // that's not necessarily the same as "on the stack", so |
| 909 | // we still potentially need objc_memmove_collectable. |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 910 | EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 911 | return; |
| 912 | case TEK_Scalar: { |
John McCall | ba3dd90 | 2011-07-22 05:23:13 +0000 | [diff] [blame] | 913 | llvm::Value *value; |
| 914 | if (propType->isReferenceType()) { |
| 915 | value = LV.getAddress(); |
| 916 | } else { |
| 917 | // We want to load and autoreleaseReturnValue ARC __weak ivars. |
| 918 | if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 919 | value = emitARCRetainLoadOfScalar(*this, LV, ivarType); |
John McCall | ba3dd90 | 2011-07-22 05:23:13 +0000 | [diff] [blame] | 920 | |
| 921 | // Otherwise we want to do a simple load, suppressing the |
| 922 | // final autorelease. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 923 | } else { |
John McCall | ba3dd90 | 2011-07-22 05:23:13 +0000 | [diff] [blame] | 924 | value = EmitLoadOfLValue(LV).getScalarVal(); |
| 925 | AutoreleaseResult = false; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 926 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 927 | |
John McCall | ba3dd90 | 2011-07-22 05:23:13 +0000 | [diff] [blame] | 928 | value = Builder.CreateBitCast(value, ConvertType(propType)); |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 929 | value = Builder.CreateBitCast(value, |
| 930 | ConvertType(GetterMethodDecl->getResultType())); |
John McCall | ba3dd90 | 2011-07-22 05:23:13 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | EmitReturnOfRValue(RValue::get(value), propType); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 934 | return; |
Fariborz Jahanian | ed1d29d | 2009-03-03 18:49:40 +0000 | [diff] [blame] | 935 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 936 | } |
| 937 | llvm_unreachable("bad evaluation kind"); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 938 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 939 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 940 | } |
| 941 | llvm_unreachable("bad @property implementation strategy!"); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 942 | } |
| 943 | |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 944 | /// emitStructSetterCall - Call the runtime function to store the value |
| 945 | /// from the first formal parameter into the given ivar. |
| 946 | static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, |
| 947 | ObjCIvarDecl *ivar) { |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 948 | // objc_copyStruct (&structIvar, &Arg, |
| 949 | // sizeof (struct something), true, false); |
John McCall | bbb253c | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 950 | CallArgList args; |
| 951 | |
| 952 | // The first argument is the address of the ivar. |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 953 | llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), |
| 954 | CGF.LoadObjCSelf(), ivar, 0) |
| 955 | .getAddress(); |
| 956 | ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); |
| 957 | args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); |
John McCall | bbb253c | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 958 | |
| 959 | // The second argument is the address of the parameter variable. |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 960 | ParmVarDecl *argVar = *OMD->param_begin(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 961 | DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), |
Fariborz Jahanian | c3953aa | 2012-01-05 00:10:16 +0000 | [diff] [blame] | 962 | VK_LValue, SourceLocation()); |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 963 | llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); |
| 964 | argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); |
| 965 | args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); |
John McCall | bbb253c | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 966 | |
| 967 | // The third argument is the sizeof the type. |
| 968 | llvm::Value *size = |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 969 | CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); |
| 970 | args.add(RValue::get(size), CGF.getContext().getSizeType()); |
John McCall | bbb253c | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 971 | |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 972 | // The fourth argument is the 'isAtomic' flag. |
| 973 | args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); |
John McCall | bbb253c | 2011-09-10 09:30:49 +0000 | [diff] [blame] | 974 | |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 975 | // The fifth argument is the 'hasStrong' flag. |
| 976 | // FIXME: should this really always be false? |
| 977 | args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); |
| 978 | |
| 979 | llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 980 | CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, |
| 981 | args, |
| 982 | FunctionType::ExtInfo(), |
| 983 | RequiredArgs::All), |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 984 | copyStructFn, ReturnValueSlot(), args); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Fariborz Jahanian | cd93b96 | 2012-01-06 22:33:54 +0000 | [diff] [blame] | 987 | /// emitCPPObjectAtomicSetterCall - Call the runtime function to store |
| 988 | /// the value from the first formal parameter into the given ivar, using |
| 989 | /// the Cpp API for atomic Cpp objects with non-trivial copy assignment. |
| 990 | static void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, |
| 991 | ObjCMethodDecl *OMD, |
| 992 | ObjCIvarDecl *ivar, |
| 993 | llvm::Constant *AtomicHelperFn) { |
| 994 | // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg, |
| 995 | // AtomicHelperFn); |
| 996 | CallArgList args; |
| 997 | |
| 998 | // The first argument is the address of the ivar. |
| 999 | llvm::Value *ivarAddr = |
| 1000 | CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), |
| 1001 | CGF.LoadObjCSelf(), ivar, 0).getAddress(); |
| 1002 | ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); |
| 1003 | args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); |
| 1004 | |
| 1005 | // The second argument is the address of the parameter variable. |
| 1006 | ParmVarDecl *argVar = *OMD->param_begin(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1007 | DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), |
Fariborz Jahanian | cd93b96 | 2012-01-06 22:33:54 +0000 | [diff] [blame] | 1008 | VK_LValue, SourceLocation()); |
| 1009 | llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); |
| 1010 | argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); |
| 1011 | args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); |
| 1012 | |
| 1013 | // Third argument is the helper function. |
| 1014 | args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); |
| 1015 | |
| 1016 | llvm::Value *copyCppAtomicObjectFn = |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 1017 | CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction(); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1018 | CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, |
| 1019 | args, |
| 1020 | FunctionType::ExtInfo(), |
| 1021 | RequiredArgs::All), |
Fariborz Jahanian | cd93b96 | 2012-01-06 22:33:54 +0000 | [diff] [blame] | 1022 | copyCppAtomicObjectFn, ReturnValueSlot(), args); |
| 1023 | |
| 1024 | |
| 1025 | } |
| 1026 | |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1027 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1028 | static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { |
| 1029 | Expr *setter = PID->getSetterCXXAssignment(); |
| 1030 | if (!setter) return true; |
| 1031 | |
| 1032 | // Sema only makes only of these when the ivar has a C++ class type, |
| 1033 | // so the form is pretty constrained. |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1034 | |
| 1035 | // An operator call is trivial if the function it calls is trivial. |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1036 | // This also implies that there's nothing non-trivial going on with |
| 1037 | // the arguments, because operator= can only be trivial if it's a |
| 1038 | // synthesized assignment operator and therefore both parameters are |
| 1039 | // references. |
| 1040 | if (CallExpr *call = dyn_cast<CallExpr>(setter)) { |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1041 | if (const FunctionDecl *callee |
| 1042 | = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) |
| 1043 | if (callee->isTrivial()) |
| 1044 | return true; |
| 1045 | return false; |
Fariborz Jahanian | 01cb307 | 2011-04-06 16:05:26 +0000 | [diff] [blame] | 1046 | } |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1047 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1048 | assert(isa<ExprWithCleanups>(setter)); |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1049 | return false; |
| 1050 | } |
| 1051 | |
Benjamin Kramer | 4e494cf | 2012-03-10 20:38:56 +0000 | [diff] [blame] | 1052 | static bool UseOptimizedSetter(CodeGenModule &CGM) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1053 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1054 | return false; |
Daniel Dunbar | 7a0c064 | 2012-10-15 22:23:53 +0000 | [diff] [blame] | 1055 | return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1058 | void |
| 1059 | CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, |
Fariborz Jahanian | cd93b96 | 2012-01-06 22:33:54 +0000 | [diff] [blame] | 1060 | const ObjCPropertyImplDecl *propImpl, |
| 1061 | llvm::Constant *AtomicHelperFn) { |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1062 | const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 1063 | ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1064 | ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl(); |
Fariborz Jahanian | cd93b96 | 2012-01-06 22:33:54 +0000 | [diff] [blame] | 1065 | |
| 1066 | // Just use the setter expression if Sema gave us one and it's |
| 1067 | // non-trivial. |
| 1068 | if (!hasTrivialSetExpr(propImpl)) { |
| 1069 | if (!AtomicHelperFn) |
| 1070 | // If non-atomic, assignment is called directly. |
| 1071 | EmitStmt(propImpl->getSetterCXXAssignment()); |
| 1072 | else |
| 1073 | // If atomic, assignment is called via a locking api. |
| 1074 | emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, |
| 1075 | AtomicHelperFn); |
| 1076 | return; |
| 1077 | } |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1078 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1079 | PropertyImplStrategy strategy(CGM, propImpl); |
| 1080 | switch (strategy.getKind()) { |
| 1081 | case PropertyImplStrategy::Native: { |
Eli Friedman | aa01466 | 2012-10-26 22:38:05 +0000 | [diff] [blame] | 1082 | // We don't need to do anything for a zero-size struct. |
| 1083 | if (strategy.getIvarSize().isZero()) |
| 1084 | return; |
| 1085 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1086 | llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()]; |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1087 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1088 | LValue ivarLValue = |
| 1089 | EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0); |
| 1090 | llvm::Value *ivarAddr = ivarLValue.getAddress(); |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1091 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1092 | // Currently, all atomic accesses have to be through integer |
| 1093 | // types, so there's no point in trying to pick a prettier type. |
| 1094 | llvm::Type *bitcastType = |
| 1095 | llvm::Type::getIntNTy(getLLVMContext(), |
| 1096 | getContext().toBits(strategy.getIvarSize())); |
| 1097 | bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay |
| 1098 | |
| 1099 | // Cast both arguments to the chosen operation type. |
| 1100 | argAddr = Builder.CreateBitCast(argAddr, bitcastType); |
| 1101 | ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); |
| 1102 | |
| 1103 | // This bitcast load is likely to cause some nasty IR. |
| 1104 | llvm::Value *load = Builder.CreateLoad(argAddr); |
| 1105 | |
| 1106 | // Perform an atomic store. There are no memory ordering requirements. |
| 1107 | llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); |
| 1108 | store->setAlignment(strategy.getIvarAlignment().getQuantity()); |
| 1109 | store->setAtomic(llvm::Unordered); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | case PropertyImplStrategy::GetSetProperty: |
| 1114 | case PropertyImplStrategy::SetPropertyAndExpressionGet: { |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1115 | |
| 1116 | llvm::Value *setOptimizedPropertyFn = 0; |
| 1117 | llvm::Value *setPropertyFn = 0; |
| 1118 | if (UseOptimizedSetter(CGM)) { |
Daniel Dunbar | 7a0c064 | 2012-10-15 22:23:53 +0000 | [diff] [blame] | 1119 | // 10.8 and iOS 6.0 code and GC is off |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1120 | setOptimizedPropertyFn = |
Eric Christopher | 16098f3 | 2012-03-29 17:31:31 +0000 | [diff] [blame] | 1121 | CGM.getObjCRuntime() |
| 1122 | .GetOptimizedPropertySetFunction(strategy.isAtomic(), |
| 1123 | strategy.isCopy()); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1124 | if (!setOptimizedPropertyFn) { |
| 1125 | CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI"); |
| 1126 | return; |
| 1127 | } |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1128 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1129 | else { |
| 1130 | setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction(); |
| 1131 | if (!setPropertyFn) { |
| 1132 | CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); |
| 1133 | return; |
| 1134 | } |
| 1135 | } |
| 1136 | |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1137 | // Emit objc_setProperty((id) self, _cmd, offset, arg, |
| 1138 | // <is-atomic>, <is-copy>). |
| 1139 | llvm::Value *cmd = |
| 1140 | Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]); |
| 1141 | llvm::Value *self = |
| 1142 | Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); |
| 1143 | llvm::Value *ivarOffset = |
| 1144 | EmitIvarOffset(classImpl->getClassInterface(), ivar); |
| 1145 | llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()]; |
| 1146 | arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy); |
| 1147 | |
| 1148 | CallArgList args; |
| 1149 | args.add(RValue::get(self), getContext().getObjCIdType()); |
| 1150 | args.add(RValue::get(cmd), getContext().getObjCSelType()); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1151 | if (setOptimizedPropertyFn) { |
| 1152 | args.add(RValue::get(arg), getContext().getObjCIdType()); |
| 1153 | args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1154 | EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, |
| 1155 | FunctionType::ExtInfo(), |
| 1156 | RequiredArgs::All), |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1157 | setOptimizedPropertyFn, ReturnValueSlot(), args); |
| 1158 | } else { |
| 1159 | args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); |
| 1160 | args.add(RValue::get(arg), getContext().getObjCIdType()); |
| 1161 | args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), |
| 1162 | getContext().BoolTy); |
| 1163 | args.add(RValue::get(Builder.getInt1(strategy.isCopy())), |
| 1164 | getContext().BoolTy); |
| 1165 | // FIXME: We shouldn't need to get the function info here, the runtime |
| 1166 | // already should have computed it to build the function. |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1167 | EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, |
| 1168 | FunctionType::ExtInfo(), |
| 1169 | RequiredArgs::All), |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1170 | setPropertyFn, ReturnValueSlot(), args); |
| 1171 | } |
| 1172 | |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1173 | return; |
| 1174 | } |
| 1175 | |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1176 | case PropertyImplStrategy::CopyStruct: |
John McCall | 41bdde9 | 2011-09-12 23:06:44 +0000 | [diff] [blame] | 1177 | emitStructSetterCall(*this, setterMethod, ivar); |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1178 | return; |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1179 | |
| 1180 | case PropertyImplStrategy::Expression: |
| 1181 | break; |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | // Otherwise, fake up some ASTs and emit a normal assignment. |
| 1185 | ValueDecl *selfDecl = setterMethod->getSelfDecl(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1186 | DeclRefExpr self(selfDecl, false, selfDecl->getType(), |
| 1187 | VK_LValue, SourceLocation()); |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1188 | ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, |
| 1189 | selfDecl->getType(), CK_LValueToRValue, &self, |
| 1190 | VK_RValue); |
| 1191 | ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), |
Fariborz Jahanian | 0c70181 | 2013-04-02 18:57:54 +0000 | [diff] [blame] | 1192 | SourceLocation(), SourceLocation(), |
| 1193 | &selfLoad, true, true); |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1194 | |
| 1195 | ParmVarDecl *argDecl = *setterMethod->param_begin(); |
| 1196 | QualType argType = argDecl->getType().getNonReferenceType(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1197 | DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation()); |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1198 | ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, |
| 1199 | argType.getUnqualifiedType(), CK_LValueToRValue, |
| 1200 | &arg, VK_RValue); |
| 1201 | |
| 1202 | // The property type can differ from the ivar type in some situations with |
| 1203 | // Objective-C pointer types, we can always bit cast the RHS in these cases. |
| 1204 | // The following absurdity is just to ensure well-formed IR. |
| 1205 | CastKind argCK = CK_NoOp; |
| 1206 | if (ivarRef.getType()->isObjCObjectPointerType()) { |
| 1207 | if (argLoad.getType()->isObjCObjectPointerType()) |
| 1208 | argCK = CK_BitCast; |
| 1209 | else if (argLoad.getType()->isBlockPointerType()) |
| 1210 | argCK = CK_BlockPointerToObjCPointerCast; |
| 1211 | else |
| 1212 | argCK = CK_CPointerToObjCPointerCast; |
| 1213 | } else if (ivarRef.getType()->isBlockPointerType()) { |
| 1214 | if (argLoad.getType()->isBlockPointerType()) |
| 1215 | argCK = CK_BitCast; |
| 1216 | else |
| 1217 | argCK = CK_AnyPointerToBlockPointerCast; |
| 1218 | } else if (ivarRef.getType()->isPointerType()) { |
| 1219 | argCK = CK_BitCast; |
| 1220 | } |
| 1221 | ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, |
| 1222 | ivarRef.getType(), argCK, &argLoad, |
| 1223 | VK_RValue); |
| 1224 | Expr *finalArg = &argLoad; |
| 1225 | if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), |
| 1226 | argLoad.getType())) |
| 1227 | finalArg = &argCast; |
| 1228 | |
| 1229 | |
| 1230 | BinaryOperator assign(&ivarRef, finalArg, BO_Assign, |
| 1231 | ivarRef.getType(), VK_RValue, OK_Ordinary, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 1232 | SourceLocation(), false); |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1233 | EmitStmt(&assign); |
Fariborz Jahanian | 01cb307 | 2011-04-06 16:05:26 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
James Dennett | 2ee5ba3 | 2012-06-15 22:10:14 +0000 | [diff] [blame] | 1236 | /// \brief Generate an Objective-C property setter function. |
| 1237 | /// |
| 1238 | /// The given Decl must be an ObjCImplementationDecl. \@synthesize |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 1239 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1240 | void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, |
| 1241 | const ObjCPropertyImplDecl *PID) { |
Fariborz Jahanian | b6e5fe3 | 2012-01-07 18:56:22 +0000 | [diff] [blame] | 1242 | llvm::Constant *AtomicHelperFn = |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1243 | GenerateObjCAtomicSetterCopyHelperFunction(PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1244 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1245 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); |
| 1246 | assert(OMD && "Invalid call to generate setter (empty method)"); |
Eric Christopher | ea32047 | 2012-04-03 00:44:15 +0000 | [diff] [blame] | 1247 | StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 1248 | |
Fariborz Jahanian | cd93b96 | 2012-01-06 22:33:54 +0000 | [diff] [blame] | 1249 | generateObjCSetterBody(IMP, PID, AtomicHelperFn); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1250 | |
| 1251 | FinishFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1254 | namespace { |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1255 | struct DestroyIvar : EHScopeStack::Cleanup { |
| 1256 | private: |
| 1257 | llvm::Value *addr; |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1258 | const ObjCIvarDecl *ivar; |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1259 | CodeGenFunction::Destroyer *destroyer; |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1260 | bool useEHCleanupForArray; |
| 1261 | public: |
| 1262 | DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, |
| 1263 | CodeGenFunction::Destroyer *destroyer, |
| 1264 | bool useEHCleanupForArray) |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1265 | : addr(addr), ivar(ivar), destroyer(destroyer), |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1266 | useEHCleanupForArray(useEHCleanupForArray) {} |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1267 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1268 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1269 | LValue lvalue |
| 1270 | = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); |
| 1271 | CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer, |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1272 | flags.isForNormalCleanup() && useEHCleanupForArray); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1273 | } |
| 1274 | }; |
| 1275 | } |
| 1276 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1277 | /// Like CodeGenFunction::destroyARCStrong, but do it with a call. |
| 1278 | static void destroyARCStrongWithStore(CodeGenFunction &CGF, |
| 1279 | llvm::Value *addr, |
| 1280 | QualType type) { |
| 1281 | llvm::Value *null = getNullForVariable(addr); |
| 1282 | CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); |
| 1283 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1284 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1285 | static void emitCXXDestructMethod(CodeGenFunction &CGF, |
| 1286 | ObjCImplementationDecl *impl) { |
| 1287 | CodeGenFunction::RunCleanupsScope scope(CGF); |
| 1288 | |
| 1289 | llvm::Value *self = CGF.LoadObjCSelf(); |
| 1290 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1291 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 1292 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1293 | ivar; ivar = ivar->getNextIvar()) { |
| 1294 | QualType type = ivar->getType(); |
| 1295 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1296 | // Check whether the ivar is a destructible type. |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1297 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 1298 | if (!dtorKind) continue; |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1299 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1300 | CodeGenFunction::Destroyer *destroyer = 0; |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1301 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1302 | // Use a call to objc_storeStrong to destroy strong ivars, for the |
| 1303 | // general benefit of the tools. |
| 1304 | if (dtorKind == QualType::DK_objc_strong_lifetime) { |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1305 | destroyer = destroyARCStrongWithStore; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1306 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1307 | // Otherwise use the default for the destruction kind. |
| 1308 | } else { |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1309 | destroyer = CGF.getDestroyer(dtorKind); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1310 | } |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1311 | |
| 1312 | CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); |
| 1313 | |
| 1314 | CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, |
| 1315 | cleanupKind & EHCleanup); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); |
| 1319 | } |
| 1320 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1321 | void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, |
| 1322 | ObjCMethodDecl *MD, |
| 1323 | bool ctor) { |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1324 | MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 1325 | StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart()); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1326 | |
| 1327 | // Emit .cxx_construct. |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1328 | if (ctor) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1329 | // Suppress the final autorelease in ARC. |
| 1330 | AutoreleaseResult = false; |
| 1331 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1332 | SmallVector<CXXCtorInitializer *, 8> IvarInitializers; |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1333 | for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), |
| 1334 | E = IMP->init_end(); B != E; ++B) { |
| 1335 | CXXCtorInitializer *IvarInit = (*B); |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1336 | FieldDecl *Field = IvarInit->getAnyMember(); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1337 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Fariborz Jahanian | 9b4d4fc | 2010-04-28 22:30:33 +0000 | [diff] [blame] | 1338 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
| 1339 | LoadObjCSelf(), Ivar, 0); |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 1340 | EmitAggExpr(IvarInit->getInit(), |
| 1341 | AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed, |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 1342 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1343 | AggValueSlot::IsNotAliased)); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1344 | } |
| 1345 | // constructor returns 'self'. |
| 1346 | CodeGenTypes &Types = CGM.getTypes(); |
| 1347 | QualType IdTy(CGM.getContext().getObjCIdType()); |
| 1348 | llvm::Value *SelfAsId = |
| 1349 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
| 1350 | EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1351 | |
| 1352 | // Emit .cxx_destruct. |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 1353 | } else { |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 1354 | emitCXXDestructMethod(*this, IMP); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1355 | } |
| 1356 | FinishFunction(); |
| 1357 | } |
| 1358 | |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 1359 | bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { |
| 1360 | CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); |
| 1361 | it++; it++; |
| 1362 | const ABIArgInfo &AI = it->info; |
| 1363 | // FIXME. Is this sufficient check? |
| 1364 | return (AI.getKind() == ABIArgInfo::Indirect); |
| 1365 | } |
| 1366 | |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 1367 | bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1368 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 1369 | return false; |
| 1370 | if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) |
| 1371 | return FDTTy->getDecl()->hasObjectMember(); |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 1375 | llvm::Value *CodeGenFunction::LoadObjCSelf() { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1376 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 1377 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1380 | QualType CodeGenFunction::TypeOfSelfObject() { |
| 1381 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 1382 | ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1383 | const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( |
| 1384 | getContext().getCanonicalType(selfDecl->getType())); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1385 | return PTy->getPointeeType(); |
| 1386 | } |
| 1387 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1388 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | llvm::Constant *EnumerationMutationFn = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 1390 | CGM.getObjCRuntime().EnumerationMutationFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1391 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 1392 | if (!EnumerationMutationFn) { |
| 1393 | CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); |
| 1394 | return; |
| 1395 | } |
| 1396 | |
Devang Patel | bcbd03a | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 1397 | CGDebugInfo *DI = getDebugInfo(); |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1398 | if (DI) |
| 1399 | DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); |
Devang Patel | bcbd03a | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 1400 | |
Devang Patel | 9d99f2d | 2011-06-13 23:15:32 +0000 | [diff] [blame] | 1401 | // The local variable comes into scope immediately. |
| 1402 | AutoVarEmission variable = AutoVarEmission::invalid(); |
| 1403 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) |
| 1404 | variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); |
| 1405 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1406 | JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1408 | // Fast enumeration state. |
Douglas Gregor | 0815b57 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 1409 | QualType StateTy = CGM.getObjCFastEnumerationStateType(); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1410 | llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 1411 | EmitNullInitialization(StatePtr, StateTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1413 | // Number of elements in the items array. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1414 | static const unsigned NumItems = 16; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1416 | // Fetch the countByEnumeratingWithState:objects:count: selector. |
Benjamin Kramer | ad46886 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 1417 | IdentifierInfo *II[] = { |
| 1418 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 1419 | &CGM.getContext().Idents.get("objects"), |
| 1420 | &CGM.getContext().Idents.get("count") |
| 1421 | }; |
| 1422 | Selector FastEnumSel = |
| 1423 | CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1424 | |
| 1425 | QualType ItemsTy = |
| 1426 | getContext().getConstantArrayType(getContext().getObjCIdType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1427 | llvm::APInt(32, NumItems), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1428 | ArrayType::Normal, 0); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1429 | llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1431 | // Emit the collection pointer. In ARC, we do a retain. |
| 1432 | llvm::Value *Collection; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1433 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1434 | Collection = EmitARCRetainScalarExpr(S.getCollection()); |
| 1435 | |
| 1436 | // Enter a cleanup to do the release. |
| 1437 | EmitObjCConsumeObject(S.getCollection()->getType(), Collection); |
| 1438 | } else { |
| 1439 | Collection = EmitScalarExpr(S.getCollection()); |
| 1440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | |
John McCall | 4b302d3 | 2011-08-05 00:14:38 +0000 | [diff] [blame] | 1442 | // The 'continue' label needs to appear within the cleanup for the |
| 1443 | // collection object. |
| 1444 | JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); |
| 1445 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1446 | // Send it our message: |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1447 | CallArgList Args; |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1448 | |
| 1449 | // The first argument is a temporary of the enumeration-state type. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1450 | Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1452 | // The second argument is a temporary array with space for NumItems |
| 1453 | // pointers. We'll actually be loading elements from the array |
| 1454 | // pointer written into the control state; this buffer is so that |
| 1455 | // collections that *aren't* backed by arrays can still queue up |
| 1456 | // batches of elements. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1457 | Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1459 | // The third argument is the capacity of that temporary array. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1460 | llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1461 | llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1462 | Args.add(RValue::get(Count), getContext().UnsignedLongTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1464 | // Start the enumeration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | RValue CountRV = |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1466 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1467 | getContext().UnsignedLongTy, |
| 1468 | FastEnumSel, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1469 | Collection, Args); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1470 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1471 | // The initial number of objects that were returned in the buffer. |
| 1472 | llvm::Value *initialBufferLimit = CountRV.getScalarVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1474 | llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); |
| 1475 | llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1477 | llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1478 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1479 | // If the limit pointer was zero to begin with, the collection is |
| 1480 | // empty; skip all this. |
| 1481 | Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), |
| 1482 | EmptyBB, LoopInitBB); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1483 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1484 | // Otherwise, initialize the loop. |
| 1485 | EmitBlock(LoopInitBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1487 | // Save the initial mutations value. This is the value at an |
| 1488 | // address that was written into the state object by |
| 1489 | // countByEnumeratingWithState:objects:count:. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | llvm::Value *StateMutationsPtrPtr = |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1491 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1493 | "mutationsptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1495 | llvm::Value *initialMutations = |
| 1496 | Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1498 | // Start looping. This is the point we return to whenever we have a |
| 1499 | // fresh, non-empty batch of objects. |
| 1500 | llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); |
| 1501 | EmitBlock(LoopBodyBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1503 | // The current index into the buffer. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1504 | llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1505 | index->addIncoming(zero, LoopInitBB); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1506 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1507 | // The current buffer size. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1508 | llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1509 | count->addIncoming(initialBufferLimit, LoopInitBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1511 | // Check whether the mutations value has changed from where it was |
| 1512 | // at start. StateMutationsPtr should actually be invariant between |
| 1513 | // refreshes. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1514 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1515 | llvm::Value *currentMutations |
| 1516 | = Builder.CreateLoad(StateMutationsPtr, "statemutations"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1517 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1518 | llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); |
Dan Gohman | 361cf98 | 2011-03-02 22:39:34 +0000 | [diff] [blame] | 1519 | llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1521 | Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), |
| 1522 | WasNotMutatedBB, WasMutatedBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1524 | // If so, call the enumeration-mutation function. |
| 1525 | EmitBlock(WasMutatedBB); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1526 | llvm::Value *V = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | Builder.CreateBitCast(Collection, |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1528 | ConvertType(getContext().getObjCIdType())); |
Daniel Dunbar | 2b2105e | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 1529 | CallArgList Args2; |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1530 | Args2.add(RValue::get(V), getContext().getObjCIdType()); |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1531 | // FIXME: We shouldn't need to get the function info here, the runtime already |
| 1532 | // should have computed it to build the function. |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1533 | EmitCall(CGM.getTypes().arrangeFreeFunctionCall(getContext().VoidTy, Args2, |
| 1534 | FunctionType::ExtInfo(), |
| 1535 | RequiredArgs::All), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 1536 | EnumerationMutationFn, ReturnValueSlot(), Args2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1538 | // Otherwise, or if the mutation function returns, just continue. |
| 1539 | EmitBlock(WasNotMutatedBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1541 | // Initialize the element variable. |
| 1542 | RunCleanupsScope elementVariableScope(*this); |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1543 | bool elementIsVariable; |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1544 | LValue elementLValue; |
| 1545 | QualType elementType; |
| 1546 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1547 | // Initialize the variable, in case it's a __block variable or something. |
| 1548 | EmitAutoVarInit(variable); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1549 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1550 | const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1551 | DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(), |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1552 | VK_LValue, SourceLocation()); |
| 1553 | elementLValue = EmitLValue(&tempDRE); |
| 1554 | elementType = D->getType(); |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1555 | elementIsVariable = true; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1556 | |
| 1557 | if (D->isARCPseudoStrong()) |
| 1558 | elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1559 | } else { |
| 1560 | elementLValue = LValue(); // suppress warning |
| 1561 | elementType = cast<Expr>(S.getElement())->getType(); |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1562 | elementIsVariable = false; |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1563 | } |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1564 | llvm::Type *convertedElementType = ConvertType(elementType); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1565 | |
| 1566 | // Fetch the buffer out of the enumeration state. |
| 1567 | // TODO: this pointer should actually be invariant between |
| 1568 | // refreshes, which would help us do certain loop optimizations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | llvm::Value *StateItemsPtr = |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1570 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1571 | llvm::Value *EnumStateItems = |
| 1572 | Builder.CreateLoad(StateItemsPtr, "stateitems"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1573 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1574 | // Fetch the value at the current index from the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | llvm::Value *CurrentItemPtr = |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1576 | Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); |
| 1577 | llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1579 | // Cast that value to the right type. |
| 1580 | CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, |
| 1581 | "currentitem"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1583 | // Make sure we have an l-value. Yes, this gets evaluated every |
| 1584 | // time through the loop. |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1585 | if (!elementIsVariable) { |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1586 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1587 | EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1588 | } else { |
| 1589 | EmitScalarInit(CurrentItem, elementLValue); |
| 1590 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1592 | // If we do have an element variable, this assignment is the end of |
| 1593 | // its initialization. |
| 1594 | if (elementIsVariable) |
| 1595 | EmitAutoVarCleanups(variable); |
| 1596 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1597 | // Perform the loop body, setting up break and continue labels. |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 1598 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1599 | { |
| 1600 | RunCleanupsScope Scope(*this); |
| 1601 | EmitStmt(S.getBody()); |
| 1602 | } |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1603 | BreakContinueStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1605 | // Destroy the element variable now. |
| 1606 | elementVariableScope.ForceCleanup(); |
| 1607 | |
| 1608 | // Check whether there are more elements. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1609 | EmitBlock(AfterBody.getBlock()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1611 | llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); |
Fariborz Jahanian | f0906c4 | 2009-01-06 18:56:31 +0000 | [diff] [blame] | 1612 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1613 | // First we check in the local buffer. |
| 1614 | llvm::Value *indexPlusOne |
| 1615 | = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1616 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1617 | // If we haven't overrun the buffer yet, we can continue. |
| 1618 | Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), |
| 1619 | LoopBodyBB, FetchMoreBB); |
| 1620 | |
| 1621 | index->addIncoming(indexPlusOne, AfterBody.getBlock()); |
| 1622 | count->addIncoming(count, AfterBody.getBlock()); |
| 1623 | |
| 1624 | // Otherwise, we have to fetch more elements. |
| 1625 | EmitBlock(FetchMoreBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
| 1627 | CountRV = |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1628 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1629 | getContext().UnsignedLongTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | FastEnumSel, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1631 | Collection, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1633 | // If we got a zero count, we're done. |
| 1634 | llvm::Value *refetchCount = CountRV.getScalarVal(); |
| 1635 | |
| 1636 | // (note that the message send might split FetchMoreBB) |
| 1637 | index->addIncoming(zero, Builder.GetInsertBlock()); |
| 1638 | count->addIncoming(refetchCount, Builder.GetInsertBlock()); |
| 1639 | |
| 1640 | Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), |
| 1641 | EmptyBB, LoopBodyBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1643 | // No more elements. |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1644 | EmitBlock(EmptyBB); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1645 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1646 | if (!elementIsVariable) { |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1647 | // If the element was not a declaration, set it to be null. |
| 1648 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1649 | llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); |
| 1650 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1651 | EmitStoreThroughLValue(RValue::get(null), elementLValue); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1654 | if (DI) |
| 1655 | DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); |
Devang Patel | bcbd03a | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 1656 | |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1657 | // Leave the cleanup we entered in ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1658 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1659 | PopCleanupBlock(); |
| 1660 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1661 | EmitBlock(LoopEnd.getBlock()); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1665 | CGM.getObjCRuntime().EmitTryStmt(*this, S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1669 | CGM.getObjCRuntime().EmitThrowStmt(*this, S); |
| 1670 | } |
| 1671 | |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 1672 | void CodeGenFunction::EmitObjCAtSynchronizedStmt( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | const ObjCAtSynchronizedStmt &S) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1674 | CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 1677 | /// Produce the code for a CK_ARCProduceObject. Just does a |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1678 | /// primitive retain. |
| 1679 | llvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type, |
| 1680 | llvm::Value *value) { |
| 1681 | return EmitARCRetain(type, value); |
| 1682 | } |
| 1683 | |
| 1684 | namespace { |
| 1685 | struct CallObjCRelease : EHScopeStack::Cleanup { |
John McCall | bddfd87 | 2011-08-03 22:24:24 +0000 | [diff] [blame] | 1686 | CallObjCRelease(llvm::Value *object) : object(object) {} |
| 1687 | llvm::Value *object; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1688 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1689 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 1690 | // Releases at the end of the full-expression are imprecise. |
| 1691 | CGF.EmitARCRelease(object, ARCImpreciseLifetime); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1692 | } |
| 1693 | }; |
| 1694 | } |
| 1695 | |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 1696 | /// Produce the code for a CK_ARCConsumeObject. Does a primitive |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1697 | /// release at the end of the full-expression. |
| 1698 | llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, |
| 1699 | llvm::Value *object) { |
| 1700 | // 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] | 1701 | // conditional. |
| 1702 | pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1703 | return object; |
| 1704 | } |
| 1705 | |
| 1706 | llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, |
| 1707 | llvm::Value *value) { |
| 1708 | return EmitARCRetainAutorelease(type, value); |
| 1709 | } |
| 1710 | |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 1711 | /// Given a number of pointers, inform the optimizer that they're |
| 1712 | /// being intrinsically used up until this point in the program. |
| 1713 | void CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) { |
| 1714 | llvm::Constant *&fn = CGM.getARCEntrypoints().clang_arc_use; |
| 1715 | if (!fn) { |
| 1716 | llvm::FunctionType *fnType = |
| 1717 | llvm::FunctionType::get(CGM.VoidTy, ArrayRef<llvm::Type*>(), true); |
| 1718 | fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use"); |
| 1719 | } |
| 1720 | |
| 1721 | // This isn't really a "runtime" function, but as an intrinsic it |
| 1722 | // doesn't really matter as long as we align things up. |
| 1723 | EmitNounwindRuntimeCall(fn, values); |
| 1724 | } |
| 1725 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1726 | |
| 1727 | static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1728 | llvm::FunctionType *type, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1729 | StringRef fnName) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1730 | llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName); |
| 1731 | |
Michael Gottesman | 554b07d | 2013-02-02 00:57:44 +0000 | [diff] [blame] | 1732 | if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) { |
Michael Gottesman | cfe18a1 | 2013-02-02 01:05:06 +0000 | [diff] [blame] | 1733 | // If the target runtime doesn't naturally support ARC, emit weak |
| 1734 | // references to the runtime support library. We don't really |
| 1735 | // permit this to fail, but we need a particular relocation style. |
Michael Gottesman | 554b07d | 2013-02-02 00:57:44 +0000 | [diff] [blame] | 1736 | if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1737 | f->setLinkage(llvm::Function::ExternalWeakLinkage); |
Michael Gottesman | 554b07d | 2013-02-02 00:57:44 +0000 | [diff] [blame] | 1738 | } else if (fnName == "objc_retain" || fnName == "objc_release") { |
| 1739 | // If we have Native ARC, set nonlazybind attribute for these APIs for |
| 1740 | // performance. |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1741 | f->addFnAttr(llvm::Attribute::NonLazyBind); |
Michael Gottesman | db99e8b | 2013-02-02 01:03:01 +0000 | [diff] [blame] | 1742 | } |
Michael Gottesman | 554b07d | 2013-02-02 00:57:44 +0000 | [diff] [blame] | 1743 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1744 | |
| 1745 | return fn; |
| 1746 | } |
| 1747 | |
| 1748 | /// Perform an operation having the signature |
| 1749 | /// i8* (i8*) |
| 1750 | /// where a null input causes a no-op and returns null. |
| 1751 | static llvm::Value *emitARCValueOperation(CodeGenFunction &CGF, |
| 1752 | llvm::Value *value, |
| 1753 | llvm::Constant *&fn, |
Chad Rosier | df76f1e | 2012-12-12 17:52:21 +0000 | [diff] [blame] | 1754 | StringRef fnName, |
| 1755 | bool isTailCall = false) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1756 | if (isa<llvm::ConstantPointerNull>(value)) return value; |
| 1757 | |
| 1758 | if (!fn) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1759 | llvm::FunctionType *fnType = |
Benjamin Kramer | 76ecdfc | 2013-03-07 21:18:31 +0000 | [diff] [blame] | 1760 | llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1761 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); |
| 1762 | } |
| 1763 | |
| 1764 | // Cast the argument to 'id'. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1765 | llvm::Type *origType = value->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1766 | value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); |
| 1767 | |
| 1768 | // Call the function. |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1769 | llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value); |
Chad Rosier | df76f1e | 2012-12-12 17:52:21 +0000 | [diff] [blame] | 1770 | if (isTailCall) |
| 1771 | call->setTailCall(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Cast the result back to the original type. |
| 1774 | return CGF.Builder.CreateBitCast(call, origType); |
| 1775 | } |
| 1776 | |
| 1777 | /// Perform an operation having the following signature: |
| 1778 | /// i8* (i8**) |
| 1779 | static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, |
| 1780 | llvm::Value *addr, |
| 1781 | llvm::Constant *&fn, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1782 | StringRef fnName) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1783 | if (!fn) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1784 | llvm::FunctionType *fnType = |
Benjamin Kramer | 76ecdfc | 2013-03-07 21:18:31 +0000 | [diff] [blame] | 1785 | llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1786 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); |
| 1787 | } |
| 1788 | |
| 1789 | // Cast the argument to 'id*'. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1790 | llvm::Type *origType = addr->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1791 | addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); |
| 1792 | |
| 1793 | // Call the function. |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1794 | llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1795 | |
| 1796 | // Cast the result back to a dereference of the original type. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1797 | if (origType != CGF.Int8PtrPtrTy) |
| 1798 | result = CGF.Builder.CreateBitCast(result, |
| 1799 | cast<llvm::PointerType>(origType)->getElementType()); |
| 1800 | |
| 1801 | return result; |
| 1802 | } |
| 1803 | |
| 1804 | /// Perform an operation having the following signature: |
| 1805 | /// i8* (i8**, i8*) |
| 1806 | static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, |
| 1807 | llvm::Value *addr, |
| 1808 | llvm::Value *value, |
| 1809 | llvm::Constant *&fn, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1810 | StringRef fnName, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1811 | bool ignored) { |
| 1812 | assert(cast<llvm::PointerType>(addr->getType())->getElementType() |
| 1813 | == value->getType()); |
| 1814 | |
| 1815 | if (!fn) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1816 | llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy }; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1817 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1818 | llvm::FunctionType *fnType |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1819 | = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false); |
| 1820 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); |
| 1821 | } |
| 1822 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1823 | llvm::Type *origType = value->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1824 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1825 | llvm::Value *args[] = { |
| 1826 | CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy), |
| 1827 | CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy) |
| 1828 | }; |
| 1829 | llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1830 | |
| 1831 | if (ignored) return 0; |
| 1832 | |
| 1833 | return CGF.Builder.CreateBitCast(result, origType); |
| 1834 | } |
| 1835 | |
| 1836 | /// Perform an operation having the following signature: |
| 1837 | /// void (i8**, i8**) |
| 1838 | static void emitARCCopyOperation(CodeGenFunction &CGF, |
| 1839 | llvm::Value *dst, |
| 1840 | llvm::Value *src, |
| 1841 | llvm::Constant *&fn, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1842 | StringRef fnName) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1843 | assert(dst->getType() == src->getType()); |
| 1844 | |
| 1845 | if (!fn) { |
Benjamin Kramer | 76ecdfc | 2013-03-07 21:18:31 +0000 | [diff] [blame] | 1846 | llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy }; |
| 1847 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1848 | llvm::FunctionType *fnType |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1849 | = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false); |
| 1850 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); |
| 1851 | } |
| 1852 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1853 | llvm::Value *args[] = { |
| 1854 | CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy), |
| 1855 | CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy) |
| 1856 | }; |
| 1857 | CGF.EmitNounwindRuntimeCall(fn, args); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | /// Produce the code to do a retain. Based on the type, calls one of: |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 1861 | /// call i8* \@objc_retain(i8* %value) |
| 1862 | /// call i8* \@objc_retainBlock(i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1863 | llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { |
| 1864 | if (type->isBlockPointerType()) |
John McCall | 348f16f | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 1865 | return EmitARCRetainBlock(value, /*mandatory*/ false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1866 | else |
| 1867 | return EmitARCRetainNonBlock(value); |
| 1868 | } |
| 1869 | |
| 1870 | /// Retain the given object, with normal retain semantics. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 1871 | /// call i8* \@objc_retain(i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1872 | llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { |
| 1873 | return emitARCValueOperation(*this, value, |
| 1874 | CGM.getARCEntrypoints().objc_retain, |
| 1875 | "objc_retain"); |
| 1876 | } |
| 1877 | |
| 1878 | /// Retain the given block, with _Block_copy semantics. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 1879 | /// call i8* \@objc_retainBlock(i8* %value) |
John McCall | 348f16f | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 1880 | /// |
| 1881 | /// \param mandatory - If false, emit the call with metadata |
| 1882 | /// indicating that it's okay for the optimizer to eliminate this call |
| 1883 | /// if it can prove that the block never escapes except down the stack. |
| 1884 | llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, |
| 1885 | bool mandatory) { |
| 1886 | llvm::Value *result |
| 1887 | = emitARCValueOperation(*this, value, |
| 1888 | CGM.getARCEntrypoints().objc_retainBlock, |
| 1889 | "objc_retainBlock"); |
| 1890 | |
| 1891 | // If the copy isn't mandatory, add !clang.arc.copy_on_escape to |
| 1892 | // tell the optimizer that it doesn't need to do this copy if the |
| 1893 | // block doesn't escape, where being passed as an argument doesn't |
| 1894 | // count as escaping. |
| 1895 | if (!mandatory && isa<llvm::Instruction>(result)) { |
| 1896 | llvm::CallInst *call |
| 1897 | = cast<llvm::CallInst>(result->stripPointerCasts()); |
| 1898 | assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock); |
| 1899 | |
| 1900 | SmallVector<llvm::Value*,1> args; |
| 1901 | call->setMetadata("clang.arc.copy_on_escape", |
| 1902 | llvm::MDNode::get(Builder.getContext(), args)); |
| 1903 | } |
| 1904 | |
| 1905 | return result; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /// Retain the given object which is the result of a function call. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 1909 | /// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1910 | /// |
| 1911 | /// Yes, this function name is one character away from a different |
| 1912 | /// call with completely different semantics. |
| 1913 | llvm::Value * |
| 1914 | CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { |
| 1915 | // Fetch the void(void) inline asm which marks that we're going to |
| 1916 | // retain the autoreleased return value. |
| 1917 | llvm::InlineAsm *&marker |
| 1918 | = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker; |
| 1919 | if (!marker) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1920 | StringRef assembly |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1921 | = CGM.getTargetCodeGenInfo() |
| 1922 | .getARCRetainAutoreleasedReturnValueMarker(); |
| 1923 | |
| 1924 | // If we have an empty assembly string, there's nothing to do. |
| 1925 | if (assembly.empty()) { |
| 1926 | |
| 1927 | // Otherwise, at -O0, build an inline asm that we're going to call |
| 1928 | // in a moment. |
| 1929 | } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) { |
| 1930 | llvm::FunctionType *type = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1931 | llvm::FunctionType::get(VoidTy, /*variadic*/false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1932 | |
| 1933 | marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); |
| 1934 | |
| 1935 | // If we're at -O1 and above, we don't want to litter the code |
| 1936 | // with this marker yet, so leave a breadcrumb for the ARC |
| 1937 | // optimizer to pick up. |
| 1938 | } else { |
| 1939 | llvm::NamedMDNode *metadata = |
| 1940 | CGM.getModule().getOrInsertNamedMetadata( |
| 1941 | "clang.arc.retainAutoreleasedReturnValueMarker"); |
| 1942 | assert(metadata->getNumOperands() <= 1); |
| 1943 | if (metadata->getNumOperands() == 0) { |
| 1944 | llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly); |
Jay Foad | da549e8 | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1945 | metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | // Call the marker asm if we made one, which we do only at -O0. |
| 1951 | if (marker) Builder.CreateCall(marker); |
| 1952 | |
| 1953 | return emitARCValueOperation(*this, value, |
| 1954 | CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue, |
| 1955 | "objc_retainAutoreleasedReturnValue"); |
| 1956 | } |
| 1957 | |
| 1958 | /// Release the given object. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 1959 | /// call void \@objc_release(i8* %value) |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 1960 | void CodeGenFunction::EmitARCRelease(llvm::Value *value, |
| 1961 | ARCPreciseLifetime_t precise) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1962 | if (isa<llvm::ConstantPointerNull>(value)) return; |
| 1963 | |
| 1964 | llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release; |
| 1965 | if (!fn) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1966 | llvm::FunctionType *fnType = |
Benjamin Kramer | 76ecdfc | 2013-03-07 21:18:31 +0000 | [diff] [blame] | 1967 | llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1968 | fn = createARCRuntimeFunction(CGM, fnType, "objc_release"); |
| 1969 | } |
| 1970 | |
| 1971 | // Cast the argument to 'id'. |
| 1972 | value = Builder.CreateBitCast(value, Int8PtrTy); |
| 1973 | |
| 1974 | // Call objc_release. |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1975 | llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1976 | |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 1977 | if (precise == ARCImpreciseLifetime) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1978 | SmallVector<llvm::Value*,1> args; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1979 | call->setMetadata("clang.imprecise_release", |
| 1980 | llvm::MDNode::get(Builder.getContext(), args)); |
| 1981 | } |
| 1982 | } |
| 1983 | |
John McCall | 015f33b | 2012-10-17 02:28:37 +0000 | [diff] [blame] | 1984 | /// Destroy a __strong variable. |
| 1985 | /// |
| 1986 | /// At -O0, emit a call to store 'null' into the address; |
| 1987 | /// instrumenting tools prefer this because the address is exposed, |
| 1988 | /// but it's relatively cumbersome to optimize. |
| 1989 | /// |
| 1990 | /// At -O1 and above, just load and call objc_release. |
| 1991 | /// |
| 1992 | /// call void \@objc_storeStrong(i8** %addr, i8* null) |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 1993 | void CodeGenFunction::EmitARCDestroyStrong(llvm::Value *addr, |
| 1994 | ARCPreciseLifetime_t precise) { |
John McCall | 015f33b | 2012-10-17 02:28:37 +0000 | [diff] [blame] | 1995 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) { |
| 1996 | llvm::PointerType *addrTy = cast<llvm::PointerType>(addr->getType()); |
| 1997 | llvm::Value *null = llvm::ConstantPointerNull::get( |
| 1998 | cast<llvm::PointerType>(addrTy->getElementType())); |
| 1999 | EmitARCStoreStrongCall(addr, null, /*ignored*/ true); |
| 2000 | return; |
| 2001 | } |
| 2002 | |
| 2003 | llvm::Value *value = Builder.CreateLoad(addr); |
| 2004 | EmitARCRelease(value, precise); |
| 2005 | } |
| 2006 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2007 | /// Store into a strong object. Always calls this: |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2008 | /// call void \@objc_storeStrong(i8** %addr, i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2009 | llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr, |
| 2010 | llvm::Value *value, |
| 2011 | bool ignored) { |
| 2012 | assert(cast<llvm::PointerType>(addr->getType())->getElementType() |
| 2013 | == value->getType()); |
| 2014 | |
| 2015 | llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong; |
| 2016 | if (!fn) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2017 | llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy }; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2018 | llvm::FunctionType *fnType |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2019 | = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false); |
| 2020 | fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong"); |
| 2021 | } |
| 2022 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2023 | llvm::Value *args[] = { |
| 2024 | Builder.CreateBitCast(addr, Int8PtrPtrTy), |
| 2025 | Builder.CreateBitCast(value, Int8PtrTy) |
| 2026 | }; |
| 2027 | EmitNounwindRuntimeCall(fn, args); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2028 | |
| 2029 | if (ignored) return 0; |
| 2030 | return value; |
| 2031 | } |
| 2032 | |
| 2033 | /// Store into a strong object. Sometimes calls this: |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2034 | /// call void \@objc_storeStrong(i8** %addr, i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2035 | /// Other times, breaks it down into components. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2036 | llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2037 | llvm::Value *newValue, |
| 2038 | bool ignored) { |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2039 | QualType type = dst.getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2040 | bool isBlock = type->isBlockPointerType(); |
| 2041 | |
| 2042 | // Use a store barrier at -O0 unless this is a block type or the |
| 2043 | // lvalue is inadequately aligned. |
| 2044 | if (shouldUseFusedARCCalls() && |
| 2045 | !isBlock && |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2046 | (dst.getAlignment().isZero() || |
| 2047 | dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2048 | return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); |
| 2049 | } |
| 2050 | |
| 2051 | // Otherwise, split it out. |
| 2052 | |
| 2053 | // Retain the new value. |
| 2054 | newValue = EmitARCRetain(type, newValue); |
| 2055 | |
| 2056 | // Read the old value. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2057 | llvm::Value *oldValue = EmitLoadOfScalar(dst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2058 | |
| 2059 | // Store. We do this before the release so that any deallocs won't |
| 2060 | // see the old value. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2061 | EmitStoreOfScalar(newValue, dst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2062 | |
| 2063 | // Finally, release the old value. |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2064 | EmitARCRelease(oldValue, dst.isARCPreciseLifetime()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2065 | |
| 2066 | return newValue; |
| 2067 | } |
| 2068 | |
| 2069 | /// Autorelease the given object. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2070 | /// call i8* \@objc_autorelease(i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2071 | llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { |
| 2072 | return emitARCValueOperation(*this, value, |
| 2073 | CGM.getARCEntrypoints().objc_autorelease, |
| 2074 | "objc_autorelease"); |
| 2075 | } |
| 2076 | |
| 2077 | /// Autorelease the given object. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2078 | /// call i8* \@objc_autoreleaseReturnValue(i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2079 | llvm::Value * |
| 2080 | CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { |
| 2081 | return emitARCValueOperation(*this, value, |
| 2082 | CGM.getARCEntrypoints().objc_autoreleaseReturnValue, |
Chad Rosier | df76f1e | 2012-12-12 17:52:21 +0000 | [diff] [blame] | 2083 | "objc_autoreleaseReturnValue", |
| 2084 | /*isTailCall*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | /// Do a fused retain/autorelease of the given object. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2088 | /// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2089 | llvm::Value * |
| 2090 | CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { |
| 2091 | return emitARCValueOperation(*this, value, |
| 2092 | CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue, |
Chad Rosier | df76f1e | 2012-12-12 17:52:21 +0000 | [diff] [blame] | 2093 | "objc_retainAutoreleaseReturnValue", |
| 2094 | /*isTailCall*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /// Do a fused retain/autorelease of the given object. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2098 | /// call i8* \@objc_retainAutorelease(i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2099 | /// or |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2100 | /// %retain = call i8* \@objc_retainBlock(i8* %value) |
| 2101 | /// call i8* \@objc_autorelease(i8* %retain) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2102 | llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, |
| 2103 | llvm::Value *value) { |
| 2104 | if (!type->isBlockPointerType()) |
| 2105 | return EmitARCRetainAutoreleaseNonBlock(value); |
| 2106 | |
| 2107 | if (isa<llvm::ConstantPointerNull>(value)) return value; |
| 2108 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2109 | llvm::Type *origType = value->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2110 | value = Builder.CreateBitCast(value, Int8PtrTy); |
John McCall | 348f16f | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 2111 | value = EmitARCRetainBlock(value, /*mandatory*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2112 | value = EmitARCAutorelease(value); |
| 2113 | return Builder.CreateBitCast(value, origType); |
| 2114 | } |
| 2115 | |
| 2116 | /// Do a fused retain/autorelease of the given object. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2117 | /// call i8* \@objc_retainAutorelease(i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2118 | llvm::Value * |
| 2119 | CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { |
| 2120 | return emitARCValueOperation(*this, value, |
| 2121 | CGM.getARCEntrypoints().objc_retainAutorelease, |
| 2122 | "objc_retainAutorelease"); |
| 2123 | } |
| 2124 | |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2125 | /// i8* \@objc_loadWeak(i8** %addr) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2126 | /// Essentially objc_autorelease(objc_loadWeakRetained(addr)). |
| 2127 | llvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) { |
| 2128 | return emitARCLoadOperation(*this, addr, |
| 2129 | CGM.getARCEntrypoints().objc_loadWeak, |
| 2130 | "objc_loadWeak"); |
| 2131 | } |
| 2132 | |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2133 | /// i8* \@objc_loadWeakRetained(i8** %addr) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2134 | llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) { |
| 2135 | return emitARCLoadOperation(*this, addr, |
| 2136 | CGM.getARCEntrypoints().objc_loadWeakRetained, |
| 2137 | "objc_loadWeakRetained"); |
| 2138 | } |
| 2139 | |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2140 | /// i8* \@objc_storeWeak(i8** %addr, i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2141 | /// Returns %value. |
| 2142 | llvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr, |
| 2143 | llvm::Value *value, |
| 2144 | bool ignored) { |
| 2145 | return emitARCStoreOperation(*this, addr, value, |
| 2146 | CGM.getARCEntrypoints().objc_storeWeak, |
| 2147 | "objc_storeWeak", ignored); |
| 2148 | } |
| 2149 | |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2150 | /// i8* \@objc_initWeak(i8** %addr, i8* %value) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2151 | /// Returns %value. %addr is known to not have a current weak entry. |
| 2152 | /// Essentially equivalent to: |
| 2153 | /// *addr = nil; objc_storeWeak(addr, value); |
| 2154 | void CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) { |
| 2155 | // If we're initializing to null, just write null to memory; no need |
| 2156 | // to get the runtime involved. But don't do this if optimization |
| 2157 | // is enabled, because accounting for this would make the optimizer |
| 2158 | // much more complicated. |
| 2159 | if (isa<llvm::ConstantPointerNull>(value) && |
| 2160 | CGM.getCodeGenOpts().OptimizationLevel == 0) { |
| 2161 | Builder.CreateStore(value, addr); |
| 2162 | return; |
| 2163 | } |
| 2164 | |
| 2165 | emitARCStoreOperation(*this, addr, value, |
| 2166 | CGM.getARCEntrypoints().objc_initWeak, |
| 2167 | "objc_initWeak", /*ignored*/ true); |
| 2168 | } |
| 2169 | |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2170 | /// void \@objc_destroyWeak(i8** %addr) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2171 | /// Essentially objc_storeWeak(addr, nil). |
| 2172 | void CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) { |
| 2173 | llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak; |
| 2174 | if (!fn) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2175 | llvm::FunctionType *fnType = |
Benjamin Kramer | 76ecdfc | 2013-03-07 21:18:31 +0000 | [diff] [blame] | 2176 | llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2177 | fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak"); |
| 2178 | } |
| 2179 | |
| 2180 | // Cast the argument to 'id*'. |
| 2181 | addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); |
| 2182 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2183 | EmitNounwindRuntimeCall(fn, addr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2186 | /// void \@objc_moveWeak(i8** %dest, i8** %src) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2187 | /// Disregards the current value in %dest. Leaves %src pointing to nothing. |
| 2188 | /// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). |
| 2189 | void CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) { |
| 2190 | emitARCCopyOperation(*this, dst, src, |
| 2191 | CGM.getARCEntrypoints().objc_moveWeak, |
| 2192 | "objc_moveWeak"); |
| 2193 | } |
| 2194 | |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2195 | /// void \@objc_copyWeak(i8** %dest, i8** %src) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2196 | /// Disregards the current value in %dest. Essentially |
| 2197 | /// objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) |
| 2198 | void CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) { |
| 2199 | emitARCCopyOperation(*this, dst, src, |
| 2200 | CGM.getARCEntrypoints().objc_copyWeak, |
| 2201 | "objc_copyWeak"); |
| 2202 | } |
| 2203 | |
| 2204 | /// Produce the code to do a objc_autoreleasepool_push. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2205 | /// call i8* \@objc_autoreleasePoolPush(void) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2206 | llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { |
| 2207 | llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush; |
| 2208 | if (!fn) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2209 | llvm::FunctionType *fnType = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2210 | llvm::FunctionType::get(Int8PtrTy, false); |
| 2211 | fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush"); |
| 2212 | } |
| 2213 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2214 | return EmitNounwindRuntimeCall(fn); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | /// Produce the code to do a primitive release. |
James Dennett | 9d96e9c | 2012-06-22 05:41:30 +0000 | [diff] [blame] | 2218 | /// call void \@objc_autoreleasePoolPop(i8* %ptr) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2219 | void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { |
| 2220 | assert(value->getType() == Int8PtrTy); |
| 2221 | |
| 2222 | llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop; |
| 2223 | if (!fn) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2224 | llvm::FunctionType *fnType = |
Benjamin Kramer | 76ecdfc | 2013-03-07 21:18:31 +0000 | [diff] [blame] | 2225 | llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2226 | |
| 2227 | // We don't want to use a weak import here; instead we should not |
| 2228 | // fall into this path. |
| 2229 | fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop"); |
| 2230 | } |
| 2231 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2232 | EmitNounwindRuntimeCall(fn, value); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | /// Produce the code to do an MRR version objc_autoreleasepool_push. |
| 2236 | /// Which is: [[NSAutoreleasePool alloc] init]; |
| 2237 | /// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. |
| 2238 | /// init is declared as: - (id) init; in its NSObject super class. |
| 2239 | /// |
| 2240 | llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { |
| 2241 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2242 | llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2243 | // [NSAutoreleasePool alloc] |
| 2244 | IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); |
| 2245 | Selector AllocSel = getContext().Selectors.getSelector(0, &II); |
| 2246 | CallArgList Args; |
| 2247 | RValue AllocRV = |
| 2248 | Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
| 2249 | getContext().getObjCIdType(), |
| 2250 | AllocSel, Receiver, Args); |
| 2251 | |
| 2252 | // [Receiver init] |
| 2253 | Receiver = AllocRV.getScalarVal(); |
| 2254 | II = &CGM.getContext().Idents.get("init"); |
| 2255 | Selector InitSel = getContext().Selectors.getSelector(0, &II); |
| 2256 | RValue InitRV = |
| 2257 | Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
| 2258 | getContext().getObjCIdType(), |
| 2259 | InitSel, Receiver, Args); |
| 2260 | return InitRV.getScalarVal(); |
| 2261 | } |
| 2262 | |
| 2263 | /// Produce the code to do a primitive release. |
| 2264 | /// [tmp drain]; |
| 2265 | void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { |
| 2266 | IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); |
| 2267 | Selector DrainSel = getContext().Selectors.getSelector(0, &II); |
| 2268 | CallArgList Args; |
| 2269 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
| 2270 | getContext().VoidTy, DrainSel, Arg, Args); |
| 2271 | } |
| 2272 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2273 | void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, |
| 2274 | llvm::Value *addr, |
| 2275 | QualType type) { |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2276 | CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, |
| 2280 | llvm::Value *addr, |
| 2281 | QualType type) { |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2282 | CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, |
| 2286 | llvm::Value *addr, |
| 2287 | QualType type) { |
| 2288 | CGF.EmitARCDestroyWeak(addr); |
| 2289 | } |
| 2290 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2291 | namespace { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2292 | struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup { |
| 2293 | llvm::Value *Token; |
| 2294 | |
| 2295 | CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} |
| 2296 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 2297 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2298 | CGF.EmitObjCAutoreleasePoolPop(Token); |
| 2299 | } |
| 2300 | }; |
| 2301 | struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup { |
| 2302 | llvm::Value *Token; |
| 2303 | |
| 2304 | CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} |
| 2305 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 2306 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2307 | CGF.EmitObjCMRRAutoreleasePoolPop(Token); |
| 2308 | } |
| 2309 | }; |
| 2310 | } |
| 2311 | |
| 2312 | void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2313 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2314 | EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); |
| 2315 | else |
| 2316 | EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); |
| 2317 | } |
| 2318 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2319 | static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
| 2320 | LValue lvalue, |
| 2321 | QualType type) { |
| 2322 | switch (type.getObjCLifetime()) { |
| 2323 | case Qualifiers::OCL_None: |
| 2324 | case Qualifiers::OCL_ExplicitNone: |
| 2325 | case Qualifiers::OCL_Strong: |
| 2326 | case Qualifiers::OCL_Autoreleasing: |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2327 | return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2328 | false); |
| 2329 | |
| 2330 | case Qualifiers::OCL_Weak: |
| 2331 | return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()), |
| 2332 | true); |
| 2333 | } |
| 2334 | |
| 2335 | llvm_unreachable("impossible lifetime!"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
| 2339 | const Expr *e) { |
| 2340 | e = e->IgnoreParens(); |
| 2341 | QualType type = e->getType(); |
| 2342 | |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 2343 | // If we're loading retained from a __strong xvalue, we can avoid |
| 2344 | // an extra retain/release pair by zeroing out the source of this |
| 2345 | // "move" operation. |
| 2346 | if (e->isXValue() && |
| 2347 | !type.isConstQualified() && |
| 2348 | type.getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 2349 | // Emit the lvalue. |
| 2350 | LValue lv = CGF.EmitLValue(e); |
| 2351 | |
| 2352 | // Load the object pointer. |
| 2353 | llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal(); |
| 2354 | |
| 2355 | // Set the source pointer to NULL. |
| 2356 | CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv); |
| 2357 | |
| 2358 | return TryEmitResult(result, true); |
| 2359 | } |
| 2360 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2361 | // As a very special optimization, in ARC++, if the l-value is the |
| 2362 | // result of a non-volatile assignment, do a simple retain of the |
| 2363 | // result of the call to objc_storeWeak instead of reloading. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2364 | if (CGF.getLangOpts().CPlusPlus && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2365 | !type.isVolatileQualified() && |
| 2366 | type.getObjCLifetime() == Qualifiers::OCL_Weak && |
| 2367 | isa<BinaryOperator>(e) && |
| 2368 | cast<BinaryOperator>(e)->getOpcode() == BO_Assign) |
| 2369 | return TryEmitResult(CGF.EmitScalarExpr(e), false); |
| 2370 | |
| 2371 | return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); |
| 2372 | } |
| 2373 | |
| 2374 | static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, |
| 2375 | llvm::Value *value); |
| 2376 | |
| 2377 | /// Given that the given expression is some sort of call (which does |
| 2378 | /// not return retained), emit a retain following it. |
| 2379 | static llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) { |
| 2380 | llvm::Value *value = CGF.EmitScalarExpr(e); |
| 2381 | return emitARCRetainAfterCall(CGF, value); |
| 2382 | } |
| 2383 | |
| 2384 | static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, |
| 2385 | llvm::Value *value) { |
| 2386 | if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { |
| 2387 | CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); |
| 2388 | |
| 2389 | // Place the retain immediately following the call. |
| 2390 | CGF.Builder.SetInsertPoint(call->getParent(), |
| 2391 | ++llvm::BasicBlock::iterator(call)); |
| 2392 | value = CGF.EmitARCRetainAutoreleasedReturnValue(value); |
| 2393 | |
| 2394 | CGF.Builder.restoreIP(ip); |
| 2395 | return value; |
| 2396 | } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { |
| 2397 | CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); |
| 2398 | |
| 2399 | // Place the retain at the beginning of the normal destination block. |
| 2400 | llvm::BasicBlock *BB = invoke->getNormalDest(); |
| 2401 | CGF.Builder.SetInsertPoint(BB, BB->begin()); |
| 2402 | value = CGF.EmitARCRetainAutoreleasedReturnValue(value); |
| 2403 | |
| 2404 | CGF.Builder.restoreIP(ip); |
| 2405 | return value; |
| 2406 | |
| 2407 | // Bitcasts can arise because of related-result returns. Rewrite |
| 2408 | // the operand. |
| 2409 | } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { |
| 2410 | llvm::Value *operand = bitcast->getOperand(0); |
| 2411 | operand = emitARCRetainAfterCall(CGF, operand); |
| 2412 | bitcast->setOperand(0, operand); |
| 2413 | return bitcast; |
| 2414 | |
| 2415 | // Generic fall-back case. |
| 2416 | } else { |
| 2417 | // Retain using the non-block variant: we never need to do a copy |
| 2418 | // of a block that's been returned to us. |
| 2419 | return CGF.EmitARCRetainNonBlock(value); |
| 2420 | } |
| 2421 | } |
| 2422 | |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2423 | /// Determine whether it might be important to emit a separate |
| 2424 | /// objc_retain_block on the result of the given expression, or |
| 2425 | /// whether it's okay to just emit it in a +1 context. |
| 2426 | static bool shouldEmitSeparateBlockRetain(const Expr *e) { |
| 2427 | assert(e->getType()->isBlockPointerType()); |
| 2428 | e = e->IgnoreParens(); |
| 2429 | |
| 2430 | // For future goodness, emit block expressions directly in +1 |
| 2431 | // contexts if we can. |
| 2432 | if (isa<BlockExpr>(e)) |
| 2433 | return false; |
| 2434 | |
| 2435 | if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 2436 | switch (cast->getCastKind()) { |
| 2437 | // Emitting these operations in +1 contexts is goodness. |
| 2438 | case CK_LValueToRValue: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2439 | case CK_ARCReclaimReturnedObject: |
| 2440 | case CK_ARCConsumeObject: |
| 2441 | case CK_ARCProduceObject: |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2442 | return false; |
| 2443 | |
| 2444 | // These operations preserve a block type. |
| 2445 | case CK_NoOp: |
| 2446 | case CK_BitCast: |
| 2447 | return shouldEmitSeparateBlockRetain(cast->getSubExpr()); |
| 2448 | |
| 2449 | // These operations are known to be bad (or haven't been considered). |
| 2450 | case CK_AnyPointerToBlockPointerCast: |
| 2451 | default: |
| 2452 | return true; |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | return true; |
| 2457 | } |
| 2458 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2459 | /// Try to emit a PseudoObjectExpr at +1. |
| 2460 | /// |
| 2461 | /// This massively duplicates emitPseudoObjectRValue. |
| 2462 | static TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF, |
| 2463 | const PseudoObjectExpr *E) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2464 | SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2465 | |
| 2466 | // Find the result expression. |
| 2467 | const Expr *resultExpr = E->getResultExpr(); |
| 2468 | assert(resultExpr); |
| 2469 | TryEmitResult result; |
| 2470 | |
| 2471 | for (PseudoObjectExpr::const_semantics_iterator |
| 2472 | i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { |
| 2473 | const Expr *semantic = *i; |
| 2474 | |
| 2475 | // If this semantic expression is an opaque value, bind it |
| 2476 | // to the result of its source expression. |
| 2477 | if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { |
| 2478 | typedef CodeGenFunction::OpaqueValueMappingData OVMA; |
| 2479 | OVMA opaqueData; |
| 2480 | |
| 2481 | // If this semantic is the result of the pseudo-object |
| 2482 | // expression, try to evaluate the source as +1. |
| 2483 | if (ov == resultExpr) { |
| 2484 | assert(!OVMA::shouldBindAsLValue(ov)); |
| 2485 | result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr()); |
| 2486 | opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer())); |
| 2487 | |
| 2488 | // Otherwise, just bind it. |
| 2489 | } else { |
| 2490 | opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); |
| 2491 | } |
| 2492 | opaques.push_back(opaqueData); |
| 2493 | |
| 2494 | // Otherwise, if the expression is the result, evaluate it |
| 2495 | // and remember the result. |
| 2496 | } else if (semantic == resultExpr) { |
| 2497 | result = tryEmitARCRetainScalarExpr(CGF, semantic); |
| 2498 | |
| 2499 | // Otherwise, evaluate the expression in an ignored context. |
| 2500 | } else { |
| 2501 | CGF.EmitIgnoredExpr(semantic); |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | // Unbind all the opaques now. |
| 2506 | for (unsigned i = 0, e = opaques.size(); i != e; ++i) |
| 2507 | opaques[i].unbind(CGF); |
| 2508 | |
| 2509 | return result; |
| 2510 | } |
| 2511 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2512 | static TryEmitResult |
| 2513 | tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { |
John McCall | 72dcecc | 2013-02-12 00:25:08 +0000 | [diff] [blame] | 2514 | // We should *never* see a nested full-expression here, because if |
| 2515 | // we fail to emit at +1, our caller must not retain after we close |
| 2516 | // out the full-expression. |
| 2517 | assert(!isa<ExprWithCleanups>(e)); |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 2518 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2519 | // The desired result type, if it differs from the type of the |
| 2520 | // ultimate opaque expression. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2521 | llvm::Type *resultType = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2522 | |
| 2523 | while (true) { |
| 2524 | e = e->IgnoreParens(); |
| 2525 | |
| 2526 | // There's a break at the end of this if-chain; anything |
| 2527 | // that wants to keep looping has to explicitly continue. |
| 2528 | if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 2529 | switch (ce->getCastKind()) { |
| 2530 | // No-op casts don't change the type, so we just ignore them. |
| 2531 | case CK_NoOp: |
| 2532 | e = ce->getSubExpr(); |
| 2533 | continue; |
| 2534 | |
| 2535 | case CK_LValueToRValue: { |
| 2536 | TryEmitResult loadResult |
| 2537 | = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr()); |
| 2538 | if (resultType) { |
| 2539 | llvm::Value *value = loadResult.getPointer(); |
| 2540 | value = CGF.Builder.CreateBitCast(value, resultType); |
| 2541 | loadResult.setPointer(value); |
| 2542 | } |
| 2543 | return loadResult; |
| 2544 | } |
| 2545 | |
| 2546 | // These casts can change the type, so remember that and |
| 2547 | // soldier on. We only need to remember the outermost such |
| 2548 | // cast, though. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2549 | case CK_CPointerToObjCPointerCast: |
| 2550 | case CK_BlockPointerToObjCPointerCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2551 | case CK_AnyPointerToBlockPointerCast: |
| 2552 | case CK_BitCast: |
| 2553 | if (!resultType) |
| 2554 | resultType = CGF.ConvertType(ce->getType()); |
| 2555 | e = ce->getSubExpr(); |
| 2556 | assert(e->getType()->hasPointerRepresentation()); |
| 2557 | continue; |
| 2558 | |
| 2559 | // For consumptions, just emit the subexpression and thus elide |
| 2560 | // the retain/release pair. |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2561 | case CK_ARCConsumeObject: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2562 | llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr()); |
| 2563 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 2564 | return TryEmitResult(result, true); |
| 2565 | } |
| 2566 | |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2567 | // Block extends are net +0. Naively, we could just recurse on |
| 2568 | // the subexpression, but actually we need to ensure that the |
| 2569 | // 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] | 2570 | case CK_ARCExtendBlockObject: { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2571 | llvm::Value *result; // will be a +0 value |
| 2572 | |
| 2573 | // If we can't safely assume the sub-expression will produce a |
| 2574 | // block-copied value, emit the sub-expression at +0. |
| 2575 | if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) { |
| 2576 | result = CGF.EmitScalarExpr(ce->getSubExpr()); |
| 2577 | |
| 2578 | // Otherwise, try to emit the sub-expression at +1 recursively. |
| 2579 | } else { |
| 2580 | TryEmitResult subresult |
| 2581 | = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr()); |
| 2582 | result = subresult.getPointer(); |
| 2583 | |
| 2584 | // If that produced a retained value, just use that, |
| 2585 | // possibly casting down. |
| 2586 | if (subresult.getInt()) { |
| 2587 | if (resultType) |
| 2588 | result = CGF.Builder.CreateBitCast(result, resultType); |
| 2589 | return TryEmitResult(result, true); |
| 2590 | } |
| 2591 | |
| 2592 | // Otherwise it's +0. |
| 2593 | } |
| 2594 | |
| 2595 | // Retain the object as a block, then cast down. |
John McCall | 348f16f | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 2596 | result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true); |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2597 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 2598 | return TryEmitResult(result, true); |
| 2599 | } |
| 2600 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 2601 | // For reclaims, emit the subexpression as a retained call and |
| 2602 | // skip the consumption. |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2603 | case CK_ARCReclaimReturnedObject: { |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 2604 | llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr()); |
| 2605 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 2606 | return TryEmitResult(result, true); |
| 2607 | } |
| 2608 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2609 | default: |
| 2610 | break; |
| 2611 | } |
| 2612 | |
| 2613 | // Skip __extension__. |
| 2614 | } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 2615 | if (op->getOpcode() == UO_Extension) { |
| 2616 | e = op->getSubExpr(); |
| 2617 | continue; |
| 2618 | } |
| 2619 | |
| 2620 | // For calls and message sends, use the retained-call logic. |
| 2621 | // Delegate inits are a special case in that they're the only |
| 2622 | // returns-retained expression that *isn't* surrounded by |
| 2623 | // a consume. |
| 2624 | } else if (isa<CallExpr>(e) || |
| 2625 | (isa<ObjCMessageExpr>(e) && |
| 2626 | !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { |
| 2627 | llvm::Value *result = emitARCRetainCall(CGF, e); |
| 2628 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 2629 | return TryEmitResult(result, true); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2630 | |
| 2631 | // Look through pseudo-object expressions. |
| 2632 | } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 2633 | TryEmitResult result |
| 2634 | = tryEmitARCRetainPseudoObject(CGF, pseudo); |
| 2635 | if (resultType) { |
| 2636 | llvm::Value *value = result.getPointer(); |
| 2637 | value = CGF.Builder.CreateBitCast(value, resultType); |
| 2638 | result.setPointer(value); |
| 2639 | } |
| 2640 | return result; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | // Conservatively halt the search at any other expression kind. |
| 2644 | break; |
| 2645 | } |
| 2646 | |
| 2647 | // We didn't find an obvious production, so emit what we've got and |
| 2648 | // tell the caller that we didn't manage to retain. |
| 2649 | llvm::Value *result = CGF.EmitScalarExpr(e); |
| 2650 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 2651 | return TryEmitResult(result, false); |
| 2652 | } |
| 2653 | |
| 2654 | static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
| 2655 | LValue lvalue, |
| 2656 | QualType type) { |
| 2657 | TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); |
| 2658 | llvm::Value *value = result.getPointer(); |
| 2659 | if (!result.getInt()) |
| 2660 | value = CGF.EmitARCRetain(type, value); |
| 2661 | return value; |
| 2662 | } |
| 2663 | |
| 2664 | /// EmitARCRetainScalarExpr - Semantically equivalent to |
| 2665 | /// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a |
| 2666 | /// best-effort attempt to peephole expressions that naturally produce |
| 2667 | /// retained objects. |
| 2668 | llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { |
John McCall | 72dcecc | 2013-02-12 00:25:08 +0000 | [diff] [blame] | 2669 | // The retain needs to happen within the full-expression. |
| 2670 | if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { |
| 2671 | enterFullExpression(cleanups); |
| 2672 | RunCleanupsScope scope(*this); |
| 2673 | return EmitARCRetainScalarExpr(cleanups->getSubExpr()); |
| 2674 | } |
| 2675 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2676 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); |
| 2677 | llvm::Value *value = result.getPointer(); |
| 2678 | if (!result.getInt()) |
| 2679 | value = EmitARCRetain(e->getType(), value); |
| 2680 | return value; |
| 2681 | } |
| 2682 | |
| 2683 | llvm::Value * |
| 2684 | CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { |
John McCall | 72dcecc | 2013-02-12 00:25:08 +0000 | [diff] [blame] | 2685 | // The retain needs to happen within the full-expression. |
| 2686 | if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { |
| 2687 | enterFullExpression(cleanups); |
| 2688 | RunCleanupsScope scope(*this); |
| 2689 | return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr()); |
| 2690 | } |
| 2691 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2692 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); |
| 2693 | llvm::Value *value = result.getPointer(); |
| 2694 | if (result.getInt()) |
| 2695 | value = EmitARCAutorelease(value); |
| 2696 | else |
| 2697 | value = EmitARCRetainAutorelease(e->getType(), value); |
| 2698 | return value; |
| 2699 | } |
| 2700 | |
John McCall | 348f16f | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 2701 | llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { |
| 2702 | llvm::Value *result; |
| 2703 | bool doRetain; |
| 2704 | |
| 2705 | if (shouldEmitSeparateBlockRetain(e)) { |
| 2706 | result = EmitScalarExpr(e); |
| 2707 | doRetain = true; |
| 2708 | } else { |
| 2709 | TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); |
| 2710 | result = subresult.getPointer(); |
| 2711 | doRetain = !subresult.getInt(); |
| 2712 | } |
| 2713 | |
| 2714 | if (doRetain) |
| 2715 | result = EmitARCRetainBlock(result, /*mandatory*/ true); |
| 2716 | return EmitObjCConsumeObject(e->getType(), result); |
| 2717 | } |
| 2718 | |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2719 | llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { |
| 2720 | // In ARC, retain and autorelease the expression. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2721 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2722 | // Do so before running any cleanups for the full-expression. |
John McCall | 72dcecc | 2013-02-12 00:25:08 +0000 | [diff] [blame] | 2723 | // EmitARCRetainAutoreleaseScalarExpr does this for us. |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2724 | return EmitARCRetainAutoreleaseScalarExpr(expr); |
| 2725 | } |
| 2726 | |
| 2727 | // Otherwise, use the normal scalar-expression emission. The |
| 2728 | // exception machinery doesn't do anything special with the |
| 2729 | // exception like retaining it, so there's no safety associated with |
| 2730 | // only running cleanups after the throw has started, and when it |
| 2731 | // matters it tends to be substantially inferior code. |
| 2732 | return EmitScalarExpr(expr); |
| 2733 | } |
| 2734 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2735 | std::pair<LValue,llvm::Value*> |
| 2736 | CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, |
| 2737 | bool ignored) { |
| 2738 | // Evaluate the RHS first. |
| 2739 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); |
| 2740 | llvm::Value *value = result.getPointer(); |
| 2741 | |
John McCall | fb72081 | 2011-07-28 07:23:35 +0000 | [diff] [blame] | 2742 | bool hasImmediateRetain = result.getInt(); |
| 2743 | |
| 2744 | // If we didn't emit a retained object, and the l-value is of block |
| 2745 | // type, then we need to emit the block-retain immediately in case |
| 2746 | // it invalidates the l-value. |
| 2747 | if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { |
John McCall | 348f16f | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 2748 | value = EmitARCRetainBlock(value, /*mandatory*/ false); |
John McCall | fb72081 | 2011-07-28 07:23:35 +0000 | [diff] [blame] | 2749 | hasImmediateRetain = true; |
| 2750 | } |
| 2751 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2752 | LValue lvalue = EmitLValue(e->getLHS()); |
| 2753 | |
| 2754 | // If the RHS was emitted retained, expand this. |
John McCall | fb72081 | 2011-07-28 07:23:35 +0000 | [diff] [blame] | 2755 | if (hasImmediateRetain) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2756 | llvm::Value *oldValue = |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2757 | EmitLoadOfScalar(lvalue); |
| 2758 | EmitStoreOfScalar(value, lvalue); |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2759 | EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2760 | } else { |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2761 | value = EmitARCStoreStrong(lvalue, value, ignored); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | return std::pair<LValue,llvm::Value*>(lvalue, value); |
| 2765 | } |
| 2766 | |
| 2767 | std::pair<LValue,llvm::Value*> |
| 2768 | CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { |
| 2769 | llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); |
| 2770 | LValue lvalue = EmitLValue(e->getLHS()); |
| 2771 | |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2772 | EmitStoreOfScalar(value, lvalue); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2773 | |
| 2774 | return std::pair<LValue,llvm::Value*>(lvalue, value); |
| 2775 | } |
| 2776 | |
| 2777 | void CodeGenFunction::EmitObjCAutoreleasePoolStmt( |
Eric Christopher | 16098f3 | 2012-03-29 17:31:31 +0000 | [diff] [blame] | 2778 | const ObjCAutoreleasePoolStmt &ARPS) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2779 | const Stmt *subStmt = ARPS.getSubStmt(); |
| 2780 | const CompoundStmt &S = cast<CompoundStmt>(*subStmt); |
| 2781 | |
| 2782 | CGDebugInfo *DI = getDebugInfo(); |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2783 | if (DI) |
| 2784 | DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2785 | |
| 2786 | // Keep track of the current cleanup stack depth. |
| 2787 | RunCleanupsScope Scope(*this); |
John McCall | 0a7dd78 | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2788 | if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2789 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); |
| 2790 | EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); |
| 2791 | } else { |
| 2792 | llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); |
| 2793 | EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); |
| 2794 | } |
| 2795 | |
| 2796 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 2797 | E = S.body_end(); I != E; ++I) |
| 2798 | EmitStmt(*I); |
| 2799 | |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2800 | if (DI) |
| 2801 | DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2802 | } |
John McCall | 0c24c80 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2803 | |
| 2804 | /// EmitExtendGCLifetime - Given a pointer to an Objective-C object, |
| 2805 | /// make sure it survives garbage collection until this point. |
| 2806 | void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { |
| 2807 | // We just use an inline assembly. |
John McCall | 0c24c80 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2808 | llvm::FunctionType *extenderType |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2809 | = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All); |
John McCall | 0c24c80 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2810 | llvm::Value *extender |
| 2811 | = llvm::InlineAsm::get(extenderType, |
| 2812 | /* assembly */ "", |
| 2813 | /* constraints */ "r", |
| 2814 | /* side effects */ true); |
| 2815 | |
| 2816 | object = Builder.CreateBitCast(object, VoidPtrTy); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2817 | EmitNounwindRuntimeCall(extender, object); |
John McCall | 0c24c80 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2820 | /// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2821 | /// non-trivial copy assignment function, produce following helper function. |
| 2822 | /// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; } |
| 2823 | /// |
| 2824 | llvm::Constant * |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2825 | CodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction( |
| 2826 | const ObjCPropertyImplDecl *PID) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2827 | if (!getLangOpts().CPlusPlus || |
Rafael Espindola | 90f6926 | 2012-12-18 04:29:34 +0000 | [diff] [blame] | 2828 | !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2829 | return 0; |
| 2830 | QualType Ty = PID->getPropertyIvarDecl()->getType(); |
| 2831 | if (!Ty->isRecordType()) |
| 2832 | return 0; |
| 2833 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2834 | if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2835 | return 0; |
Fariborz Jahanian | b08cfb3 | 2012-01-08 19:13:23 +0000 | [diff] [blame] | 2836 | llvm::Constant * HelperFn = 0; |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2837 | if (hasTrivialSetExpr(PID)) |
| 2838 | return 0; |
| 2839 | assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null"); |
| 2840 | if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty))) |
| 2841 | return HelperFn; |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2842 | |
| 2843 | ASTContext &C = getContext(); |
| 2844 | IdentifierInfo *II |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2845 | = &CGM.getContext().Idents.get("__assign_helper_atomic_property_"); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2846 | FunctionDecl *FD = FunctionDecl::Create(C, |
| 2847 | C.getTranslationUnitDecl(), |
| 2848 | SourceLocation(), |
| 2849 | SourceLocation(), II, C.VoidTy, 0, |
| 2850 | SC_Static, |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2851 | false, |
Eric Christopher | b92bd4b | 2012-04-12 02:16:49 +0000 | [diff] [blame] | 2852 | false); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2853 | |
| 2854 | QualType DestTy = C.getPointerType(Ty); |
| 2855 | QualType SrcTy = Ty; |
| 2856 | SrcTy.addConst(); |
| 2857 | SrcTy = C.getPointerType(SrcTy); |
| 2858 | |
| 2859 | FunctionArgList args; |
| 2860 | ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); |
| 2861 | args.push_back(&dstDecl); |
| 2862 | ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); |
| 2863 | args.push_back(&srcDecl); |
| 2864 | |
| 2865 | const CGFunctionInfo &FI = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2866 | CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, |
| 2867 | FunctionType::ExtInfo(), |
| 2868 | RequiredArgs::All); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2869 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2870 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2871 | |
| 2872 | llvm::Function *Fn = |
| 2873 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Eric Christopher | 16098f3 | 2012-03-29 17:31:31 +0000 | [diff] [blame] | 2874 | "__assign_helper_atomic_property_", |
| 2875 | &CGM.getModule()); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2876 | |
Alexey Samsonov | a240df2 | 2012-10-16 07:22:28 +0000 | [diff] [blame] | 2877 | // Initialize debug info if needed. |
| 2878 | maybeInitializeDebugInfo(); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2879 | |
| 2880 | StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); |
| 2881 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2882 | DeclRefExpr DstExpr(&dstDecl, false, DestTy, |
| 2883 | VK_RValue, SourceLocation()); |
| 2884 | UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(), |
| 2885 | VK_LValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2886 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2887 | DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, |
| 2888 | VK_RValue, SourceLocation()); |
| 2889 | UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), |
| 2890 | VK_LValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2891 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2892 | Expr *Args[2] = { &DST, &SRC }; |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2893 | CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2894 | CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2895 | Args, DestTy->getPointeeType(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 2896 | VK_LValue, SourceLocation(), false); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2897 | |
| 2898 | EmitStmt(&TheCall); |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2899 | |
| 2900 | FinishFunction(); |
Fariborz Jahanian | cd93b96 | 2012-01-06 22:33:54 +0000 | [diff] [blame] | 2901 | HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2902 | CGM.setAtomicSetterHelperFnMap(Ty, HelperFn); |
Fariborz Jahanian | cd93b96 | 2012-01-06 22:33:54 +0000 | [diff] [blame] | 2903 | return HelperFn; |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | llvm::Constant * |
| 2907 | CodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction( |
| 2908 | const ObjCPropertyImplDecl *PID) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2909 | if (!getLangOpts().CPlusPlus || |
Rafael Espindola | 90f6926 | 2012-12-18 04:29:34 +0000 | [diff] [blame] | 2910 | !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2911 | return 0; |
| 2912 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2913 | QualType Ty = PD->getType(); |
| 2914 | if (!Ty->isRecordType()) |
| 2915 | return 0; |
| 2916 | if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) |
| 2917 | return 0; |
| 2918 | llvm::Constant * HelperFn = 0; |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 2919 | |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2920 | if (hasTrivialGetExpr(PID)) |
| 2921 | return 0; |
| 2922 | assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null"); |
| 2923 | if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty))) |
| 2924 | return HelperFn; |
| 2925 | |
| 2926 | |
| 2927 | ASTContext &C = getContext(); |
| 2928 | IdentifierInfo *II |
| 2929 | = &CGM.getContext().Idents.get("__copy_helper_atomic_property_"); |
| 2930 | FunctionDecl *FD = FunctionDecl::Create(C, |
| 2931 | C.getTranslationUnitDecl(), |
| 2932 | SourceLocation(), |
| 2933 | SourceLocation(), II, C.VoidTy, 0, |
| 2934 | SC_Static, |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2935 | false, |
Eric Christopher | b92bd4b | 2012-04-12 02:16:49 +0000 | [diff] [blame] | 2936 | false); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2937 | |
| 2938 | QualType DestTy = C.getPointerType(Ty); |
| 2939 | QualType SrcTy = Ty; |
| 2940 | SrcTy.addConst(); |
| 2941 | SrcTy = C.getPointerType(SrcTy); |
| 2942 | |
| 2943 | FunctionArgList args; |
| 2944 | ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); |
| 2945 | args.push_back(&dstDecl); |
| 2946 | ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); |
| 2947 | args.push_back(&srcDecl); |
| 2948 | |
| 2949 | const CGFunctionInfo &FI = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2950 | CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, |
| 2951 | FunctionType::ExtInfo(), |
| 2952 | RequiredArgs::All); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2953 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2954 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2955 | |
| 2956 | llvm::Function *Fn = |
| 2957 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 2958 | "__copy_helper_atomic_property_", &CGM.getModule()); |
| 2959 | |
Alexey Samsonov | a240df2 | 2012-10-16 07:22:28 +0000 | [diff] [blame] | 2960 | // Initialize debug info if needed. |
| 2961 | maybeInitializeDebugInfo(); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2962 | |
| 2963 | StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); |
| 2964 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2965 | DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2966 | VK_RValue, SourceLocation()); |
| 2967 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2968 | UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), |
| 2969 | VK_LValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2970 | |
| 2971 | CXXConstructExpr *CXXConstExpr = |
| 2972 | cast<CXXConstructExpr>(PID->getGetterCXXConstructor()); |
| 2973 | |
| 2974 | SmallVector<Expr*, 4> ConstructorArgs; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2975 | ConstructorArgs.push_back(&SRC); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2976 | CXXConstructExpr::arg_iterator A = CXXConstExpr->arg_begin(); |
| 2977 | ++A; |
| 2978 | |
| 2979 | for (CXXConstructExpr::arg_iterator AEnd = CXXConstExpr->arg_end(); |
| 2980 | A != AEnd; ++A) |
| 2981 | ConstructorArgs.push_back(*A); |
| 2982 | |
| 2983 | CXXConstructExpr *TheCXXConstructExpr = |
| 2984 | CXXConstructExpr::Create(C, Ty, SourceLocation(), |
| 2985 | CXXConstExpr->getConstructor(), |
| 2986 | CXXConstExpr->isElidable(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2987 | ConstructorArgs, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2988 | CXXConstExpr->hadMultipleCandidates(), |
| 2989 | CXXConstExpr->isListInitialization(), |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2990 | CXXConstExpr->requiresZeroInitialization(), |
Eric Christopher | 16098f3 | 2012-03-29 17:31:31 +0000 | [diff] [blame] | 2991 | CXXConstExpr->getConstructionKind(), |
| 2992 | SourceRange()); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2993 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2994 | DeclRefExpr DstExpr(&dstDecl, false, DestTy, |
| 2995 | VK_RValue, SourceLocation()); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 2996 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2997 | RValue DV = EmitAnyExpr(&DstExpr); |
Eric Christopher | 16098f3 | 2012-03-29 17:31:31 +0000 | [diff] [blame] | 2998 | CharUnits Alignment |
| 2999 | = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType()); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 3000 | EmitAggExpr(TheCXXConstructExpr, |
| 3001 | AggValueSlot::forAddr(DV.getScalarVal(), Alignment, Qualifiers(), |
| 3002 | AggValueSlot::IsDestructed, |
| 3003 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 3004 | AggValueSlot::IsNotAliased)); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 3005 | |
| 3006 | FinishFunction(); |
| 3007 | HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
| 3008 | CGM.setAtomicGetterHelperFnMap(Ty, HelperFn); |
| 3009 | return HelperFn; |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
Eli Friedman | cae40c4 | 2012-02-28 01:08:45 +0000 | [diff] [blame] | 3012 | llvm::Value * |
| 3013 | CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { |
| 3014 | // Get selectors for retain/autorelease. |
Eli Friedman | 8c72a7d | 2012-03-01 22:52:28 +0000 | [diff] [blame] | 3015 | IdentifierInfo *CopyID = &getContext().Idents.get("copy"); |
| 3016 | Selector CopySelector = |
| 3017 | getContext().Selectors.getNullarySelector(CopyID); |
Eli Friedman | cae40c4 | 2012-02-28 01:08:45 +0000 | [diff] [blame] | 3018 | IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease"); |
| 3019 | Selector AutoreleaseSelector = |
| 3020 | getContext().Selectors.getNullarySelector(AutoreleaseID); |
| 3021 | |
| 3022 | // Emit calls to retain/autorelease. |
| 3023 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
| 3024 | llvm::Value *Val = Block; |
| 3025 | RValue Result; |
| 3026 | Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
Eli Friedman | 8c72a7d | 2012-03-01 22:52:28 +0000 | [diff] [blame] | 3027 | Ty, CopySelector, |
Eli Friedman | cae40c4 | 2012-02-28 01:08:45 +0000 | [diff] [blame] | 3028 | Val, CallArgList(), 0, 0); |
| 3029 | Val = Result.getScalarVal(); |
| 3030 | Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
| 3031 | Ty, AutoreleaseSelector, |
| 3032 | Val, CallArgList(), 0, 0); |
| 3033 | Val = Result.getScalarVal(); |
| 3034 | return Val; |
| 3035 | } |
| 3036 | |
Fariborz Jahanian | 84e4986 | 2012-01-06 00:29:35 +0000 | [diff] [blame] | 3037 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 3038 | CGObjCRuntime::~CGObjCRuntime() {} |