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