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