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