blob: 5cfc7efade42b6dd1e3d3447b0091144804c44fd [file] [log] [blame]
Anders Carlsson5b955922009-11-24 05:51:11 +00001//===--- CGTemporaries.cpp - Emit LLVM Code for C++ temporaries -----------===//
Anders Carlsson2ce66122009-06-03 18:40:21 +00002//
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"
15using namespace clang;
16using namespace CodeGen;
17
Mike Stump1eb44332009-09-09 15:08:12 +000018void CodeGenFunction::PushCXXTemporary(const CXXTemporary *Temporary,
Anders Carlsson2ce66122009-06-03 18:40:21 +000019 llvm::Value *Ptr) {
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000020 llvm::BasicBlock *DtorBlock = createBasicBlock("temp.dtor");
Mike Stump1eb44332009-09-09 15:08:12 +000021
Anders Carlsson8c0b2032009-06-04 02:47:33 +000022 llvm::Value *CondPtr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000023
24 // Check if temporaries need to be conditional. If so, we'll create a
25 // condition boolean, initialize it to 0 and
Anders Carlssona36bf8f2009-11-20 17:27:56 +000026 if (ConditionalBranchLevel != 0) {
Owen Anderson0032b272009-08-13 21:57:51 +000027 CondPtr = CreateTempAlloca(llvm::Type::getInt1Ty(VMContext), "cond");
Mike Stump1eb44332009-09-09 15:08:12 +000028
Anders Carlsson8c0b2032009-06-04 02:47:33 +000029 // Initialize it to false. This initialization takes place right after
30 // the alloca insert point.
Mike Stump1eb44332009-09-09 15:08:12 +000031 llvm::StoreInst *SI =
Owen Anderson3b144ba2009-07-31 17:39:36 +000032 new llvm::StoreInst(llvm::ConstantInt::getFalse(VMContext), CondPtr);
Anders Carlsson8c0b2032009-06-04 02:47:33 +000033 llvm::BasicBlock *Block = AllocaInsertPt->getParent();
34 Block->getInstList().insertAfter((llvm::Instruction *)AllocaInsertPt, SI);
35
36 // Now set it to true.
Owen Anderson3b144ba2009-07-31 17:39:36 +000037 Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext), CondPtr);
Anders Carlsson8c0b2032009-06-04 02:47:33 +000038 }
Mike Stump1eb44332009-09-09 15:08:12 +000039
40 LiveTemporaries.push_back(CXXLiveTemporaryInfo(Temporary, Ptr, DtorBlock,
Anders Carlsson8c0b2032009-06-04 02:47:33 +000041 CondPtr));
Anders Carlssond8bc5a92009-06-04 02:08:08 +000042
43 PushCleanupBlock(DtorBlock);
Anders Carlsson2ce66122009-06-03 18:40:21 +000044}
45
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000046void CodeGenFunction::PopCXXTemporary() {
47 const CXXLiveTemporaryInfo& Info = LiveTemporaries.back();
Mike Stump1eb44332009-09-09 15:08:12 +000048
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000049 CleanupBlockInfo CleanupInfo = PopCleanupBlock();
Mike Stump1eb44332009-09-09 15:08:12 +000050 assert(CleanupInfo.CleanupBlock == Info.DtorBlock &&
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000051 "Cleanup block mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +000052 assert(!CleanupInfo.SwitchBlock &&
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000053 "Should not have a switch block for temporary cleanup!");
Mike Stump1eb44332009-09-09 15:08:12 +000054 assert(!CleanupInfo.EndBlock &&
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000055 "Should not have an end block for temporary cleanup!");
Mike Stump1eb44332009-09-09 15:08:12 +000056
Anders Carlsson283e4d52009-09-14 01:30:44 +000057 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
58 if (CurBB && !CurBB->getTerminator() &&
59 Info.DtorBlock->getNumUses() == 0) {
60 CurBB->getInstList().splice(CurBB->end(), Info.DtorBlock->getInstList());
61 delete Info.DtorBlock;
62 } else
63 EmitBlock(Info.DtorBlock);
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000064
Anders Carlsson8c0b2032009-06-04 02:47:33 +000065 llvm::BasicBlock *CondEnd = 0;
66
67 // If this is a conditional temporary, we need to check the condition
68 // boolean and only call the destructor if it's true.
69 if (Info.CondPtr) {
70 llvm::BasicBlock *CondBlock = createBasicBlock("cond.dtor.call");
71 CondEnd = createBasicBlock("cond.dtor.end");
Mike Stump1eb44332009-09-09 15:08:12 +000072
Anders Carlsson8c0b2032009-06-04 02:47:33 +000073 llvm::Value *Cond = Builder.CreateLoad(Info.CondPtr);
74 Builder.CreateCondBr(Cond, CondBlock, CondEnd);
75 EmitBlock(CondBlock);
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000078 EmitCXXDestructorCall(Info.Temporary->getDestructor(),
79 Dtor_Complete, Info.ThisPtr);
80
Anders Carlsson8c0b2032009-06-04 02:47:33 +000081 if (CondEnd) {
82 // Reset the condition. to false.
Owen Anderson3b144ba2009-07-31 17:39:36 +000083 Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), Info.CondPtr);
Anders Carlsson8c0b2032009-06-04 02:47:33 +000084 EmitBlock(CondEnd);
85 }
Mike Stump1eb44332009-09-09 15:08:12 +000086
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000087 LiveTemporaries.pop_back();
88}
89
90RValue
Anders Carlsson2ce66122009-06-03 18:40:21 +000091CodeGenFunction::EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E,
92 llvm::Value *AggLoc,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000093 bool IsAggLocVolatile,
94 bool IsInitializer) {
Anders Carlssonf54741e2009-06-16 03:37:31 +000095 // If we shouldn't destroy the temporaries, just emit the
96 // child expression.
97 if (!E->shouldDestroyTemporaries())
Mike Stump1eb44332009-09-09 15:08:12 +000098 return EmitAnyExpr(E->getSubExpr(), AggLoc, IsAggLocVolatile,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000099 /*IgnoreResult=*/false, IsInitializer);
Anders Carlssonf54741e2009-06-16 03:37:31 +0000100
Anders Carlsson2ce66122009-06-03 18:40:21 +0000101 // Keep track of the current cleanup stack depth.
102 size_t CleanupStackDepth = CleanupEntries.size();
Daniel Dunbarb4d4c4b2009-06-05 02:03:25 +0000103 (void) CleanupStackDepth;
Anders Carlsson2ce66122009-06-03 18:40:21 +0000104
105 unsigned OldNumLiveTemporaries = LiveTemporaries.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000106
107 RValue RV = EmitAnyExpr(E->getSubExpr(), AggLoc, IsAggLocVolatile,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000108 /*IgnoreResult=*/false, IsInitializer);
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Anders Carlsson8c0b2032009-06-04 02:47:33 +0000110 // Pop temporaries.
111 while (LiveTemporaries.size() > OldNumLiveTemporaries)
112 PopCXXTemporary();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Anders Carlsson8c0b2032009-06-04 02:47:33 +0000114 assert(CleanupEntries.size() == CleanupStackDepth &&
115 "Cleanup size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Anders Carlsson2ce66122009-06-03 18:40:21 +0000117 return RV;
118}
Anders Carlsson1d847502009-06-04 02:22:12 +0000119
Anders Carlssonb9ea0b52009-09-14 01:10:45 +0000120LValue CodeGenFunction::EmitCXXExprWithTemporariesLValue(
121 const CXXExprWithTemporaries *E) {
122 // If we shouldn't destroy the temporaries, just emit the
123 // child expression.
124 if (!E->shouldDestroyTemporaries())
125 return EmitLValue(E->getSubExpr());
126
127 // Keep track of the current cleanup stack depth.
128 size_t CleanupStackDepth = CleanupEntries.size();
129 (void) CleanupStackDepth;
130
131 unsigned OldNumLiveTemporaries = LiveTemporaries.size();
132
133 LValue LV = EmitLValue(E->getSubExpr());
134
135 // Pop temporaries.
136 while (LiveTemporaries.size() > OldNumLiveTemporaries)
137 PopCXXTemporary();
138
139 assert(CleanupEntries.size() == CleanupStackDepth &&
140 "Cleanup size mismatch!");
141
142 return LV;
143}