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