Anders Carlsson | 5b95592 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGTemporaries.cpp - Emit LLVM Code for C++ temporaries -----------===// |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 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 contains code dealing with C++ code generation of temporaries |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | using namespace clang; |
| 16 | using namespace CodeGen; |
| 17 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 18 | void CodeGenFunction::PushCXXTemporary(const CXXTemporary *Temporary, |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 19 | llvm::Value *Ptr) { |
Douglas Gregor | 400fd3d | 2009-12-24 17:48:05 +0000 | [diff] [blame] | 20 | assert((LiveTemporaries.empty() || |
Douglas Gregor | 0c94c02 | 2009-12-24 18:16:21 +0000 | [diff] [blame] | 21 | LiveTemporaries.back().ThisPtr != Ptr || |
| 22 | ConditionalBranchLevel) && |
Douglas Gregor | 400fd3d | 2009-12-24 17:48:05 +0000 | [diff] [blame] | 23 | "Pushed the same temporary twice; AST is likely wrong"); |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 24 | llvm::BasicBlock *DtorBlock = createBasicBlock("temp.dtor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 26 | llvm::Value *CondPtr = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | |
| 28 | // Check if temporaries need to be conditional. If so, we'll create a |
| 29 | // condition boolean, initialize it to 0 and |
Anders Carlsson | a36bf8f | 2009-11-20 17:27:56 +0000 | [diff] [blame] | 30 | if (ConditionalBranchLevel != 0) { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 31 | CondPtr = CreateTempAlloca(llvm::Type::getInt1Ty(VMContext), "cond"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 33 | // Initialize it to false. This initialization takes place right after |
| 34 | // the alloca insert point. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | llvm::StoreInst *SI = |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 36 | new llvm::StoreInst(llvm::ConstantInt::getFalse(VMContext), CondPtr); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 37 | llvm::BasicBlock *Block = AllocaInsertPt->getParent(); |
| 38 | Block->getInstList().insertAfter((llvm::Instruction *)AllocaInsertPt, SI); |
| 39 | |
| 40 | // Now set it to true. |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 41 | Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext), CondPtr); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 42 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
| 44 | LiveTemporaries.push_back(CXXLiveTemporaryInfo(Temporary, Ptr, DtorBlock, |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 45 | CondPtr)); |
Anders Carlsson | d8bc5a9 | 2009-06-04 02:08:08 +0000 | [diff] [blame] | 46 | |
| 47 | PushCleanupBlock(DtorBlock); |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 48 | |
| 49 | if (Exceptions) { |
| 50 | const CXXLiveTemporaryInfo& Info = LiveTemporaries.back(); |
| 51 | llvm::BasicBlock *CondEnd = 0; |
| 52 | |
| 53 | EHCleanupBlock Cleanup(*this); |
| 54 | |
| 55 | // If this is a conditional temporary, we need to check the condition |
| 56 | // boolean and only call the destructor if it's true. |
| 57 | if (Info.CondPtr) { |
| 58 | llvm::BasicBlock *CondBlock = createBasicBlock("cond.dtor.call"); |
| 59 | CondEnd = createBasicBlock("cond.dtor.end"); |
| 60 | |
| 61 | llvm::Value *Cond = Builder.CreateLoad(Info.CondPtr); |
| 62 | Builder.CreateCondBr(Cond, CondBlock, CondEnd); |
| 63 | EmitBlock(CondBlock); |
| 64 | } |
| 65 | |
| 66 | EmitCXXDestructorCall(Info.Temporary->getDestructor(), |
| 67 | Dtor_Complete, Info.ThisPtr); |
| 68 | |
| 69 | if (CondEnd) { |
| 70 | // Reset the condition. to false. |
| 71 | Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), Info.CondPtr); |
| 72 | EmitBlock(CondEnd); |
| 73 | } |
| 74 | } |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 77 | void CodeGenFunction::PopCXXTemporary() { |
| 78 | const CXXLiveTemporaryInfo& Info = LiveTemporaries.back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 80 | CleanupBlockInfo CleanupInfo = PopCleanupBlock(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | assert(CleanupInfo.CleanupBlock == Info.DtorBlock && |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 82 | "Cleanup block mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | assert(!CleanupInfo.SwitchBlock && |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 84 | "Should not have a switch block for temporary cleanup!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | assert(!CleanupInfo.EndBlock && |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 86 | "Should not have an end block for temporary cleanup!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 88 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 89 | if (CurBB && !CurBB->getTerminator() && |
| 90 | Info.DtorBlock->getNumUses() == 0) { |
| 91 | CurBB->getInstList().splice(CurBB->end(), Info.DtorBlock->getInstList()); |
| 92 | delete Info.DtorBlock; |
| 93 | } else |
| 94 | EmitBlock(Info.DtorBlock); |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 95 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 96 | llvm::BasicBlock *CondEnd = 0; |
| 97 | |
| 98 | // If this is a conditional temporary, we need to check the condition |
| 99 | // boolean and only call the destructor if it's true. |
| 100 | if (Info.CondPtr) { |
| 101 | llvm::BasicBlock *CondBlock = createBasicBlock("cond.dtor.call"); |
| 102 | CondEnd = createBasicBlock("cond.dtor.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 104 | llvm::Value *Cond = Builder.CreateLoad(Info.CondPtr); |
| 105 | Builder.CreateCondBr(Cond, CondBlock, CondEnd); |
| 106 | EmitBlock(CondBlock); |
| 107 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 109 | EmitCXXDestructorCall(Info.Temporary->getDestructor(), |
| 110 | Dtor_Complete, Info.ThisPtr); |
| 111 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 112 | if (CondEnd) { |
| 113 | // Reset the condition. to false. |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 114 | Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), Info.CondPtr); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 115 | EmitBlock(CondEnd); |
| 116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 118 | LiveTemporaries.pop_back(); |
| 119 | } |
| 120 | |
| 121 | RValue |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 122 | CodeGenFunction::EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E, |
| 123 | llvm::Value *AggLoc, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 124 | bool IsAggLocVolatile, |
| 125 | bool IsInitializer) { |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 126 | // Keep track of the current cleanup stack depth. |
| 127 | size_t CleanupStackDepth = CleanupEntries.size(); |
Daniel Dunbar | b4d4c4b | 2009-06-05 02:03:25 +0000 | [diff] [blame] | 128 | (void) CleanupStackDepth; |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 129 | |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 130 | RValue RV; |
| 131 | |
| 132 | { |
| 133 | CXXTemporariesCleanupScope Scope(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 135 | RV = EmitAnyExpr(E->getSubExpr(), AggLoc, IsAggLocVolatile, |
| 136 | /*IgnoreResult=*/false, IsInitializer); |
| 137 | } |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 138 | assert(CleanupEntries.size() == CleanupStackDepth && |
| 139 | "Cleanup size mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 141 | return RV; |
| 142 | } |
Anders Carlsson | 1d84750 | 2009-06-04 02:22:12 +0000 | [diff] [blame] | 143 | |
Anders Carlsson | b9ea0b5 | 2009-09-14 01:10:45 +0000 | [diff] [blame] | 144 | LValue CodeGenFunction::EmitCXXExprWithTemporariesLValue( |
| 145 | const CXXExprWithTemporaries *E) { |
Anders Carlsson | b9ea0b5 | 2009-09-14 01:10:45 +0000 | [diff] [blame] | 146 | // Keep track of the current cleanup stack depth. |
| 147 | size_t CleanupStackDepth = CleanupEntries.size(); |
| 148 | (void) CleanupStackDepth; |
| 149 | |
| 150 | unsigned OldNumLiveTemporaries = LiveTemporaries.size(); |
| 151 | |
| 152 | LValue LV = EmitLValue(E->getSubExpr()); |
| 153 | |
| 154 | // Pop temporaries. |
| 155 | while (LiveTemporaries.size() > OldNumLiveTemporaries) |
| 156 | PopCXXTemporary(); |
| 157 | |
| 158 | assert(CleanupEntries.size() == CleanupStackDepth && |
| 159 | "Cleanup size mismatch!"); |
| 160 | |
| 161 | return LV; |
| 162 | } |