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