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