Coverage Report

Created: 2021-09-21 08:58

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