Coverage Report

Created: 2020-09-22 08:39

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