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