Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGCleanup.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CGCleanup.cpp - Bookkeeping and code emission for cleanups -------===//
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 file contains code dealing with the IR generation for cleanups
10
// and related information.
11
//
12
// A "cleanup" is a piece of code which needs to be executed whenever
13
// control transfers out of a particular scope.  This can be
14
// conditionalized to occur only on exceptional control flow, only on
15
// normal control flow, or both.
16
//
17
//===----------------------------------------------------------------------===//
18
19
#include "CGCleanup.h"
20
#include "CodeGenFunction.h"
21
#include "llvm/Support/SaveAndRestore.h"
22
23
using namespace clang;
24
using namespace CodeGen;
25
26
0
bool DominatingValue<RValue>::saved_type::needsSaving(RValue rv) {
27
0
  if (rv.isScalar())
28
0
    return DominatingLLVMValue::needsSaving(rv.getScalarVal());
29
0
  if (rv.isAggregate())
30
0
    return DominatingLLVMValue::needsSaving(rv.getAggregatePointer());
31
0
  return true;
32
0
}
33
34
DominatingValue<RValue>::saved_type
35
16
DominatingValue<RValue>::saved_type::save(CodeGenFunction &CGF, RValue rv) {
36
16
  if (rv.isScalar()) {
37
16
    llvm::Value *V = rv.getScalarVal();
38
39
    // These automatically dominate and don't need to be saved.
40
16
    if (!DominatingLLVMValue::needsSaving(V))
41
10
      return saved_type(V, ScalarLiteral);
42
43
    // Everything else needs an alloca.
44
6
    Address addr =
45
6
      CGF.CreateDefaultAlignTempAlloca(V->getType(), "saved-rvalue");
46
6
    CGF.Builder.CreateStore(V, addr);
47
6
    return saved_type(addr.getPointer(), ScalarAddress);
48
16
  }
49
50
0
  if (rv.isComplex()) {
51
0
    CodeGenFunction::ComplexPairTy V = rv.getComplexVal();
52
0
    llvm::Type *ComplexTy =
53
0
        llvm::StructType::get(V.first->getType(), V.second->getType());
54
0
    Address addr = CGF.CreateDefaultAlignTempAlloca(ComplexTy, "saved-complex");
55
0
    CGF.Builder.CreateStore(V.first, CGF.Builder.CreateStructGEP(addr, 0));
56
0
    CGF.Builder.CreateStore(V.second, CGF.Builder.CreateStructGEP(addr, 1));
57
0
    return saved_type(addr.getPointer(), ComplexAddress);
58
0
  }
59
60
0
  assert(rv.isAggregate());
61
0
  Address V = rv.getAggregateAddress(); // TODO: volatile?
62
0
  if (!DominatingLLVMValue::needsSaving(V.getPointer()))
63
0
    return saved_type(V.getPointer(), AggregateLiteral,
64
0
                      V.getAlignment().getQuantity());
65
66
0
  Address addr =
67
0
    CGF.CreateTempAlloca(V.getType(), CGF.getPointerAlign(), "saved-rvalue");
68
0
  CGF.Builder.CreateStore(V.getPointer(), addr);
69
0
  return saved_type(addr.getPointer(), AggregateAddress,
70
0
                    V.getAlignment().getQuantity());
71
0
}
72
73
/// Given a saved r-value produced by SaveRValue, perform the code
74
/// necessary to restore it to usability at the current insertion
75
/// point.
76
10
RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) {
77
10
  auto getSavingAddress = [&](llvm::Value *value) {
78
6
    auto alignment = cast<llvm::AllocaInst>(value)->getAlignment();
79
6
    return Address(value, CharUnits::fromQuantity(alignment));
80
6
  };
81
10
  switch (K) {
82
4
  case ScalarLiteral:
83
4
    return RValue::get(Value);
84
6
  case ScalarAddress:
85
6
    return RValue::get(CGF.Builder.CreateLoad(getSavingAddress(Value)));
86
0
  case AggregateLiteral:
87
0
    return RValue::getAggregate(Address(Value, CharUnits::fromQuantity(Align)));
88
0
  case AggregateAddress: {
89
0
    auto addr = CGF.Builder.CreateLoad(getSavingAddress(Value));
90
0
    return RValue::getAggregate(Address(addr, CharUnits::fromQuantity(Align)));
91
0
  }
92
0
  case ComplexAddress: {
93
0
    Address address = getSavingAddress(Value);
94
0
    llvm::Value *real =
95
0
        CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(address, 0));
96
0
    llvm::Value *imag =
97
0
        CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(address, 1));
98
0
    return RValue::getComplex(real, imag);
99
0
  }
100
10
  }
101
102
0
  llvm_unreachable("bad saved r-value kind");
103
0
}
104
105
/// Push an entry of the given size onto this protected-scope stack.
106
177k
char *EHScopeStack::allocate(size_t Size) {
107
177k
  Size = llvm::alignTo(Size, ScopeStackAlignment);
108
177k
  if (!StartOfBuffer) {
109
100k
    unsigned Capacity = 1024;
110
100k
    while (Capacity < Size) 
Capacity *= 20
;
111
100k
    StartOfBuffer = new char[Capacity];
112
100k
    StartOfData = EndOfBuffer = StartOfBuffer + Capacity;
113
100k
  } else 
if (76.5k
static_cast<size_t>(StartOfData - StartOfBuffer) < Size76.5k
) {
114
116
    unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer;
115
116
    unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer);
116
117
116
    unsigned NewCapacity = CurrentCapacity;
118
116
    do {
119
116
      NewCapacity *= 2;
120
116
    } while (NewCapacity < UsedCapacity + Size);
121
122
116
    char *NewStartOfBuffer = new char[NewCapacity];
123
116
    char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity;
124
116
    char *NewStartOfData = NewEndOfBuffer - UsedCapacity;
125
116
    memcpy(NewStartOfData, StartOfData, UsedCapacity);
126
116
    delete [] StartOfBuffer;
127
116
    StartOfBuffer = NewStartOfBuffer;
128
116
    EndOfBuffer = NewEndOfBuffer;
129
116
    StartOfData = NewStartOfData;
130
116
  }
131
132
177k
  assert(StartOfBuffer + Size <= StartOfData);
133
0
  StartOfData -= Size;
134
177k
  return StartOfData;
135
177k
}
136
137
177k
void EHScopeStack::deallocate(size_t Size) {
138
177k
  StartOfData += llvm::alignTo(Size, ScopeStackAlignment);
139
177k
}
140
141
bool EHScopeStack::containsOnlyLifetimeMarkers(
142
8.65k
    EHScopeStack::stable_iterator Old) const {
143
12.5k
  for (EHScopeStack::iterator it = begin(); stabilize(it) != Old; 
it++3.89k
) {
144
9.43k
    EHCleanupScope *cleanup = dyn_cast<EHCleanupScope>(&*it);
145
9.43k
    if (!cleanup || !cleanup->isLifetimeMarker())
146
5.54k
      return false;
147
9.43k
  }
148
149
3.10k
  return true;
150
8.65k
}
151
152
305k
bool EHScopeStack::requiresLandingPad() const {
153
314k
  for (stable_iterator si = getInnermostEHScope(); si != stable_end(); ) {
154
    // Skip lifetime markers.
155
108k
    if (auto *cleanup = dyn_cast<EHCleanupScope>(&*find(si)))
156
74.5k
      if (cleanup->isLifetimeMarker()) {
157
9.11k
        si = cleanup->getEnclosingEHScope();
158
9.11k
        continue;
159
9.11k
      }
160
99.0k
    return true;
161
108k
  }
162
163
206k
  return false;
164
305k
}
165
166
EHScopeStack::stable_iterator
167
237k
EHScopeStack::getInnermostActiveNormalCleanup() const {
168
237k
  for (stable_iterator si = getInnermostNormalCleanup(), se = stable_end();
169
237k
         si != se; ) {
170
9.89k
    EHCleanupScope &cleanup = cast<EHCleanupScope>(*find(si));
171
9.89k
    if (cleanup.isActive()) return si;
172
0
    si = cleanup.getEnclosingNormalCleanup();
173
0
  }
174
227k
  return stable_end();
175
237k
}
176
177
178
50.0k
void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) {
179
50.0k
  char *Buffer = allocate(EHCleanupScope::getSizeForCleanupSize(Size));
180
50.0k
  bool IsNormalCleanup = Kind & NormalCleanup;
181
50.0k
  bool IsEHCleanup = Kind & EHCleanup;
182
50.0k
  bool IsLifetimeMarker = Kind & LifetimeMarker;
183
50.0k
  EHCleanupScope *Scope =
184
50.0k
    new (Buffer) EHCleanupScope(IsNormalCleanup,
185
50.0k
                                IsEHCleanup,
186
50.0k
                                Size,
187
50.0k
                                BranchFixups.size(),
188
50.0k
                                InnermostNormalCleanup,
189
50.0k
                                InnermostEHScope);
190
50.0k
  if (IsNormalCleanup)
191
43.7k
    InnermostNormalCleanup = stable_begin();
192
50.0k
  if (IsEHCleanup)
193
45.9k
    InnermostEHScope = stable_begin();
194
50.0k
  if (IsLifetimeMarker)
195
6.45k
    Scope->setLifetimeMarker();
196
197
  // With Windows -EHa, Invoke llvm.seh.scope.begin() for EHCleanup
198
50.0k
  if (CGF->getLangOpts().EHAsynch && 
IsEHCleanup20
&&
!IsLifetimeMarker19
&&
199
50.0k
      
CGF->getTarget().getCXXABI().isMicrosoft()19
)
200
19
    CGF->EmitSehCppScopeBegin();
201
202
50.0k
  return Scope->getCleanupBuffer();
203
50.0k
}
204
205
50.0k
void EHScopeStack::popCleanup() {
206
50.0k
  assert(!empty() && "popping exception stack when not empty");
207
208
0
  assert(isa<EHCleanupScope>(*begin()));
209
0
  EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin());
210
50.0k
  InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup();
211
50.0k
  InnermostEHScope = Cleanup.getEnclosingEHScope();
212
50.0k
  deallocate(Cleanup.getAllocatedSize());
213
214
  // Destroy the cleanup.
215
50.0k
  Cleanup.Destroy();
216
217
  // Check whether we can shrink the branch-fixups stack.
218
50.0k
  if (!BranchFixups.empty()) {
219
    // If we no longer have any normal cleanups, all the fixups are
220
    // complete.
221
11
    if (!hasNormalCleanups())
222
0
      BranchFixups.clear();
223
224
    // Otherwise we can still trim out unnecessary nulls.
225
11
    else
226
11
      popNullFixups();
227
11
  }
228
50.0k
}
229
230
97
EHFilterScope *EHScopeStack::pushFilter(unsigned numFilters) {
231
97
  assert(getInnermostEHScope() == stable_end());
232
0
  char *buffer = allocate(EHFilterScope::getSizeForNumFilters(numFilters));
233
97
  EHFilterScope *filter = new (buffer) EHFilterScope(numFilters);
234
97
  InnermostEHScope = stable_begin();
235
97
  return filter;
236
97
}
237
238
97
void EHScopeStack::popFilter() {
239
97
  assert(!empty() && "popping exception stack when not empty");
240
241
0
  EHFilterScope &filter = cast<EHFilterScope>(*begin());
242
97
  deallocate(EHFilterScope::getSizeForNumFilters(filter.getNumFilters()));
243
244
97
  InnermostEHScope = filter.getEnclosingEHScope();
245
97
}
246
247
618
EHCatchScope *EHScopeStack::pushCatch(unsigned numHandlers) {
248
618
  char *buffer = allocate(EHCatchScope::getSizeForNumHandlers(numHandlers));
249
618
  EHCatchScope *scope =
250
618
    new (buffer) EHCatchScope(numHandlers, InnermostEHScope);
251
618
  InnermostEHScope = stable_begin();
252
618
  return scope;
253
618
}
254
255
126k
void EHScopeStack::pushTerminate() {
256
126k
  char *Buffer = allocate(EHTerminateScope::getSize());
257
126k
  new (Buffer) EHTerminateScope(InnermostEHScope);
258
126k
  InnermostEHScope = stable_begin();
259
126k
}
260
261
/// Remove any 'null' fixups on the stack.  However, we can't pop more
262
/// fixups than the fixup depth on the innermost normal cleanup, or
263
/// else fixups that we try to add to that cleanup will end up in the
264
/// wrong place.  We *could* try to shrink fixup depths, but that's
265
/// actually a lot of work for little benefit.
266
16
void EHScopeStack::popNullFixups() {
267
  // We expect this to only be called when there's still an innermost
268
  // normal cleanup;  otherwise there really shouldn't be any fixups.
269
16
  assert(hasNormalCleanups());
270
271
0
  EHScopeStack::iterator it = find(InnermostNormalCleanup);
272
16
  unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth();
273
16
  assert(BranchFixups.size() >= MinSize && "fixup stack out of order");
274
275
3.02k
  while (BranchFixups.size() > MinSize &&
276
3.02k
         
BranchFixups.back().Destination == nullptr3.01k
)
277
3.00k
    BranchFixups.pop_back();
278
16
}
279
280
268
Address CodeGenFunction::createCleanupActiveFlag() {
281
  // Create a variable to decide whether the cleanup needs to be run.
282
268
  Address active = CreateTempAllocaWithoutCast(
283
268
      Builder.getInt1Ty(), CharUnits::One(), "cleanup.cond");
284
285
  // Initialize it to false at a site that's guaranteed to be run
286
  // before each evaluation.
287
268
  setBeforeOutermostConditional(Builder.getFalse(), active);
288
289
  // Initialize it to true at the current location.
290
268
  Builder.CreateStore(Builder.getTrue(), active);
291
292
268
  return active;
293
268
}
294
295
283
void CodeGenFunction::initFullExprCleanupWithFlag(Address ActiveFlag) {
296
  // Set that as the active flag in the cleanup.
297
283
  EHCleanupScope &cleanup = cast<EHCleanupScope>(*EHStack.begin());
298
283
  assert(!cleanup.hasActiveFlag() && "cleanup already has active flag?");
299
0
  cleanup.setActiveFlag(ActiveFlag);
300
301
283
  if (cleanup.isNormalCleanup()) 
cleanup.setTestFlagInNormalCleanup()241
;
302
283
  if (cleanup.isEHCleanup()) 
cleanup.setTestFlagInEHCleanup()257
;
303
283
}
304
305
0
void EHScopeStack::Cleanup::anchor() {}
306
307
static void createStoreInstBefore(llvm::Value *value, Address addr,
308
5.90k
                                  llvm::Instruction *beforeInst) {
309
5.90k
  auto store = new llvm::StoreInst(value, addr.getPointer(), beforeInst);
310
5.90k
  store->setAlignment(addr.getAlignment().getAsAlign());
311
5.90k
}
312
313
static llvm::LoadInst *createLoadInstBefore(Address addr, const Twine &name,
314
360
                                            llvm::Instruction *beforeInst) {
315
360
  return new llvm::LoadInst(addr.getElementType(), addr.getPointer(), name,
316
360
                            false, addr.getAlignment().getAsAlign(),
317
360
                            beforeInst);
318
360
}
319
320
/// All the branch fixups on the EH stack have propagated out past the
321
/// outermost normal cleanup; resolve them all by adding cases to the
322
/// given switch instruction.
323
static void ResolveAllBranchFixups(CodeGenFunction &CGF,
324
                                   llvm::SwitchInst *Switch,
325
4
                                   llvm::BasicBlock *CleanupEntry) {
326
4
  llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded;
327
328
8
  for (unsigned I = 0, E = CGF.EHStack.getNumBranchFixups(); I != E; 
++I4
) {
329
    // Skip this fixup if its destination isn't set.
330
4
    BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I);
331
4
    if (Fixup.Destination == nullptr) 
continue0
;
332
333
    // If there isn't an OptimisticBranchBlock, then InitialBranch is
334
    // still pointing directly to its destination; forward it to the
335
    // appropriate cleanup entry.  This is required in the specific
336
    // case of
337
    //   { std::string s; goto lbl; }
338
    //   lbl:
339
    // i.e. where there's an unresolved fixup inside a single cleanup
340
    // entry which we're currently popping.
341
4
    if (Fixup.OptimisticBranchBlock == nullptr) {
342
2
      createStoreInstBefore(CGF.Builder.getInt32(Fixup.DestinationIndex),
343
2
                            CGF.getNormalCleanupDestSlot(),
344
2
                            Fixup.InitialBranch);
345
2
      Fixup.InitialBranch->setSuccessor(0, CleanupEntry);
346
2
    }
347
348
    // Don't add this case to the switch statement twice.
349
4
    if (!CasesAdded.insert(Fixup.Destination).second)
350
0
      continue;
351
352
4
    Switch->addCase(CGF.Builder.getInt32(Fixup.DestinationIndex),
353
4
                    Fixup.Destination);
354
4
  }
355
356
4
  CGF.EHStack.clearFixups();
357
4
}
358
359
/// Transitions the terminator of the given exit-block of a cleanup to
360
/// be a cleanup switch.
361
static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF,
362
4
                                                   llvm::BasicBlock *Block) {
363
  // If it's a branch, turn it into a switch whose default
364
  // destination is its original target.
365
4
  llvm::Instruction *Term = Block->getTerminator();
366
4
  assert(Term && "can't transition block without terminator");
367
368
4
  if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
369
4
    assert(Br->isUnconditional());
370
0
    auto Load = createLoadInstBefore(CGF.getNormalCleanupDestSlot(),
371
4
                                     "cleanup.dest", Term);
372
4
    llvm::SwitchInst *Switch =
373
4
      llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block);
374
4
    Br->eraseFromParent();
375
4
    return Switch;
376
4
  } else {
377
0
    return cast<llvm::SwitchInst>(Term);
378
0
  }
379
4
}
380
381
142
void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) {
382
142
  assert(Block && "resolving a null target block");
383
142
  if (!EHStack.getNumBranchFixups()) 
return137
;
384
385
5
  assert(EHStack.hasNormalCleanups() &&
386
5
         "branch fixups exist with no normal cleanups on stack");
387
388
0
  llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
389
5
  bool ResolvedAny = false;
390
391
3.00k
  for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; 
++I3.00k
) {
392
    // Skip this fixup if its destination doesn't match.
393
3.00k
    BranchFixup &Fixup = EHStack.getBranchFixup(I);
394
3.00k
    if (Fixup.Destination != Block) 
continue0
;
395
396
3.00k
    Fixup.Destination = nullptr;
397
3.00k
    ResolvedAny = true;
398
399
    // If it doesn't have an optimistic branch block, LatestBranch is
400
    // already pointing to the right place.
401
3.00k
    llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock;
402
3.00k
    if (!BranchBB)
403
3.00k
      continue;
404
405
    // Don't process the same optimistic branch block twice.
406
4
    if (!ModifiedOptimisticBlocks.insert(BranchBB).second)
407
0
      continue;
408
409
4
    llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB);
410
411
    // Add a case to the switch.
412
4
    Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block);
413
4
  }
414
415
5
  if (ResolvedAny)
416
5
    EHStack.popNullFixups();
417
5
}
418
419
/// Pops cleanup blocks until the given savepoint is reached.
420
void CodeGenFunction::PopCleanupBlocks(
421
    EHScopeStack::stable_iterator Old,
422
1.04M
    std::initializer_list<llvm::Value **> ValuesToReload) {
423
1.04M
  assert(Old.isValid());
424
425
0
  bool HadBranches = false;
426
1.08M
  while (EHStack.stable_begin() != Old) {
427
45.4k
    EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
428
45.4k
    HadBranches |= Scope.hasBranches();
429
430
    // As long as Old strictly encloses the scope's enclosing normal
431
    // cleanup, we're going to emit another normal cleanup which
432
    // fallthrough can propagate through.
433
45.4k
    bool FallThroughIsBranchThrough =
434
45.4k
      Old.strictlyEncloses(Scope.getEnclosingNormalCleanup());
435
436
45.4k
    PopCleanupBlock(FallThroughIsBranchThrough);
437
45.4k
  }
438
439
  // If we didn't have any branches, the insertion point before cleanups must
440
  // dominate the current insertion point and we don't need to reload any
441
  // values.
442
1.04M
  if (!HadBranches)
443
1.03M
    return;
444
445
  // Spill and reload all values that the caller wants to be live at the current
446
  // insertion point.
447
5.68k
  for (llvm::Value **ReloadedValue : ValuesToReload) {
448
137
    auto *Inst = dyn_cast_or_null<llvm::Instruction>(*ReloadedValue);
449
137
    if (!Inst)
450
110
      continue;
451
452
    // Don't spill static allocas, they dominate all cleanups. These are created
453
    // by binding a reference to a local variable or temporary.
454
27
    auto *AI = dyn_cast<llvm::AllocaInst>(Inst);
455
27
    if (AI && 
AI->isStaticAlloca()2
)
456
2
      continue;
457
458
25
    Address Tmp =
459
25
        CreateDefaultAlignTempAlloca(Inst->getType(), "tmp.exprcleanup");
460
461
    // Find an insertion point after Inst and spill it to the temporary.
462
25
    llvm::BasicBlock::iterator InsertBefore;
463
25
    if (auto *Invoke = dyn_cast<llvm::InvokeInst>(Inst))
464
0
      InsertBefore = Invoke->getNormalDest()->getFirstInsertionPt();
465
25
    else
466
25
      InsertBefore = std::next(Inst->getIterator());
467
25
    CGBuilderTy(CGM, &*InsertBefore).CreateStore(Inst, Tmp);
468
469
    // Reload the value at the current insertion point.
470
25
    *ReloadedValue = Builder.CreateLoad(Tmp);
471
25
  }
472
5.68k
}
473
474
/// Pops cleanup blocks until the given savepoint is reached, then add the
475
/// cleanups from the given savepoint in the lifetime-extended cleanups stack.
476
void CodeGenFunction::PopCleanupBlocks(
477
    EHScopeStack::stable_iterator Old, size_t OldLifetimeExtendedSize,
478
1.03M
    std::initializer_list<llvm::Value **> ValuesToReload) {
479
1.03M
  PopCleanupBlocks(Old, ValuesToReload);
480
481
  // Move our deferred cleanups onto the EH stack.
482
1.03M
  for (size_t I = OldLifetimeExtendedSize,
483
1.03M
              E = LifetimeExtendedCleanupStack.size(); I != E; /**/) {
484
    // Alignment should be guaranteed by the vptrs in the individual cleanups.
485
547
    assert((I % alignof(LifetimeExtendedCleanupHeader) == 0) &&
486
547
           "misaligned cleanup stack entry");
487
488
0
    LifetimeExtendedCleanupHeader &Header =
489
547
        reinterpret_cast<LifetimeExtendedCleanupHeader&>(
490
547
            LifetimeExtendedCleanupStack[I]);
491
547
    I += sizeof(Header);
492
493
547
    EHStack.pushCopyOfCleanup(Header.getKind(),
494
547
                              &LifetimeExtendedCleanupStack[I],
495
547
                              Header.getSize());
496
547
    I += Header.getSize();
497
498
547
    if (Header.isConditional()) {
499
28
      Address ActiveFlag =
500
28
          reinterpret_cast<Address &>(LifetimeExtendedCleanupStack[I]);
501
28
      initFullExprCleanupWithFlag(ActiveFlag);
502
28
      I += sizeof(ActiveFlag);
503
28
    }
504
547
  }
505
1.03M
  LifetimeExtendedCleanupStack.resize(OldLifetimeExtendedSize);
506
1.03M
}
507
508
static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF,
509
16.6k
                                           EHCleanupScope &Scope) {
510
16.6k
  assert(Scope.isNormalCleanup());
511
0
  llvm::BasicBlock *Entry = Scope.getNormalBlock();
512
16.6k
  if (!Entry) {
513
8.20k
    Entry = CGF.createBasicBlock("cleanup");
514
8.20k
    Scope.setNormalBlock(Entry);
515
8.20k
  }
516
16.6k
  return Entry;
517
16.6k
}
518
519
/// Attempts to reduce a cleanup's entry block to a fallthrough.  This
520
/// is basically llvm::MergeBlockIntoPredecessor, except
521
/// simplified/optimized for the tighter constraints on cleanup blocks.
522
///
523
/// Returns the new block, whatever it is.
524
static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF,
525
16.2k
                                              llvm::BasicBlock *Entry) {
526
16.2k
  llvm::BasicBlock *Pred = Entry->getSinglePredecessor();
527
16.2k
  if (!Pred) 
return Entry1.76k
;
528
529
14.4k
  llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
530
14.4k
  if (!Br || 
Br->isConditional()14.2k
)
return Entry219
;
531
14.2k
  assert(Br->getSuccessor(0) == Entry);
532
533
  // If we were previously inserting at the end of the cleanup entry
534
  // block, we'll need to continue inserting at the end of the
535
  // predecessor.
536
0
  bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry;
537
14.2k
  assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end());
538
539
  // Kill the branch.
540
0
  Br->eraseFromParent();
541
542
  // Replace all uses of the entry with the predecessor, in case there
543
  // are phis in the cleanup.
544
14.2k
  Entry->replaceAllUsesWith(Pred);
545
546
  // Merge the blocks.
547
14.2k
  Pred->getInstList().splice(Pred->end(), Entry->getInstList());
548
549
  // Kill the entry block.
550
14.2k
  Entry->eraseFromParent();
551
552
14.2k
  if (WasInsertBlock)
553
52
    CGF.Builder.SetInsertPoint(Pred);
554
555
14.2k
  return Pred;
556
14.4k
}
557
558
static void EmitCleanup(CodeGenFunction &CGF,
559
                        EHScopeStack::Cleanup *Fn,
560
                        EHScopeStack::Cleanup::Flags flags,
561
51.6k
                        Address ActiveFlag) {
562
  // If there's an active flag, load it and skip the cleanup if it's
563
  // false.
564
51.6k
  llvm::BasicBlock *ContBB = nullptr;
565
51.6k
  if (ActiveFlag.isValid()) {
566
336
    ContBB = CGF.createBasicBlock("cleanup.done");
567
336
    llvm::BasicBlock *CleanupBB = CGF.createBasicBlock("cleanup.action");
568
336
    llvm::Value *IsActive
569
336
      = CGF.Builder.CreateLoad(ActiveFlag, "cleanup.is_active");
570
336
    CGF.Builder.CreateCondBr(IsActive, CleanupBB, ContBB);
571
336
    CGF.EmitBlock(CleanupBB);
572
336
  }
573
574
  // Ask the cleanup to emit itself.
575
51.6k
  Fn->Emit(CGF, flags);
576
51.6k
  assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?");
577
578
  // Emit the continuation block if there was an active flag.
579
51.6k
  if (ActiveFlag.isValid())
580
336
    CGF.EmitBlock(ContBB);
581
51.6k
}
582
583
static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit,
584
                                          llvm::BasicBlock *From,
585
0
                                          llvm::BasicBlock *To) {
586
  // Exit is the exit block of a cleanup, so it always terminates in
587
  // an unconditional branch or a switch.
588
0
  llvm::Instruction *Term = Exit->getTerminator();
589
590
0
  if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
591
0
    assert(Br->isUnconditional() && Br->getSuccessor(0) == From);
592
0
    Br->setSuccessor(0, To);
593
0
  } else {
594
0
    llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term);
595
0
    for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I)
596
0
      if (Switch->getSuccessor(I) == From)
597
0
        Switch->setSuccessor(I, To);
598
0
  }
599
0
}
600
601
/// We don't need a normal entry block for the given cleanup.
602
/// Optimistic fixup branches can cause these blocks to come into
603
/// existence anyway;  if so, destroy it.
604
///
605
/// The validity of this transformation is very much specific to the
606
/// exact ways in which we form branches to cleanup entries.
607
static void destroyOptimisticNormalEntry(CodeGenFunction &CGF,
608
41.8k
                                         EHCleanupScope &scope) {
609
41.8k
  llvm::BasicBlock *entry = scope.getNormalBlock();
610
41.8k
  if (!entry) 
return41.8k
;
611
612
  // Replace all the uses with unreachable.
613
4
  llvm::BasicBlock *unreachableBB = CGF.getUnreachableBlock();
614
4
  for (llvm::BasicBlock::use_iterator
615
8
         i = entry->use_begin(), e = entry->use_end(); i != e; ) {
616
4
    llvm::Use &use = *i;
617
4
    ++i;
618
619
4
    use.set(unreachableBB);
620
621
    // The only uses should be fixup switches.
622
4
    llvm::SwitchInst *si = cast<llvm::SwitchInst>(use.getUser());
623
4
    if (si->getNumCases() == 1 && si->getDefaultDest() == unreachableBB) {
624
      // Replace the switch with a branch.
625
4
      llvm::BranchInst::Create(si->case_begin()->getCaseSuccessor(), si);
626
627
      // The switch operand is a load from the cleanup-dest alloca.
628
4
      llvm::LoadInst *condition = cast<llvm::LoadInst>(si->getCondition());
629
630
      // Destroy the switch.
631
4
      si->eraseFromParent();
632
633
      // Destroy the load.
634
4
      assert(condition->getOperand(0) == CGF.NormalCleanupDest.getPointer());
635
0
      assert(condition->use_empty());
636
0
      condition->eraseFromParent();
637
4
    }
638
4
  }
639
640
4
  assert(entry->use_empty());
641
0
  delete entry;
642
4
}
643
644
/// Pops a cleanup block.  If the block includes a normal cleanup, the
645
/// current insertion point is threaded through the cleanup, as are
646
/// any branch fixups on the cleanup.
647
50.0k
void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
648
50.0k
  assert(!EHStack.empty() && "cleanup stack is empty!");
649
0
  assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!");
650
0
  EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
651
50.0k
  assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups());
652
653
  // Remember activation information.
654
0
  bool IsActive = Scope.isActive();
655
50.0k
  Address NormalActiveFlag =
656
50.0k
    Scope.shouldTestFlagInNormalCleanup() ? 
Scope.getActiveFlag()247
657
50.0k
                                          : 
Address::invalid()49.8k
;
658
50.0k
  Address EHActiveFlag =
659
50.0k
    Scope.shouldTestFlagInEHCleanup() ? 
Scope.getActiveFlag()320
660
50.0k
                                      : 
Address::invalid()49.7k
;
661
662
  // Check whether we need an EH cleanup.  This is only true if we've
663
  // generated a lazy EH cleanup block.
664
50.0k
  llvm::BasicBlock *EHEntry = Scope.getCachedEHDispatchBlock();
665
50.0k
  assert(Scope.hasEHBranches() == (EHEntry != nullptr));
666
0
  bool RequiresEHCleanup = (EHEntry != nullptr);
667
50.0k
  EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope();
668
669
  // Check the three conditions which might require a normal cleanup:
670
671
  // - whether there are branch fix-ups through this cleanup
672
50.0k
  unsigned FixupDepth = Scope.getFixupDepth();
673
50.0k
  bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
674
675
  // - whether there are branch-throughs or branch-afters
676
50.0k
  bool HasExistingBranches = Scope.hasBranches();
677
678
  // - whether there's a fallthrough
679
50.0k
  llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock();
680
50.0k
  bool HasFallthrough = (FallthroughSource != nullptr && 
IsActive40.6k
);
681
682
  // Branch-through fall-throughs leave the insertion point set to the
683
  // end of the last cleanup, which points to the current scope.  The
684
  // rest of IR gen doesn't need to worry about this; it only happens
685
  // during the execution of PopCleanupBlocks().
686
50.0k
  bool HasPrebranchedFallthrough =
687
50.0k
    (FallthroughSource && 
FallthroughSource->getTerminator()40.6k
);
688
689
  // If this is a normal cleanup, then having a prebranched
690
  // fallthrough implies that the fallthrough source unconditionally
691
  // jumps here.
692
50.0k
  assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough ||
693
50.0k
         (Scope.getNormalBlock() &&
694
50.0k
          FallthroughSource->getTerminator()->getSuccessor(0)
695
50.0k
            == Scope.getNormalBlock()));
696
697
0
  bool RequiresNormalCleanup = false;
698
50.0k
  if (Scope.isNormalCleanup() &&
699
50.0k
      
(43.7k
HasFixups43.7k
||
HasExistingBranches43.7k
||
HasFallthrough35.5k
)) {
700
43.6k
    RequiresNormalCleanup = true;
701
43.6k
  }
702
703
  // If we have a prebranched fallthrough into an inactive normal
704
  // cleanup, rewrite it so that it leads to the appropriate place.
705
50.0k
  if (Scope.isNormalCleanup() && 
HasPrebranchedFallthrough43.7k
&&
!IsActive125
) {
706
0
    llvm::BasicBlock *prebranchDest;
707
708
    // If the prebranch is semantically branching through the next
709
    // cleanup, just forward it to the next block, leaving the
710
    // insertion point in the prebranched block.
711
0
    if (FallthroughIsBranchThrough) {
712
0
      EHScope &enclosing = *EHStack.find(Scope.getEnclosingNormalCleanup());
713
0
      prebranchDest = CreateNormalEntry(*this, cast<EHCleanupScope>(enclosing));
714
715
    // Otherwise, we need to make a new block.  If the normal cleanup
716
    // isn't being used at all, we could actually reuse the normal
717
    // entry block, but this is simpler, and it avoids conflicts with
718
    // dead optimistic fixup branches.
719
0
    } else {
720
0
      prebranchDest = createBasicBlock("forwarded-prebranch");
721
0
      EmitBlock(prebranchDest);
722
0
    }
723
724
0
    llvm::BasicBlock *normalEntry = Scope.getNormalBlock();
725
0
    assert(normalEntry && !normalEntry->use_empty());
726
727
0
    ForwardPrebranchedFallthrough(FallthroughSource,
728
0
                                  normalEntry, prebranchDest);
729
0
  }
730
731
  // If we don't need the cleanup at all, we're done.
732
50.0k
  if (!RequiresNormalCleanup && 
!RequiresEHCleanup6.39k
) {
733
4.65k
    destroyOptimisticNormalEntry(*this, Scope);
734
4.65k
    EHStack.popCleanup(); // safe because there are no fixups
735
4.65k
    assert(EHStack.getNumBranchFixups() == 0 ||
736
4.65k
           EHStack.hasNormalCleanups());
737
0
    return;
738
4.65k
  }
739
740
  // Copy the cleanup emission data out.  This uses either a stack
741
  // array or malloc'd memory, depending on the size, which is
742
  // behavior that SmallVector would provide, if we could use it
743
  // here. Unfortunately, if you ask for a SmallVector<char>, the
744
  // alignment isn't sufficient.
745
45.4k
  auto *CleanupSource = reinterpret_cast<char *>(Scope.getCleanupBuffer());
746
45.4k
  alignas(EHScopeStack::ScopeStackAlignment) char
747
45.4k
      CleanupBufferStack[8 * sizeof(void *)];
748
45.4k
  std::unique_ptr<char[]> CleanupBufferHeap;
749
45.4k
  size_t CleanupSize = Scope.getCleanupSize();
750
45.4k
  EHScopeStack::Cleanup *Fn;
751
752
45.4k
  if (CleanupSize <= sizeof(CleanupBufferStack)) {
753
45.3k
    memcpy(CleanupBufferStack, CleanupSource, CleanupSize);
754
45.3k
    Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferStack);
755
45.3k
  } else {
756
77
    CleanupBufferHeap.reset(new char[CleanupSize]);
757
77
    memcpy(CleanupBufferHeap.get(), CleanupSource, CleanupSize);
758
77
    Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferHeap.get());
759
77
  }
760
761
45.4k
  EHScopeStack::Cleanup::Flags cleanupFlags;
762
45.4k
  if (Scope.isNormalCleanup())
763
43.7k
    cleanupFlags.setIsNormalCleanupKind();
764
45.4k
  if (Scope.isEHCleanup())
765
41.3k
    cleanupFlags.setIsEHCleanupKind();
766
767
  // Under -EHa, invoke seh.scope.end() to mark scope end before dtor
768
45.4k
  bool IsEHa = getLangOpts().EHAsynch && 
!Scope.isLifetimeMarker()20
;
769
45.4k
  const EHPersonality &Personality = EHPersonality::get(*this);
770
45.4k
  if (!RequiresNormalCleanup) {
771
    // Mark CPP scope end for passed-by-value Arg temp
772
    //   per Windows ABI which is "normally" Cleanup in callee
773
1.73k
    if (IsEHa && 
getInvokeDest()4
) {
774
4
      if (Personality.isMSVCXXPersonality())
775
4
        EmitSehCppScopeEnd();
776
4
    }
777
1.73k
    destroyOptimisticNormalEntry(*this, Scope);
778
1.73k
    EHStack.popCleanup();
779
43.6k
  } else {
780
    // If we have a fallthrough and no other need for the cleanup,
781
    // emit it directly.
782
43.6k
    if (HasFallthrough && 
!HasPrebranchedFallthrough35.9k
&&
!HasFixups35.8k
&&
783
43.6k
        
!HasExistingBranches35.8k
) {
784
785
      // mark SEH scope end for fall-through flow
786
35.4k
      if (IsEHa && 
getInvokeDest()11
) {
787
10
        if (Personality.isMSVCXXPersonality())
788
8
          EmitSehCppScopeEnd();
789
2
        else
790
2
          EmitSehTryScopeEnd();
791
10
      }
792
793
35.4k
      destroyOptimisticNormalEntry(*this, Scope);
794
35.4k
      EHStack.popCleanup();
795
796
35.4k
      EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
797
798
    // Otherwise, the best approach is to thread everything through
799
    // the cleanup block and then try to clean up after ourselves.
800
35.4k
    } else {
801
      // Force the entry block to exist.
802
8.20k
      llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope);
803
804
      // I.  Set up the fallthrough edge in.
805
806
8.20k
      CGBuilderTy::InsertPoint savedInactiveFallthroughIP;
807
808
      // If there's a fallthrough, we need to store the cleanup
809
      // destination index.  For fall-throughs this is always zero.
810
8.20k
      if (HasFallthrough) {
811
473
        if (!HasPrebranchedFallthrough)
812
348
          Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot());
813
814
      // Otherwise, save and clear the IP if we don't have fallthrough
815
      // because the cleanup is inactive.
816
7.73k
      } else if (FallthroughSource) {
817
0
        assert(!IsActive && "source without fallthrough for active cleanup");
818
0
        savedInactiveFallthroughIP = Builder.saveAndClearIP();
819
0
      }
820
821
      // II.  Emit the entry block.  This implicitly branches to it if
822
      // we have fallthrough.  All the fixups and existing branches
823
      // should already be branched to it.
824
0
      EmitBlock(NormalEntry);
825
826
      // intercept normal cleanup to mark SEH scope end
827
8.20k
      if (IsEHa) {
828
5
        if (Personality.isMSVCXXPersonality())
829
5
          EmitSehCppScopeEnd();
830
0
        else
831
0
          EmitSehTryScopeEnd();
832
5
      }
833
834
      // III.  Figure out where we're going and build the cleanup
835
      // epilogue.
836
837
8.20k
      bool HasEnclosingCleanups =
838
8.20k
        (Scope.getEnclosingNormalCleanup() != EHStack.stable_end());
839
840
      // Compute the branch-through dest if we need it:
841
      //   - if there are branch-throughs threaded through the scope
842
      //   - if fall-through is a branch-through
843
      //   - if there are fixups that will be optimistically forwarded
844
      //     to the enclosing cleanup
845
8.20k
      llvm::BasicBlock *BranchThroughDest = nullptr;
846
8.20k
      if (Scope.hasBranchThroughs() ||
847
8.20k
          
(5.57k
FallthroughSource5.57k
&&
FallthroughIsBranchThrough205
) ||
848
8.20k
          
(5.57k
HasFixups5.57k
&&
HasEnclosingCleanups14
)) {
849
2.63k
        assert(HasEnclosingCleanups);
850
0
        EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup());
851
2.63k
        BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S));
852
2.63k
      }
853
854
0
      llvm::BasicBlock *FallthroughDest = nullptr;
855
8.20k
      SmallVector<llvm::Instruction*, 2> InstsToAppend;
856
857
      // If there's exactly one branch-after and no other threads,
858
      // we can route it without a switch.
859
8.20k
      if (!Scope.hasBranchThroughs() && 
!HasFixups5.57k
&&
!HasFallthrough5.56k
&&
860
8.20k
          
Scope.getNumBranchAfters() == 15.36k
) {
861
5.35k
        assert(!BranchThroughDest || !IsActive);
862
863
        // Clean up the possibly dead store to the cleanup dest slot.
864
0
        llvm::Instruction *NormalCleanupDestSlot =
865
5.35k
            cast<llvm::Instruction>(getNormalCleanupDestSlot().getPointer());
866
5.35k
        if (NormalCleanupDestSlot->hasOneUse()) {
867
5.25k
          NormalCleanupDestSlot->user_back()->eraseFromParent();
868
5.25k
          NormalCleanupDestSlot->eraseFromParent();
869
5.25k
          NormalCleanupDest = Address::invalid();
870
5.25k
        }
871
872
5.35k
        llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0);
873
5.35k
        InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter));
874
875
      // Build a switch-out if we need it:
876
      //   - if there are branch-afters threaded through the scope
877
      //   - if fall-through is a branch-after
878
      //   - if there are fixups that have nowhere left to go and
879
      //     so must be immediately resolved
880
5.35k
      } else 
if (2.84k
Scope.getNumBranchAfters()2.84k
||
881
2.84k
                 
(2.63k
HasFallthrough2.63k
&&
!FallthroughIsBranchThrough270
) ||
882
2.84k
                 
(2.49k
HasFixups2.49k
&&
!HasEnclosingCleanups12
)) {
883
884
356
        llvm::BasicBlock *Default =
885
356
          (BranchThroughDest ? 
BranchThroughDest146
:
getUnreachableBlock()210
);
886
887
        // TODO: base this on the number of branch-afters and fixups
888
356
        const unsigned SwitchCapacity = 10;
889
890
        // pass the abnormal exit flag to Fn (SEH cleanup)
891
356
        cleanupFlags.setHasExitSwitch();
892
893
356
        llvm::LoadInst *Load =
894
356
          createLoadInstBefore(getNormalCleanupDestSlot(), "cleanup.dest",
895
356
                               nullptr);
896
356
        llvm::SwitchInst *Switch =
897
356
          llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
898
899
356
        InstsToAppend.push_back(Load);
900
356
        InstsToAppend.push_back(Switch);
901
902
        // Branch-after fallthrough.
903
356
        if (FallthroughSource && 
!FallthroughIsBranchThrough349
) {
904
348
          FallthroughDest = createBasicBlock("cleanup.cont");
905
348
          if (HasFallthrough)
906
348
            Switch->addCase(Builder.getInt32(0), FallthroughDest);
907
348
        }
908
909
575
        for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; 
++I219
) {
910
219
          Switch->addCase(Scope.getBranchAfterIndex(I),
911
219
                          Scope.getBranchAfterBlock(I));
912
219
        }
913
914
        // If there aren't any enclosing cleanups, we can resolve all
915
        // the fixups now.
916
356
        if (HasFixups && 
!HasEnclosingCleanups4
)
917
4
          ResolveAllBranchFixups(*this, Switch, NormalEntry);
918
2.49k
      } else {
919
        // We should always have a branch-through destination in this case.
920
2.49k
        assert(BranchThroughDest);
921
0
        InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest));
922
2.49k
      }
923
924
      // IV.  Pop the cleanup and emit it.
925
0
      EHStack.popCleanup();
926
8.20k
      assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
927
928
0
      EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
929
930
      // Append the prepared cleanup prologue from above.
931
8.20k
      llvm::BasicBlock *NormalExit = Builder.GetInsertBlock();
932
16.7k
      for (unsigned I = 0, E = InstsToAppend.size(); I != E; 
++I8.56k
)
933
8.56k
        NormalExit->getInstList().push_back(InstsToAppend[I]);
934
935
      // Optimistically hope that any fixups will continue falling through.
936
8.20k
      for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
937
8.21k
           I < E; 
++I10
) {
938
10
        BranchFixup &Fixup = EHStack.getBranchFixup(I);
939
10
        if (!Fixup.Destination) 
continue0
;
940
10
        if (!Fixup.OptimisticBranchBlock) {
941
6
          createStoreInstBefore(Builder.getInt32(Fixup.DestinationIndex),
942
6
                                getNormalCleanupDestSlot(),
943
6
                                Fixup.InitialBranch);
944
6
          Fixup.InitialBranch->setSuccessor(0, NormalEntry);
945
6
        }
946
10
        Fixup.OptimisticBranchBlock = NormalExit;
947
10
      }
948
949
      // V.  Set up the fallthrough edge out.
950
951
      // Case 1: a fallthrough source exists but doesn't branch to the
952
      // cleanup because the cleanup is inactive.
953
8.20k
      if (!HasFallthrough && 
FallthroughSource7.73k
) {
954
        // Prebranched fallthrough was forwarded earlier.
955
        // Non-prebranched fallthrough doesn't need to be forwarded.
956
        // Either way, all we need to do is restore the IP we cleared before.
957
0
        assert(!IsActive);
958
0
        Builder.restoreIP(savedInactiveFallthroughIP);
959
960
      // Case 2: a fallthrough source exists and should branch to the
961
      // cleanup, but we're not supposed to branch through to the next
962
      // cleanup.
963
8.20k
      } else if (HasFallthrough && 
FallthroughDest473
) {
964
348
        assert(!FallthroughIsBranchThrough);
965
0
        EmitBlock(FallthroughDest);
966
967
      // Case 3: a fallthrough source exists and should branch to the
968
      // cleanup and then through to the next.
969
7.85k
      } else if (HasFallthrough) {
970
        // Everything is already set up for this.
971
972
      // Case 4: no fallthrough source exists.
973
7.73k
      } else {
974
7.73k
        Builder.ClearInsertionPoint();
975
7.73k
      }
976
977
      // VI.  Assorted cleaning.
978
979
      // Check whether we can merge NormalEntry into a single predecessor.
980
      // This might invalidate (non-IR) pointers to NormalEntry.
981
0
      llvm::BasicBlock *NewNormalEntry =
982
8.20k
        SimplifyCleanupEntry(*this, NormalEntry);
983
984
      // If it did invalidate those pointers, and NormalEntry was the same
985
      // as NormalExit, go back and patch up the fixups.
986
8.20k
      if (NewNormalEntry != NormalEntry && 
NormalEntry == NormalExit7.75k
)
987
6.59k
        for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
988
6.59k
               I < E; 
++I5
)
989
5
          EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
990
8.20k
    }
991
43.6k
  }
992
993
0
  assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
994
995
  // Emit the EH cleanup if required.
996
45.4k
  if (RequiresEHCleanup) {
997
8.00k
    CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
998
999
8.00k
    EmitBlock(EHEntry);
1000
1001
8.00k
    llvm::BasicBlock *NextAction = getEHDispatchBlock(EHParent);
1002
1003
    // Push a terminate scope or cleanupendpad scope around the potentially
1004
    // throwing cleanups. For funclet EH personalities, the cleanupendpad models
1005
    // program termination when cleanups throw.
1006
8.00k
    bool PushedTerminate = false;
1007
8.00k
    SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
1008
8.00k
        CurrentFuncletPad);
1009
8.00k
    llvm::CleanupPadInst *CPI = nullptr;
1010
1011
8.00k
    const EHPersonality &Personality = EHPersonality::get(*this);
1012
8.00k
    if (Personality.usesFuncletPads()) {
1013
283
      llvm::Value *ParentPad = CurrentFuncletPad;
1014
283
      if (!ParentPad)
1015
217
        ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext());
1016
283
      CurrentFuncletPad = CPI = Builder.CreateCleanupPad(ParentPad);
1017
283
    }
1018
1019
    // Non-MSVC personalities need to terminate when an EH cleanup throws.
1020
8.00k
    if (!Personality.isMSVCPersonality()) {
1021
7.81k
      EHStack.pushTerminate();
1022
7.81k
      PushedTerminate = true;
1023
7.81k
    }
1024
1025
    // We only actually emit the cleanup code if the cleanup is either
1026
    // active or was used before it was deactivated.
1027
8.00k
    if (EHActiveFlag.isValid() || 
IsActive7.91k
) {
1028
8.00k
      cleanupFlags.setIsForEHCleanup();
1029
8.00k
      EmitCleanup(*this, Fn, cleanupFlags, EHActiveFlag);
1030
8.00k
    }
1031
1032
8.00k
    if (CPI)
1033
283
      Builder.CreateCleanupRet(CPI, NextAction);
1034
7.72k
    else
1035
7.72k
      Builder.CreateBr(NextAction);
1036
1037
    // Leave the terminate scope.
1038
8.00k
    if (PushedTerminate)
1039
7.81k
      EHStack.popTerminate();
1040
1041
8.00k
    Builder.restoreIP(SavedIP);
1042
1043
8.00k
    SimplifyCleanupEntry(*this, EHEntry);
1044
8.00k
  }
1045
45.4k
}
1046
1047
/// isObviouslyBranchWithoutCleanups - Return true if a branch to the
1048
/// specified destination obviously has no cleanups to run.  'false' is always
1049
/// a conservatively correct answer for this method.
1050
15
bool CodeGenFunction::isObviouslyBranchWithoutCleanups(JumpDest Dest) const {
1051
15
  assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
1052
15
         && "stale jump destination");
1053
1054
  // Calculate the innermost active normal cleanup.
1055
0
  EHScopeStack::stable_iterator TopCleanup =
1056
15
    EHStack.getInnermostActiveNormalCleanup();
1057
1058
  // If we're not in an active normal cleanup scope, or if the
1059
  // destination scope is within the innermost active normal cleanup
1060
  // scope, we don't need to worry about fixups.
1061
15
  if (TopCleanup == EHStack.stable_end() ||
1062
15
      
TopCleanup.encloses(Dest.getScopeDepth())0
) // works for invalid
1063
15
    return true;
1064
1065
  // Otherwise, we might need some cleanups.
1066
0
  return false;
1067
15
}
1068
1069
1070
/// Terminate the current block by emitting a branch which might leave
1071
/// the current cleanup-protected scope.  The target scope may not yet
1072
/// be known, in which case this will require a fixup.
1073
///
1074
/// As a side-effect, this method clears the insertion point.
1075
237k
void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {
1076
237k
  assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
1077
237k
         && "stale jump destination");
1078
1079
237k
  if (!HaveInsertPoint())
1080
212
    return;
1081
1082
  // Create the branch.
1083
237k
  llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
1084
1085
  // Calculate the innermost active normal cleanup.
1086
237k
  EHScopeStack::stable_iterator
1087
237k
    TopCleanup = EHStack.getInnermostActiveNormalCleanup();
1088
1089
  // If we're not in an active normal cleanup scope, or if the
1090
  // destination scope is within the innermost active normal cleanup
1091
  // scope, we don't need to worry about fixups.
1092
237k
  if (TopCleanup == EHStack.stable_end() ||
1093
237k
      
TopCleanup.encloses(Dest.getScopeDepth())9.89k
) { // works for invalid
1094
228k
    Builder.ClearInsertionPoint();
1095
228k
    return;
1096
228k
  }
1097
1098
  // If we can't resolve the destination cleanup scope, just add this
1099
  // to the current cleanup scope as a branch fixup.
1100
8.84k
  if (!Dest.getScopeDepth().isValid()) {
1101
3.00k
    BranchFixup &Fixup = EHStack.addBranchFixup();
1102
3.00k
    Fixup.Destination = Dest.getBlock();
1103
3.00k
    Fixup.DestinationIndex = Dest.getDestIndex();
1104
3.00k
    Fixup.InitialBranch = BI;
1105
3.00k
    Fixup.OptimisticBranchBlock = nullptr;
1106
1107
3.00k
    Builder.ClearInsertionPoint();
1108
3.00k
    return;
1109
3.00k
  }
1110
1111
  // Otherwise, thread through all the normal cleanups in scope.
1112
1113
  // Store the index at the start.
1114
5.83k
  llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
1115
5.83k
  createStoreInstBefore(Index, getNormalCleanupDestSlot(), BI);
1116
1117
  // Adjust BI to point to the first cleanup block.
1118
5.83k
  {
1119
5.83k
    EHCleanupScope &Scope =
1120
5.83k
      cast<EHCleanupScope>(*EHStack.find(TopCleanup));
1121
5.83k
    BI->setSuccessor(0, CreateNormalEntry(*this, Scope));
1122
5.83k
  }
1123
1124
  // Add this destination to all the scopes involved.
1125
5.83k
  EHScopeStack::stable_iterator I = TopCleanup;
1126
5.83k
  EHScopeStack::stable_iterator E = Dest.getScopeDepth();
1127
5.83k
  if (E.strictlyEncloses(I)) {
1128
8.46k
    while (true) {
1129
8.46k
      EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
1130
8.46k
      assert(Scope.isNormalCleanup());
1131
0
      I = Scope.getEnclosingNormalCleanup();
1132
1133
      // If this is the last cleanup we're propagating through, tell it
1134
      // that there's a resolved jump moving through it.
1135
8.46k
      if (!E.strictlyEncloses(I)) {
1136
5.69k
        Scope.addBranchAfter(Index, Dest.getBlock());
1137
5.69k
        break;
1138
5.69k
      }
1139
1140
      // Otherwise, tell the scope that there's a jump propagating
1141
      // through it.  If this isn't new information, all the rest of
1142
      // the work has been done before.
1143
2.76k
      if (!Scope.addBranchThrough(Dest.getBlock()))
1144
141
        break;
1145
2.76k
    }
1146
5.83k
  }
1147
1148
5.83k
  Builder.ClearInsertionPoint();
1149
5.83k
}
1150
1151
static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack,
1152
2
                                  EHScopeStack::stable_iterator C) {
1153
  // If we needed a normal block for any reason, that counts.
1154
2
  if (cast<EHCleanupScope>(*EHStack.find(C)).getNormalBlock())
1155
0
    return true;
1156
1157
  // Check whether any enclosed cleanups were needed.
1158
2
  for (EHScopeStack::stable_iterator
1159
2
         I = EHStack.getInnermostNormalCleanup();
1160
3
         I != C; ) {
1161
1
    assert(C.strictlyEncloses(I));
1162
0
    EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
1163
1
    if (S.getNormalBlock()) 
return true0
;
1164
1
    I = S.getEnclosingNormalCleanup();
1165
1
  }
1166
1167
2
  return false;
1168
2
}
1169
1170
static bool IsUsedAsEHCleanup(EHScopeStack &EHStack,
1171
101
                              EHScopeStack::stable_iterator cleanup) {
1172
  // If we needed an EH block for any reason, that counts.
1173
101
  if (EHStack.find(cleanup)->hasEHBranches())
1174
61
    return true;
1175
1176
  // Check whether any enclosed cleanups were needed.
1177
40
  for (EHScopeStack::stable_iterator
1178
57
         i = EHStack.getInnermostEHScope(); i != cleanup; ) {
1179
21
    assert(cleanup.strictlyEncloses(i));
1180
1181
0
    EHScope &scope = *EHStack.find(i);
1182
21
    if (scope.hasEHBranches())
1183
4
      return true;
1184
1185
17
    i = scope.getEnclosingEHScope();
1186
17
  }
1187
1188
36
  return false;
1189
40
}
1190
1191
enum ForActivation_t {
1192
  ForActivation,
1193
  ForDeactivation
1194
};
1195
1196
/// The given cleanup block is changing activation state.  Configure a
1197
/// cleanup variable if necessary.
1198
///
1199
/// It would be good if we had some way of determining if there were
1200
/// extra uses *after* the change-over point.
1201
static void SetupCleanupBlockActivation(CodeGenFunction &CGF,
1202
                                        EHScopeStack::stable_iterator C,
1203
                                        ForActivation_t kind,
1204
102
                                        llvm::Instruction *dominatingIP) {
1205
102
  EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C));
1206
1207
  // We always need the flag if we're activating the cleanup in a
1208
  // conditional context, because we have to assume that the current
1209
  // location doesn't necessarily dominate the cleanup's code.
1210
102
  bool isActivatedInConditional =
1211
102
    (kind == ForActivation && 
CGF.isInConditionalBranch()0
);
1212
1213
102
  bool needFlag = false;
1214
1215
  // Calculate whether the cleanup was used:
1216
1217
  //   - as a normal cleanup
1218
102
  if (Scope.isNormalCleanup() &&
1219
102
      
(2
isActivatedInConditional2
||
IsUsedAsNormalCleanup(CGF.EHStack, C)2
)) {
1220
0
    Scope.setTestFlagInNormalCleanup();
1221
0
    needFlag = true;
1222
0
  }
1223
1224
  //  - as an EH cleanup
1225
102
  if (Scope.isEHCleanup() &&
1226
102
      
(101
isActivatedInConditional101
||
IsUsedAsEHCleanup(CGF.EHStack, C)101
)) {
1227
65
    Scope.setTestFlagInEHCleanup();
1228
65
    needFlag = true;
1229
65
  }
1230
1231
  // If it hasn't yet been used as either, we're done.
1232
102
  if (!needFlag) 
return37
;
1233
1234
65
  Address var = Scope.getActiveFlag();
1235
65
  if (!var.isValid()) {
1236
57
    var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), CharUnits::One(),
1237
57
                               "cleanup.isactive");
1238
57
    Scope.setActiveFlag(var);
1239
1240
57
    assert(dominatingIP && "no existing variable and no dominating IP!");
1241
1242
    // Initialize to true or false depending on whether it was
1243
    // active up to this point.
1244
0
    llvm::Constant *value = CGF.Builder.getInt1(kind == ForDeactivation);
1245
1246
    // If we're in a conditional block, ignore the dominating IP and
1247
    // use the outermost conditional branch.
1248
57
    if (CGF.isInConditionalBranch()) {
1249
0
      CGF.setBeforeOutermostConditional(value, var);
1250
57
    } else {
1251
57
      createStoreInstBefore(value, var, dominatingIP);
1252
57
    }
1253
57
  }
1254
1255
0
  CGF.Builder.CreateStore(CGF.Builder.getInt1(kind == ForActivation), var);
1256
65
}
1257
1258
/// Activate a cleanup that was created in an inactivated state.
1259
void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C,
1260
0
                                           llvm::Instruction *dominatingIP) {
1261
0
  assert(C != EHStack.stable_end() && "activating bottom of stack?");
1262
0
  EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
1263
0
  assert(!Scope.isActive() && "double activation");
1264
1265
0
  SetupCleanupBlockActivation(*this, C, ForActivation, dominatingIP);
1266
1267
0
  Scope.setActive(true);
1268
0
}
1269
1270
/// Deactive a cleanup that was created in an active state.
1271
void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C,
1272
1.61k
                                             llvm::Instruction *dominatingIP) {
1273
1.61k
  assert(C != EHStack.stable_end() && "deactivating bottom of stack?");
1274
0
  EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
1275
1.61k
  assert(Scope.isActive() && "double deactivation");
1276
1277
  // If it's the top of the stack, just pop it, but do so only if it belongs
1278
  // to the current RunCleanupsScope.
1279
1.61k
  if (C == EHStack.stable_begin() &&
1280
1.61k
      
CurrentCleanupScopeDepth.strictlyEncloses(C)1.53k
) {
1281
    // Per comment below, checking EHAsynch is not really necessary
1282
    // it's there to assure zero-impact w/o EHAsynch option
1283
1.51k
    if (!Scope.isNormalCleanup() && 
getLangOpts().EHAsynch1.48k
) {
1284
2
      PopCleanupBlock();
1285
1.50k
    } else {
1286
      // If it's a normal cleanup, we need to pretend that the
1287
      // fallthrough is unreachable.
1288
1.50k
      CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1289
1.50k
      PopCleanupBlock();
1290
1.50k
      Builder.restoreIP(SavedIP);
1291
1.50k
    }
1292
1.51k
    return;
1293
1.51k
  }
1294
1295
  // Otherwise, follow the general case.
1296
102
  SetupCleanupBlockActivation(*this, C, ForDeactivation, dominatingIP);
1297
1298
102
  Scope.setActive(false);
1299
102
}
1300
1301
11.9k
Address CodeGenFunction::getNormalCleanupDestSlot() {
1302
11.9k
  if (!NormalCleanupDest.isValid())
1303
5.55k
    NormalCleanupDest =
1304
5.55k
      CreateDefaultAlignTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot");
1305
11.9k
  return NormalCleanupDest;
1306
11.9k
}
1307
1308
/// Emits all the code to cause the given temporary to be cleaned up.
1309
void CodeGenFunction::EmitCXXTemporary(const CXXTemporary *Temporary,
1310
                                       QualType TempType,
1311
783
                                       Address Ptr) {
1312
783
  pushDestroy(NormalAndEHCleanup, Ptr, TempType, destroyCXXObject,
1313
783
              /*useEHCleanup*/ true);
1314
783
}
1315
1316
// Need to set "funclet" in OperandBundle properly for noThrow
1317
//       intrinsic (see CGCall.cpp)
1318
static void EmitSehScope(CodeGenFunction &CGF,
1319
38
                         llvm::FunctionCallee &SehCppScope) {
1320
38
  llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
1321
38
  assert(CGF.Builder.GetInsertBlock() && InvokeDest);
1322
0
  llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
1323
38
  SmallVector<llvm::OperandBundleDef, 1> BundleList =
1324
38
      CGF.getBundlesForFunclet(SehCppScope.getCallee());
1325
38
  if (CGF.CurrentFuncletPad)
1326
0
    BundleList.emplace_back("funclet", CGF.CurrentFuncletPad);
1327
38
  CGF.Builder.CreateInvoke(SehCppScope, Cont, InvokeDest, None, BundleList);
1328
38
  CGF.EmitBlock(Cont);
1329
38
}
1330
1331
// Invoke a llvm.seh.scope.begin at the beginning of a CPP scope for -EHa
1332
19
void CodeGenFunction::EmitSehCppScopeBegin() {
1333
19
  assert(getLangOpts().EHAsynch);
1334
0
  llvm::FunctionType *FTy =
1335
19
      llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1336
19
  llvm::FunctionCallee SehCppScope =
1337
19
      CGM.CreateRuntimeFunction(FTy, "llvm.seh.scope.begin");
1338
19
  EmitSehScope(*this, SehCppScope);
1339
19
}
1340
1341
// Invoke a llvm.seh.scope.end at the end of a CPP scope for -EHa
1342
//   llvm.seh.scope.end is emitted before popCleanup, so it's "invoked"
1343
17
void CodeGenFunction::EmitSehCppScopeEnd() {
1344
17
  assert(getLangOpts().EHAsynch);
1345
0
  llvm::FunctionType *FTy =
1346
17
      llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1347
17
  llvm::FunctionCallee SehCppScope =
1348
17
      CGM.CreateRuntimeFunction(FTy, "llvm.seh.scope.end");
1349
17
  EmitSehScope(*this, SehCppScope);
1350
17
}
1351
1352
// Invoke a llvm.seh.try.begin at the beginning of a SEH scope for -EHa
1353
0
void CodeGenFunction::EmitSehTryScopeBegin() {
1354
0
  assert(getLangOpts().EHAsynch);
1355
0
  llvm::FunctionType *FTy =
1356
0
      llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1357
0
  llvm::FunctionCallee SehCppScope =
1358
0
      CGM.CreateRuntimeFunction(FTy, "llvm.seh.try.begin");
1359
0
  EmitSehScope(*this, SehCppScope);
1360
0
}
1361
1362
// Invoke a llvm.seh.try.end at the end of a SEH scope for -EHa
1363
2
void CodeGenFunction::EmitSehTryScopeEnd() {
1364
2
  assert(getLangOpts().EHAsynch);
1365
0
  llvm::FunctionType *FTy =
1366
2
      llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1367
2
  llvm::FunctionCallee SehCppScope =
1368
2
      CGM.CreateRuntimeFunction(FTy, "llvm.seh.try.end");
1369
2
  EmitSehScope(*this, SehCppScope);
1370
2
}