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