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