Coverage Report

Created: 2019-07-24 05:18

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