CGCleanup.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. //===--- CGCleanup.cpp - Bookkeeping and code emission for cleanups -------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains code dealing with the IR generation for cleanups
  11. // and related information.
  12. //
  13. // A "cleanup" is a piece of code which needs to be executed whenever
  14. // control transfers out of a particular scope. This can be
  15. // conditionalized to occur only on exceptional control flow, only on
  16. // normal control flow, or both.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "CodeGenFunction.h"
  20. #include "CGCleanup.h"
  21. using namespace clang;
  22. using namespace CodeGen;
  23. bool DominatingValue<RValue>::saved_type::needsSaving(RValue rv) {
  24. if (rv.isScalar())
  25. return DominatingLLVMValue::needsSaving(rv.getScalarVal());
  26. if (rv.isAggregate())
  27. return DominatingLLVMValue::needsSaving(rv.getAggregateAddr());
  28. return true;
  29. }
  30. DominatingValue<RValue>::saved_type
  31. DominatingValue<RValue>::saved_type::save(CodeGenFunction &CGF, RValue rv) {
  32. if (rv.isScalar()) {
  33. llvm::Value *V = rv.getScalarVal();
  34. // These automatically dominate and don't need to be saved.
  35. if (!DominatingLLVMValue::needsSaving(V))
  36. return saved_type(V, ScalarLiteral);
  37. // Everything else needs an alloca.
  38. llvm::Value *addr = CGF.CreateTempAlloca(V->getType(), "saved-rvalue");
  39. CGF.Builder.CreateStore(V, addr);
  40. return saved_type(addr, ScalarAddress);
  41. }
  42. if (rv.isComplex()) {
  43. CodeGenFunction::ComplexPairTy V = rv.getComplexVal();
  44. llvm::Type *ComplexTy =
  45. llvm::StructType::get(V.first->getType(), V.second->getType(),
  46. (void*) 0);
  47. llvm::Value *addr = CGF.CreateTempAlloca(ComplexTy, "saved-complex");
  48. CGF.StoreComplexToAddr(V, addr, /*volatile*/ false);
  49. return saved_type(addr, ComplexAddress);
  50. }
  51. assert(rv.isAggregate());
  52. llvm::Value *V = rv.getAggregateAddr(); // TODO: volatile?
  53. if (!DominatingLLVMValue::needsSaving(V))
  54. return saved_type(V, AggregateLiteral);
  55. llvm::Value *addr = CGF.CreateTempAlloca(V->getType(), "saved-rvalue");
  56. CGF.Builder.CreateStore(V, addr);
  57. return saved_type(addr, AggregateAddress);
  58. }
  59. /// Given a saved r-value produced by SaveRValue, perform the code
  60. /// necessary to restore it to usability at the current insertion
  61. /// point.
  62. RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) {
  63. switch (K) {
  64. case ScalarLiteral:
  65. return RValue::get(Value);
  66. case ScalarAddress:
  67. return RValue::get(CGF.Builder.CreateLoad(Value));
  68. case AggregateLiteral:
  69. return RValue::getAggregate(Value);
  70. case AggregateAddress:
  71. return RValue::getAggregate(CGF.Builder.CreateLoad(Value));
  72. case ComplexAddress:
  73. return RValue::getComplex(CGF.LoadComplexFromAddr(Value, false));
  74. }
  75. llvm_unreachable("bad saved r-value kind");
  76. }
  77. /// Push an entry of the given size onto this protected-scope stack.
  78. char *EHScopeStack::allocate(size_t Size) {
  79. if (!StartOfBuffer) {
  80. unsigned Capacity = 1024;
  81. while (Capacity < Size) Capacity *= 2;
  82. StartOfBuffer = new char[Capacity];
  83. StartOfData = EndOfBuffer = StartOfBuffer + Capacity;
  84. } else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) {
  85. unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer;
  86. unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer);
  87. unsigned NewCapacity = CurrentCapacity;
  88. do {
  89. NewCapacity *= 2;
  90. } while (NewCapacity < UsedCapacity + Size);
  91. char *NewStartOfBuffer = new char[NewCapacity];
  92. char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity;
  93. char *NewStartOfData = NewEndOfBuffer - UsedCapacity;
  94. memcpy(NewStartOfData, StartOfData, UsedCapacity);
  95. delete [] StartOfBuffer;
  96. StartOfBuffer = NewStartOfBuffer;
  97. EndOfBuffer = NewEndOfBuffer;
  98. StartOfData = NewStartOfData;
  99. }
  100. assert(StartOfBuffer + Size <= StartOfData);
  101. StartOfData -= Size;
  102. return StartOfData;
  103. }
  104. EHScopeStack::stable_iterator
  105. EHScopeStack::getInnermostActiveNormalCleanup() const {
  106. for (stable_iterator si = getInnermostNormalCleanup(), se = stable_end();
  107. si != se; ) {
  108. EHCleanupScope &cleanup = cast<EHCleanupScope>(*find(si));
  109. if (cleanup.isActive()) return si;
  110. si = cleanup.getEnclosingNormalCleanup();
  111. }
  112. return stable_end();
  113. }
  114. EHScopeStack::stable_iterator EHScopeStack::getInnermostActiveEHScope() const {
  115. for (stable_iterator si = getInnermostEHScope(), se = stable_end();
  116. si != se; ) {
  117. // Skip over inactive cleanups.
  118. EHCleanupScope *cleanup = dyn_cast<EHCleanupScope>(&*find(si));
  119. if (cleanup && !cleanup->isActive()) {
  120. si = cleanup->getEnclosingEHScope();
  121. continue;
  122. }
  123. // All other scopes are always active.
  124. return si;
  125. }
  126. return stable_end();
  127. }
  128. void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) {
  129. assert(((Size % sizeof(void*)) == 0) && "cleanup type is misaligned");
  130. char *Buffer = allocate(EHCleanupScope::getSizeForCleanupSize(Size));
  131. bool IsNormalCleanup = Kind & NormalCleanup;
  132. bool IsEHCleanup = Kind & EHCleanup;
  133. bool IsActive = !(Kind & InactiveCleanup);
  134. EHCleanupScope *Scope =
  135. new (Buffer) EHCleanupScope(IsNormalCleanup,
  136. IsEHCleanup,
  137. IsActive,
  138. Size,
  139. BranchFixups.size(),
  140. InnermostNormalCleanup,
  141. InnermostEHScope);
  142. if (IsNormalCleanup)
  143. InnermostNormalCleanup = stable_begin();
  144. if (IsEHCleanup)
  145. InnermostEHScope = stable_begin();
  146. return Scope->getCleanupBuffer();
  147. }
  148. void EHScopeStack::popCleanup() {
  149. assert(!empty() && "popping exception stack when not empty");
  150. assert(isa<EHCleanupScope>(*begin()));
  151. EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin());
  152. InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup();
  153. InnermostEHScope = Cleanup.getEnclosingEHScope();
  154. StartOfData += Cleanup.getAllocatedSize();
  155. // Destroy the cleanup.
  156. Cleanup.~EHCleanupScope();
  157. // Check whether we can shrink the branch-fixups stack.
  158. if (!BranchFixups.empty()) {
  159. // If we no longer have any normal cleanups, all the fixups are
  160. // complete.
  161. if (!hasNormalCleanups())
  162. BranchFixups.clear();
  163. // Otherwise we can still trim out unnecessary nulls.
  164. else
  165. popNullFixups();
  166. }
  167. }
  168. EHFilterScope *EHScopeStack::pushFilter(unsigned numFilters) {
  169. assert(getInnermostEHScope() == stable_end());
  170. char *buffer = allocate(EHFilterScope::getSizeForNumFilters(numFilters));
  171. EHFilterScope *filter = new (buffer) EHFilterScope(numFilters);
  172. InnermostEHScope = stable_begin();
  173. return filter;
  174. }
  175. void EHScopeStack::popFilter() {
  176. assert(!empty() && "popping exception stack when not empty");
  177. EHFilterScope &filter = cast<EHFilterScope>(*begin());
  178. StartOfData += EHFilterScope::getSizeForNumFilters(filter.getNumFilters());
  179. InnermostEHScope = filter.getEnclosingEHScope();
  180. }
  181. EHCatchScope *EHScopeStack::pushCatch(unsigned numHandlers) {
  182. char *buffer = allocate(EHCatchScope::getSizeForNumHandlers(numHandlers));
  183. EHCatchScope *scope =
  184. new (buffer) EHCatchScope(numHandlers, InnermostEHScope);
  185. InnermostEHScope = stable_begin();
  186. return scope;
  187. }
  188. void EHScopeStack::pushTerminate() {
  189. char *Buffer = allocate(EHTerminateScope::getSize());
  190. new (Buffer) EHTerminateScope(InnermostEHScope);
  191. InnermostEHScope = stable_begin();
  192. }
  193. /// Remove any 'null' fixups on the stack. However, we can't pop more
  194. /// fixups than the fixup depth on the innermost normal cleanup, or
  195. /// else fixups that we try to add to that cleanup will end up in the
  196. /// wrong place. We *could* try to shrink fixup depths, but that's
  197. /// actually a lot of work for little benefit.
  198. void EHScopeStack::popNullFixups() {
  199. // We expect this to only be called when there's still an innermost
  200. // normal cleanup; otherwise there really shouldn't be any fixups.
  201. assert(hasNormalCleanups());
  202. EHScopeStack::iterator it = find(InnermostNormalCleanup);
  203. unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth();
  204. assert(BranchFixups.size() >= MinSize && "fixup stack out of order");
  205. while (BranchFixups.size() > MinSize &&
  206. BranchFixups.back().Destination == 0)
  207. BranchFixups.pop_back();
  208. }
  209. void CodeGenFunction::initFullExprCleanup() {
  210. // Create a variable to decide whether the cleanup needs to be run.
  211. llvm::AllocaInst *active
  212. = CreateTempAlloca(Builder.getInt1Ty(), "cleanup.cond");
  213. // Initialize it to false at a site that's guaranteed to be run
  214. // before each evaluation.
  215. setBeforeOutermostConditional(Builder.getFalse(), active);
  216. // Initialize it to true at the current location.
  217. Builder.CreateStore(Builder.getTrue(), active);
  218. // Set that as the active flag in the cleanup.
  219. EHCleanupScope &cleanup = cast<EHCleanupScope>(*EHStack.begin());
  220. assert(cleanup.getActiveFlag() == 0 && "cleanup already has active flag?");
  221. cleanup.setActiveFlag(active);
  222. if (cleanup.isNormalCleanup()) cleanup.setTestFlagInNormalCleanup();
  223. if (cleanup.isEHCleanup()) cleanup.setTestFlagInEHCleanup();
  224. }
  225. void EHScopeStack::Cleanup::anchor() {}
  226. /// All the branch fixups on the EH stack have propagated out past the
  227. /// outermost normal cleanup; resolve them all by adding cases to the
  228. /// given switch instruction.
  229. static void ResolveAllBranchFixups(CodeGenFunction &CGF,
  230. llvm::SwitchInst *Switch,
  231. llvm::BasicBlock *CleanupEntry) {
  232. llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded;
  233. for (unsigned I = 0, E = CGF.EHStack.getNumBranchFixups(); I != E; ++I) {
  234. // Skip this fixup if its destination isn't set.
  235. BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I);
  236. if (Fixup.Destination == 0) continue;
  237. // If there isn't an OptimisticBranchBlock, then InitialBranch is
  238. // still pointing directly to its destination; forward it to the
  239. // appropriate cleanup entry. This is required in the specific
  240. // case of
  241. // { std::string s; goto lbl; }
  242. // lbl:
  243. // i.e. where there's an unresolved fixup inside a single cleanup
  244. // entry which we're currently popping.
  245. if (Fixup.OptimisticBranchBlock == 0) {
  246. new llvm::StoreInst(CGF.Builder.getInt32(Fixup.DestinationIndex),
  247. CGF.getNormalCleanupDestSlot(),
  248. Fixup.InitialBranch);
  249. Fixup.InitialBranch->setSuccessor(0, CleanupEntry);
  250. }
  251. // Don't add this case to the switch statement twice.
  252. if (!CasesAdded.insert(Fixup.Destination)) continue;
  253. Switch->addCase(CGF.Builder.getInt32(Fixup.DestinationIndex),
  254. Fixup.Destination);
  255. }
  256. CGF.EHStack.clearFixups();
  257. }
  258. /// Transitions the terminator of the given exit-block of a cleanup to
  259. /// be a cleanup switch.
  260. static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF,
  261. llvm::BasicBlock *Block) {
  262. // If it's a branch, turn it into a switch whose default
  263. // destination is its original target.
  264. llvm::TerminatorInst *Term = Block->getTerminator();
  265. assert(Term && "can't transition block without terminator");
  266. if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
  267. assert(Br->isUnconditional());
  268. llvm::LoadInst *Load =
  269. new llvm::LoadInst(CGF.getNormalCleanupDestSlot(), "cleanup.dest", Term);
  270. llvm::SwitchInst *Switch =
  271. llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block);
  272. Br->eraseFromParent();
  273. return Switch;
  274. } else {
  275. return cast<llvm::SwitchInst>(Term);
  276. }
  277. }
  278. void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) {
  279. assert(Block && "resolving a null target block");
  280. if (!EHStack.getNumBranchFixups()) return;
  281. assert(EHStack.hasNormalCleanups() &&
  282. "branch fixups exist with no normal cleanups on stack");
  283. llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
  284. bool ResolvedAny = false;
  285. for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
  286. // Skip this fixup if its destination doesn't match.
  287. BranchFixup &Fixup = EHStack.getBranchFixup(I);
  288. if (Fixup.Destination != Block) continue;
  289. Fixup.Destination = 0;
  290. ResolvedAny = true;
  291. // If it doesn't have an optimistic branch block, LatestBranch is
  292. // already pointing to the right place.
  293. llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock;
  294. if (!BranchBB)
  295. continue;
  296. // Don't process the same optimistic branch block twice.
  297. if (!ModifiedOptimisticBlocks.insert(BranchBB))
  298. continue;
  299. llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB);
  300. // Add a case to the switch.
  301. Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block);
  302. }
  303. if (ResolvedAny)
  304. EHStack.popNullFixups();
  305. }
  306. /// Pops cleanup blocks until the given savepoint is reached.
  307. void CodeGenFunction::PopCleanupBlocks(EHScopeStack::stable_iterator Old) {
  308. assert(Old.isValid());
  309. while (EHStack.stable_begin() != Old) {
  310. EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
  311. // As long as Old strictly encloses the scope's enclosing normal
  312. // cleanup, we're going to emit another normal cleanup which
  313. // fallthrough can propagate through.
  314. bool FallThroughIsBranchThrough =
  315. Old.strictlyEncloses(Scope.getEnclosingNormalCleanup());
  316. PopCleanupBlock(FallThroughIsBranchThrough);
  317. }
  318. }
  319. static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF,
  320. EHCleanupScope &Scope) {
  321. assert(Scope.isNormalCleanup());
  322. llvm::BasicBlock *Entry = Scope.getNormalBlock();
  323. if (!Entry) {
  324. Entry = CGF.createBasicBlock("cleanup");
  325. Scope.setNormalBlock(Entry);
  326. }
  327. return Entry;
  328. }
  329. /// Attempts to reduce a cleanup's entry block to a fallthrough. This
  330. /// is basically llvm::MergeBlockIntoPredecessor, except
  331. /// simplified/optimized for the tighter constraints on cleanup blocks.
  332. ///
  333. /// Returns the new block, whatever it is.
  334. static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF,
  335. llvm::BasicBlock *Entry) {
  336. llvm::BasicBlock *Pred = Entry->getSinglePredecessor();
  337. if (!Pred) return Entry;
  338. llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
  339. if (!Br || Br->isConditional()) return Entry;
  340. assert(Br->getSuccessor(0) == Entry);
  341. // If we were previously inserting at the end of the cleanup entry
  342. // block, we'll need to continue inserting at the end of the
  343. // predecessor.
  344. bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry;
  345. assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end());
  346. // Kill the branch.
  347. Br->eraseFromParent();
  348. // Replace all uses of the entry with the predecessor, in case there
  349. // are phis in the cleanup.
  350. Entry->replaceAllUsesWith(Pred);
  351. // Merge the blocks.
  352. Pred->getInstList().splice(Pred->end(), Entry->getInstList());
  353. // Kill the entry block.
  354. Entry->eraseFromParent();
  355. if (WasInsertBlock)
  356. CGF.Builder.SetInsertPoint(Pred);
  357. return Pred;
  358. }
  359. static void EmitCleanup(CodeGenFunction &CGF,
  360. EHScopeStack::Cleanup *Fn,
  361. EHScopeStack::Cleanup::Flags flags,
  362. llvm::Value *ActiveFlag) {
  363. // EH cleanups always occur within a terminate scope.
  364. if (flags.isForEHCleanup()) CGF.EHStack.pushTerminate();
  365. // If there's an active flag, load it and skip the cleanup if it's
  366. // false.
  367. llvm::BasicBlock *ContBB = 0;
  368. if (ActiveFlag) {
  369. ContBB = CGF.createBasicBlock("cleanup.done");
  370. llvm::BasicBlock *CleanupBB = CGF.createBasicBlock("cleanup.action");
  371. llvm::Value *IsActive
  372. = CGF.Builder.CreateLoad(ActiveFlag, "cleanup.is_active");
  373. CGF.Builder.CreateCondBr(IsActive, CleanupBB, ContBB);
  374. CGF.EmitBlock(CleanupBB);
  375. }
  376. // Ask the cleanup to emit itself.
  377. Fn->Emit(CGF, flags);
  378. assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?");
  379. // Emit the continuation block if there was an active flag.
  380. if (ActiveFlag)
  381. CGF.EmitBlock(ContBB);
  382. // Leave the terminate scope.
  383. if (flags.isForEHCleanup()) CGF.EHStack.popTerminate();
  384. }
  385. static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit,
  386. llvm::BasicBlock *From,
  387. llvm::BasicBlock *To) {
  388. // Exit is the exit block of a cleanup, so it always terminates in
  389. // an unconditional branch or a switch.
  390. llvm::TerminatorInst *Term = Exit->getTerminator();
  391. if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
  392. assert(Br->isUnconditional() && Br->getSuccessor(0) == From);
  393. Br->setSuccessor(0, To);
  394. } else {
  395. llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term);
  396. for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I)
  397. if (Switch->getSuccessor(I) == From)
  398. Switch->setSuccessor(I, To);
  399. }
  400. }
  401. /// We don't need a normal entry block for the given cleanup.
  402. /// Optimistic fixup branches can cause these blocks to come into
  403. /// existence anyway; if so, destroy it.
  404. ///
  405. /// The validity of this transformation is very much specific to the
  406. /// exact ways in which we form branches to cleanup entries.
  407. static void destroyOptimisticNormalEntry(CodeGenFunction &CGF,
  408. EHCleanupScope &scope) {
  409. llvm::BasicBlock *entry = scope.getNormalBlock();
  410. if (!entry) return;
  411. // Replace all the uses with unreachable.
  412. llvm::BasicBlock *unreachableBB = CGF.getUnreachableBlock();
  413. for (llvm::BasicBlock::use_iterator
  414. i = entry->use_begin(), e = entry->use_end(); i != e; ) {
  415. llvm::Use &use = i.getUse();
  416. ++i;
  417. use.set(unreachableBB);
  418. // The only uses should be fixup switches.
  419. llvm::SwitchInst *si = cast<llvm::SwitchInst>(use.getUser());
  420. if (si->getNumCases() == 1 && si->getDefaultDest() == unreachableBB) {
  421. // Replace the switch with a branch.
  422. llvm::BranchInst::Create(si->getCaseSuccessor(0), si);
  423. // The switch operand is a load from the cleanup-dest alloca.
  424. llvm::LoadInst *condition = cast<llvm::LoadInst>(si->getCondition());
  425. // Destroy the switch.
  426. si->eraseFromParent();
  427. // Destroy the load.
  428. assert(condition->getOperand(0) == CGF.NormalCleanupDest);
  429. assert(condition->use_empty());
  430. condition->eraseFromParent();
  431. }
  432. }
  433. assert(entry->use_empty());
  434. delete entry;
  435. }
  436. /// Pops a cleanup block. If the block includes a normal cleanup, the
  437. /// current insertion point is threaded through the cleanup, as are
  438. /// any branch fixups on the cleanup.
  439. void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
  440. assert(!EHStack.empty() && "cleanup stack is empty!");
  441. assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!");
  442. EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
  443. assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups());
  444. // Remember activation information.
  445. bool IsActive = Scope.isActive();
  446. llvm::Value *NormalActiveFlag =
  447. Scope.shouldTestFlagInNormalCleanup() ? Scope.getActiveFlag() : 0;
  448. llvm::Value *EHActiveFlag =
  449. Scope.shouldTestFlagInEHCleanup() ? Scope.getActiveFlag() : 0;
  450. // Check whether we need an EH cleanup. This is only true if we've
  451. // generated a lazy EH cleanup block.
  452. llvm::BasicBlock *EHEntry = Scope.getCachedEHDispatchBlock();
  453. assert(Scope.hasEHBranches() == (EHEntry != 0));
  454. bool RequiresEHCleanup = (EHEntry != 0);
  455. EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope();
  456. // Check the three conditions which might require a normal cleanup:
  457. // - whether there are branch fix-ups through this cleanup
  458. unsigned FixupDepth = Scope.getFixupDepth();
  459. bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
  460. // - whether there are branch-throughs or branch-afters
  461. bool HasExistingBranches = Scope.hasBranches();
  462. // - whether there's a fallthrough
  463. llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock();
  464. bool HasFallthrough = (FallthroughSource != 0 && IsActive);
  465. // Branch-through fall-throughs leave the insertion point set to the
  466. // end of the last cleanup, which points to the current scope. The
  467. // rest of IR gen doesn't need to worry about this; it only happens
  468. // during the execution of PopCleanupBlocks().
  469. bool HasPrebranchedFallthrough =
  470. (FallthroughSource && FallthroughSource->getTerminator());
  471. // If this is a normal cleanup, then having a prebranched
  472. // fallthrough implies that the fallthrough source unconditionally
  473. // jumps here.
  474. assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough ||
  475. (Scope.getNormalBlock() &&
  476. FallthroughSource->getTerminator()->getSuccessor(0)
  477. == Scope.getNormalBlock()));
  478. bool RequiresNormalCleanup = false;
  479. if (Scope.isNormalCleanup() &&
  480. (HasFixups || HasExistingBranches || HasFallthrough)) {
  481. RequiresNormalCleanup = true;
  482. }
  483. // If we have a prebranched fallthrough into an inactive normal
  484. // cleanup, rewrite it so that it leads to the appropriate place.
  485. if (Scope.isNormalCleanup() && HasPrebranchedFallthrough && !IsActive) {
  486. llvm::BasicBlock *prebranchDest;
  487. // If the prebranch is semantically branching through the next
  488. // cleanup, just forward it to the next block, leaving the
  489. // insertion point in the prebranched block.
  490. if (FallthroughIsBranchThrough) {
  491. EHScope &enclosing = *EHStack.find(Scope.getEnclosingNormalCleanup());
  492. prebranchDest = CreateNormalEntry(*this, cast<EHCleanupScope>(enclosing));
  493. // Otherwise, we need to make a new block. If the normal cleanup
  494. // isn't being used at all, we could actually reuse the normal
  495. // entry block, but this is simpler, and it avoids conflicts with
  496. // dead optimistic fixup branches.
  497. } else {
  498. prebranchDest = createBasicBlock("forwarded-prebranch");
  499. EmitBlock(prebranchDest);
  500. }
  501. llvm::BasicBlock *normalEntry = Scope.getNormalBlock();
  502. assert(normalEntry && !normalEntry->use_empty());
  503. ForwardPrebranchedFallthrough(FallthroughSource,
  504. normalEntry, prebranchDest);
  505. }
  506. // If we don't need the cleanup at all, we're done.
  507. if (!RequiresNormalCleanup && !RequiresEHCleanup) {
  508. destroyOptimisticNormalEntry(*this, Scope);
  509. EHStack.popCleanup(); // safe because there are no fixups
  510. assert(EHStack.getNumBranchFixups() == 0 ||
  511. EHStack.hasNormalCleanups());
  512. return;
  513. }
  514. // Copy the cleanup emission data out. Note that SmallVector
  515. // guarantees maximal alignment for its buffer regardless of its
  516. // type parameter.
  517. SmallVector<char, 8*sizeof(void*)> CleanupBuffer;
  518. CleanupBuffer.reserve(Scope.getCleanupSize());
  519. memcpy(CleanupBuffer.data(),
  520. Scope.getCleanupBuffer(), Scope.getCleanupSize());
  521. CleanupBuffer.set_size(Scope.getCleanupSize());
  522. EHScopeStack::Cleanup *Fn =
  523. reinterpret_cast<EHScopeStack::Cleanup*>(CleanupBuffer.data());
  524. EHScopeStack::Cleanup::Flags cleanupFlags;
  525. if (Scope.isNormalCleanup())
  526. cleanupFlags.setIsNormalCleanupKind();
  527. if (Scope.isEHCleanup())
  528. cleanupFlags.setIsEHCleanupKind();
  529. if (!RequiresNormalCleanup) {
  530. destroyOptimisticNormalEntry(*this, Scope);
  531. EHStack.popCleanup();
  532. } else {
  533. // If we have a fallthrough and no other need for the cleanup,
  534. // emit it directly.
  535. if (HasFallthrough && !HasPrebranchedFallthrough &&
  536. !HasFixups && !HasExistingBranches) {
  537. destroyOptimisticNormalEntry(*this, Scope);
  538. EHStack.popCleanup();
  539. EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
  540. // Otherwise, the best approach is to thread everything through
  541. // the cleanup block and then try to clean up after ourselves.
  542. } else {
  543. // Force the entry block to exist.
  544. llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope);
  545. // I. Set up the fallthrough edge in.
  546. CGBuilderTy::InsertPoint savedInactiveFallthroughIP;
  547. // If there's a fallthrough, we need to store the cleanup
  548. // destination index. For fall-throughs this is always zero.
  549. if (HasFallthrough) {
  550. if (!HasPrebranchedFallthrough)
  551. Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot());
  552. // Otherwise, save and clear the IP if we don't have fallthrough
  553. // because the cleanup is inactive.
  554. } else if (FallthroughSource) {
  555. assert(!IsActive && "source without fallthrough for active cleanup");
  556. savedInactiveFallthroughIP = Builder.saveAndClearIP();
  557. }
  558. // II. Emit the entry block. This implicitly branches to it if
  559. // we have fallthrough. All the fixups and existing branches
  560. // should already be branched to it.
  561. EmitBlock(NormalEntry);
  562. // III. Figure out where we're going and build the cleanup
  563. // epilogue.
  564. bool HasEnclosingCleanups =
  565. (Scope.getEnclosingNormalCleanup() != EHStack.stable_end());
  566. // Compute the branch-through dest if we need it:
  567. // - if there are branch-throughs threaded through the scope
  568. // - if fall-through is a branch-through
  569. // - if there are fixups that will be optimistically forwarded
  570. // to the enclosing cleanup
  571. llvm::BasicBlock *BranchThroughDest = 0;
  572. if (Scope.hasBranchThroughs() ||
  573. (FallthroughSource && FallthroughIsBranchThrough) ||
  574. (HasFixups && HasEnclosingCleanups)) {
  575. assert(HasEnclosingCleanups);
  576. EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup());
  577. BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S));
  578. }
  579. llvm::BasicBlock *FallthroughDest = 0;
  580. SmallVector<llvm::Instruction*, 2> InstsToAppend;
  581. // If there's exactly one branch-after and no other threads,
  582. // we can route it without a switch.
  583. if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough &&
  584. Scope.getNumBranchAfters() == 1) {
  585. assert(!BranchThroughDest || !IsActive);
  586. // TODO: clean up the possibly dead stores to the cleanup dest slot.
  587. llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0);
  588. InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter));
  589. // Build a switch-out if we need it:
  590. // - if there are branch-afters threaded through the scope
  591. // - if fall-through is a branch-after
  592. // - if there are fixups that have nowhere left to go and
  593. // so must be immediately resolved
  594. } else if (Scope.getNumBranchAfters() ||
  595. (HasFallthrough && !FallthroughIsBranchThrough) ||
  596. (HasFixups && !HasEnclosingCleanups)) {
  597. llvm::BasicBlock *Default =
  598. (BranchThroughDest ? BranchThroughDest : getUnreachableBlock());
  599. // TODO: base this on the number of branch-afters and fixups
  600. const unsigned SwitchCapacity = 10;
  601. llvm::LoadInst *Load =
  602. new llvm::LoadInst(getNormalCleanupDestSlot(), "cleanup.dest");
  603. llvm::SwitchInst *Switch =
  604. llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
  605. InstsToAppend.push_back(Load);
  606. InstsToAppend.push_back(Switch);
  607. // Branch-after fallthrough.
  608. if (FallthroughSource && !FallthroughIsBranchThrough) {
  609. FallthroughDest = createBasicBlock("cleanup.cont");
  610. if (HasFallthrough)
  611. Switch->addCase(Builder.getInt32(0), FallthroughDest);
  612. }
  613. for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) {
  614. Switch->addCase(Scope.getBranchAfterIndex(I),
  615. Scope.getBranchAfterBlock(I));
  616. }
  617. // If there aren't any enclosing cleanups, we can resolve all
  618. // the fixups now.
  619. if (HasFixups && !HasEnclosingCleanups)
  620. ResolveAllBranchFixups(*this, Switch, NormalEntry);
  621. } else {
  622. // We should always have a branch-through destination in this case.
  623. assert(BranchThroughDest);
  624. InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest));
  625. }
  626. // IV. Pop the cleanup and emit it.
  627. EHStack.popCleanup();
  628. assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
  629. EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
  630. // Append the prepared cleanup prologue from above.
  631. llvm::BasicBlock *NormalExit = Builder.GetInsertBlock();
  632. for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I)
  633. NormalExit->getInstList().push_back(InstsToAppend[I]);
  634. // Optimistically hope that any fixups will continue falling through.
  635. for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
  636. I < E; ++I) {
  637. BranchFixup &Fixup = EHStack.getBranchFixup(I);
  638. if (!Fixup.Destination) continue;
  639. if (!Fixup.OptimisticBranchBlock) {
  640. new llvm::StoreInst(Builder.getInt32(Fixup.DestinationIndex),
  641. getNormalCleanupDestSlot(),
  642. Fixup.InitialBranch);
  643. Fixup.InitialBranch->setSuccessor(0, NormalEntry);
  644. }
  645. Fixup.OptimisticBranchBlock = NormalExit;
  646. }
  647. // V. Set up the fallthrough edge out.
  648. // Case 1: a fallthrough source exists but doesn't branch to the
  649. // cleanup because the cleanup is inactive.
  650. if (!HasFallthrough && FallthroughSource) {
  651. // Prebranched fallthrough was forwarded earlier.
  652. // Non-prebranched fallthrough doesn't need to be forwarded.
  653. // Either way, all we need to do is restore the IP we cleared before.
  654. assert(!IsActive);
  655. Builder.restoreIP(savedInactiveFallthroughIP);
  656. // Case 2: a fallthrough source exists and should branch to the
  657. // cleanup, but we're not supposed to branch through to the next
  658. // cleanup.
  659. } else if (HasFallthrough && FallthroughDest) {
  660. assert(!FallthroughIsBranchThrough);
  661. EmitBlock(FallthroughDest);
  662. // Case 3: a fallthrough source exists and should branch to the
  663. // cleanup and then through to the next.
  664. } else if (HasFallthrough) {
  665. // Everything is already set up for this.
  666. // Case 4: no fallthrough source exists.
  667. } else {
  668. Builder.ClearInsertionPoint();
  669. }
  670. // VI. Assorted cleaning.
  671. // Check whether we can merge NormalEntry into a single predecessor.
  672. // This might invalidate (non-IR) pointers to NormalEntry.
  673. llvm::BasicBlock *NewNormalEntry =
  674. SimplifyCleanupEntry(*this, NormalEntry);
  675. // If it did invalidate those pointers, and NormalEntry was the same
  676. // as NormalExit, go back and patch up the fixups.
  677. if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit)
  678. for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
  679. I < E; ++I)
  680. EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
  681. }
  682. }
  683. assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
  684. // Emit the EH cleanup if required.
  685. if (RequiresEHCleanup) {
  686. CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
  687. EmitBlock(EHEntry);
  688. cleanupFlags.setIsForEHCleanup();
  689. EmitCleanup(*this, Fn, cleanupFlags, EHActiveFlag);
  690. Builder.CreateBr(getEHDispatchBlock(EHParent));
  691. Builder.restoreIP(SavedIP);
  692. SimplifyCleanupEntry(*this, EHEntry);
  693. }
  694. }
  695. /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
  696. /// specified destination obviously has no cleanups to run. 'false' is always
  697. /// a conservatively correct answer for this method.
  698. bool CodeGenFunction::isObviouslyBranchWithoutCleanups(JumpDest Dest) const {
  699. assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
  700. && "stale jump destination");
  701. // Calculate the innermost active normal cleanup.
  702. EHScopeStack::stable_iterator TopCleanup =
  703. EHStack.getInnermostActiveNormalCleanup();
  704. // If we're not in an active normal cleanup scope, or if the
  705. // destination scope is within the innermost active normal cleanup
  706. // scope, we don't need to worry about fixups.
  707. if (TopCleanup == EHStack.stable_end() ||
  708. TopCleanup.encloses(Dest.getScopeDepth())) // works for invalid
  709. return true;
  710. // Otherwise, we might need some cleanups.
  711. return false;
  712. }
  713. /// Terminate the current block by emitting a branch which might leave
  714. /// the current cleanup-protected scope. The target scope may not yet
  715. /// be known, in which case this will require a fixup.
  716. ///
  717. /// As a side-effect, this method clears the insertion point.
  718. void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {
  719. assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
  720. && "stale jump destination");
  721. if (!HaveInsertPoint())
  722. return;
  723. // Create the branch.
  724. llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
  725. // Calculate the innermost active normal cleanup.
  726. EHScopeStack::stable_iterator
  727. TopCleanup = EHStack.getInnermostActiveNormalCleanup();
  728. // If we're not in an active normal cleanup scope, or if the
  729. // destination scope is within the innermost active normal cleanup
  730. // scope, we don't need to worry about fixups.
  731. if (TopCleanup == EHStack.stable_end() ||
  732. TopCleanup.encloses(Dest.getScopeDepth())) { // works for invalid
  733. Builder.ClearInsertionPoint();
  734. return;
  735. }
  736. // If we can't resolve the destination cleanup scope, just add this
  737. // to the current cleanup scope as a branch fixup.
  738. if (!Dest.getScopeDepth().isValid()) {
  739. BranchFixup &Fixup = EHStack.addBranchFixup();
  740. Fixup.Destination = Dest.getBlock();
  741. Fixup.DestinationIndex = Dest.getDestIndex();
  742. Fixup.InitialBranch = BI;
  743. Fixup.OptimisticBranchBlock = 0;
  744. Builder.ClearInsertionPoint();
  745. return;
  746. }
  747. // Otherwise, thread through all the normal cleanups in scope.
  748. // Store the index at the start.
  749. llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
  750. new llvm::StoreInst(Index, getNormalCleanupDestSlot(), BI);
  751. // Adjust BI to point to the first cleanup block.
  752. {
  753. EHCleanupScope &Scope =
  754. cast<EHCleanupScope>(*EHStack.find(TopCleanup));
  755. BI->setSuccessor(0, CreateNormalEntry(*this, Scope));
  756. }
  757. // Add this destination to all the scopes involved.
  758. EHScopeStack::stable_iterator I = TopCleanup;
  759. EHScopeStack::stable_iterator E = Dest.getScopeDepth();
  760. if (E.strictlyEncloses(I)) {
  761. while (true) {
  762. EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
  763. assert(Scope.isNormalCleanup());
  764. I = Scope.getEnclosingNormalCleanup();
  765. // If this is the last cleanup we're propagating through, tell it
  766. // that there's a resolved jump moving through it.
  767. if (!E.strictlyEncloses(I)) {
  768. Scope.addBranchAfter(Index, Dest.getBlock());
  769. break;
  770. }
  771. // Otherwise, tell the scope that there's a jump propoagating
  772. // through it. If this isn't new information, all the rest of
  773. // the work has been done before.
  774. if (!Scope.addBranchThrough(Dest.getBlock()))
  775. break;
  776. }
  777. }
  778. Builder.ClearInsertionPoint();
  779. }
  780. static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack,
  781. EHScopeStack::stable_iterator C) {
  782. // If we needed a normal block for any reason, that counts.
  783. if (cast<EHCleanupScope>(*EHStack.find(C)).getNormalBlock())
  784. return true;
  785. // Check whether any enclosed cleanups were needed.
  786. for (EHScopeStack::stable_iterator
  787. I = EHStack.getInnermostNormalCleanup();
  788. I != C; ) {
  789. assert(C.strictlyEncloses(I));
  790. EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
  791. if (S.getNormalBlock()) return true;
  792. I = S.getEnclosingNormalCleanup();
  793. }
  794. return false;
  795. }
  796. static bool IsUsedAsEHCleanup(EHScopeStack &EHStack,
  797. EHScopeStack::stable_iterator cleanup) {
  798. // If we needed an EH block for any reason, that counts.
  799. if (EHStack.find(cleanup)->hasEHBranches())
  800. return true;
  801. // Check whether any enclosed cleanups were needed.
  802. for (EHScopeStack::stable_iterator
  803. i = EHStack.getInnermostEHScope(); i != cleanup; ) {
  804. assert(cleanup.strictlyEncloses(i));
  805. EHScope &scope = *EHStack.find(i);
  806. if (scope.hasEHBranches())
  807. return true;
  808. i = scope.getEnclosingEHScope();
  809. }
  810. return false;
  811. }
  812. enum ForActivation_t {
  813. ForActivation,
  814. ForDeactivation
  815. };
  816. /// The given cleanup block is changing activation state. Configure a
  817. /// cleanup variable if necessary.
  818. ///
  819. /// It would be good if we had some way of determining if there were
  820. /// extra uses *after* the change-over point.
  821. static void SetupCleanupBlockActivation(CodeGenFunction &CGF,
  822. EHScopeStack::stable_iterator C,
  823. ForActivation_t kind,
  824. llvm::Instruction *dominatingIP) {
  825. EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C));
  826. // We always need the flag if we're activating the cleanup in a
  827. // conditional context, because we have to assume that the current
  828. // location doesn't necessarily dominate the cleanup's code.
  829. bool isActivatedInConditional =
  830. (kind == ForActivation && CGF.isInConditionalBranch());
  831. bool needFlag = false;
  832. // Calculate whether the cleanup was used:
  833. // - as a normal cleanup
  834. if (Scope.isNormalCleanup() &&
  835. (isActivatedInConditional || IsUsedAsNormalCleanup(CGF.EHStack, C))) {
  836. Scope.setTestFlagInNormalCleanup();
  837. needFlag = true;
  838. }
  839. // - as an EH cleanup
  840. if (Scope.isEHCleanup() &&
  841. (isActivatedInConditional || IsUsedAsEHCleanup(CGF.EHStack, C))) {
  842. Scope.setTestFlagInEHCleanup();
  843. needFlag = true;
  844. }
  845. // If it hasn't yet been used as either, we're done.
  846. if (!needFlag) return;
  847. llvm::AllocaInst *var = Scope.getActiveFlag();
  848. if (!var) {
  849. var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "cleanup.isactive");
  850. Scope.setActiveFlag(var);
  851. assert(dominatingIP && "no existing variable and no dominating IP!");
  852. // Initialize to true or false depending on whether it was
  853. // active up to this point.
  854. llvm::Value *value = CGF.Builder.getInt1(kind == ForDeactivation);
  855. // If we're in a conditional block, ignore the dominating IP and
  856. // use the outermost conditional branch.
  857. if (CGF.isInConditionalBranch()) {
  858. CGF.setBeforeOutermostConditional(value, var);
  859. } else {
  860. new llvm::StoreInst(value, var, dominatingIP);
  861. }
  862. }
  863. CGF.Builder.CreateStore(CGF.Builder.getInt1(kind == ForActivation), var);
  864. }
  865. /// Activate a cleanup that was created in an inactivated state.
  866. void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C,
  867. llvm::Instruction *dominatingIP) {
  868. assert(C != EHStack.stable_end() && "activating bottom of stack?");
  869. EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
  870. assert(!Scope.isActive() && "double activation");
  871. SetupCleanupBlockActivation(*this, C, ForActivation, dominatingIP);
  872. Scope.setActive(true);
  873. }
  874. /// Deactive a cleanup that was created in an active state.
  875. void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C,
  876. llvm::Instruction *dominatingIP) {
  877. assert(C != EHStack.stable_end() && "deactivating bottom of stack?");
  878. EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
  879. assert(Scope.isActive() && "double deactivation");
  880. // If it's the top of the stack, just pop it.
  881. if (C == EHStack.stable_begin()) {
  882. // If it's a normal cleanup, we need to pretend that the
  883. // fallthrough is unreachable.
  884. CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
  885. PopCleanupBlock();
  886. Builder.restoreIP(SavedIP);
  887. return;
  888. }
  889. // Otherwise, follow the general case.
  890. SetupCleanupBlockActivation(*this, C, ForDeactivation, dominatingIP);
  891. Scope.setActive(false);
  892. }
  893. llvm::Value *CodeGenFunction::getNormalCleanupDestSlot() {
  894. if (!NormalCleanupDest)
  895. NormalCleanupDest =
  896. CreateTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot");
  897. return NormalCleanupDest;
  898. }
  899. /// Emits all the code to cause the given temporary to be cleaned up.
  900. void CodeGenFunction::EmitCXXTemporary(const CXXTemporary *Temporary,
  901. QualType TempType,
  902. llvm::Value *Ptr) {
  903. pushDestroy(NormalAndEHCleanup, Ptr, TempType, destroyCXXObject,
  904. /*useEHCleanup*/ true);
  905. }