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