blob: ccd961ac28335dfc6a750ce3243934dc4d6de8ff [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the per-function state used while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Eli Friedman3f2af102008-05-22 01:40:10 +000016#include "CGDebugInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner31a09842008-11-12 08:04:58 +000018#include "clang/AST/APValue.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000020#include "clang/AST/Decl.h"
Devang Pateld9363c32007-09-28 21:49:18 +000021#include "llvm/Support/CFG.h"
Mike Stump4e7a1f72009-02-21 20:00:35 +000022#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace CodeGen;
25
26CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Anders Carlssone896d982009-02-13 08:11:52 +000027 : CGM(cgm), Target(CGM.getContext().Target), DebugInfo(0), SwitchInsn(0),
Daniel Dunbar9834ffb2009-02-23 17:26:39 +000028 CaseRangeBlock(0), InvokeDest(0) {
Mike Stump4e7a1f72009-02-21 20:00:35 +000029 LLVMIntTy = ConvertType(getContext().IntTy);
30 LLVMPointerWidth = Target.getPointerWidth(0);
31
32 // FIXME: We need to rearrange the code for copy/dispose so we have this
33 // sooner, so we can calculate offsets correctly.
34 BlockHasCopyDispose = false;
35 if (!BlockHasCopyDispose)
36 BlockOffset = CGM.getTargetData()
37 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
38 else
39 BlockOffset = CGM.getTargetData()
40 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
Chris Lattner41110242008-06-17 18:05:57 +000041}
Reid Spencer5f016e22007-07-11 17:01:13 +000042
43ASTContext &CodeGenFunction::getContext() const {
44 return CGM.getContext();
45}
46
47
48llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
49 llvm::BasicBlock *&BB = LabelMap[S];
50 if (BB) return BB;
51
52 // Create, but don't insert, the new block.
Daniel Dunbar55e87422008-11-11 02:29:29 +000053 return BB = createBasicBlock(S->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +000054}
55
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000056llvm::Constant *
Steve Naroff248a7532008-04-15 22:42:06 +000057CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000058 return cast<llvm::Constant>(LocalDeclMap[BVD]);
59}
Reid Spencer5f016e22007-07-11 17:01:13 +000060
Anders Carlssondde0a942008-09-11 09:15:33 +000061llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD)
62{
63 return LocalDeclMap[VD];
64}
65
Daniel Dunbar8b1a3432009-02-03 23:03:55 +000066const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
67 return CGM.getTypes().ConvertTypeForMem(T);
68}
69
Reid Spencer5f016e22007-07-11 17:01:13 +000070const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
71 return CGM.getTypes().ConvertType(T);
72}
73
Chris Lattner41110242008-06-17 18:05:57 +000074bool CodeGenFunction::isObjCPointerType(QualType T) {
75 // All Objective-C types are pointers.
76 return T->isObjCInterfaceType() ||
77 T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
78}
79
Reid Spencer5f016e22007-07-11 17:01:13 +000080bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Daniel Dunbara782ca72009-01-09 02:44:18 +000081 // FIXME: Use positive checks instead of negative ones to be more
82 // robust in the face of extension.
Chris Lattner41110242008-06-17 18:05:57 +000083 return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
Daniel Dunbara782ca72009-01-09 02:44:18 +000084 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
85 !T->isBlockPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Daniel Dunbar1c1d6072009-01-26 23:27:52 +000088void CodeGenFunction::EmitReturnBlock() {
89 // For cleanliness, we try to avoid emitting the return block for
90 // simple cases.
91 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
92
93 if (CurBB) {
94 assert(!CurBB->getTerminator() && "Unexpected terminated block.");
95
96 // We have a valid insert point, reuse it if there are no explicit
97 // jumps to the return block.
98 if (ReturnBlock->use_empty())
99 delete ReturnBlock;
100 else
101 EmitBlock(ReturnBlock);
102 return;
103 }
104
105 // Otherwise, if the return block is the target of a single direct
106 // branch then we can just put the code in that block instead. This
107 // cleans up functions which started with a unified return block.
108 if (ReturnBlock->hasOneUse()) {
109 llvm::BranchInst *BI =
110 dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
111 if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
112 // Reset insertion point and delete the branch.
113 Builder.SetInsertPoint(BI->getParent());
114 BI->eraseFromParent();
115 delete ReturnBlock;
116 return;
117 }
118 }
119
120 // FIXME: We are at an unreachable point, there is no reason to emit
121 // the block unless it has uses. However, we still need a place to
122 // put the debug region.end for now.
123
124 EmitBlock(ReturnBlock);
125}
126
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000127void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000128 // Finish emission of indirect switches.
129 EmitIndirectSwitches();
130
Chris Lattnerda138702007-07-16 21:28:45 +0000131 assert(BreakContinueStack.empty() &&
132 "mismatched push/pop in break/continue stack!");
Anders Carlssonbd6fa3d2009-02-08 00:16:35 +0000133 assert(BlockScopes.empty() &&
134 "did not remove all blocks from block scope map!");
135 assert(CleanupEntries.empty() &&
136 "mismatched push/pop in cleanup stack!");
137
Daniel Dunbar1c1d6072009-01-26 23:27:52 +0000138 // Emit function epilog (to return).
139 EmitReturnBlock();
Daniel Dunbarf5bd45c2008-11-11 20:59:54 +0000140
141 // Emit debug descriptor for function end.
Anders Carlssone896d982009-02-13 08:11:52 +0000142 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbarf5bd45c2008-11-11 20:59:54 +0000143 DI->setLocation(EndLoc);
144 DI->EmitRegionEnd(CurFn, Builder);
145 }
146
Daniel Dunbar88b53962009-02-02 22:03:45 +0000147 EmitFunctionEpilog(*CurFnInfo, ReturnValue);
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000148
Chris Lattner5a2fa142007-12-02 06:32:24 +0000149 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
150 AllocaInsertPt->eraseFromParent();
151 AllocaInsertPt = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000152}
153
Daniel Dunbar7c086512008-09-09 23:14:03 +0000154void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
155 llvm::Function *Fn,
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000156 const FunctionArgList &Args,
157 SourceLocation StartLoc) {
Anders Carlsson4cc1a472009-02-09 20:20:56 +0000158 DidCallStackSave = false;
Daniel Dunbar7c086512008-09-09 23:14:03 +0000159 CurFuncDecl = D;
160 FnRetTy = RetTy;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000161 CurFn = Fn;
Chris Lattner41110242008-06-17 18:05:57 +0000162 assert(CurFn->isDeclaration() && "Function already has body?");
163
Daniel Dunbar55e87422008-11-11 02:29:29 +0000164 llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000165
Chris Lattner41110242008-06-17 18:05:57 +0000166 // Create a marker to make it easy to insert allocas into the entryblock
167 // later. Don't create this with the builder, because we don't want it
168 // folded.
169 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
170 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
171 EntryBB);
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000172
Daniel Dunbar55e87422008-11-11 02:29:29 +0000173 ReturnBlock = createBasicBlock("return");
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000174 ReturnValue = 0;
Daniel Dunbar7c086512008-09-09 23:14:03 +0000175 if (!RetTy->isVoidType())
176 ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000177
Chris Lattner41110242008-06-17 18:05:57 +0000178 Builder.SetInsertPoint(EntryBB);
179
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000180 // Emit subprogram debug descriptor.
Daniel Dunbar7c086512008-09-09 23:14:03 +0000181 // FIXME: The cast here is a huge hack.
Anders Carlssone896d982009-02-13 08:11:52 +0000182 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000183 DI->setLocation(StartLoc);
184 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor6ec36682009-02-18 23:53:56 +0000185 DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder);
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000186 } else {
187 // Just use LLVM function name.
188 DI->EmitFunctionStart(Fn->getName().c_str(),
189 RetTy, CurFn, Builder);
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000190 }
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000191 }
192
Daniel Dunbar88b53962009-02-02 22:03:45 +0000193 // FIXME: Leaked.
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000194 CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args);
Daniel Dunbar88b53962009-02-02 22:03:45 +0000195 EmitFunctionProlog(*CurFnInfo, CurFn, Args);
Anders Carlsson751358f2008-12-20 21:28:43 +0000196
197 // If any of the arguments have a variably modified type, make sure to
198 // emit the type size.
199 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
200 i != e; ++i) {
201 QualType Ty = i->second;
202
203 if (Ty->isVariablyModifiedType())
204 EmitVLASize(Ty);
205 }
Daniel Dunbar7c086512008-09-09 23:14:03 +0000206}
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000207
Daniel Dunbar7c086512008-09-09 23:14:03 +0000208void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
209 llvm::Function *Fn) {
Anders Carlssone896d982009-02-13 08:11:52 +0000210 // Check if we should generate debug info for this function.
211 if (CGM.getDebugInfo() && !FD->getAttr<NodebugAttr>())
212 DebugInfo = CGM.getDebugInfo();
213
Daniel Dunbar7c086512008-09-09 23:14:03 +0000214 FunctionArgList Args;
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000215 if (FD->getNumParams()) {
216 const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
217 assert(FProto && "Function def must have prototype!");
Daniel Dunbar7c086512008-09-09 23:14:03 +0000218
219 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
220 Args.push_back(std::make_pair(FD->getParamDecl(i),
221 FProto->getArgType(i)));
Chris Lattner41110242008-06-17 18:05:57 +0000222 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000223
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000224 StartFunction(FD, FD->getResultType(), Fn, Args,
225 cast<CompoundStmt>(FD->getBody())->getLBracLoc());
Daniel Dunbar7c086512008-09-09 23:14:03 +0000226
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000227 EmitStmt(FD->getBody());
228
229 const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
230 if (S) {
231 FinishFunction(S->getRBracLoc());
232 } else {
233 FinishFunction();
234 }
Chris Lattner41110242008-06-17 18:05:57 +0000235}
236
Chris Lattner0946ccd2008-11-11 07:41:27 +0000237/// ContainsLabel - Return true if the statement contains a label in it. If
238/// this statement is not executed normally, it not containing a label means
239/// that we can just remove the code.
240bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
241 // Null statement, not a label!
242 if (S == 0) return false;
243
244 // If this is a label, we have to emit the code, consider something like:
245 // if (0) { ... foo: bar(); } goto foo;
246 if (isa<LabelStmt>(S))
247 return true;
248
249 // If this is a case/default statement, and we haven't seen a switch, we have
250 // to emit the code.
251 if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
252 return true;
253
254 // If this is a switch statement, we want to ignore cases below it.
255 if (isa<SwitchStmt>(S))
256 IgnoreCaseStmts = true;
257
258 // Scan subexpressions for verboten labels.
259 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
260 I != E; ++I)
261 if (ContainsLabel(*I, IgnoreCaseStmts))
262 return true;
263
264 return false;
265}
266
Chris Lattner31a09842008-11-12 08:04:58 +0000267
268/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
269/// a constant, or if it does but contains a label, return 0. If it constant
270/// folds to 'true' and does not contain a label, return 1, if it constant folds
271/// to 'false' and does not contain a label, return -1.
272int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
Daniel Dunbar36bc14c2008-11-12 22:37:10 +0000273 // FIXME: Rename and handle conversion of other evaluatable things
274 // to bool.
Anders Carlsson64712f12008-12-01 02:46:24 +0000275 Expr::EvalResult Result;
276 if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
277 Result.HasSideEffects)
Anders Carlssonef5a66d2008-11-22 22:32:07 +0000278 return 0; // Not foldable, not integer or not fully evaluatable.
Chris Lattner31a09842008-11-12 08:04:58 +0000279
280 if (CodeGenFunction::ContainsLabel(Cond))
281 return 0; // Contains a label.
282
Anders Carlsson64712f12008-12-01 02:46:24 +0000283 return Result.Val.getInt().getBoolValue() ? 1 : -1;
Chris Lattner31a09842008-11-12 08:04:58 +0000284}
285
286
287/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
288/// statement) to the specified blocks. Based on the condition, this might try
289/// to simplify the codegen of the conditional based on the branch.
290///
291void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
292 llvm::BasicBlock *TrueBlock,
293 llvm::BasicBlock *FalseBlock) {
294 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
295 return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
296
297 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
298 // Handle X && Y in a condition.
299 if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
300 // If we have "1 && X", simplify the code. "0 && X" would have constant
301 // folded if the case was simple enough.
302 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
303 // br(1 && X) -> br(X).
304 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
305 }
306
307 // If we have "X && 1", simplify the code to use an uncond branch.
308 // "X && 0" would have been constant folded to 0.
309 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
310 // br(X && 1) -> br(X).
311 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
312 }
313
314 // Emit the LHS as a conditional. If the LHS conditional is false, we
315 // want to jump to the FalseBlock.
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000316 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
Chris Lattner31a09842008-11-12 08:04:58 +0000317 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
318 EmitBlock(LHSTrue);
319
320 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
321 return;
322 } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
323 // If we have "0 || X", simplify the code. "1 || X" would have constant
324 // folded if the case was simple enough.
325 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
326 // br(0 || X) -> br(X).
327 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
328 }
329
330 // If we have "X || 0", simplify the code to use an uncond branch.
331 // "X || 1" would have been constant folded to 1.
332 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
333 // br(X || 0) -> br(X).
334 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
335 }
336
337 // Emit the LHS as a conditional. If the LHS conditional is true, we
338 // want to jump to the TrueBlock.
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000339 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
Chris Lattner31a09842008-11-12 08:04:58 +0000340 EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
341 EmitBlock(LHSFalse);
342
343 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
344 return;
345 }
Chris Lattner552f4c42008-11-12 08:13:36 +0000346 }
347
348 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
349 // br(!x, t, f) -> br(x, f, t)
350 if (CondUOp->getOpcode() == UnaryOperator::LNot)
351 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
Chris Lattner31a09842008-11-12 08:04:58 +0000352 }
353
Daniel Dunbar09b14892008-11-12 10:30:32 +0000354 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
355 // Handle ?: operator.
356
357 // Just ignore GNU ?: extension.
358 if (CondOp->getLHS()) {
359 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
360 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
361 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
362 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
363 EmitBlock(LHSBlock);
364 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
365 EmitBlock(RHSBlock);
366 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
367 return;
368 }
369 }
370
Chris Lattner31a09842008-11-12 08:04:58 +0000371 // Emit the code with the fully general case.
372 llvm::Value *CondV = EvaluateExprAsBool(Cond);
373 Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
374}
375
Devang Patel88a981b2007-11-01 19:11:01 +0000376/// getCGRecordLayout - Return record layout info.
377const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattneraf319132008-02-05 06:55:31 +0000378 QualType Ty) {
379 const RecordType *RTy = Ty->getAsRecordType();
380 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelb84a06e2007-10-23 02:10:49 +0000381
Chris Lattneraf319132008-02-05 06:55:31 +0000382 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelb84a06e2007-10-23 02:10:49 +0000383}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000384
Daniel Dunbar488e9932008-08-16 00:56:44 +0000385/// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000386/// specified stmt yet.
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000387void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
388 bool OmitOnError) {
389 CGM.ErrorUnsupported(S, Type, OmitOnError);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000390}
391
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000392unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
393 // Use LabelIDs.size() as the new ID if one hasn't been assigned.
394 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
395}
396
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000397void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
398{
399 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
400 if (DestPtr->getType() != BP)
401 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
402
403 // Get size and alignment info for this aggregate.
404 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
405
406 // FIXME: Handle variable sized types.
407 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
408
409 Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
410 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
411 // TypeInfo.first describes size in bits.
412 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
413 llvm::ConstantInt::get(llvm::Type::Int32Ty,
414 TypeInfo.second/8));
415}
416
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000417void CodeGenFunction::EmitIndirectSwitches() {
418 llvm::BasicBlock *Default;
419
Daniel Dunbar76526a52008-08-04 17:24:44 +0000420 if (IndirectSwitches.empty())
421 return;
422
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000423 if (!LabelIDs.empty()) {
424 Default = getBasicBlockForLabel(LabelIDs.begin()->first);
425 } else {
426 // No possible targets for indirect goto, just emit an infinite
427 // loop.
Daniel Dunbar55e87422008-11-11 02:29:29 +0000428 Default = createBasicBlock("indirectgoto.loop", CurFn);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000429 llvm::BranchInst::Create(Default, Default);
430 }
431
432 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
433 e = IndirectSwitches.end(); i != e; ++i) {
434 llvm::SwitchInst *I = *i;
435
436 I->setSuccessor(0, Default);
437 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
438 LE = LabelIDs.end(); LI != LE; ++LI) {
439 I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
440 LI->second),
441 getBasicBlockForLabel(LI->first));
442 }
443 }
444}
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000445
Anders Carlssondcc90d82008-12-12 07:19:02 +0000446llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
447{
448 llvm::Value *&SizeEntry = VLASizeMap[VAT];
Anders Carlssondcc90d82008-12-12 07:19:02 +0000449
Anders Carlssonf666b772008-12-20 20:27:15 +0000450 assert(SizeEntry && "Did not emit size for type");
451 return SizeEntry;
452}
Anders Carlssondcc90d82008-12-12 07:19:02 +0000453
Anders Carlsson60d35412008-12-20 20:46:34 +0000454llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
Anders Carlssonf666b772008-12-20 20:27:15 +0000455{
Anders Carlsson60d35412008-12-20 20:46:34 +0000456 assert(Ty->isVariablyModifiedType() &&
457 "Must pass variably modified type to EmitVLASizes!");
Anders Carlssonf666b772008-12-20 20:27:15 +0000458
Anders Carlsson60d35412008-12-20 20:46:34 +0000459 if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
460 llvm::Value *&SizeEntry = VLASizeMap[VAT];
461
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000462 if (!SizeEntry) {
463 // Get the element size;
464 llvm::Value *ElemSize;
Anders Carlsson60d35412008-12-20 20:46:34 +0000465
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000466 QualType ElemTy = VAT->getElementType();
Anders Carlsson96f21472009-02-05 19:43:10 +0000467
468 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
469
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000470 if (ElemTy->isVariableArrayType())
471 ElemSize = EmitVLASize(ElemTy);
472 else {
Anders Carlsson96f21472009-02-05 19:43:10 +0000473 ElemSize = llvm::ConstantInt::get(SizeTy,
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000474 getContext().getTypeSize(ElemTy) / 8);
475 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000476
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000477 llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
Anders Carlsson96f21472009-02-05 19:43:10 +0000478 NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
479
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000480 SizeEntry = Builder.CreateMul(ElemSize, NumElements);
Anders Carlsson60d35412008-12-20 20:46:34 +0000481 }
482
Anders Carlsson60d35412008-12-20 20:46:34 +0000483 return SizeEntry;
484 } else if (const PointerType *PT = Ty->getAsPointerType())
485 EmitVLASize(PT->getPointeeType());
Anders Carlssonf666b772008-12-20 20:27:15 +0000486 else {
Anders Carlsson60d35412008-12-20 20:46:34 +0000487 assert(0 && "unknown VM type!");
Anders Carlssondcc90d82008-12-12 07:19:02 +0000488 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000489
490 return 0;
Anders Carlssondcc90d82008-12-12 07:19:02 +0000491}
Eli Friedman4fd0aa52009-01-20 17:46:04 +0000492
493llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
494 if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
495 return EmitScalarExpr(E);
496 }
497 return EmitLValue(E).getAddress();
498}
Anders Carlsson6ccc4762009-02-07 22:53:43 +0000499
Anders Carlsson6fc55912009-02-08 03:22:36 +0000500void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock)
Anders Carlsson6ccc4762009-02-07 22:53:43 +0000501{
Anders Carlsson6ccc4762009-02-07 22:53:43 +0000502 CleanupEntries.push_back(CleanupEntry(CleanupBlock));
Anders Carlsson6ccc4762009-02-07 22:53:43 +0000503}
Anders Carlssonc71c8452009-02-07 23:50:39 +0000504
505void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize)
506{
507 assert(CleanupEntries.size() >= OldCleanupStackSize &&
508 "Cleanup stack mismatch!");
509
510 while (CleanupEntries.size() > OldCleanupStackSize)
511 EmitCleanupBlock();
512}
513
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000514CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock()
Anders Carlssonc71c8452009-02-07 23:50:39 +0000515{
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000516 CleanupEntry &CE = CleanupEntries.back();
517
518 llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
519
520 std::vector<llvm::BasicBlock *> Blocks;
521 std::swap(Blocks, CE.Blocks);
522
523 std::vector<llvm::BranchInst *> BranchFixups;
524 std::swap(BranchFixups, CE.BranchFixups);
525
526 CleanupEntries.pop_back();
527
Anders Carlssonad9d00e2009-02-08 22:45:15 +0000528 // Check if any branch fixups pointed to the scope we just popped. If so,
529 // we can remove them.
530 for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
531 llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
532 BlockScopeMap::iterator I = BlockScopes.find(Dest);
Anders Carlssond66a9f92009-02-08 03:55:35 +0000533
Anders Carlssonad9d00e2009-02-08 22:45:15 +0000534 if (I == BlockScopes.end())
535 continue;
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000536
Anders Carlssonad9d00e2009-02-08 22:45:15 +0000537 assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
Anders Carlssond66a9f92009-02-08 03:55:35 +0000538
Anders Carlssonad9d00e2009-02-08 22:45:15 +0000539 if (I->second == CleanupEntries.size()) {
540 // We don't need to do this branch fixup.
541 BranchFixups[i] = BranchFixups.back();
542 BranchFixups.pop_back();
543 i--;
544 e--;
545 continue;
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000546 }
547 }
Anders Carlssond66a9f92009-02-08 03:55:35 +0000548
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000549 llvm::BasicBlock *SwitchBlock = 0;
550 llvm::BasicBlock *EndBlock = 0;
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000551 if (!BranchFixups.empty()) {
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000552 SwitchBlock = createBasicBlock("cleanup.switch");
553 EndBlock = createBasicBlock("cleanup.end");
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000554
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000555 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
556
557 Builder.SetInsertPoint(SwitchBlock);
558
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000559 llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::Int32Ty,
560 "cleanup.dst");
561 llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
562
563 // Create a switch instruction to determine where to jump next.
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000564 llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000565 BranchFixups.size());
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000566
Anders Carlsson46831a92009-02-08 22:13:37 +0000567 // Restore the current basic block (if any)
568 if (CurBB)
569 Builder.SetInsertPoint(CurBB);
570 else
571 Builder.ClearInsertionPoint();
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000572
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000573 for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
574 llvm::BranchInst *BI = BranchFixups[i];
575 llvm::BasicBlock *Dest = BI->getSuccessor(0);
576
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000577 // Fixup the branch instruction to point to the cleanup block.
578 BI->setSuccessor(0, CleanupBlock);
Anders Carlssond66a9f92009-02-08 03:55:35 +0000579
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000580 if (CleanupEntries.empty()) {
Anders Carlssoncc899202009-02-08 22:46:50 +0000581 llvm::ConstantInt *ID;
582
583 // Check if we already have a destination for this block.
584 if (Dest == SI->getDefaultDest())
585 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
586 else {
587 ID = SI->findCaseDest(Dest);
588 if (!ID) {
589 // No code found, get a new unique one by using the number of
590 // switch successors.
591 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty,
592 SI->getNumSuccessors());
593 SI->addCase(ID, Dest);
594 }
595 }
596
597 // Store the jump destination before the branch instruction.
598 new llvm::StoreInst(ID, DestCodePtr, BI);
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000599 } else {
600 // We need to jump through another cleanup block. Create a pad block
601 // with a branch instruction that jumps to the final destination and
602 // add it as a branch fixup to the current cleanup scope.
603
604 // Create the pad block.
605 llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
Anders Carlssoncc899202009-02-08 22:46:50 +0000606
607 // Create a unique case ID.
608 llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::Int32Ty,
609 SI->getNumSuccessors());
610
611 // Store the jump destination before the branch instruction.
612 new llvm::StoreInst(ID, DestCodePtr, BI);
613
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000614 // Add it as the destination.
Anders Carlssoncc899202009-02-08 22:46:50 +0000615 SI->addCase(ID, CleanupPad);
Anders Carlsson1093c2c2009-02-08 01:23:05 +0000616
617 // Create the branch to the final destination.
618 llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
619 CleanupPad->getInstList().push_back(BI);
620
621 // And add it as a branch fixup.
622 CleanupEntries.back().BranchFixups.push_back(BI);
623 }
624 }
625 }
Anders Carlssond66a9f92009-02-08 03:55:35 +0000626
Anders Carlssonbd6fa3d2009-02-08 00:16:35 +0000627 // Remove all blocks from the block scope map.
628 for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
629 assert(BlockScopes.count(Blocks[i]) &&
630 "Did not find block in scope map!");
631
632 BlockScopes.erase(Blocks[i]);
633 }
Anders Carlssond66a9f92009-02-08 03:55:35 +0000634
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000635 return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock);
Anders Carlssond66a9f92009-02-08 03:55:35 +0000636}
637
638void CodeGenFunction::EmitCleanupBlock()
639{
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000640 CleanupBlockInfo Info = PopCleanupBlock();
Anders Carlssond66a9f92009-02-08 03:55:35 +0000641
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000642 EmitBlock(Info.CleanupBlock);
Anders Carlssond66a9f92009-02-08 03:55:35 +0000643
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000644 if (Info.SwitchBlock)
645 EmitBlock(Info.SwitchBlock);
646 if (Info.EndBlock)
647 EmitBlock(Info.EndBlock);
Anders Carlssond66a9f92009-02-08 03:55:35 +0000648}
649
Anders Carlsson87eaf172009-02-08 00:50:42 +0000650void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI)
651{
652 assert(!CleanupEntries.empty() &&
653 "Trying to add branch fixup without cleanup block!");
654
655 // FIXME: We could be more clever here and check if there's already a
656 // branch fixup for this destination and recycle it.
657 CleanupEntries.back().BranchFixups.push_back(BI);
658}
659
660void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest)
661{
Anders Carlsson46831a92009-02-08 22:13:37 +0000662 if (!HaveInsertPoint())
663 return;
664
Anders Carlsson87eaf172009-02-08 00:50:42 +0000665 llvm::BranchInst* BI = Builder.CreateBr(Dest);
666
Anders Carlsson46831a92009-02-08 22:13:37 +0000667 Builder.ClearInsertionPoint();
668
Anders Carlsson87eaf172009-02-08 00:50:42 +0000669 // The stack is empty, no need to do any cleanup.
670 if (CleanupEntries.empty())
671 return;
672
673 if (!Dest->getParent()) {
674 // We are trying to branch to a block that hasn't been inserted yet.
675 AddBranchFixup(BI);
676 return;
677 }
678
679 BlockScopeMap::iterator I = BlockScopes.find(Dest);
680 if (I == BlockScopes.end()) {
681 // We are trying to jump to a block that is outside of any cleanup scope.
682 AddBranchFixup(BI);
683 return;
684 }
685
686 assert(I->second < CleanupEntries.size() &&
687 "Trying to branch into cleanup region");
688
689 if (I->second == CleanupEntries.size() - 1) {
690 // We have a branch to a block in the same scope.
691 return;
692 }
693
694 AddBranchFixup(BI);
695}