Coverage Report

Created: 2022-01-18 06:27

/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.3k
                                   RValue Result) {
271
12.3k
  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.66k
    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()927
) {
389
        // [Foo alloc] -> objc_alloc(Foo) or
390
        // [self alloc] -> objc_alloc(self)
391
927
        if (Sel.isUnarySelector() && 
Sel.getNameForSlot(0) == "alloc"890
)
392
886
          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
196
  case OMF_release:
422
196
    if (ResultType->isVoidType() &&
423
196
        
CGM.getLangOpts().getGC() == LangOptions::NonGC195
&&
424
196
        
Runtime.shouldUseARCFunctionsForRetainRelease()194
) {
425
186
      CGF.EmitObjCRelease(Receiver, ARCPreciseLifetime);
426
186
      return nullptr;
427
186
    }
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.36k
                                      ObjCProtocolDecl::protocol_iterator end) {
466
2.36k
  std::vector<const ObjCProtocolDecl *> RuntimePds;
467
2.36k
  llvm::DenseSet<const ObjCProtocolDecl *> NonRuntimePDs;
468
469
2.49k
  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.36k
  if (NonRuntimePDs.empty())
480
2.34k
    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.36k
}
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.98k
    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.17k
||
!Sel.isUnarySelector()2.32k
||
532
10.3k
      
Sel.getNameForSlot(0) != "init"1.18k
)
533
9.93k
    return None;
534
535
  // Okay, this is '[receiver init]', check if 'receiver' is '[cls alloc]'
536
  // with 'cls' a Class.
537
374
  auto *SubOME =
538
374
      dyn_cast<ObjCMessageExpr>(OME->getInstanceReceiver()->IgnoreParenCasts());
539
374
  if (!SubOME)
540
0
    return None;
541
374
  Selector SubSel = SubOME->getSelector();
542
543
374
  if (!SubOME->getType()->isObjCObjectPointerType() ||
544
374
      !SubSel.isUnarySelector() || SubSel.getNameForSlot(0) != "alloc")
545
0
    return None;
546
547
374
  llvm::Value *Receiver = nullptr;
548
374
  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
368
  case ObjCMessageExpr::Class: {
556
368
    QualType ReceiverType = SubOME->getClassReceiver();
557
368
    const ObjCObjectType *ObjTy = ReceiverType->castAs<ObjCObjectType>();
558
368
    const ObjCInterfaceDecl *ID = ObjTy->getInterface();
559
368
    assert(ID && "null interface should be impossible here");
560
0
    Receiver = CGF.CGM.getObjCRuntime().GetClass(CGF, ID);
561
368
    break;
562
6
  }
563
0
  case ObjCMessageExpr::SuperInstance:
564
0
  case ObjCMessageExpr::SuperClass:
565
0
    return None;
566
374
  }
567
568
372
  return CGF.EmitObjCAllocInit(Receiver, CGF.ConvertType(OME->getType()));
569
374
}
570
571
RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
572
12.3k
                                            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.3k
  bool isDelegateInit = E->isDelegateInitCall();
578
579
12.3k
  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.3k
  if (method && 
E->getReceiverKind() == ObjCMessageExpr::Instance11.7k
&&
584
12.3k
      
method->getMethodFamily() == OMF_retain7.09k
) {
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
372
    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
     
method234
&&
603
11.9k
     
method->hasAttr<NSConsumesSelfAttr>()234
);
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.29k
  case ObjCMessageExpr::Instance:
614
7.29k
    ReceiverType = E->getInstanceReceiver()->getType();
615
7.29k
    isClassMessage = ReceiverType->isObjCClassType();
616
7.29k
    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.28k
      Receiver = EmitScalarExpr(E->getInstanceReceiver());
623
7.29k
    break;
624
625
4.14k
  case ObjCMessageExpr::Class: {
626
4.14k
    ReceiverType = E->getClassReceiver();
627
4.14k
    OID = ReceiverType->castAs<ObjCObjectType>()->getInterface();
628
4.14k
    assert(OID && "Invalid Objective-C class message send");
629
0
    Receiver = Runtime.GetClass(*this, OID);
630
4.14k
    isClassMessage = true;
631
4.14k
    break;
632
0
  }
633
634
443
  case ObjCMessageExpr::SuperInstance:
635
443
    ReceiverType = E->getSuperType();
636
443
    Receiver = LoadObjCSelf();
637
443
    isSuperMessage = true;
638
443
    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 && 
method243
&&
655
11.9k
      
method->hasAttr<ObjCReturnsInnerPointerAttr>()243
&&
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()597
;
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
485
    const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
685
485
    bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
686
485
    result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
687
485
                                              E->getSelector(),
688
485
                                              OMD->getClassInterface(),
689
485
                                              isCategoryImpl,
690
485
                                              Receiver,
691
485
                                              isClassMessage,
692
485
                                              Args,
693
485
                                              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.58k
                                      const ObjCContainerDecl *CD) {
752
2.58k
  SourceLocation StartLoc = OMD->getBeginLoc();
753
2.58k
  FunctionArgList args;
754
  // Check if we should generate debug info for this method.
755
2.58k
  if (OMD->hasAttr<NoDebugAttr>())
756
0
    DebugInfo = nullptr; // disable debug info indefinitely for this function
757
758
2.58k
  llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
759
760
2.58k
  const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD);
761
2.58k
  if (OMD->isDirectMethod()) {
762
40
    Fn->setVisibility(llvm::Function::HiddenVisibility);
763
40
    CGM.SetLLVMFunctionAttributes(OMD, FI, Fn, /*IsThunk=*/false);
764
40
    CGM.SetLLVMFunctionAttributesForDefinition(OMD, Fn);
765
2.54k
  } else {
766
2.54k
    CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
767
2.54k
  }
768
769
2.58k
  args.push_back(OMD->getSelfDecl());
770
2.58k
  args.push_back(OMD->getCmdDecl());
771
772
2.58k
  args.append(OMD->param_begin(), OMD->param_end());
773
774
2.58k
  CurGD = OMD;
775
2.58k
  CurEHLocation = OMD->getEndLoc();
776
777
2.58k
  StartFunction(OMD, OMD->getReturnType(), Fn, FI, args,
778
2.58k
                OMD->getLocation(), StartLoc);
779
780
2.58k
  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
40
    CGM.getObjCRuntime().GenerateDirectMethodPrologue(*this, Fn, OMD, CD);
786
40
  }
787
788
  // In ARC, certain methods get an extra cleanup.
789
2.58k
  if (CGM.getLangOpts().ObjCAutoRefCount &&
790
2.58k
      
OMD->isInstanceMethod()299
&&
791
2.58k
      
OMD->getSelector().isUnarySelector()277
) {
792
194
    const IdentifierInfo *ident =
793
194
      OMD->getSelector().getIdentifierInfoForSlot(0);
794
194
    if (ident->isStr("dealloc"))
795
8
      EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
796
194
  }
797
2.58k
}
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.59k
void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
805
1.59k
  StartObjCMethod(OMD, OMD->getClassInterface());
806
1.59k
  PGO.assignRegionCounters(GlobalDecl(OMD), CurFn);
807
1.59k
  assert(isa<CompoundStmt>(OMD->getBody()));
808
0
  incrementProfileCounter(OMD->getBody());
809
1.59k
  EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody()));
810
1.59k
  FinishFunction(OMD->getBodyRBrace());
811
1.59k
}
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 false;
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
15
  } else if (argLoad.getType()->isAtomicType() &&
1559
15
             
!ivarRef.getType()->isAtomicType()0
) {
1560
0
    argCK = CK_AtomicToNonAtomic;
1561
15
  } else if (!argLoad.getType()->isAtomicType() &&
1562
15
             ivarRef.getType()->isAtomicType()) {
1563
1
    argCK = CK_NonAtomicToAtomic;
1564
1
  }
1565
49
  ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, ivarRef.getType(), argCK,
1566
49
                           &argLoad, VK_PRValue, FPOptionsOverride());
1567
49
  Expr *finalArg = &argLoad;
1568
49
  if (!getContext().hasSameUnqualifiedType(ivarRef.getType(),
1569
49
                                           argLoad.getType()))
1570
3
    finalArg = &argCast;
1571
1572
49
  BinaryOperator *assign = BinaryOperator::Create(
1573
49
      getContext(), &ivarRef, finalArg, BO_Assign, ivarRef.getType(),
1574
49
      VK_PRValue, OK_Ordinary, SourceLocation(), FPOptionsOverride());
1575
49
  EmitStmt(assign);
1576
49
}
1577
1578
/// Generate an Objective-C property setter function.
1579
///
1580
/// The given Decl must be an ObjCImplementationDecl. \@synthesize
1581
/// is illegal within a category.
1582
void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
1583
440
                                         const ObjCPropertyImplDecl *PID) {
1584
440
  llvm::Constant *AtomicHelperFn =
1585
440
      CodeGenFunction(CGM).GenerateObjCAtomicSetterCopyHelperFunction(PID);
1586
440
  ObjCMethodDecl *OMD = PID->getSetterMethodDecl();
1587
440
  assert(OMD && "Invalid call to generate setter (empty method)");
1588
0
  StartObjCMethod(OMD, IMP->getClassInterface());
1589
1590
440
  generateObjCSetterBody(IMP, PID, AtomicHelperFn);
1591
1592
440
  FinishFunction(OMD->getEndLoc());
1593
440
}
1594
1595
namespace {
1596
  struct DestroyIvar final : EHScopeStack::Cleanup {
1597
  private:
1598
    llvm::Value *addr;
1599
    const ObjCIvarDecl *ivar;
1600
    CodeGenFunction::Destroyer *destroyer;
1601
    bool useEHCleanupForArray;
1602
  public:
1603
    DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
1604
                CodeGenFunction::Destroyer *destroyer,
1605
                bool useEHCleanupForArray)
1606
      : addr(addr), ivar(ivar), destroyer(destroyer),
1607
111
        useEHCleanupForArray(useEHCleanupForArray) {}
1608
1609
111
    void Emit(CodeGenFunction &CGF, Flags flags) override {
1610
111
      LValue lvalue
1611
111
        = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
1612
111
      CGF.emitDestroy(lvalue.getAddress(CGF), ivar->getType(), destroyer,
1613
111
                      flags.isForNormalCleanup() && useEHCleanupForArray);
1614
111
    }
1615
  };
1616
}
1617
1618
/// Like CodeGenFunction::destroyARCStrong, but do it with a call.
1619
static void destroyARCStrongWithStore(CodeGenFunction &CGF,
1620
                                      Address addr,
1621
64
                                      QualType type) {
1622
64
  llvm::Value *null = getNullForVariable(addr);
1623
64
  CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
1624
64
}
1625
1626
static void emitCXXDestructMethod(CodeGenFunction &CGF,
1627
75
                                  ObjCImplementationDecl *impl) {
1628
75
  CodeGenFunction::RunCleanupsScope scope(CGF);
1629
1630
75
  llvm::Value *self = CGF.LoadObjCSelf();
1631
1632
75
  const ObjCInterfaceDecl *iface = impl->getClassInterface();
1633
75
  for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
1634
207
       ivar; 
ivar = ivar->getNextIvar()132
) {
1635
132
    QualType type = ivar->getType();
1636
1637
    // Check whether the ivar is a destructible type.
1638
132
    QualType::DestructionKind dtorKind = type.isDestructedType();
1639
132
    if (!dtorKind) 
continue21
;
1640
1641
111
    CodeGenFunction::Destroyer *destroyer = nullptr;
1642
1643
    // Use a call to objc_storeStrong to destroy strong ivars, for the
1644
    // general benefit of the tools.
1645
111
    if (dtorKind == QualType::DK_objc_strong_lifetime) {
1646
64
      destroyer = destroyARCStrongWithStore;
1647
1648
    // Otherwise use the default for the destruction kind.
1649
64
    } else {
1650
47
      destroyer = CGF.getDestroyer(dtorKind);
1651
47
    }
1652
1653
111
    CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
1654
1655
111
    CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
1656
111
                                         cleanupKind & EHCleanup);
1657
111
  }
1658
1659
75
  assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
1660
75
}
1661
1662
void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1663
                                                 ObjCMethodDecl *MD,
1664
97
                                                 bool ctor) {
1665
97
  MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
1666
97
  StartObjCMethod(MD, IMP->getClassInterface());
1667
1668
  // Emit .cxx_construct.
1669
97
  if (ctor) {
1670
    // Suppress the final autorelease in ARC.
1671
22
    AutoreleaseResult = false;
1672
1673
31
    for (const auto *IvarInit : IMP->inits()) {
1674
31
      FieldDecl *Field = IvarInit->getAnyMember();
1675
31
      ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
1676
31
      LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
1677
31
                                    LoadObjCSelf(), Ivar, 0);
1678
31
      EmitAggExpr(IvarInit->getInit(),
1679
31
                  AggValueSlot::forLValue(LV, *this, AggValueSlot::IsDestructed,
1680
31
                                          AggValueSlot::DoesNotNeedGCBarriers,
1681
31
                                          AggValueSlot::IsNotAliased,
1682
31
                                          AggValueSlot::DoesNotOverlap));
1683
31
    }
1684
    // constructor returns 'self'.
1685
22
    CodeGenTypes &Types = CGM.getTypes();
1686
22
    QualType IdTy(CGM.getContext().getObjCIdType());
1687
22
    llvm::Value *SelfAsId =
1688
22
      Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
1689
22
    EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
1690
1691
  // Emit .cxx_destruct.
1692
75
  } else {
1693
75
    emitCXXDestructMethod(*this, IMP);
1694
75
  }
1695
97
  FinishFunction();
1696
97
}
1697
1698
1.45k
llvm::Value *CodeGenFunction::LoadObjCSelf() {
1699
1.45k
  VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
1700
1.45k
  DeclRefExpr DRE(getContext(), Self,
1701
1.45k
                  /*is enclosing local*/ (CurFuncDecl != CurCodeDecl),
1702
1.45k
                  Self->getType(), VK_LValue, SourceLocation());
1703
1.45k
  return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation());
1704
1.45k
}
1705
1706
715
QualType CodeGenFunction::TypeOfSelfObject() {
1707
715
  const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
1708
715
  ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
1709
715
  const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
1710
715
    getContext().getCanonicalType(selfDecl->getType()));
1711
715
  return PTy->getPointeeType();
1712
715
}
1713
1714
68
void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
1715
68
  llvm::FunctionCallee EnumerationMutationFnPtr =
1716
68
      CGM.getObjCRuntime().EnumerationMutationFunction();
1717
68
  if (!EnumerationMutationFnPtr) {
1718
0
    CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
1719
0
    return;
1720
0
  }
1721
68
  CGCallee EnumerationMutationFn =
1722
68
    CGCallee::forDirect(EnumerationMutationFnPtr);
1723
1724
68
  CGDebugInfo *DI = getDebugInfo();
1725
68
  if (DI)
1726
31
    DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
1727
1728
68
  RunCleanupsScope ForScope(*this);
1729
1730
  // The local variable comes into scope immediately.
1731
68
  AutoVarEmission variable = AutoVarEmission::invalid();
1732
68
  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
1733
68
    variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
1734
1735
68
  JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
1736
1737
  // Fast enumeration state.
1738
68
  QualType StateTy = CGM.getObjCFastEnumerationStateType();
1739
68
  Address StatePtr = CreateMemTemp(StateTy, "state.ptr");
1740
68
  EmitNullInitialization(StatePtr, StateTy);
1741
1742
  // Number of elements in the items array.
1743
68
  static const unsigned NumItems = 16;
1744
1745
  // Fetch the countByEnumeratingWithState:objects:count: selector.
1746
68
  IdentifierInfo *II[] = {
1747
68
    &CGM.getContext().Idents.get("countByEnumeratingWithState"),
1748
68
    &CGM.getContext().Idents.get("objects"),
1749
68
    &CGM.getContext().Idents.get("count")
1750
68
  };
1751
68
  Selector FastEnumSel =
1752
68
    CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
1753
1754
68
  QualType ItemsTy =
1755
68
    getContext().getConstantArrayType(getContext().getObjCIdType(),
1756
68
                                      llvm::APInt(32, NumItems), nullptr,
1757
68
                                      ArrayType::Normal, 0);
1758
68
  Address ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
1759
1760
  // Emit the collection pointer.  In ARC, we do a retain.
1761
68
  llvm::Value *Collection;
1762
68
  if (getLangOpts().ObjCAutoRefCount) {
1763
12
    Collection = EmitARCRetainScalarExpr(S.getCollection());
1764
1765
    // Enter a cleanup to do the release.
1766
12
    EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
1767
56
  } else {
1768
56
    Collection = EmitScalarExpr(S.getCollection());
1769
56
  }
1770
1771
  // The 'continue' label needs to appear within the cleanup for the
1772
  // collection object.
1773
68
  JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
1774
1775
  // Send it our message:
1776
68
  CallArgList Args;
1777
1778
  // The first argument is a temporary of the enumeration-state type.
1779
68
  Args.add(RValue::get(StatePtr.getPointer()),
1780
68
           getContext().getPointerType(StateTy));
1781
1782
  // The second argument is a temporary array with space for NumItems
1783
  // pointers.  We'll actually be loading elements from the array
1784
  // pointer written into the control state; this buffer is so that
1785
  // collections that *aren't* backed by arrays can still queue up
1786
  // batches of elements.
1787
68
  Args.add(RValue::get(ItemsPtr.getPointer()),
1788
68
           getContext().getPointerType(ItemsTy));
1789
1790
  // The third argument is the capacity of that temporary array.
1791
68
  llvm::Type *NSUIntegerTy = ConvertType(getContext().getNSUIntegerType());
1792
68
  llvm::Constant *Count = llvm::ConstantInt::get(NSUIntegerTy, NumItems);
1793
68
  Args.add(RValue::get(Count), getContext().getNSUIntegerType());
1794
1795
  // Start the enumeration.
1796
68
  RValue CountRV =
1797
68
      CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1798
68
                                               getContext().getNSUIntegerType(),
1799
68
                                               FastEnumSel, Collection, Args);
1800
1801
  // The initial number of objects that were returned in the buffer.
1802
68
  llvm::Value *initialBufferLimit = CountRV.getScalarVal();
1803
1804
68
  llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
1805
68
  llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
1806
1807
68
  llvm::Value *zero = llvm::Constant::getNullValue(NSUIntegerTy);
1808
1809
  // If the limit pointer was zero to begin with, the collection is
1810
  // empty; skip all this. Set the branch weight assuming this has the same
1811
  // probability of exiting the loop as any other loop exit.
1812
68
  uint64_t EntryCount = getCurrentProfileCount();
1813
68
  Builder.CreateCondBr(
1814
68
      Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), EmptyBB,
1815
68
      LoopInitBB,
1816
68
      createProfileWeights(EntryCount, getProfileCount(S.getBody())));
1817
1818
  // Otherwise, initialize the loop.
1819
68
  EmitBlock(LoopInitBB);
1820
1821
  // Save the initial mutations value.  This is the value at an
1822
  // address that was written into the state object by
1823
  // countByEnumeratingWithState:objects:count:.
1824
68
  Address StateMutationsPtrPtr =
1825
68
      Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
1826
68
  llvm::Value *StateMutationsPtr
1827
68
    = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1828
1829
68
  llvm::Type *UnsignedLongTy = ConvertType(getContext().UnsignedLongTy);
1830
68
  llvm::Value *initialMutations =
1831
68
    Builder.CreateAlignedLoad(UnsignedLongTy, StateMutationsPtr,
1832
68
                              getPointerAlign(), "forcoll.initial-mutations");
1833
1834
  // Start looping.  This is the point we return to whenever we have a
1835
  // fresh, non-empty batch of objects.
1836
68
  llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
1837
68
  EmitBlock(LoopBodyBB);
1838
1839
  // The current index into the buffer.
1840
68
  llvm::PHINode *index = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.index");
1841
68
  index->addIncoming(zero, LoopInitBB);
1842
1843
  // The current buffer size.
1844
68
  llvm::PHINode *count = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.count");
1845
68
  count->addIncoming(initialBufferLimit, LoopInitBB);
1846
1847
68
  incrementProfileCounter(&S);
1848
1849
  // Check whether the mutations value has changed from where it was
1850
  // at start.  StateMutationsPtr should actually be invariant between
1851
  // refreshes.
1852
68
  StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1853
68
  llvm::Value *currentMutations
1854
68
    = Builder.CreateAlignedLoad(UnsignedLongTy, StateMutationsPtr,
1855
68
                                getPointerAlign(), "statemutations");
1856
1857
68
  llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
1858
68
  llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
1859
1860
68
  Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
1861
68
                       WasNotMutatedBB, WasMutatedBB);
1862
1863
  // If so, call the enumeration-mutation function.
1864
68
  EmitBlock(WasMutatedBB);
1865
68
  llvm::Type *ObjCIdType = ConvertType(getContext().getObjCIdType());
1866
68
  llvm::Value *V =
1867
68
    Builder.CreateBitCast(Collection, ObjCIdType);
1868
68
  CallArgList Args2;
1869
68
  Args2.add(RValue::get(V), getContext().getObjCIdType());
1870
  // FIXME: We shouldn't need to get the function info here, the runtime already
1871
  // should have computed it to build the function.
1872
68
  EmitCall(
1873
68
          CGM.getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, Args2),
1874
68
           EnumerationMutationFn, ReturnValueSlot(), Args2);
1875
1876
  // Otherwise, or if the mutation function returns, just continue.
1877
68
  EmitBlock(WasNotMutatedBB);
1878
1879
  // Initialize the element variable.
1880
68
  RunCleanupsScope elementVariableScope(*this);
1881
68
  bool elementIsVariable;
1882
68
  LValue elementLValue;
1883
68
  QualType elementType;
1884
68
  if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
1885
    // Initialize the variable, in case it's a __block variable or something.
1886
68
    EmitAutoVarInit(variable);
1887
1888
68
    const VarDecl *D = cast<VarDecl>(SD->getSingleDecl());
1889
68
    DeclRefExpr tempDRE(getContext(), const_cast<VarDecl *>(D), false,
1890
68
                        D->getType(), VK_LValue, SourceLocation());
1891
68
    elementLValue = EmitLValue(&tempDRE);
1892
68
    elementType = D->getType();
1893
68
    elementIsVariable = true;
1894
1895
68
    if (D->isARCPseudoStrong())
1896
9
      elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone);
1897
68
  } else {
1898
0
    elementLValue = LValue(); // suppress warning
1899
0
    elementType = cast<Expr>(S.getElement())->getType();
1900
0
    elementIsVariable = false;
1901
0
  }
1902
68
  llvm::Type *convertedElementType = ConvertType(elementType);
1903
1904
  // Fetch the buffer out of the enumeration state.
1905
  // TODO: this pointer should actually be invariant between
1906
  // refreshes, which would help us do certain loop optimizations.
1907
68
  Address StateItemsPtr =
1908
68
      Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
1909
68
  llvm::Value *EnumStateItems =
1910
68
    Builder.CreateLoad(StateItemsPtr, "stateitems");
1911
1912
  // Fetch the value at the current index from the buffer.
1913
68
  llvm::Value *CurrentItemPtr = Builder.CreateGEP(
1914
68
      EnumStateItems->getType()->getPointerElementType(), EnumStateItems, index,
1915
68
      "currentitem.ptr");
1916
68
  llvm::Value *CurrentItem =
1917
68
    Builder.CreateAlignedLoad(ObjCIdType, CurrentItemPtr, getPointerAlign());
1918
1919
68
  if (SanOpts.has(SanitizerKind::ObjCCast)) {
1920
    // Before using an item from the collection, check that the implicit cast
1921
    // from id to the element type is valid. This is done with instrumentation
1922
    // roughly corresponding to:
1923
    //
1924
    //   if (![item isKindOfClass:expectedCls]) { /* emit diagnostic */ }
1925
3
    const ObjCObjectPointerType *ObjPtrTy =
1926
3
        elementType->getAsObjCInterfacePointerType();
1927
3
    const ObjCInterfaceType *InterfaceTy =
1928
3
        ObjPtrTy ? ObjPtrTy->getInterfaceType() : 
nullptr0
;
1929
3
    if (InterfaceTy) {
1930
3
      SanitizerScope SanScope(this);
1931
3
      auto &C = CGM.getContext();
1932
3
      assert(InterfaceTy->getDecl() && "No decl for ObjC interface type");
1933
0
      Selector IsKindOfClassSel = GetUnarySelector("isKindOfClass", C);
1934
3
      CallArgList IsKindOfClassArgs;
1935
3
      llvm::Value *Cls =
1936
3
          CGM.getObjCRuntime().GetClass(*this, InterfaceTy->getDecl());
1937
3
      IsKindOfClassArgs.add(RValue::get(Cls), C.getObjCClassType());
1938
3
      llvm::Value *IsClass =
1939
3
          CGM.getObjCRuntime()
1940
3
              .GenerateMessageSend(*this, ReturnValueSlot(), C.BoolTy,
1941
3
                                   IsKindOfClassSel, CurrentItem,
1942
3
                                   IsKindOfClassArgs)
1943
3
              .getScalarVal();
1944
3
      llvm::Constant *StaticData[] = {
1945
3
          EmitCheckSourceLocation(S.getBeginLoc()),
1946
3
          EmitCheckTypeDescriptor(QualType(InterfaceTy, 0))};
1947
3
      EmitCheck({{IsClass, SanitizerKind::ObjCCast}},
1948
3
                SanitizerHandler::InvalidObjCCast,
1949
3
                ArrayRef<llvm::Constant *>(StaticData), CurrentItem);
1950
3
    }
1951
3
  }
1952
1953
  // Cast that value to the right type.
1954
0
  CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
1955
68
                                      "currentitem");
1956
1957
  // Make sure we have an l-value.  Yes, this gets evaluated every
1958
  // time through the loop.
1959
68
  if (!elementIsVariable) {
1960
0
    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1961
0
    EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
1962
68
  } else {
1963
68
    EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue,
1964
68
                           /*isInit*/ true);
1965
68
  }
1966
1967
  // If we do have an element variable, this assignment is the end of
1968
  // its initialization.
1969
68
  if (elementIsVariable)
1970
68
    EmitAutoVarCleanups(variable);
1971
1972
  // Perform the loop body, setting up break and continue labels.
1973
68
  BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
1974
68
  {
1975
68
    RunCleanupsScope Scope(*this);
1976
68
    EmitStmt(S.getBody());
1977
68
  }
1978
68
  BreakContinueStack.pop_back();
1979
1980
  // Destroy the element variable now.
1981
68
  elementVariableScope.ForceCleanup();
1982
1983
  // Check whether there are more elements.
1984
68
  EmitBlock(AfterBody.getBlock());
1985
1986
68
  llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
1987
1988
  // First we check in the local buffer.
1989
68
  llvm::Value *indexPlusOne =
1990
68
      Builder.CreateAdd(index, llvm::ConstantInt::get(NSUIntegerTy, 1));
1991
1992
  // If we haven't overrun the buffer yet, we can continue.
1993
  // Set the branch weights based on the simplifying assumption that this is
1994
  // like a while-loop, i.e., ignoring that the false branch fetches more
1995
  // elements and then returns to the loop.
1996
68
  Builder.CreateCondBr(
1997
68
      Builder.CreateICmpULT(indexPlusOne, count), LoopBodyBB, FetchMoreBB,
1998
68
      createProfileWeights(getProfileCount(S.getBody()), EntryCount));
1999
2000
68
  index->addIncoming(indexPlusOne, AfterBody.getBlock());
2001
68
  count->addIncoming(count, AfterBody.getBlock());
2002
2003
  // Otherwise, we have to fetch more elements.
2004
68
  EmitBlock(FetchMoreBB);
2005
2006
68
  CountRV =
2007
68
      CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2008
68
                                               getContext().getNSUIntegerType(),
2009
68
                                               FastEnumSel, Collection, Args);
2010
2011
  // If we got a zero count, we're done.
2012
68
  llvm::Value *refetchCount = CountRV.getScalarVal();
2013
2014
  // (note that the message send might split FetchMoreBB)
2015
68
  index->addIncoming(zero, Builder.GetInsertBlock());
2016
68
  count->addIncoming(refetchCount, Builder.GetInsertBlock());
2017
2018
68
  Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
2019
68
                       EmptyBB, LoopBodyBB);
2020
2021
  // No more elements.
2022
68
  EmitBlock(EmptyBB);
2023
2024
68
  if (!elementIsVariable) {
2025
    // If the element was not a declaration, set it to be null.
2026
2027
0
    llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
2028
0
    elementLValue = EmitLValue(cast<Expr>(S.getElement()));
2029
0
    EmitStoreThroughLValue(RValue::get(null), elementLValue);
2030
0
  }
2031
2032
68
  if (DI)
2033
31
    DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
2034
2035
68
  ForScope.ForceCleanup();
2036
68
  EmitBlock(LoopEnd.getBlock());
2037
68
}
2038
2039
220
void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
2040
220
  CGM.getObjCRuntime().EmitTryStmt(*this, S);
2041
220
}
2042
2043
50
void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
2044
50
  CGM.getObjCRuntime().EmitThrowStmt(*this, S);
2045
50
}
2046
2047
void CodeGenFunction::EmitObjCAtSynchronizedStmt(
2048
13
                                              const ObjCAtSynchronizedStmt &S) {
2049
13
  CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
2050
13
}
2051
2052
namespace {
2053
  struct CallObjCRelease final : EHScopeStack::Cleanup {
2054
297
    CallObjCRelease(llvm::Value *object) : object(object) {}
2055
    llvm::Value *object;
2056
2057
301
    void Emit(CodeGenFunction &CGF, Flags flags) override {
2058
      // Releases at the end of the full-expression are imprecise.
2059
301
      CGF.EmitARCRelease(object, ARCImpreciseLifetime);
2060
301
    }
2061
  };
2062
}
2063
2064
/// Produce the code for a CK_ARCConsumeObject.  Does a primitive
2065
/// release at the end of the full-expression.
2066
llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type,
2067
297
                                                    llvm::Value *object) {
2068
  // If we're in a conditional branch, we need to make the cleanup
2069
  // conditional.
2070
297
  pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object);
2071
297
  return object;
2072
297
}
2073
2074
llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type,
2075
0
                                                           llvm::Value *value) {
2076
0
  return EmitARCRetainAutorelease(type, value);
2077
0
}
2078
2079
/// Given a number of pointers, inform the optimizer that they're
2080
/// being intrinsically used up until this point in the program.
2081
23
void CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) {
2082
23
  llvm::Function *&fn = CGM.getObjCEntrypoints().clang_arc_use;
2083
23
  if (!fn)
2084
9
    fn = CGM.getIntrinsic(llvm::Intrinsic::objc_clang_arc_use);
2085
2086
  // This isn't really a "runtime" function, but as an intrinsic it
2087
  // doesn't really matter as long as we align things up.
2088
23
  EmitNounwindRuntimeCall(fn, values);
2089
23
}
2090
2091
/// Emit a call to "clang.arc.noop.use", which consumes the result of a call
2092
/// that has operand bundle "clang.arc.attachedcall".
2093
129
void CodeGenFunction::EmitARCNoopIntrinsicUse(ArrayRef<llvm::Value *> values) {
2094
129
  llvm::Function *&fn = CGM.getObjCEntrypoints().clang_arc_noop_use;
2095
129
  if (!fn)
2096
20
    fn = CGM.getIntrinsic(llvm::Intrinsic::objc_clang_arc_noop_use);
2097
129
  EmitNounwindRuntimeCall(fn, values);
2098
129
}
2099
2100
1.23k
static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM, llvm::Value *RTF) {
2101
1.23k
  if (auto *F = dyn_cast<llvm::Function>(RTF)) {
2102
    // If the target runtime doesn't naturally support ARC, emit weak
2103
    // references to the runtime support library.  We don't really
2104
    // permit this to fail, but we need a particular relocation style.
2105
1.23k
    if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC() &&
2106
1.23k
        
!CGM.getTriple().isOSBinFormatCOFF()665
) {
2107
660
      F->setLinkage(llvm::Function::ExternalWeakLinkage);
2108
660
    }
2109
1.23k
  }
2110
1.23k
}
2111
2112
static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM,
2113
97
                                         llvm::FunctionCallee RTF) {
2114
97
  setARCRuntimeFunctionLinkage(CGM, RTF.getCallee());
2115
97
}
2116
2117
static llvm::Function *getARCIntrinsic(llvm::Intrinsic::ID IntID,
2118
1.13k
                                       CodeGenModule &CGM) {
2119
1.13k
  llvm::Function *fn = CGM.getIntrinsic(IntID);
2120
1.13k
  setARCRuntimeFunctionLinkage(CGM, fn);
2121
1.13k
  return fn;
2122
1.13k
}
2123
2124
/// Perform an operation having the signature
2125
///   i8* (i8*)
2126
/// where a null input causes a no-op and returns null.
2127
static llvm::Value *emitARCValueOperation(
2128
    CodeGenFunction &CGF, llvm::Value *value, llvm::Type *returnType,
2129
    llvm::Function *&fn, llvm::Intrinsic::ID IntID,
2130
1.26k
    llvm::CallInst::TailCallKind tailKind = llvm::CallInst::TCK_None) {
2131
1.26k
  if (isa<llvm::ConstantPointerNull>(value))
2132
203
    return value;
2133
2134
1.05k
  if (!fn)
2135
166
    fn = getARCIntrinsic(IntID, CGF.CGM);
2136
2137
  // Cast the argument to 'id'.
2138
1.05k
  llvm::Type *origType = returnType ? 
returnType0
: value->getType();
2139
1.05k
  value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
2140
2141
  // Call the function.
2142
1.05k
  llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value);
2143
1.05k
  call->setTailCallKind(tailKind);
2144
2145
  // Cast the result back to the original type.
2146
1.05k
  return CGF.Builder.CreateBitCast(call, origType);
2147
1.26k
}
2148
2149
/// Perform an operation having the following signature:
2150
///   i8* (i8**)
2151
static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, Address addr,
2152
                                         llvm::Function *&fn,
2153
192
                                         llvm::Intrinsic::ID IntID) {
2154
192
  if (!fn)
2155
38
    fn = getARCIntrinsic(IntID, CGF.CGM);
2156
2157
  // Cast the argument to 'id*'.
2158
192
  llvm::Type *origType = addr.getElementType();
2159
192
  addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
2160
2161
  // Call the function.
2162
192
  llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr.getPointer());
2163
2164
  // Cast the result back to a dereference of the original type.
2165
192
  if (origType != CGF.Int8PtrTy)
2166
19
    result = CGF.Builder.CreateBitCast(result, origType);
2167
2168
192
  return result;
2169
192
}
2170
2171
/// Perform an operation having the following signature:
2172
///   i8* (i8**, i8*)
2173
static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, Address addr,
2174
                                          llvm::Value *value,
2175
                                          llvm::Function *&fn,
2176
                                          llvm::Intrinsic::ID IntID,
2177
101
                                          bool ignored) {
2178
101
  assert(addr.getElementType() == value->getType());
2179
2180
101
  if (!fn)
2181
55
    fn = getARCIntrinsic(IntID, CGF.CGM);
2182
2183
101
  llvm::Type *origType = value->getType();
2184
2185
101
  llvm::Value *args[] = {
2186
101
    CGF.Builder.CreateBitCast(addr.getPointer(), CGF.Int8PtrPtrTy),
2187
101
    CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy)
2188
101
  };
2189
101
  llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args);
2190
2191
101
  if (ignored) 
return nullptr80
;
2192
2193
21
  return CGF.Builder.CreateBitCast(result, origType);
2194
101
}
2195
2196
/// Perform an operation having the following signature:
2197
///   void (i8**, i8**)
2198
static void emitARCCopyOperation(CodeGenFunction &CGF, Address dst, Address src,
2199
                                 llvm::Function *&fn,
2200
326
                                 llvm::Intrinsic::ID IntID) {
2201
326
  assert(dst.getType() == src.getType());
2202
2203
326
  if (!fn)
2204
49
    fn = getARCIntrinsic(IntID, CGF.CGM);
2205
2206
326
  llvm::Value *args[] = {
2207
326
    CGF.Builder.CreateBitCast(dst.getPointer(), CGF.Int8PtrPtrTy),
2208
326
    CGF.Builder.CreateBitCast(src.getPointer(), CGF.Int8PtrPtrTy)
2209
326
  };
2210
326
  CGF.EmitNounwindRuntimeCall(fn, args);
2211
326
}
2212
2213
/// Perform an operation having the signature
2214
///   i8* (i8*)
2215
/// where a null input causes a no-op and returns null.
2216
static llvm::Value *emitObjCValueOperation(CodeGenFunction &CGF,
2217
                                           llvm::Value *value,
2218
                                           llvm::Type *returnType,
2219
                                           llvm::FunctionCallee &fn,
2220
1.34k
                                           StringRef fnName) {
2221
1.34k
  if (isa<llvm::ConstantPointerNull>(value))
2222
0
    return value;
2223
2224
1.34k
  if (!fn) {
2225
415
    llvm::FunctionType *fnType =
2226
415
      llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false);
2227
415
    fn = CGF.CGM.CreateRuntimeFunction(fnType, fnName);
2228
2229
    // We have Native ARC, so set nonlazybind attribute for performance
2230
415
    if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee()))
2231
415
      if (fnName == "objc_retain")
2232
5
        f->addFnAttr(llvm::Attribute::NonLazyBind);
2233
415
  }
2234
2235
  // Cast the argument to 'id'.
2236
1.34k
  llvm::Type *origType = returnType ? returnType : 
value->getType()0
;
2237
1.34k
  value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
2238
2239
  // Call the function.
2240
1.34k
  llvm::CallBase *Inst = CGF.EmitCallOrInvoke(fn, value);
2241
2242
  // Mark calls to objc_autorelease as tail on the assumption that methods
2243
  // overriding autorelease do not touch anything on the stack.
2244
1.34k
  if (fnName == "objc_autorelease")
2245
43
    if (auto *Call = dyn_cast<llvm::CallInst>(Inst))
2246
39
      Call->setTailCall();
2247
2248
  // Cast the result back to the original type.
2249
1.34k
  return CGF.Builder.CreateBitCast(Inst, origType);
2250
1.34k
}
2251
2252
/// Produce the code to do a retain.  Based on the type, calls one of:
2253
///   call i8* \@objc_retain(i8* %value)
2254
///   call i8* \@objc_retainBlock(i8* %value)
2255
737
llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
2256
737
  if (type->isBlockPointerType())
2257
162
    return EmitARCRetainBlock(value, /*mandatory*/ false);
2258
575
  else
2259
575
    return EmitARCRetainNonBlock(value);
2260
737
}
2261
2262
/// Retain the given object, with normal retain semantics.
2263
///   call i8* \@objc_retain(i8* %value)
2264
678
llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
2265
678
  return emitARCValueOperation(*this, value, nullptr,
2266
678
                               CGM.getObjCEntrypoints().objc_retain,
2267
678
                               llvm::Intrinsic::objc_retain);
2268
678
}
2269
2270
/// Retain the given block, with _Block_copy semantics.
2271
///   call i8* \@objc_retainBlock(i8* %value)
2272
///
2273
/// \param mandatory - If false, emit the call with metadata
2274
/// indicating that it's okay for the optimizer to eliminate this call
2275
/// if it can prove that the block never escapes except down the stack.
2276
llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value,
2277
217
                                                 bool mandatory) {
2278
217
  llvm::Value *result
2279
217
    = emitARCValueOperation(*this, value, nullptr,
2280
217
                            CGM.getObjCEntrypoints().objc_retainBlock,
2281
217
                            llvm::Intrinsic::objc_retainBlock);
2282
2283
  // If the copy isn't mandatory, add !clang.arc.copy_on_escape to
2284
  // tell the optimizer that it doesn't need to do this copy if the
2285
  // block doesn't escape, where being passed as an argument doesn't
2286
  // count as escaping.
2287
217
  if (!mandatory && 
isa<llvm::Instruction>(result)186
) {
2288
166
    llvm::CallInst *call
2289
166
      = cast<llvm::CallInst>(result->stripPointerCasts());
2290
166
    assert(call->getCalledOperand() ==
2291
166
           CGM.getObjCEntrypoints().objc_retainBlock);
2292
2293
0
    call->setMetadata("clang.arc.copy_on_escape",
2294
166
                      llvm::MDNode::get(Builder.getContext(), None));
2295
166
  }
2296
2297
0
  return result;
2298
217
}
2299
2300
376
static void emitAutoreleasedReturnValueMarker(CodeGenFunction &CGF) {
2301
  // Fetch the void(void) inline asm which marks that we're going to
2302
  // do something with the autoreleased return value.
2303
376
  llvm::InlineAsm *&marker
2304
376
    = CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker;
2305
376
  if (!marker) {
2306
322
    StringRef assembly
2307
322
      = CGF.CGM.getTargetCodeGenInfo()
2308
322
           .getARCRetainAutoreleasedReturnValueMarker();
2309
2310
    // If we have an empty assembly string, there's nothing to do.
2311
322
    if (assembly.empty()) {
2312
2313
    // Otherwise, at -O0, build an inline asm that we're going to call
2314
    // in a moment.
2315
279
    } else 
if (43
CGF.CGM.getCodeGenOpts().OptimizationLevel == 043
) {
2316
16
      llvm::FunctionType *type =
2317
16
        llvm::FunctionType::get(CGF.VoidTy, /*variadic*/false);
2318
2319
16
      marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
2320
2321
    // If we're at -O1 and above, we don't want to litter the code
2322
    // with this marker yet, so leave a breadcrumb for the ARC
2323
    // optimizer to pick up.
2324
27
    } else {
2325
27
      const char *retainRVMarkerKey = llvm::objcarc::getRVMarkerModuleFlagStr();
2326
27
      if (!CGF.CGM.getModule().getModuleFlag(retainRVMarkerKey)) {
2327
4
        auto *str = llvm::MDString::get(CGF.getLLVMContext(), assembly);
2328
4
        CGF.CGM.getModule().addModuleFlag(llvm::Module::Error,
2329
4
                                          retainRVMarkerKey, str);
2330
4
      }
2331
27
    }
2332
322
  }
2333
2334
  // Call the marker asm if we made one, which we do only at -O0.
2335
376
  if (marker)
2336
70
    CGF.Builder.CreateCall(marker, None, CGF.getBundlesForFunclet(marker));
2337
376
}
2338
2339
static llvm::Value *emitOptimizedARCReturnCall(llvm::Value *value,
2340
                                               bool IsRetainRV,
2341
376
                                               CodeGenFunction &CGF) {
2342
376
  emitAutoreleasedReturnValueMarker(CGF);
2343
2344
  // Add operand bundle "clang.arc.attachedcall" to the call instead of emitting
2345
  // retainRV or claimRV calls in the IR. We currently do this only when the
2346
  // optimization level isn't -O0 since global-isel, which is currently run at
2347
  // -O0, doesn't know about the operand bundle.
2348
376
  ObjCEntrypoints &EPs = CGF.CGM.getObjCEntrypoints();
2349
376
  llvm::Function *&EP = IsRetainRV
2350
376
                            ? 
EPs.objc_retainAutoreleasedReturnValue328
2351
376
                            : 
EPs.objc_unsafeClaimAutoreleasedReturnValue48
;
2352
376
  llvm::Intrinsic::ID IID =
2353
376
      IsRetainRV ? 
llvm::Intrinsic::objc_retainAutoreleasedReturnValue328
2354
376
                 : 
llvm::Intrinsic::objc_unsafeClaimAutoreleasedReturnValue48
;
2355
376
  EP = getARCIntrinsic(IID, CGF.CGM);
2356
2357
376
  llvm::Triple::ArchType Arch = CGF.CGM.getTriple().getArch();
2358
2359
  // FIXME: Do this on all targets and at -O0 too. This can be enabled only if
2360
  // the target backend knows how to handle the operand bundle.
2361
376
  if (CGF.CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2362
376
      
(145
Arch == llvm::Triple::aarch64145
||
Arch == llvm::Triple::x86_64134
)) {
2363
129
    llvm::Value *bundleArgs[] = {EP};
2364
129
    llvm::OperandBundleDef OB("clang.arc.attachedcall", bundleArgs);
2365
129
    auto *oldCall = cast<llvm::CallBase>(value);
2366
129
    llvm::CallBase *newCall = llvm::CallBase::addOperandBundle(
2367
129
        oldCall, llvm::LLVMContext::OB_clang_arc_attachedcall, OB, oldCall);
2368
129
    newCall->copyMetadata(*oldCall);
2369
129
    oldCall->replaceAllUsesWith(newCall);
2370
129
    oldCall->eraseFromParent();
2371
129
    CGF.EmitARCNoopIntrinsicUse(newCall);
2372
129
    return newCall;
2373
129
  }
2374
2375
247
  bool isNoTail =
2376
247
      CGF.CGM.getTargetCodeGenInfo().markARCOptimizedReturnCallsAsNoTail();
2377
247
  llvm::CallInst::TailCallKind tailKind =
2378
247
      isNoTail ? 
llvm::CallInst::TCK_NoTail161
:
llvm::CallInst::TCK_None86
;
2379
247
  return emitARCValueOperation(CGF, value, nullptr, EP, IID, tailKind);
2380
376
}
2381
2382
/// Retain the given object which is the result of a function call.
2383
///   call i8* \@objc_retainAutoreleasedReturnValue(i8* %value)
2384
///
2385
/// Yes, this function name is one character away from a different
2386
/// call with completely different semantics.
2387
llvm::Value *
2388
328
CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) {
2389
328
  return emitOptimizedARCReturnCall(value, true, *this);
2390
328
}
2391
2392
/// Claim a possibly-autoreleased return value at +0.  This is only
2393
/// valid to do in contexts which do not rely on the retain to keep
2394
/// the object valid for all of its uses; for example, when
2395
/// the value is ignored, or when it is being assigned to an
2396
/// __unsafe_unretained variable.
2397
///
2398
///   call i8* \@objc_unsafeClaimAutoreleasedReturnValue(i8* %value)
2399
llvm::Value *
2400
48
CodeGenFunction::EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value) {
2401
48
  return emitOptimizedARCReturnCall(value, false, *this);
2402
48
}
2403
2404
/// Release the given object.
2405
///   call void \@objc_release(i8* %value)
2406
void CodeGenFunction::EmitARCRelease(llvm::Value *value,
2407
742
                                     ARCPreciseLifetime_t precise) {
2408
742
  if (isa<llvm::ConstantPointerNull>(value)) 
return0
;
2409
2410
742
  llvm::Function *&fn = CGM.getObjCEntrypoints().objc_release;
2411
742
  if (!fn)
2412
76
    fn = getARCIntrinsic(llvm::Intrinsic::objc_release, CGM);
2413
2414
  // Cast the argument to 'id'.
2415
742
  value = Builder.CreateBitCast(value, Int8PtrTy);
2416
2417
  // Call objc_release.
2418
742
  llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value);
2419
2420
742
  if (precise == ARCImpreciseLifetime) {
2421
701
    call->setMetadata("clang.imprecise_release",
2422
701
                      llvm::MDNode::get(Builder.getContext(), None));
2423
701
  }
2424
742
}
2425
2426
/// Destroy a __strong variable.
2427
///
2428
/// At -O0, emit a call to store 'null' into the address;
2429
/// instrumenting tools prefer this because the address is exposed,
2430
/// but it's relatively cumbersome to optimize.
2431
///
2432
/// At -O1 and above, just load and call objc_release.
2433
///
2434
///   call void \@objc_storeStrong(i8** %addr, i8* null)
2435
void CodeGenFunction::EmitARCDestroyStrong(Address addr,
2436
1.30k
                                           ARCPreciseLifetime_t precise) {
2437
1.30k
  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2438
990
    llvm::Value *null = getNullForVariable(addr);
2439
990
    EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
2440
990
    return;
2441
990
  }
2442
2443
311
  llvm::Value *value = Builder.CreateLoad(addr);
2444
311
  EmitARCRelease(value, precise);
2445
311
}
2446
2447
/// Store into a strong object.  Always calls this:
2448
///   call void \@objc_storeStrong(i8** %addr, i8* %value)
2449
llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(Address addr,
2450
                                                     llvm::Value *value,
2451
1.46k
                                                     bool ignored) {
2452
1.46k
  assert(addr.getElementType() == value->getType());
2453
2454
0
  llvm::Function *&fn = CGM.getObjCEntrypoints().objc_storeStrong;
2455
1.46k
  if (!fn)
2456
118
    fn = getARCIntrinsic(llvm::Intrinsic::objc_storeStrong, CGM);
2457
2458
1.46k
  llvm::Value *args[] = {
2459
1.46k
    Builder.CreateBitCast(addr.getPointer(), Int8PtrPtrTy),
2460
1.46k
    Builder.CreateBitCast(value, Int8PtrTy)
2461
1.46k
  };
2462
1.46k
  EmitNounwindRuntimeCall(fn, args);
2463
2464
1.46k
  if (ignored) 
return nullptr1.44k
;
2465
19
  return value;
2466
1.46k
}
2467
2468
/// Store into a strong object.  Sometimes calls this:
2469
///   call void \@objc_storeStrong(i8** %addr, i8* %value)
2470
/// Other times, breaks it down into components.
2471
llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst,
2472
                                                 llvm::Value *newValue,
2473
111
                                                 bool ignored) {
2474
111
  QualType type = dst.getType();
2475
111
  bool isBlock = type->isBlockPointerType();
2476
2477
  // Use a store barrier at -O0 unless this is a block type or the
2478
  // lvalue is inadequately aligned.
2479
111
  if (shouldUseFusedARCCalls() &&
2480
111
      
!isBlock101
&&
2481
111
      
(99
dst.getAlignment().isZero()99
||
2482
99
       dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) {
2483
99
    return EmitARCStoreStrongCall(dst.getAddress(*this), newValue, ignored);
2484
99
  }
2485
2486
  // Otherwise, split it out.
2487
2488
  // Retain the new value.
2489
12
  newValue = EmitARCRetain(type, newValue);
2490
2491
  // Read the old value.
2492
12
  llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation());
2493
2494
  // Store.  We do this before the release so that any deallocs won't
2495
  // see the old value.
2496
12
  EmitStoreOfScalar(newValue, dst);
2497
2498
  // Finally, release the old value.
2499
12
  EmitARCRelease(oldValue, dst.isARCPreciseLifetime());
2500
2501
12
  return newValue;
2502
111
}
2503
2504
/// Autorelease the given object.
2505
///   call i8* \@objc_autorelease(i8* %value)
2506
11
llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
2507
11
  return emitARCValueOperation(*this, value, nullptr,
2508
11
                               CGM.getObjCEntrypoints().objc_autorelease,
2509
11
                               llvm::Intrinsic::objc_autorelease);
2510
11
}
2511
2512
/// Autorelease the given object.
2513
///   call i8* \@objc_autoreleaseReturnValue(i8* %value)
2514
llvm::Value *
2515
91
CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) {
2516
91
  return emitARCValueOperation(*this, value, nullptr,
2517
91
                            CGM.getObjCEntrypoints().objc_autoreleaseReturnValue,
2518
91
                               llvm::Intrinsic::objc_autoreleaseReturnValue,
2519
91
                               llvm::CallInst::TCK_Tail);
2520
91
}
2521
2522
/// Do a fused retain/autorelease of the given object.
2523
///   call i8* \@objc_retainAutoreleaseReturnValue(i8* %value)
2524
llvm::Value *
2525
8
CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) {
2526
8
  return emitARCValueOperation(*this, value, nullptr,
2527
8
                     CGM.getObjCEntrypoints().objc_retainAutoreleaseReturnValue,
2528
8
                             llvm::Intrinsic::objc_retainAutoreleaseReturnValue,
2529
8
                               llvm::CallInst::TCK_Tail);
2530
8
}
2531
2532
/// Do a fused retain/autorelease of the given object.
2533
///   call i8* \@objc_retainAutorelease(i8* %value)
2534
/// or
2535
///   %retain = call i8* \@objc_retainBlock(i8* %value)
2536
///   call i8* \@objc_autorelease(i8* %retain)
2537
llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type,
2538
14
                                                       llvm::Value *value) {
2539
14
  if (!type->isBlockPointerType())
2540
10
    return EmitARCRetainAutoreleaseNonBlock(value);
2541
2542
4
  if (isa<llvm::ConstantPointerNull>(value)) 
return value0
;
2543
2544
4
  llvm::Type *origType = value->getType();
2545
4
  value = Builder.CreateBitCast(value, Int8PtrTy);
2546
4
  value = EmitARCRetainBlock(value, /*mandatory*/ true);
2547
4
  value = EmitARCAutorelease(value);
2548
4
  return Builder.CreateBitCast(value, origType);
2549
4
}
2550
2551
/// Do a fused retain/autorelease of the given object.
2552
///   call i8* \@objc_retainAutorelease(i8* %value)
2553
llvm::Value *
2554
10
CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) {
2555
10
  return emitARCValueOperation(*this, value, nullptr,
2556
10
                               CGM.getObjCEntrypoints().objc_retainAutorelease,
2557
10
                               llvm::Intrinsic::objc_retainAutorelease);
2558
10
}
2559
2560
/// i8* \@objc_loadWeak(i8** %addr)
2561
/// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
2562
16
llvm::Value *CodeGenFunction::EmitARCLoadWeak(Address addr) {
2563
16
  return emitARCLoadOperation(*this, addr,
2564
16
                              CGM.getObjCEntrypoints().objc_loadWeak,
2565
16
                              llvm::Intrinsic::objc_loadWeak);
2566
16
}
2567
2568
/// i8* \@objc_loadWeakRetained(i8** %addr)
2569
176
llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(Address addr) {
2570
176
  return emitARCLoadOperation(*this, addr,
2571
176
                              CGM.getObjCEntrypoints().objc_loadWeakRetained,
2572
176
                              llvm::Intrinsic::objc_loadWeakRetained);
2573
176
}
2574
2575
/// i8* \@objc_storeWeak(i8** %addr, i8* %value)
2576
/// Returns %value.
2577
llvm::Value *CodeGenFunction::EmitARCStoreWeak(Address addr,
2578
                                               llvm::Value *value,
2579
51
                                               bool ignored) {
2580
51
  return emitARCStoreOperation(*this, addr, value,
2581
51
                               CGM.getObjCEntrypoints().objc_storeWeak,
2582
51
                               llvm::Intrinsic::objc_storeWeak, ignored);
2583
51
}
2584
2585
/// i8* \@objc_initWeak(i8** %addr, i8* %value)
2586
/// Returns %value.  %addr is known to not have a current weak entry.
2587
/// Essentially equivalent to:
2588
///   *addr = nil; objc_storeWeak(addr, value);
2589
187
void CodeGenFunction::EmitARCInitWeak(Address addr, llvm::Value *value) {
2590
  // If we're initializing to null, just write null to memory; no need
2591
  // to get the runtime involved.  But don't do this if optimization
2592
  // is enabled, because accounting for this would make the optimizer
2593
  // much more complicated.
2594
187
  if (isa<llvm::ConstantPointerNull>(value) &&
2595
187
      
CGM.getCodeGenOpts().OptimizationLevel == 0143
) {
2596
137
    Builder.CreateStore(value, addr);
2597
137
    return;
2598
137
  }
2599
2600
50
  emitARCStoreOperation(*this, addr, value,
2601
50
                        CGM.getObjCEntrypoints().objc_initWeak,
2602
50
                        llvm::Intrinsic::objc_initWeak, /*ignored*/ true);
2603
50
}
2604
2605
/// void \@objc_destroyWeak(i8** %addr)
2606
/// Essentially objc_storeWeak(addr, nil).
2607
554
void CodeGenFunction::EmitARCDestroyWeak(Address addr) {
2608
554
  llvm::Function *&fn = CGM.getObjCEntrypoints().objc_destroyWeak;
2609
554
  if (!fn)
2610
61
    fn = getARCIntrinsic(llvm::Intrinsic::objc_destroyWeak, CGM);
2611
2612
  // Cast the argument to 'id*'.
2613
554
  addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
2614
2615
554
  EmitNounwindRuntimeCall(fn, addr.getPointer());
2616
554
}
2617
2618
/// void \@objc_moveWeak(i8** %dest, i8** %src)
2619
/// Disregards the current value in %dest.  Leaves %src pointing to nothing.
2620
/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
2621
15
void CodeGenFunction::EmitARCMoveWeak(Address dst, Address src) {
2622
15
  emitARCCopyOperation(*this, dst, src,
2623
15
                       CGM.getObjCEntrypoints().objc_moveWeak,
2624
15
                       llvm::Intrinsic::objc_moveWeak);
2625
15
}
2626
2627
/// void \@objc_copyWeak(i8** %dest, i8** %src)
2628
/// Disregards the current value in %dest.  Essentially
2629
///   objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
2630
311
void CodeGenFunction::EmitARCCopyWeak(Address dst, Address src) {
2631
311
  emitARCCopyOperation(*this, dst, src,
2632
311
                       CGM.getObjCEntrypoints().objc_copyWeak,
2633
311
                       llvm::Intrinsic::objc_copyWeak);
2634
311
}
2635
2636
void CodeGenFunction::emitARCCopyAssignWeak(QualType Ty, Address DstAddr,
2637
4
                                            Address SrcAddr) {
2638
4
  llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr);
2639
4
  Object = EmitObjCConsumeObject(Ty, Object);
2640
4
  EmitARCStoreWeak(DstAddr, Object, false);
2641
4
}
2642
2643
void CodeGenFunction::emitARCMoveAssignWeak(QualType Ty, Address DstAddr,
2644
4
                                            Address SrcAddr) {
2645
4
  llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr);
2646
4
  Object = EmitObjCConsumeObject(Ty, Object);
2647
4
  EmitARCStoreWeak(DstAddr, Object, false);
2648
4
  EmitARCDestroyWeak(SrcAddr);
2649
4
}
2650
2651
/// Produce the code to do a objc_autoreleasepool_push.
2652
///   call i8* \@objc_autoreleasePoolPush(void)
2653
100
llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() {
2654
100
  llvm::Function *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPush;
2655
100
  if (!fn)
2656
98
    fn = getARCIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPush, CGM);
2657
2658
100
  return EmitNounwindRuntimeCall(fn);
2659
100
}
2660
2661
/// Produce the code to do a primitive release.
2662
///   call void \@objc_autoreleasePoolPop(i8* %ptr)
2663
100
void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) {
2664
100
  assert(value->getType() == Int8PtrTy);
2665
2666
100
  if (getInvokeDest()) {
2667
    // Call the runtime method not the intrinsic if we are handling exceptions
2668
2
    llvm::FunctionCallee &fn =
2669
2
        CGM.getObjCEntrypoints().objc_autoreleasePoolPopInvoke;
2670
2
    if (!fn) {
2671
2
      llvm::FunctionType *fnType =
2672
2
        llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2673
2
      fn = CGM.CreateRuntimeFunction(fnType, "objc_autoreleasePoolPop");
2674
2
      setARCRuntimeFunctionLinkage(CGM, fn);
2675
2
    }
2676
2677
    // objc_autoreleasePoolPop can throw.
2678
2
    EmitRuntimeCallOrInvoke(fn, value);
2679
98
  } else {
2680
98
    llvm::FunctionCallee &fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPop;
2681
98
    if (!fn)
2682
98
      fn = getARCIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPop, CGM);
2683
2684
98
    EmitRuntimeCall(fn, value);
2685
98
  }
2686
100
}
2687
2688
/// Produce the code to do an MRR version objc_autoreleasepool_push.
2689
/// Which is: [[NSAutoreleasePool alloc] init];
2690
/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
2691
/// init is declared as: - (id) init; in its NSObject super class.
2692
///
2693
17
llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() {
2694
17
  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
2695
17
  llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this);
2696
  // [NSAutoreleasePool alloc]
2697
17
  IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
2698
17
  Selector AllocSel = getContext().Selectors.getSelector(0, &II);
2699
17
  CallArgList Args;
2700
17
  RValue AllocRV =
2701
17
    Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2702
17
                                getContext().getObjCIdType(),
2703
17
                                AllocSel, Receiver, Args);
2704
2705
  // [Receiver init]
2706
17
  Receiver = AllocRV.getScalarVal();
2707
17
  II = &CGM.getContext().Idents.get("init");
2708
17
  Selector InitSel = getContext().Selectors.getSelector(0, &II);
2709
17
  RValue InitRV =
2710
17
    Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2711
17
                                getContext().getObjCIdType(),
2712
17
                                InitSel, Receiver, Args);
2713
17
  return InitRV.getScalarVal();
2714
17
}
2715
2716
/// Allocate the given objc object.
2717
///   call i8* \@objc_alloc(i8* %value)
2718
llvm::Value *CodeGenFunction::EmitObjCAlloc(llvm::Value *value,
2719
886
                                            llvm::Type *resultType) {
2720
886
  return emitObjCValueOperation(*this, value, resultType,
2721
886
                                CGM.getObjCEntrypoints().objc_alloc,
2722
886
                                "objc_alloc");
2723
886
}
2724
2725
/// Allocate the given objc object.
2726
///   call i8* \@objc_allocWithZone(i8* %value)
2727
llvm::Value *CodeGenFunction::EmitObjCAllocWithZone(llvm::Value *value,
2728
25
                                                    llvm::Type *resultType) {
2729
25
  return emitObjCValueOperation(*this, value, resultType,
2730
25
                                CGM.getObjCEntrypoints().objc_allocWithZone,
2731
25
                                "objc_allocWithZone");
2732
25
}
2733
2734
llvm::Value *CodeGenFunction::EmitObjCAllocInit(llvm::Value *value,
2735
372
                                                llvm::Type *resultType) {
2736
372
  return emitObjCValueOperation(*this, value, resultType,
2737
372
                                CGM.getObjCEntrypoints().objc_alloc_init,
2738
372
                                "objc_alloc_init");
2739
372
}
2740
2741
/// Produce the code to do a primitive release.
2742
/// [tmp drain];
2743
17
void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) {
2744
17
  IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
2745
17
  Selector DrainSel = getContext().Selectors.getSelector(0, &II);
2746
17
  CallArgList Args;
2747
17
  CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2748
17
                              getContext().VoidTy, DrainSel, Arg, Args);
2749
17
}
2750
2751
void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF,
2752
                                              Address addr,
2753
53
                                              QualType type) {
2754
53
  CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime);
2755
53
}
2756
2757
void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF,
2758
                                                Address addr,
2759
1.22k
                                                QualType type) {
2760
1.22k
  CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime);
2761
1.22k
}
2762
2763
void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF,
2764
                                     Address addr,
2765
538
                                     QualType type) {
2766
538
  CGF.EmitARCDestroyWeak(addr);
2767
538
}
2768
2769
void CodeGenFunction::emitARCIntrinsicUse(CodeGenFunction &CGF, Address addr,
2770
6
                                          QualType type) {
2771
6
  llvm::Value *value = CGF.Builder.CreateLoad(addr);
2772
6
  CGF.EmitARCIntrinsicUse(value);
2773
6
}
2774
2775
/// Autorelease the given object.
2776
///   call i8* \@objc_autorelease(i8* %value)
2777
llvm::Value *CodeGenFunction::EmitObjCAutorelease(llvm::Value *value,
2778
43
                                                  llvm::Type *returnType) {
2779
43
  return emitObjCValueOperation(
2780
43
      *this, value, returnType,
2781
43
      CGM.getObjCEntrypoints().objc_autoreleaseRuntimeFunction,
2782
43
      "objc_autorelease");
2783
43
}
2784
2785
/// Retain the given object, with normal retain semantics.
2786
///   call i8* \@objc_retain(i8* %value)
2787
llvm::Value *CodeGenFunction::EmitObjCRetainNonBlock(llvm::Value *value,
2788
17
                                                     llvm::Type *returnType) {
2789
17
  return emitObjCValueOperation(
2790
17
      *this, value, returnType,
2791
17
      CGM.getObjCEntrypoints().objc_retainRuntimeFunction, "objc_retain");
2792
17
}
2793
2794
/// Release the given object.
2795
///   call void \@objc_release(i8* %value)
2796
void CodeGenFunction::EmitObjCRelease(llvm::Value *value,
2797
186
                                      ARCPreciseLifetime_t precise) {
2798
186
  if (isa<llvm::ConstantPointerNull>(value)) 
return0
;
2799
2800
186
  llvm::FunctionCallee &fn =
2801
186
      CGM.getObjCEntrypoints().objc_releaseRuntimeFunction;
2802
186
  if (!fn) {
2803
95
    llvm::FunctionType *fnType =
2804
95
        llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2805
95
    fn = CGM.CreateRuntimeFunction(fnType, "objc_release");
2806
95
    setARCRuntimeFunctionLinkage(CGM, fn);
2807
    // We have Native ARC, so set nonlazybind attribute for performance
2808
95
    if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee()))
2809
95
      f->addFnAttr(llvm::Attribute::NonLazyBind);
2810
95
  }
2811
2812
  // Cast the argument to 'id'.
2813
186
  value = Builder.CreateBitCast(value, Int8PtrTy);
2814
2815
  // Call objc_release.
2816
186
  llvm::CallBase *call = EmitCallOrInvoke(fn, value);
2817
2818
186
  if (precise == ARCImpreciseLifetime) {
2819
0
    call->setMetadata("clang.imprecise_release",
2820
0
                      llvm::MDNode::get(Builder.getContext(), None));
2821
0
  }
2822
186
}
2823
2824
namespace {
2825
  struct CallObjCAutoreleasePoolObject final : EHScopeStack::Cleanup {
2826
    llvm::Value *Token;
2827
2828
100
    CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2829
2830
100
    void Emit(CodeGenFunction &CGF, Flags flags) override {
2831
100
      CGF.EmitObjCAutoreleasePoolPop(Token);
2832
100
    }
2833
  };
2834
  struct CallObjCMRRAutoreleasePoolObject final : EHScopeStack::Cleanup {
2835
    llvm::Value *Token;
2836
2837
17
    CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2838
2839
17
    void Emit(CodeGenFunction &CGF, Flags flags) override {
2840
17
      CGF.EmitObjCMRRAutoreleasePoolPop(Token);
2841
17
    }
2842
  };
2843
}
2844
2845
4
void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) {
2846
4
  if (CGM.getLangOpts().ObjCAutoRefCount)
2847
4
    EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
2848
0
  else
2849
0
    EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
2850
4
}
2851
2852
368
static bool shouldRetainObjCLifetime(Qualifiers::ObjCLifetime lifetime) {
2853
368
  switch (lifetime) {
2854
4
  case Qualifiers::OCL_None:
2855
7
  case Qualifiers::OCL_ExplicitNone:
2856
343
  case Qualifiers::OCL_Strong:
2857
343
  case Qualifiers::OCL_Autoreleasing:
2858
343
    return true;
2859
2860
25
  case Qualifiers::OCL_Weak:
2861
25
    return false;
2862
368
  }
2863
2864
0
  llvm_unreachable("impossible lifetime!");
2865
0
}
2866
2867
static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2868
                                                  LValue lvalue,
2869
366
                                                  QualType type) {
2870
366
  llvm::Value *result;
2871
366
  bool shouldRetain = shouldRetainObjCLifetime(type.getObjCLifetime());
2872
366
  if (shouldRetain) {
2873
342
    result = CGF.EmitLoadOfLValue(lvalue, SourceLocation()).getScalarVal();
2874
342
  } else {
2875
24
    assert(type.getObjCLifetime() == Qualifiers::OCL_Weak);
2876
0
    result = CGF.EmitARCLoadWeakRetained(lvalue.getAddress(CGF));
2877
24
  }
2878
0
  return TryEmitResult(result, !shouldRetain);
2879
366
}
2880
2881
static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2882
366
                                                  const Expr *e) {
2883
366
  e = e->IgnoreParens();
2884
366
  QualType type = e->getType();
2885
2886
  // If we're loading retained from a __strong xvalue, we can avoid
2887
  // an extra retain/release pair by zeroing out the source of this
2888
  // "move" operation.
2889
366
  if (e->isXValue() &&
2890
366
      
!type.isConstQualified()4
&&
2891
366
      
type.getObjCLifetime() == Qualifiers::OCL_Strong3
) {
2892
    // Emit the lvalue.
2893
3
    LValue lv = CGF.EmitLValue(e);
2894
2895
    // Load the object pointer.
2896
3
    llvm::Value *result = CGF.EmitLoadOfLValue(lv,
2897
3
                                               SourceLocation()).getScalarVal();
2898
2899
    // Set the source pointer to NULL.
2900
3
    CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress(CGF)), lv);
2901
2902
3
    return TryEmitResult(result, true);
2903
3
  }
2904
2905
  // As a very special optimization, in ARC++, if the l-value is the
2906
  // result of a non-volatile assignment, do a simple retain of the
2907
  // result of the call to objc_storeWeak instead of reloading.
2908
363
  if (CGF.getLangOpts().CPlusPlus &&
2909
363
      
!type.isVolatileQualified()121
&&
2910
363
      
type.getObjCLifetime() == Qualifiers::OCL_Weak120
&&
2911
363
      
isa<BinaryOperator>(e)2
&&
2912
363
      
cast<BinaryOperator>(e)->getOpcode() == BO_Assign1
)
2913
1
    return TryEmitResult(CGF.EmitScalarExpr(e), false);
2914
2915
  // Try to emit code for scalar constant instead of emitting LValue and
2916
  // loading it because we are not guaranteed to have an l-value. One of such
2917
  // cases is DeclRefExpr referencing non-odr-used constant-evaluated variable.
2918
362
  if (const auto *decl_expr = dyn_cast<DeclRefExpr>(e)) {
2919
324
    auto *DRE = const_cast<DeclRefExpr *>(decl_expr);
2920
324
    if (CodeGenFunction::ConstantEmission constant = CGF.tryEmitAsConstant(DRE))
2921
2
      return TryEmitResult(CGF.emitScalarConstant(constant, DRE),
2922
2
                           !shouldRetainObjCLifetime(type.getObjCLifetime()));
2923
324
  }
2924
2925
360
  return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
2926
362
}
2927
2928
typedef llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2929
                                         llvm::Value *value)>
2930
  ValueTransform;
2931
2932
/// Insert code immediately after a call.
2933
2934
// FIXME: We should find a way to emit the runtime call immediately
2935
// after the call is emitted to eliminate the need for this function.
2936
static llvm::Value *emitARCOperationAfterCall(CodeGenFunction &CGF,
2937
                                              llvm::Value *value,
2938
                                              ValueTransform doAfterCall,
2939
396
                                              ValueTransform doFallback) {
2940
396
  CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2941
396
  auto *callBase = dyn_cast<llvm::CallBase>(value);
2942
2943
396
  if (callBase && 
llvm::objcarc::hasAttachedCallOpBundle(callBase)375
) {
2944
    // Fall back if the call base has operand bundle "clang.arc.attachedcall".
2945
1
    value = doFallback(CGF, value);
2946
395
  } else if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
2947
    // Place the retain immediately following the call.
2948
360
    CGF.Builder.SetInsertPoint(call->getParent(),
2949
360
                               ++llvm::BasicBlock::iterator(call));
2950
360
    value = doAfterCall(CGF, value);
2951
360
  } else 
if (llvm::InvokeInst *35
invoke35
= dyn_cast<llvm::InvokeInst>(value)) {
2952
    // Place the retain at the beginning of the normal destination block.
2953
14
    llvm::BasicBlock *BB = invoke->getNormalDest();
2954
14
    CGF.Builder.SetInsertPoint(BB, BB->begin());
2955
14
    value = doAfterCall(CGF, value);
2956
2957
  // Bitcasts can arise because of related-result returns.  Rewrite
2958
  // the operand.
2959
21
  } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
2960
    // Change the insert point to avoid emitting the fall-back call after the
2961
    // bitcast.
2962
18
    CGF.Builder.SetInsertPoint(bitcast->getParent(), bitcast->getIterator());
2963
18
    llvm::Value *operand = bitcast->getOperand(0);
2964
18
    operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback);
2965
18
    bitcast->setOperand(0, operand);
2966
18
    value = bitcast;
2967
18
  } else {
2968
3
    auto *phi = dyn_cast<llvm::PHINode>(value);
2969
3
    if (phi && 
phi->getNumIncomingValues() == 22
&&
2970
3
        
isa<llvm::ConstantPointerNull>(phi->getIncomingValue(1))2
&&
2971
3
        
isa<llvm::CallBase>(phi->getIncomingValue(0))2
) {
2972
      // Handle phi instructions that are generated when it's necessary to check
2973
      // whether the receiver of a message is null.
2974
2
      llvm::Value *inVal = phi->getIncomingValue(0);
2975
2
      inVal = emitARCOperationAfterCall(CGF, inVal, doAfterCall, doFallback);
2976
2
      phi->setIncomingValue(0, inVal);
2977
2
      value = phi;
2978
2
    } else {
2979
      // Generic fall-back case.
2980
      // Retain using the non-block variant: we never need to do a copy
2981
      // of a block that's been returned to us.
2982
1
      value = doFallback(CGF, value);
2983
1
    }
2984
3
  }
2985
2986
396
  CGF.Builder.restoreIP(ip);
2987
396
  return value;
2988
396
}
2989
2990
/// Given that the given expression is some sort of call (which does
2991
/// not return retained), emit a retain following it.
2992
static llvm::Value *emitARCRetainCallResult(CodeGenFunction &CGF,
2993
328
                                            const Expr *e) {
2994
328
  llvm::Value *value = CGF.EmitScalarExpr(e);
2995
328
  return emitARCOperationAfterCall(CGF, value,
2996
328
           [](CodeGenFunction &CGF, llvm::Value *value) {
2997
326
             return CGF.EmitARCRetainAutoreleasedReturnValue(value);
2998
326
           },
2999
328
           [](CodeGenFunction &CGF, llvm::Value *value) {
3000
2
             return CGF.EmitARCRetainNonBlock(value);
3001
2
           });
3002
328
}
3003
3004
/// Given that the given expression is some sort of call (which does
3005
/// not return retained), perform an unsafeClaim following it.
3006
static llvm::Value *emitARCUnsafeClaimCallResult(CodeGenFunction &CGF,
3007
48
                                                 const Expr *e) {
3008
48
  llvm::Value *value = CGF.EmitScalarExpr(e);
3009
48
  return emitARCOperationAfterCall(CGF, value,
3010
48
           [](CodeGenFunction &CGF, llvm::Value *value) {
3011
48
             return CGF.EmitARCUnsafeClaimAutoreleasedReturnValue(value);
3012
48
           },
3013
48
           [](CodeGenFunction &CGF, llvm::Value *value) {
3014
0
             return value;
3015
0
           });
3016
48
}
3017
3018
llvm::Value *CodeGenFunction::EmitARCReclaimReturnedObject(const Expr *E,
3019
151
                                                      bool allowUnsafeClaim) {
3020
151
  if (allowUnsafeClaim &&
3021
151
      
CGM.getLangOpts().ObjCRuntime.hasARCUnsafeClaimAutoreleasedReturnValue()73
) {
3022
48
    return emitARCUnsafeClaimCallResult(*this, E);
3023
103
  } else {
3024
103
    llvm::Value *value = emitARCRetainCallResult(*this, E);
3025
103
    return EmitObjCConsumeObject(E->getType(), value);
3026
103
  }
3027
151
}
3028
3029
/// Determine whether it might be important to emit a separate
3030
/// objc_retain_block on the result of the given expression, or
3031
/// whether it's okay to just emit it in a +1 context.
3032
24
static bool shouldEmitSeparateBlockRetain(const Expr *e) {
3033
24
  assert(e->getType()->isBlockPointerType());
3034
0
  e = e->IgnoreParens();
3035
3036
  // For future goodness, emit block expressions directly in +1
3037
  // contexts if we can.
3038
24
  if (isa<BlockExpr>(e))
3039
19
    return false;
3040
3041
5
  if (const CastExpr *cast = dyn_cast<CastExpr>(e)) {
3042
4
    switch (cast->getCastKind()) {
3043
    // Emitting these operations in +1 contexts is goodness.
3044
4
    case CK_LValueToRValue:
3045
4
    case CK_ARCReclaimReturnedObject:
3046
4
    case CK_ARCConsumeObject:
3047
4
    case CK_ARCProduceObject:
3048
4
      return false;
3049
3050
    // These operations preserve a block type.
3051
0
    case CK_NoOp:
3052
0
    case CK_BitCast:
3053
0
      return shouldEmitSeparateBlockRetain(cast->getSubExpr());
3054
3055
    // These operations are known to be bad (or haven't been considered).
3056
0
    case CK_AnyPointerToBlockPointerCast:
3057
0
    default:
3058
0
      return true;
3059
4
    }
3060
4
  }
3061
3062
1
  return true;
3063
5
}
3064
3065
namespace {
3066
/// A CRTP base class for emitting expressions of retainable object
3067
/// pointer type in ARC.
3068
template <typename Impl, typename Result> class ARCExprEmitter {
3069
protected:
3070
  CodeGenFunction &CGF;
3071
3.21k
  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
3071
2.70k
  Impl &asImpl() { return *static_cast<Impl*>(this); }
CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::asImpl()
Line
Count
Source
3071
508
  Impl &asImpl() { return *static_cast<Impl*>(this); }
3072
3073
1.32k
  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
3073
1.16k
  ARCExprEmitter(CodeGenFunction &CGF) : CGF(CGF) {}
CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::ARCExprEmitter(clang::CodeGen::CodeGenFunction&)
Line
Count
Source
3073
160
  ARCExprEmitter(CodeGenFunction &CGF) : CGF(CGF) {}
3074
3075
public:
3076
  Result visit(const Expr *e);
3077
  Result visitCastExpr(const CastExpr *e);
3078
  Result visitPseudoObjectExpr(const PseudoObjectExpr *e);
3079
  Result visitBlockExpr(const BlockExpr *e);
3080
  Result visitBinaryOperator(const BinaryOperator *e);
3081
  Result visitBinAssign(const BinaryOperator *e);
3082
  Result visitBinAssignUnsafeUnretained(const BinaryOperator *e);
3083
  Result visitBinAssignAutoreleasing(const BinaryOperator *e);
3084
  Result visitBinAssignWeak(const BinaryOperator *e);
3085
  Result visitBinAssignStrong(const BinaryOperator *e);
3086
3087
  // Minimal implementation:
3088
  //   Result visitLValueToRValue(const Expr *e)
3089
  //   Result visitConsumeObject(const Expr *e)
3090
  //   Result visitExtendBlockObject(const Expr *e)
3091
  //   Result visitReclaimReturnedObject(const Expr *e)
3092
  //   Result visitCall(const Expr *e)
3093
  //   Result visitExpr(const Expr *e)
3094
  //
3095
  //   Result emitBitCast(Result result, llvm::Type *resultType)
3096
  //   llvm::Value *getValueOfResult(Result result)
3097
};
3098
}
3099
3100
/// Try to emit a PseudoObjectExpr under special ARC rules.
3101
///
3102
/// This massively duplicates emitPseudoObjectRValue.
3103
template <typename Impl, typename Result>
3104
Result
3105
10
ARCExprEmitter<Impl,Result>::visitPseudoObjectExpr(const PseudoObjectExpr *E) {
3106
10
  SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
3107
3108
  // Find the result expression.
3109
10
  const Expr *resultExpr = E->getResultExpr();
3110
10
  assert(resultExpr);
3111
0
  Result result;
3112
3113
10
  for (PseudoObjectExpr::const_semantics_iterator
3114
31
         i = E->semantics_begin(), e = E->semantics_end(); i != e; 
++i21
) {
3115
21
    const Expr *semantic = *i;
3116
3117
    // If this semantic expression is an opaque value, bind it
3118
    // to the result of its source expression.
3119
21
    if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
3120
11
      typedef CodeGenFunction::OpaqueValueMappingData OVMA;
3121
11
      OVMA opaqueData;
3122
3123
      // If this semantic is the result of the pseudo-object
3124
      // expression, try to evaluate the source as +1.
3125
11
      if (ov == resultExpr) {
3126
0
        assert(!OVMA::shouldBindAsLValue(ov));
3127
0
        result = asImpl().visit(ov->getSourceExpr());
3128
0
        opaqueData = OVMA::bind(CGF, ov,
3129
0
                            RValue::get(asImpl().getValueOfResult(result)));
3130
3131
      // Otherwise, just bind it.
3132
11
      } else {
3133
11
        opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
3134
11
      }
3135
0
      opaques.push_back(opaqueData);
3136
3137
    // Otherwise, if the expression is the result, evaluate it
3138
    // and remember the result.
3139
11
    } else 
if (10
semantic == resultExpr10
) {
3140
10
      result = asImpl().visit(semantic);
3141
3142
    // Otherwise, evaluate the expression in an ignored context.
3143
10
    } else {
3144
0
      CGF.EmitIgnoredExpr(semantic);
3145
0
    }
3146
21
  }
3147
3148
  // Unbind all the opaques now.
3149
21
  for (unsigned i = 0, e = opaques.size(); i != e; 
++i11
)
3150
11
    opaques[i].unbind(CGF);
3151
3152
10
  return result;
3153
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
3105
10
ARCExprEmitter<Impl,Result>::visitPseudoObjectExpr(const PseudoObjectExpr *E) {
3106
10
  SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
3107
3108
  // Find the result expression.
3109
10
  const Expr *resultExpr = E->getResultExpr();
3110
10
  assert(resultExpr);
3111
0
  Result result;
3112
3113
10
  for (PseudoObjectExpr::const_semantics_iterator
3114
31
         i = E->semantics_begin(), e = E->semantics_end(); i != e; 
++i21
) {
3115
21
    const Expr *semantic = *i;
3116
3117
    // If this semantic expression is an opaque value, bind it
3118
    // to the result of its source expression.
3119
21
    if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
3120
11
      typedef CodeGenFunction::OpaqueValueMappingData OVMA;
3121
11
      OVMA opaqueData;
3122
3123
      // If this semantic is the result of the pseudo-object
3124
      // expression, try to evaluate the source as +1.
3125
11
      if (ov == resultExpr) {
3126
0
        assert(!OVMA::shouldBindAsLValue(ov));
3127
0
        result = asImpl().visit(ov->getSourceExpr());
3128
0
        opaqueData = OVMA::bind(CGF, ov,
3129
0
                            RValue::get(asImpl().getValueOfResult(result)));
3130
3131
      // Otherwise, just bind it.
3132
11
      } else {
3133
11
        opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
3134
11
      }
3135
0
      opaques.push_back(opaqueData);
3136
3137
    // Otherwise, if the expression is the result, evaluate it
3138
    // and remember the result.
3139
11
    } else 
if (10
semantic == resultExpr10
) {
3140
10
      result = asImpl().visit(semantic);
3141
3142
    // Otherwise, evaluate the expression in an ignored context.
3143
10
    } else {
3144
0
      CGF.EmitIgnoredExpr(semantic);
3145
0
    }
3146
21
  }
3147
3148
  // Unbind all the opaques now.
3149
21
  for (unsigned i = 0, e = opaques.size(); i != e; 
++i11
)
3150
11
    opaques[i].unbind(CGF);
3151
3152
10
  return result;
3153
10
}
Unexecuted instantiation: CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::visitPseudoObjectExpr(clang::PseudoObjectExpr const*)
3154
3155
template <typename Impl, typename Result>
3156
0
Result ARCExprEmitter<Impl, Result>::visitBlockExpr(const BlockExpr *e) {
3157
  // The default implementation just forwards the expression to visitExpr.
3158
0
  return asImpl().visitExpr(e);
3159
0
}
3160
3161
template <typename Impl, typename Result>
3162
1.07k
Result ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) {
3163
1.07k
  switch (e->getCastKind()) {
3164
3165
  // No-op casts don't change the type, so we just ignore them.
3166
18
  case CK_NoOp:
3167
18
    return asImpl().visit(e->getSubExpr());
3168
3169
  // These casts can change the type.
3170
11
  case CK_CPointerToObjCPointerCast:
3171
24
  case CK_BlockPointerToObjCPointerCast:
3172
25
  case CK_AnyPointerToBlockPointerCast:
3173
220
  case CK_BitCast: {
3174
220
    llvm::Type *resultType = CGF.ConvertType(e->getType());
3175
220
    assert(e->getSubExpr()->getType()->hasPointerRepresentation());
3176
0
    Result result = asImpl().visit(e->getSubExpr());
3177
220
    return asImpl().emitBitCast(result, resultType);
3178
25
  }
3179
3180
  // Handle some casts specially.
3181
382
  case CK_LValueToRValue:
3182
382
    return asImpl().visitLValueToRValue(e->getSubExpr());
3183
86
  case CK_ARCConsumeObject:
3184
86
    return asImpl().visitConsumeObject(e->getSubExpr());
3185
13
  case CK_ARCExtendBlockObject:
3186
13
    return asImpl().visitExtendBlockObject(e->getSubExpr());
3187
258
  case CK_ARCReclaimReturnedObject:
3188
258
    return asImpl().visitReclaimReturnedObject(e->getSubExpr());
3189
3190
  // Otherwise, use the default logic.
3191
102
  default:
3192
102
    return asImpl().visitExpr(e);
3193
1.07k
  }
3194
1.07k
}
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
3162
973
Result ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) {
3163
973
  switch (e->getCastKind()) {
3164
3165
  // No-op casts don't change the type, so we just ignore them.
3166
18
  case CK_NoOp:
3167
18
    return asImpl().visit(e->getSubExpr());
3168
3169
  // These casts can change the type.
3170
10
  case CK_CPointerToObjCPointerCast:
3171
23
  case CK_BlockPointerToObjCPointerCast:
3172
24
  case CK_AnyPointerToBlockPointerCast:
3173
179
  case CK_BitCast: {
3174
179
    llvm::Type *resultType = CGF.ConvertType(e->getType());
3175
179
    assert(e->getSubExpr()->getType()->hasPointerRepresentation());
3176
0
    Result result = asImpl().visit(e->getSubExpr());
3177
179
    return asImpl().emitBitCast(result, resultType);
3178
24
  }
3179
3180
  // Handle some casts specially.
3181
366
  case CK_LValueToRValue:
3182
366
    return asImpl().visitLValueToRValue(e->getSubExpr());
3183
82
  case CK_ARCConsumeObject:
3184
82
    return asImpl().visitConsumeObject(e->getSubExpr());
3185
13
  case CK_ARCExtendBlockObject:
3186
13
    return asImpl().visitExtendBlockObject(e->getSubExpr());
3187
215
  case CK_ARCReclaimReturnedObject:
3188
215
    return asImpl().visitReclaimReturnedObject(e->getSubExpr());
3189
3190
  // Otherwise, use the default logic.
3191
100
  default:
3192
100
    return asImpl().visitExpr(e);
3193
973
  }
3194
973
}
CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::visitCastExpr(clang::CastExpr const*)
Line
Count
Source
3162
106
Result ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) {
3163
106
  switch (e->getCastKind()) {
3164
3165
  // No-op casts don't change the type, so we just ignore them.
3166
0
  case CK_NoOp:
3167
0
    return asImpl().visit(e->getSubExpr());
3168
3169
  // These casts can change the type.
3170
1
  case CK_CPointerToObjCPointerCast:
3171
1
  case CK_BlockPointerToObjCPointerCast:
3172
1
  case CK_AnyPointerToBlockPointerCast:
3173
41
  case CK_BitCast: {
3174
41
    llvm::Type *resultType = CGF.ConvertType(e->getType());
3175
41
    assert(e->getSubExpr()->getType()->hasPointerRepresentation());
3176
0
    Result result = asImpl().visit(e->getSubExpr());
3177
41
    return asImpl().emitBitCast(result, resultType);
3178
1
  }
3179
3180
  // Handle some casts specially.
3181
16
  case CK_LValueToRValue:
3182
16
    return asImpl().visitLValueToRValue(e->getSubExpr());
3183
4
  case CK_ARCConsumeObject:
3184
4
    return asImpl().visitConsumeObject(e->getSubExpr());
3185
0
  case CK_ARCExtendBlockObject:
3186
0
    return asImpl().visitExtendBlockObject(e->getSubExpr());
3187
43
  case CK_ARCReclaimReturnedObject:
3188
43
    return asImpl().visitReclaimReturnedObject(e->getSubExpr());
3189
3190
  // Otherwise, use the default logic.
3191
2
  default:
3192
2
    return asImpl().visitExpr(e);
3193
106
  }
3194
106
}
3195
3196
template <typename Impl, typename Result>
3197
Result
3198
76
ARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) {
3199
76
  switch (e->getOpcode()) {
3200
0
  case BO_Comma:
3201
0
    CGF.EmitIgnoredExpr(e->getLHS());
3202
0
    CGF.EnsureInsertPoint();
3203
0
    return asImpl().visit(e->getRHS());
3204
3205
76
  case BO_Assign:
3206
76
    return asImpl().visitBinAssign(e);
3207
3208
0
  default:
3209
0
    return asImpl().visitExpr(e);
3210
76
  }
3211
76
}
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
3198
36
ARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) {
3199
36
  switch (e->getOpcode()) {
3200
0
  case BO_Comma:
3201
0
    CGF.EmitIgnoredExpr(e->getLHS());
3202
0
    CGF.EnsureInsertPoint();
3203
0
    return asImpl().visit(e->getRHS());
3204
3205
36
  case BO_Assign:
3206
36
    return asImpl().visitBinAssign(e);
3207
3208
0
  default:
3209
0
    return asImpl().visitExpr(e);
3210
36
  }
3211
36
}
CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::visitBinaryOperator(clang::BinaryOperator const*)
Line
Count
Source
3198
40
ARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) {
3199
40
  switch (e->getOpcode()) {
3200
0
  case BO_Comma:
3201
0
    CGF.EmitIgnoredExpr(e->getLHS());
3202
0
    CGF.EnsureInsertPoint();
3203
0
    return asImpl().visit(e->getRHS());
3204
3205
40
  case BO_Assign:
3206
40
    return asImpl().visitBinAssign(e);
3207
3208
0
  default:
3209
0
    return asImpl().visitExpr(e);
3210
40
  }
3211
40
}
3212
3213
template <typename Impl, typename Result>
3214
76
Result ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) {
3215
76
  switch (e->getLHS()->getType().getObjCLifetime()) {
3216
40
  case Qualifiers::OCL_ExplicitNone:
3217
40
    return asImpl().visitBinAssignUnsafeUnretained(e);
3218
3219
8
  case Qualifiers::OCL_Weak:
3220
8
    return asImpl().visitBinAssignWeak(e);
3221
3222
4
  case Qualifiers::OCL_Autoreleasing:
3223
4
    return asImpl().visitBinAssignAutoreleasing(e);
3224
3225
24
  case Qualifiers::OCL_Strong:
3226
24
    return asImpl().visitBinAssignStrong(e);
3227
3228
0
  case Qualifiers::OCL_None:
3229
0
    return asImpl().visitExpr(e);
3230
76
  }
3231
0
  llvm_unreachable("bad ObjC ownership qualifier");
3232
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
3214
36
Result ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) {
3215
36
  switch (e->getLHS()->getType().getObjCLifetime()) {
3216
20
  case Qualifiers::OCL_ExplicitNone:
3217
20
    return asImpl().visitBinAssignUnsafeUnretained(e);
3218
3219
8
  case Qualifiers::OCL_Weak:
3220
8
    return asImpl().visitBinAssignWeak(e);
3221
3222
4
  case Qualifiers::OCL_Autoreleasing:
3223
4
    return asImpl().visitBinAssignAutoreleasing(e);
3224
3225
4
  case Qualifiers::OCL_Strong:
3226
4
    return asImpl().visitBinAssignStrong(e);
3227
3228
0
  case Qualifiers::OCL_None:
3229
0
    return asImpl().visitExpr(e);
3230
36
  }
3231
0
  llvm_unreachable("bad ObjC ownership qualifier");
3232
0
}
CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::visitBinAssign(clang::BinaryOperator const*)
Line
Count
Source
3214
40
Result ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) {
3215
40
  switch (e->getLHS()->getType().getObjCLifetime()) {
3216
20
  case Qualifiers::OCL_ExplicitNone:
3217
20
    return asImpl().visitBinAssignUnsafeUnretained(e);
3218
3219
0
  case Qualifiers::OCL_Weak:
3220
0
    return asImpl().visitBinAssignWeak(e);
3221
3222
0
  case Qualifiers::OCL_Autoreleasing:
3223
0
    return asImpl().visitBinAssignAutoreleasing(e);
3224
3225
20
  case Qualifiers::OCL_Strong:
3226
20
    return asImpl().visitBinAssignStrong(e);
3227
3228
0
  case Qualifiers::OCL_None:
3229
0
    return asImpl().visitExpr(e);
3230
40
  }
3231
0
  llvm_unreachable("bad ObjC ownership qualifier");
3232
0
}
3233
3234
/// The default rule for __unsafe_unretained emits the RHS recursively,
3235
/// stores into the unsafe variable, and propagates the result outward.
3236
template <typename Impl, typename Result>
3237
Result ARCExprEmitter<Impl,Result>::
3238
40
                    visitBinAssignUnsafeUnretained(const BinaryOperator *e) {
3239
  // Recursively emit the RHS.
3240
  // For __block safety, do this before emitting the LHS.
3241
40
  Result result = asImpl().visit(e->getRHS());
3242
3243
  // Perform the store.
3244
40
  LValue lvalue =
3245
40
    CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store);
3246
40
  CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)),
3247
40
                             lvalue);
3248
3249
40
  return result;
3250
40
}
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
3238
20
                    visitBinAssignUnsafeUnretained(const BinaryOperator *e) {
3239
  // Recursively emit the RHS.
3240
  // For __block safety, do this before emitting the LHS.
3241
20
  Result result = asImpl().visit(e->getRHS());
3242
3243
  // Perform the store.
3244
20
  LValue lvalue =
3245
20
    CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store);
3246
20
  CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)),
3247
20
                             lvalue);
3248
3249
20
  return result;
3250
20
}
CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::visitBinAssignUnsafeUnretained(clang::BinaryOperator const*)
Line
Count
Source
3238
20
                    visitBinAssignUnsafeUnretained(const BinaryOperator *e) {
3239
  // Recursively emit the RHS.
3240
  // For __block safety, do this before emitting the LHS.
3241
20
  Result result = asImpl().visit(e->getRHS());
3242
3243
  // Perform the store.
3244
20
  LValue lvalue =
3245
20
    CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store);
3246
20
  CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)),
3247
20
                             lvalue);
3248
3249
20
  return result;
3250
20
}
3251
3252
template <typename Impl, typename Result>
3253
Result
3254
4
ARCExprEmitter<Impl,Result>::visitBinAssignAutoreleasing(const BinaryOperator *e) {
3255
4
  return asImpl().visitExpr(e);
3256
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
3254
4
ARCExprEmitter<Impl,Result>::visitBinAssignAutoreleasing(const BinaryOperator *e) {
3255
4
  return asImpl().visitExpr(e);
3256
4
}
Unexecuted instantiation: CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::visitBinAssignAutoreleasing(clang::BinaryOperator const*)
3257
3258
template <typename Impl, typename Result>
3259
Result
3260
8
ARCExprEmitter<Impl,Result>::visitBinAssignWeak(const BinaryOperator *e) {
3261
8
  return asImpl().visitExpr(e);
3262
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
3260
8
ARCExprEmitter<Impl,Result>::visitBinAssignWeak(const BinaryOperator *e) {
3261
8
  return asImpl().visitExpr(e);
3262
8
}
Unexecuted instantiation: CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::visitBinAssignWeak(clang::BinaryOperator const*)
3263
3264
template <typename Impl, typename Result>
3265
Result
3266
24
ARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) {
3267
24
  return asImpl().visitExpr(e);
3268
24
}
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
3266
4
ARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) {
3267
4
  return asImpl().visitExpr(e);
3268
4
}
CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::visitBinAssignStrong(clang::BinaryOperator const*)
Line
Count
Source
3266
20
ARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) {
3267
20
  return asImpl().visitExpr(e);
3268
20
}
3269
3270
/// The general expression-emission logic.
3271
template <typename Impl, typename Result>
3272
1.62k
Result ARCExprEmitter<Impl,Result>::visit(const Expr *e) {
3273
  // We should *never* see a nested full-expression here, because if
3274
  // we fail to emit at +1, our caller must not retain after we close
3275
  // out the full-expression.  This isn't as important in the unsafe
3276
  // emitter.
3277
1.62k
  assert(!isa<ExprWithCleanups>(e));
3278
3279
  // Look through parens, __extension__, generic selection, etc.
3280
0
  e = e->IgnoreParens();
3281
3282
  // Handle certain kinds of casts.
3283
1.62k
  if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
3284
1.07k
    return asImpl().visitCastExpr(ce);
3285
3286
  // Handle the comma operator.
3287
1.07k
  } else 
if (auto 546
op546
= dyn_cast<BinaryOperator>(e)) {
3288
76
    return asImpl().visitBinaryOperator(op);
3289
3290
  // TODO: handle conditional operators here
3291
3292
  // For calls and message sends, use the retained-call logic.
3293
  // Delegate inits are a special case in that they're the only
3294
  // returns-retained expression that *isn't* surrounded by
3295
  // a consume.
3296
470
  } else if (isa<CallExpr>(e) ||
3297
470
             
(460
isa<ObjCMessageExpr>(e)460
&&
3298
460
              
!cast<ObjCMessageExpr>(e)->isDelegateInitCall()13
)) {
3299
14
    return asImpl().visitCall(e);
3300
3301
  // Look through pseudo-object expressions.
3302
456
  } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
3303
10
    return asImpl().visitPseudoObjectExpr(pseudo);
3304
446
  } else if (auto *be = dyn_cast<BlockExpr>(e))
3305
183
    return asImpl().visitBlockExpr(be);
3306
3307
263
  return asImpl().visitExpr(e);
3308
1.62k
}
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
3272
1.40k
Result ARCExprEmitter<Impl,Result>::visit(const Expr *e) {
3273
  // We should *never* see a nested full-expression here, because if
3274
  // we fail to emit at +1, our caller must not retain after we close
3275
  // out the full-expression.  This isn't as important in the unsafe
3276
  // emitter.
3277
1.40k
  assert(!isa<ExprWithCleanups>(e));
3278
3279
  // Look through parens, __extension__, generic selection, etc.
3280
0
  e = e->IgnoreParens();
3281
3282
  // Handle certain kinds of casts.
3283
1.40k
  if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
3284
973
    return asImpl().visitCastExpr(ce);
3285
3286
  // Handle the comma operator.
3287
973
  } else 
if (auto 431
op431
= dyn_cast<BinaryOperator>(e)) {
3288
36
    return asImpl().visitBinaryOperator(op);
3289
3290
  // TODO: handle conditional operators here
3291
3292
  // For calls and message sends, use the retained-call logic.
3293
  // Delegate inits are a special case in that they're the only
3294
  // returns-retained expression that *isn't* surrounded by
3295
  // a consume.
3296
395
  } else if (isa<CallExpr>(e) ||
3297
395
             
(389
isa<ObjCMessageExpr>(e)389
&&
3298
389
              
!cast<ObjCMessageExpr>(e)->isDelegateInitCall()13
)) {
3299
10
    return asImpl().visitCall(e);
3300
3301
  // Look through pseudo-object expressions.
3302
385
  } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
3303
10
    return asImpl().visitPseudoObjectExpr(pseudo);
3304
375
  } else if (auto *be = dyn_cast<BlockExpr>(e))
3305
183
    return asImpl().visitBlockExpr(be);
3306
3307
192
  return asImpl().visitExpr(e);
3308
1.40k
}
CGObjC.cpp:(anonymous namespace)::ARCExprEmitter<(anonymous namespace)::ARCUnsafeUnretainedExprEmitter, llvm::Value*>::visit(clang::Expr const*)
Line
Count
Source
3272
221
Result ARCExprEmitter<Impl,Result>::visit(const Expr *e) {
3273
  // We should *never* see a nested full-expression here, because if
3274
  // we fail to emit at +1, our caller must not retain after we close
3275
  // out the full-expression.  This isn't as important in the unsafe
3276
  // emitter.
3277
221
  assert(!isa<ExprWithCleanups>(e));
3278
3279
  // Look through parens, __extension__, generic selection, etc.
3280
0
  e = e->IgnoreParens();
3281
3282
  // Handle certain kinds of casts.
3283
221
  if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
3284
106
    return asImpl().visitCastExpr(ce);
3285
3286
  // Handle the comma operator.
3287
115
  } else if (auto op = dyn_cast<BinaryOperator>(e)) {
3288
40
    return asImpl().visitBinaryOperator(op);
3289
3290
  // TODO: handle conditional operators here
3291
3292
  // For calls and message sends, use the retained-call logic.
3293
  // Delegate inits are a special case in that they're the only
3294
  // returns-retained expression that *isn't* surrounded by
3295
  // a consume.
3296
75
  } else if (isa<CallExpr>(e) ||
3297
75
             
(71
isa<ObjCMessageExpr>(e)71
&&
3298
71
              
!cast<ObjCMessageExpr>(e)->isDelegateInitCall()0
)) {
3299
4
    return asImpl().visitCall(e);
3300
3301
  // Look through pseudo-object expressions.
3302
71
  } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
3303
0
    return asImpl().visitPseudoObjectExpr(pseudo);
3304
71
  } else if (auto *be = dyn_cast<BlockExpr>(e))
3305
0
    return asImpl().visitBlockExpr(be);
3306
3307
71
  return asImpl().visitExpr(e);
3308
221
}
3309
3310
namespace {
3311
3312
/// An emitter for +1 results.
3313
struct ARCRetainExprEmitter :
3314
  public ARCExprEmitter<ARCRetainExprEmitter, TryEmitResult> {
3315
3316
1.16k
  ARCRetainExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
3317
3318
20
  llvm::Value *getValueOfResult(TryEmitResult result) {
3319
20
    return result.getPointer();
3320
20
  }
3321
3322
179
  TryEmitResult emitBitCast(TryEmitResult result, llvm::Type *resultType) {
3323
179
    llvm::Value *value = result.getPointer();
3324
179
    value = CGF.Builder.CreateBitCast(value, resultType);
3325
179
    result.setPointer(value);
3326
179
    return result;
3327
179
  }
3328
3329
366
  TryEmitResult visitLValueToRValue(const Expr *e) {
3330
366
    return tryEmitARCRetainLoadOfScalar(CGF, e);
3331
366
  }
3332
3333
  /// For consumptions, just emit the subexpression and thus elide
3334
  /// the retain/release pair.
3335
82
  TryEmitResult visitConsumeObject(const Expr *e) {
3336
82
    llvm::Value *result = CGF.EmitScalarExpr(e);
3337
82
    return TryEmitResult(result, true);
3338
82
  }
3339
3340
183
  TryEmitResult visitBlockExpr(const BlockExpr *e) {
3341
183
    TryEmitResult result = visitExpr(e);
3342
    // Avoid the block-retain if this is a block literal that doesn't need to be
3343
    // copied to the heap.
3344
183
    if (CGF.CGM.getCodeGenOpts().ObjCAvoidHeapifyLocalBlocks &&
3345
183
        
e->getBlockDecl()->canAvoidCopyToHeap()28
)
3346
13
      result.setInt(true);
3347
183
    return result;
3348
183
  }
3349
3350
  /// Block extends are net +0.  Naively, we could just recurse on
3351
  /// the subexpression, but actually we need to ensure that the
3352
  /// value is copied as a block, so there's a little filter here.
3353
13
  TryEmitResult visitExtendBlockObject(const Expr *e) {
3354
13
    llvm::Value *result; // will be a +0 value
3355
3356
    // If we can't safely assume the sub-expression will produce a
3357
    // block-copied value, emit the sub-expression at +0.
3358
13
    if (shouldEmitSeparateBlockRetain(e)) {
3359
0
      result = CGF.EmitScalarExpr(e);
3360
3361
    // Otherwise, try to emit the sub-expression at +1 recursively.
3362
13
    } else {
3363
13
      TryEmitResult subresult = asImpl().visit(e);
3364
3365
      // If that produced a retained value, just use that.
3366
13
      if (subresult.getInt()) {
3367
0
        return subresult;
3368
0
      }
3369
3370
      // Otherwise it's +0.
3371
13
      result = subresult.getPointer();
3372
13
    }
3373
3374
    // Retain the object as a block.
3375
13
    result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true);
3376
13
    return TryEmitResult(result, true);
3377
13
  }
3378
3379
  /// For reclaims, emit the subexpression as a retained call and
3380
  /// skip the consumption.
3381
215
  TryEmitResult visitReclaimReturnedObject(const Expr *e) {
3382
215
    llvm::Value *result = emitARCRetainCallResult(CGF, e);
3383
215
    return TryEmitResult(result, true);
3384
215
  }
3385
3386
  /// When we have an undecorated call, retroactively do a claim.
3387
10
  TryEmitResult visitCall(const Expr *e) {
3388
10
    llvm::Value *result = emitARCRetainCallResult(CGF, e);
3389
10
    return TryEmitResult(result, true);
3390
10
  }
3391
3392
  // TODO: maybe special-case visitBinAssignWeak?
3393
3394
491
  TryEmitResult visitExpr(const Expr *e) {
3395
    // We didn't find an obvious production, so emit what we've got and
3396
    // tell the caller that we didn't manage to retain.
3397
491
    llvm::Value *result = CGF.EmitScalarExpr(e);
3398
491
    return TryEmitResult(result, false);
3399
491
  }
3400
};
3401
}
3402
3403
static TryEmitResult
3404
1.16k
tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) {
3405
1.16k
  return ARCRetainExprEmitter(CGF).visit(e);
3406
1.16k
}
3407
3408
static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
3409
                                                LValue lvalue,
3410
6
                                                QualType type) {
3411
6
  TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
3412
6
  llvm::Value *value = result.getPointer();
3413
6
  if (!result.getInt())
3414
0
    value = CGF.EmitARCRetain(type, value);
3415
6
  return value;
3416
6
}
3417
3418
/// EmitARCRetainScalarExpr - Semantically equivalent to
3419
/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
3420
/// best-effort attempt to peephole expressions that naturally produce
3421
/// retained objects.
3422
979
llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) {
3423
  // The retain needs to happen within the full-expression.
3424
979
  if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3425
10
    RunCleanupsScope scope(*this);
3426
10
    return EmitARCRetainScalarExpr(cleanups->getSubExpr());
3427
10
  }
3428
3429
969
  TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
3430
969
  llvm::Value *value = result.getPointer();
3431
969
  if (!result.getInt())
3432
686
    value = EmitARCRetain(e->getType(), value);
3433
969
  return value;
3434
979
}
3435
3436
llvm::Value *
3437
16
CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) {
3438
  // The retain needs to happen within the full-expression.
3439
16
  if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3440
1
    RunCleanupsScope scope(*this);
3441
1
    return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr());
3442
1
  }
3443
3444
15
  TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
3445
15
  llvm::Value *value = result.getPointer();
3446
15
  if (result.getInt())
3447
7
    value = EmitARCAutorelease(value);
3448
8
  else
3449
8
    value = EmitARCRetainAutorelease(e->getType(), value);
3450
15
  return value;
3451
16
}
3452
3453
11
llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) {
3454
11
  llvm::Value *result;
3455
11
  bool doRetain;
3456
3457
11
  if (shouldEmitSeparateBlockRetain(e)) {
3458
1
    result = EmitScalarExpr(e);
3459
1
    doRetain = true;
3460
10
  } else {
3461
10
    TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e);
3462
10
    result = subresult.getPointer();
3463
10
    doRetain = !subresult.getInt();
3464
10
  }
3465
3466
11
  if (doRetain)
3467
11
    result = EmitARCRetainBlock(result, /*mandatory*/ true);
3468
11
  return EmitObjCConsumeObject(e->getType(), result);
3469
11
}
3470
3471
36
llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) {
3472
  // In ARC, retain and autorelease the expression.
3473
36
  if (getLangOpts().ObjCAutoRefCount) {
3474
    // Do so before running any cleanups for the full-expression.
3475
    // EmitARCRetainAutoreleaseScalarExpr does this for us.
3476
1
    return EmitARCRetainAutoreleaseScalarExpr(expr);
3477
1
  }
3478
3479
  // Otherwise, use the normal scalar-expression emission.  The
3480
  // exception machinery doesn't do anything special with the
3481
  // exception like retaining it, so there's no safety associated with
3482
  // only running cleanups after the throw has started, and when it
3483
  // matters it tends to be substantially inferior code.
3484
35
  return EmitScalarExpr(expr);
3485
36
}
3486
3487
namespace {
3488
3489
/// An emitter for assigning into an __unsafe_unretained context.
3490
struct ARCUnsafeUnretainedExprEmitter :
3491
  public ARCExprEmitter<ARCUnsafeUnretainedExprEmitter, llvm::Value*> {
3492
3493
160
  ARCUnsafeUnretainedExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
3494
3495
20
  llvm::Value *getValueOfResult(llvm::Value *value) {
3496
20
    return value;
3497
20
  }
3498
3499
41
  llvm::Value *emitBitCast(llvm::Value *value, llvm::Type *resultType) {
3500
41
    return CGF.Builder.CreateBitCast(value, resultType);
3501
41
  }
3502
3503
16
  llvm::Value *visitLValueToRValue(const Expr *e) {
3504
16
    return CGF.EmitScalarExpr(e);
3505
16
  }
3506
3507
  /// For consumptions, just emit the subexpression and perform the
3508
  /// consumption like normal.
3509
4
  llvm::Value *visitConsumeObject(const Expr *e) {
3510
4
    llvm::Value *value = CGF.EmitScalarExpr(e);
3511
4
    return CGF.EmitObjCConsumeObject(e->getType(), value);
3512
4
  }
3513
3514
  /// No special logic for block extensions.  (This probably can't
3515
  /// actually happen in this emitter, though.)
3516
0
  llvm::Value *visitExtendBlockObject(const Expr *e) {
3517
0
    return CGF.EmitARCExtendBlockObject(e);
3518
0
  }
3519
3520
  /// For reclaims, perform an unsafeClaim if that's enabled.
3521
43
  llvm::Value *visitReclaimReturnedObject(const Expr *e) {
3522
43
    return CGF.EmitARCReclaimReturnedObject(e, /*unsafe*/ true);
3523
43
  }
3524
3525
  /// When we have an undecorated call, just emit it without adding
3526
  /// the unsafeClaim.
3527
4
  llvm::Value *visitCall(const Expr *e) {
3528
4
    return CGF.EmitScalarExpr(e);
3529
4
  }
3530
3531
  /// Just do normal scalar emission in the default case.
3532
93
  llvm::Value *visitExpr(const Expr *e) {
3533
93
    return CGF.EmitScalarExpr(e);
3534
93
  }
3535
};
3536
}
3537
3538
static llvm::Value *emitARCUnsafeUnretainedScalarExpr(CodeGenFunction &CGF,
3539
160
                                                      const Expr *e) {
3540
160
  return ARCUnsafeUnretainedExprEmitter(CGF).visit(e);
3541
160
}
3542
3543
/// EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to
3544
/// immediately releasing the resut of EmitARCRetainScalarExpr, but
3545
/// avoiding any spurious retains, including by performing reclaims
3546
/// with objc_unsafeClaimAutoreleasedReturnValue.
3547
160
llvm::Value *CodeGenFunction::EmitARCUnsafeUnretainedScalarExpr(const Expr *e) {
3548
  // Look through full-expressions.
3549
160
  if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3550
0
    RunCleanupsScope scope(*this);
3551
0
    return emitARCUnsafeUnretainedScalarExpr(*this, cleanups->getSubExpr());
3552
0
  }
3553
3554
160
  return emitARCUnsafeUnretainedScalarExpr(*this, e);
3555
160
}
3556
3557
std::pair<LValue,llvm::Value*>
3558
CodeGenFunction::EmitARCStoreUnsafeUnretained(const BinaryOperator *e,
3559
35
                                              bool ignored) {
3560
  // Evaluate the RHS first.  If we're ignoring the result, assume
3561
  // that we can emit at an unsafe +0.
3562
35
  llvm::Value *value;
3563
35
  if (ignored) {
3564
35
    value = EmitARCUnsafeUnretainedScalarExpr(e->getRHS());
3565
35
  } else {
3566
0
    value = EmitScalarExpr(e->getRHS());
3567
0
  }
3568
3569
  // Emit the LHS and perform the store.
3570
35
  LValue lvalue = EmitLValue(e->getLHS());
3571
35
  EmitStoreOfScalar(value, lvalue);
3572
3573
35
  return std::pair<LValue,llvm::Value*>(std::move(lvalue), value);
3574
35
}
3575
3576
std::pair<LValue,llvm::Value*>
3577
CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e,
3578
159
                                    bool ignored) {
3579
  // Evaluate the RHS first.
3580
159
  TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
3581
159
  llvm::Value *value = result.getPointer();
3582
3583
159
  bool hasImmediateRetain = result.getInt();
3584
3585
  // If we didn't emit a retained object, and the l-value is of block
3586
  // type, then we need to emit the block-retain immediately in case
3587
  // it invalidates the l-value.
3588
159
  if (!hasImmediateRetain && 
e->getType()->isBlockPointerType()105
) {
3589
24
    value = EmitARCRetainBlock(value, /*mandatory*/ false);
3590
24
    hasImmediateRetain = true;
3591
24
  }
3592
3593
159
  LValue lvalue = EmitLValue(e->getLHS());
3594
3595
  // If the RHS was emitted retained, expand this.
3596
159
  if (hasImmediateRetain) {
3597
78
    llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation());
3598
78
    EmitStoreOfScalar(value, lvalue);
3599
78
    EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime());
3600
81
  } else {
3601
81
    value = EmitARCStoreStrong(lvalue, value, ignored);
3602
81
  }
3603
3604
159
  return std::pair<LValue,llvm::Value*>(lvalue, value);
3605
159
}
3606
3607
std::pair<LValue,llvm::Value*>
3608
4
CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) {
3609
4
  llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
3610
4
  LValue lvalue = EmitLValue(e->getLHS());
3611
3612
4
  EmitStoreOfScalar(value, lvalue);
3613
3614
4
  return std::pair<LValue,llvm::Value*>(lvalue, value);
3615
4
}
3616
3617
void CodeGenFunction::EmitObjCAutoreleasePoolStmt(
3618
113
                                          const ObjCAutoreleasePoolStmt &ARPS) {
3619
113
  const Stmt *subStmt = ARPS.getSubStmt();
3620
113
  const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
3621
3622
113
  CGDebugInfo *DI = getDebugInfo();
3623
113
  if (DI)
3624
94
    DI->EmitLexicalBlockStart(Builder, S.getLBracLoc());
3625
3626
  // Keep track of the current cleanup stack depth.
3627
113
  RunCleanupsScope Scope(*this);
3628
113
  if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
3629
96
    llvm::Value *token = EmitObjCAutoreleasePoolPush();
3630
96
    EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
3631
96
  } else {
3632
17
    llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
3633
17
    EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
3634
17
  }
3635
3636
113
  for (const auto *I : S.body())
3637
530
    EmitStmt(I);
3638
3639
113
  if (DI)
3640
94
    DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc());
3641
113
}
3642
3643
/// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
3644
/// make sure it survives garbage collection until this point.
3645
1
void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
3646
  // We just use an inline assembly.
3647
1
  llvm::FunctionType *extenderType
3648
1
    = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All);
3649
1
  llvm::InlineAsm *extender = llvm::InlineAsm::get(extenderType,
3650
1
                                                   /* assembly */ "",
3651
1
                                                   /* constraints */ "r",
3652
1
                                                   /* side effects */ true);
3653
3654
1
  object = Builder.CreateBitCast(object, VoidPtrTy);
3655
1
  EmitNounwindRuntimeCall(extender, object);
3656
1
}
3657
3658
/// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with
3659
/// non-trivial copy assignment function, produce following helper function.
3660
/// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; }
3661
///
3662
llvm::Constant *
3663
CodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction(
3664
440
                                        const ObjCPropertyImplDecl *PID) {
3665
440
  if (!getLangOpts().CPlusPlus ||
3666
440
      
!getLangOpts().ObjCRuntime.hasAtomicCopyHelper()32
)
3667
408
    return nullptr;
3668
32
  QualType Ty = PID->getPropertyIvarDecl()->getType();
3669
32
  if (!Ty->isRecordType())
3670
10
    return nullptr;
3671
22
  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3672
22
  if ((!(PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic)))
3673
7
    return nullptr;
3674
15
  llvm::Constant *HelperFn = nullptr;
3675
15
  if (hasTrivialSetExpr(PID))
3676
8
    return nullptr;
3677
7
  assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null");
3678
7
  if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty)))
3679
2
    return HelperFn;
3680
3681
5
  ASTContext &C = getContext();
3682
5
  IdentifierInfo *II
3683
5
    = &CGM.getContext().Idents.get("__assign_helper_atomic_property_");
3684
3685
5
  QualType ReturnTy = C.VoidTy;
3686
5
  QualType DestTy = C.getPointerType(Ty);
3687
5
  QualType SrcTy = Ty;
3688
5
  SrcTy.addConst();
3689
5
  SrcTy = C.getPointerType(SrcTy);
3690
3691
5
  SmallVector<QualType, 2> ArgTys;
3692
5
  ArgTys.push_back(DestTy);
3693
5
  ArgTys.push_back(SrcTy);
3694
5
  QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {});
3695
3696
5
  FunctionDecl *FD = FunctionDecl::Create(
3697
5
      C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II,
3698
5
      FunctionTy, nullptr, SC_Static, false, false, false);
3699
3700
5
  FunctionArgList args;
3701
5
  ParmVarDecl *Params[2];
3702
5
  ParmVarDecl *DstDecl = ParmVarDecl::Create(
3703
5
      C, FD, SourceLocation(), SourceLocation(), nullptr, DestTy,
3704
5
      C.getTrivialTypeSourceInfo(DestTy, SourceLocation()), SC_None,
3705
5
      /*DefArg=*/nullptr);
3706
5
  args.push_back(Params[0] = DstDecl);
3707
5
  ParmVarDecl *SrcDecl = ParmVarDecl::Create(
3708
5
      C, FD, SourceLocation(), SourceLocation(), nullptr, SrcTy,
3709
5
      C.getTrivialTypeSourceInfo(SrcTy, SourceLocation()), SC_None,
3710
5
      /*DefArg=*/nullptr);
3711
5
  args.push_back(Params[1] = SrcDecl);
3712
5
  FD->setParams(Params);
3713
3714
5
  const CGFunctionInfo &FI =
3715
5
      CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
3716
3717
5
  llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3718
3719
5
  llvm::Function *Fn =
3720
5
    llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
3721
5
                           "__assign_helper_atomic_property_",
3722
5
                           &CGM.getModule());
3723
3724
5
  CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
3725
3726
5
  StartFunction(FD, ReturnTy, Fn, FI, args);
3727
3728
5
  DeclRefExpr DstExpr(C, DstDecl, false, DestTy, VK_PRValue, SourceLocation());
3729
5
  UnaryOperator *DST = UnaryOperator::Create(
3730
5
      C, &DstExpr, UO_Deref, DestTy->getPointeeType(), VK_LValue, OK_Ordinary,
3731
5
      SourceLocation(), false, FPOptionsOverride());
3732
3733
5
  DeclRefExpr SrcExpr(C, SrcDecl, false, SrcTy, VK_PRValue, SourceLocation());
3734
5
  UnaryOperator *SRC = UnaryOperator::Create(
3735
5
      C, &SrcExpr, UO_Deref, SrcTy->getPointeeType(), VK_LValue, OK_Ordinary,
3736
5
      SourceLocation(), false, FPOptionsOverride());
3737
3738
5
  Expr *Args[2] = {DST, SRC};
3739
5
  CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment());
3740
5
  CXXOperatorCallExpr *TheCall = CXXOperatorCallExpr::Create(
3741
5
      C, OO_Equal, CalleeExp->getCallee(), Args, DestTy->getPointeeType(),
3742
5
      VK_LValue, SourceLocation(), FPOptionsOverride());
3743
3744
5
  EmitStmt(TheCall);
3745
3746
5
  FinishFunction();
3747
5
  HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
3748
5
  CGM.setAtomicSetterHelperFnMap(Ty, HelperFn);
3749
5
  return HelperFn;
3750
7
}
3751
3752
llvm::Constant *
3753
CodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction(
3754
451
                                            const ObjCPropertyImplDecl *PID) {
3755
451
  if (!getLangOpts().CPlusPlus ||
3756
451
      
!getLangOpts().ObjCRuntime.hasAtomicCopyHelper()32
)
3757
419
    return nullptr;
3758
32
  const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3759
32
  QualType Ty = PD->getType();
3760
32
  if (!Ty->isRecordType())
3761
13
    return nullptr;
3762
19
  if ((!(PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic)))
3763
5
    return nullptr;
3764
14
  llvm::Constant *HelperFn = nullptr;
3765
14
  if (hasTrivialGetExpr(PID))
3766
8
    return nullptr;
3767
6
  assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null");
3768
6
  if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty)))
3769
2
    return HelperFn;
3770
3771
4
  ASTContext &C = getContext();
3772
4
  IdentifierInfo *II =
3773
4
      &CGM.getContext().Idents.get("__copy_helper_atomic_property_");
3774
3775
4
  QualType ReturnTy = C.VoidTy;
3776
4
  QualType DestTy = C.getPointerType(Ty);
3777
4
  QualType SrcTy = Ty;
3778
4
  SrcTy.addConst();
3779
4
  SrcTy = C.getPointerType(SrcTy);
3780
3781
4
  SmallVector<QualType, 2> ArgTys;
3782
4
  ArgTys.push_back(DestTy);
3783
4
  ArgTys.push_back(SrcTy);
3784
4
  QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {});
3785
3786
4
  FunctionDecl *FD = FunctionDecl::Create(
3787
4
      C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II,
3788
4
      FunctionTy, nullptr, SC_Static, false, false, false);
3789
3790
4
  FunctionArgList args;
3791
4
  ParmVarDecl *Params[2];
3792
4
  ParmVarDecl *DstDecl = ParmVarDecl::Create(
3793
4
      C, FD, SourceLocation(), SourceLocation(), nullptr, DestTy,
3794
4
      C.getTrivialTypeSourceInfo(DestTy, SourceLocation()), SC_None,
3795
4
      /*DefArg=*/nullptr);
3796
4
  args.push_back(Params[0] = DstDecl);
3797
4
  ParmVarDecl *SrcDecl = ParmVarDecl::Create(
3798
4
      C, FD, SourceLocation(), SourceLocation(), nullptr, SrcTy,
3799
4
      C.getTrivialTypeSourceInfo(SrcTy, SourceLocation()), SC_None,
3800
4
      /*DefArg=*/nullptr);
3801
4
  args.push_back(Params[1] = SrcDecl);
3802
4
  FD->setParams(Params);
3803
3804
4
  const CGFunctionInfo &FI =
3805
4
      CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
3806
3807
4
  llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3808
3809
4
  llvm::Function *Fn = llvm::Function::Create(
3810
4
      LTy, llvm::GlobalValue::InternalLinkage, "__copy_helper_atomic_property_",
3811
4
      &CGM.getModule());
3812
3813
4
  CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
3814
3815
4
  StartFunction(FD, ReturnTy, Fn, FI, args);
3816
3817
4
  DeclRefExpr SrcExpr(getContext(), SrcDecl, false, SrcTy, VK_PRValue,
3818
4
                      SourceLocation());
3819
3820
4
  UnaryOperator *SRC = UnaryOperator::Create(
3821
4
      C, &SrcExpr, UO_Deref, SrcTy->getPointeeType(), VK_LValue, OK_Ordinary,
3822
4
      SourceLocation(), false, FPOptionsOverride());
3823
3824
4
  CXXConstructExpr *CXXConstExpr =
3825
4
    cast<CXXConstructExpr>(PID->getGetterCXXConstructor());
3826
3827
4
  SmallVector<Expr*, 4> ConstructorArgs;
3828
4
  ConstructorArgs.push_back(SRC);
3829
4
  ConstructorArgs.append(std::next(CXXConstExpr->arg_begin()),
3830
4
                         CXXConstExpr->arg_end());
3831
3832
4
  CXXConstructExpr *TheCXXConstructExpr =
3833
4
    CXXConstructExpr::Create(C, Ty, SourceLocation(),
3834
4
                             CXXConstExpr->getConstructor(),
3835
4
                             CXXConstExpr->isElidable(),
3836
4
                             ConstructorArgs,
3837
4
                             CXXConstExpr->hadMultipleCandidates(),
3838
4
                             CXXConstExpr->isListInitialization(),
3839
4
                             CXXConstExpr->isStdInitListInitialization(),
3840
4
                             CXXConstExpr->requiresZeroInitialization(),
3841
4
                             CXXConstExpr->getConstructionKind(),
3842
4
                             SourceRange());
3843
3844
4
  DeclRefExpr DstExpr(getContext(), DstDecl, false, DestTy, VK_PRValue,
3845
4
                      SourceLocation());
3846
3847
4
  RValue DV = EmitAnyExpr(&DstExpr);
3848
4
  CharUnits Alignment
3849
4
    = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType());
3850
4
  EmitAggExpr(TheCXXConstructExpr,
3851
4
              AggValueSlot::forAddr(Address(DV.getScalarVal(), Alignment),
3852
4
                                    Qualifiers(),
3853
4
                                    AggValueSlot::IsDestructed,
3854
4
                                    AggValueSlot::DoesNotNeedGCBarriers,
3855
4
                                    AggValueSlot::IsNotAliased,
3856
4
                                    AggValueSlot::DoesNotOverlap));
3857
3858
4
  FinishFunction();
3859
4
  HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
3860
4
  CGM.setAtomicGetterHelperFnMap(Ty, HelperFn);
3861
4
  return HelperFn;
3862
6
}
3863
3864
llvm::Value *
3865
5
CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) {
3866
  // Get selectors for retain/autorelease.
3867
5
  IdentifierInfo *CopyID = &getContext().Idents.get("copy");
3868
5
  Selector CopySelector =
3869
5
      getContext().Selectors.getNullarySelector(CopyID);
3870
5
  IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease");
3871
5
  Selector AutoreleaseSelector =
3872
5
      getContext().Selectors.getNullarySelector(AutoreleaseID);
3873
3874
  // Emit calls to retain/autorelease.
3875
5
  CGObjCRuntime &Runtime = CGM.getObjCRuntime();
3876
5
  llvm::Value *Val = Block;
3877
5
  RValue Result;
3878
5
  Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3879
5
                                       Ty, CopySelector,
3880
5
                                       Val, CallArgList(), nullptr, nullptr);
3881
5
  Val = Result.getScalarVal();
3882
5
  Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3883
5
                                       Ty, AutoreleaseSelector,
3884
5
                                       Val, CallArgList(), nullptr, nullptr);
3885
5
  Val = Result.getScalarVal();
3886
5
  return Val;
3887
5
}
3888
3889
16
static unsigned getBaseMachOPlatformID(const llvm::Triple &TT) {
3890
16
  switch (TT.getOS()) {
3891
0
  case llvm::Triple::Darwin:
3892
7
  case llvm::Triple::MacOSX:
3893
7
    return llvm::MachO::PLATFORM_MACOS;
3894
5
  case llvm::Triple::IOS:
3895
5
    return llvm::MachO::PLATFORM_IOS;
3896
2
  case llvm::Triple::TvOS:
3897
2
    return llvm::MachO::PLATFORM_TVOS;
3898
2
  case llvm::Triple::WatchOS:
3899
2
    return llvm::MachO::PLATFORM_WATCHOS;
3900
0
  default:
3901
0
    return /*Unknown platform*/ 0;
3902
16
  }
3903
16
}
3904
3905
static llvm::Value *emitIsPlatformVersionAtLeast(CodeGenFunction &CGF,
3906
16
                                                 const VersionTuple &Version) {
3907
16
  CodeGenModule &CGM = CGF.CGM;
3908
  // Note: we intend to support multi-platform version checks, so reserve
3909
  // the room for a dual platform checking invocation that will be
3910
  // implemented in the future.
3911
16
  llvm::SmallVector<llvm::Value *, 8> Args;
3912
3913
16
  auto EmitArgs = [&](const VersionTuple &Version, const llvm::Triple &TT) {
3914
16
    Optional<unsigned> Min = Version.getMinor(), SMin = Version.getSubminor();
3915
16
    Args.push_back(
3916
16
        llvm::ConstantInt::get(CGM.Int32Ty, getBaseMachOPlatformID(TT)));
3917
16
    Args.push_back(llvm::ConstantInt::get(CGM.Int32Ty, Version.getMajor()));
3918
16
    Args.push_back(llvm::ConstantInt::get(CGM.Int32Ty, Min.getValueOr(0)));
3919
16
    Args.push_back(llvm::ConstantInt::get(CGM.Int32Ty, SMin.getValueOr(0)));
3920
16
  };
3921
3922
16
  assert(!Version.empty() && "unexpected empty version");
3923
0
  EmitArgs(Version, CGM.getTarget().getTriple());
3924
3925
16
  if (!CGM.IsPlatformVersionAtLeastFn) {
3926
12
    llvm::FunctionType *FTy = llvm::FunctionType::get(
3927
12
        CGM.Int32Ty, {CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty},
3928
12
        false);
3929
12
    CGM.IsPlatformVersionAtLeastFn =
3930
12
        CGM.CreateRuntimeFunction(FTy, "__isPlatformVersionAtLeast");
3931
12
  }
3932
3933
16
  llvm::Value *Check =
3934
16
      CGF.EmitNounwindRuntimeCall(CGM.IsPlatformVersionAtLeastFn, Args);
3935
16
  return CGF.Builder.CreateICmpNE(Check,
3936
16
                                  llvm::Constant::getNullValue(CGM.Int32Ty));
3937
16
}
3938
3939
llvm::Value *
3940
16
CodeGenFunction::EmitBuiltinAvailable(const VersionTuple &Version) {
3941
  // Darwin uses the new __isPlatformVersionAtLeast family of routines.
3942
16
  if (CGM.getTarget().getTriple().isOSDarwin())
3943
16
    return emitIsPlatformVersionAtLeast(*this, Version);
3944
3945
0
  if (!CGM.IsOSVersionAtLeastFn) {
3946
0
    llvm::FunctionType *FTy =
3947
0
        llvm::FunctionType::get(Int32Ty, {Int32Ty, Int32Ty, Int32Ty}, false);
3948
0
    CGM.IsOSVersionAtLeastFn =
3949
0
        CGM.CreateRuntimeFunction(FTy, "__isOSVersionAtLeast");
3950
0
  }
3951
3952
0
  Optional<unsigned> Min = Version.getMinor(), SMin = Version.getSubminor();
3953
0
  llvm::Value *Args[] = {
3954
0
      llvm::ConstantInt::get(CGM.Int32Ty, Version.getMajor()),
3955
0
      llvm::ConstantInt::get(CGM.Int32Ty, Min.getValueOr(0)),
3956
0
      llvm::ConstantInt::get(CGM.Int32Ty, SMin.getValueOr(0))
3957
0
  };
3958
3959
0
  llvm::Value *CallRes =
3960
0
      EmitNounwindRuntimeCall(CGM.IsOSVersionAtLeastFn, Args);
3961
3962
0
  return Builder.CreateICmpNE(CallRes, llvm::Constant::getNullValue(Int32Ty));
3963
16
}
3964
3965
static bool isFoundationNeededForDarwinAvailabilityCheck(
3966
12
    const llvm::Triple &TT, const VersionTuple &TargetVersion) {
3967
12
  VersionTuple FoundationDroppedInVersion;
3968
12
  switch (TT.getOS()) {
3969
3
  case llvm::Triple::IOS:
3970
5
  case llvm::Triple::TvOS:
3971
5
    FoundationDroppedInVersion = VersionTuple(/*Major=*/13);
3972
5
    break;
3973
2
  case llvm::Triple::WatchOS:
3974
2
    FoundationDroppedInVersion = VersionTuple(/*Major=*/6);
3975
2
    break;
3976
0
  case llvm::Triple::Darwin:
3977
5
  case llvm::Triple::MacOSX:
3978
5
    FoundationDroppedInVersion = VersionTuple(/*Major=*/10, /*Minor=*/15);
3979
5
    break;
3980
0
  default:
3981
0
    llvm_unreachable("Unexpected OS");
3982
12
  }
3983
12
  return TargetVersion < FoundationDroppedInVersion;
3984
12
}
3985
3986
34.5k
void CodeGenModule::emitAtAvailableLinkGuard() {
3987
34.5k
  if (!IsPlatformVersionAtLeastFn)
3988
34.5k
    return;
3989
  // @available requires CoreFoundation only on Darwin.
3990
12
  if (!Target.getTriple().isOSDarwin())
3991
0
    return;
3992
  // @available doesn't need Foundation on macOS 10.15+, iOS/tvOS 13+, or
3993
  // watchOS 6+.
3994
12
  if (!isFoundationNeededForDarwinAvailabilityCheck(
3995
12
          Target.getTriple(), Target.getPlatformMinVersion()))
3996
5
    return;
3997
  // Add -framework CoreFoundation to the linker commands. We still want to
3998
  // emit the core foundation reference down below because otherwise if
3999
  // CoreFoundation is not used in the code, the linker won't link the
4000
  // framework.
4001
7
  auto &Context = getLLVMContext();
4002
7
  llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
4003
7
                             llvm::MDString::get(Context, "CoreFoundation")};
4004
7
  LinkerOptionsMetadata.push_back(llvm::MDNode::get(Context, Args));
4005
  // Emit a reference to a symbol from CoreFoundation to ensure that
4006
  // CoreFoundation is linked into the final binary.
4007
7
  llvm::FunctionType *FTy =
4008
7
      llvm::FunctionType::get(Int32Ty, {VoidPtrTy}, false);
4009
7
  llvm::FunctionCallee CFFunc =
4010
7
      CreateRuntimeFunction(FTy, "CFBundleGetVersionNumber");
4011
4012
7
  llvm::FunctionType *CheckFTy = llvm::FunctionType::get(VoidTy, {}, false);
4013
7
  llvm::FunctionCallee CFLinkCheckFuncRef = CreateRuntimeFunction(
4014
7
      CheckFTy, "__clang_at_available_requires_core_foundation_framework",
4015
7
      llvm::AttributeList(), /*Local=*/true);
4016
7
  llvm::Function *CFLinkCheckFunc =
4017
7
      cast<llvm::Function>(CFLinkCheckFuncRef.getCallee()->stripPointerCasts());
4018
7
  if (CFLinkCheckFunc->empty()) {
4019
7
    CFLinkCheckFunc->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage);
4020
7
    CFLinkCheckFunc->setVisibility(llvm::GlobalValue::HiddenVisibility);
4021
7
    CodeGenFunction CGF(*this);
4022
7
    CGF.Builder.SetInsertPoint(CGF.createBasicBlock("", CFLinkCheckFunc));
4023
7
    CGF.EmitNounwindRuntimeCall(CFFunc,
4024
7
                                llvm::Constant::getNullValue(VoidPtrTy));
4025
7
    CGF.Builder.CreateUnreachable();
4026
7
    addCompilerUsedGlobal(CFLinkCheckFunc);
4027
7
  }
4028
7
}
4029
4030
15.9k
CGObjCRuntime::~CGObjCRuntime() {}