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