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