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