Coverage Report

Created: 2021-01-23 06:44

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