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