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