xref: /llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp (revision b48a2d5d2fb71df0ff5cbfc33d0879da19ead9f4)
1 //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
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 coordinates the per-function state used while generating code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "CGDebugInfo.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/AST/APValue.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/StmtCXX.h"
23 #include "clang/Frontend/CodeGenOptions.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Intrinsics.h"
26 using namespace clang;
27 using namespace CodeGen;
28 
29 CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
30   : BlockFunction(cgm, *this, Builder), CGM(cgm),
31     Target(CGM.getContext().Target),
32     Builder(cgm.getModule().getContext()),
33     DebugInfo(0), IndirectBranch(0),
34     SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
35     CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0),
36     ConditionalBranchLevel(0), TerminateHandler(0), TrapBB(0) {
37   LLVMIntTy = ConvertType(getContext().IntTy);
38   LLVMPointerWidth = Target.getPointerWidth(0);
39   Exceptions = getContext().getLangOptions().Exceptions;
40   CatchUndefined = getContext().getLangOptions().CatchUndefined;
41   CGM.getMangleContext().startNewFunction();
42 }
43 
44 ASTContext &CodeGenFunction::getContext() const {
45   return CGM.getContext();
46 }
47 
48 
49 llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
50   llvm::BasicBlock *&BB = LabelMap[S];
51   if (BB) return BB;
52 
53   // Create, but don't insert, the new block.
54   return BB = createBasicBlock(S->getName());
55 }
56 
57 llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) {
58   llvm::Value *Res = LocalDeclMap[VD];
59   assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
60   return Res;
61 }
62 
63 llvm::Constant *
64 CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
65   return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
66 }
67 
68 const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
69   return CGM.getTypes().ConvertTypeForMem(T);
70 }
71 
72 const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
73   return CGM.getTypes().ConvertType(T);
74 }
75 
76 bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
77   return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() ||
78     T->isMemberFunctionPointerType();
79 }
80 
81 void CodeGenFunction::EmitReturnBlock() {
82   // For cleanliness, we try to avoid emitting the return block for
83   // simple cases.
84   llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
85 
86   if (CurBB) {
87     assert(!CurBB->getTerminator() && "Unexpected terminated block.");
88 
89     // We have a valid insert point, reuse it if it is empty or there are no
90     // explicit jumps to the return block.
91     if (CurBB->empty() || ReturnBlock->use_empty()) {
92       ReturnBlock->replaceAllUsesWith(CurBB);
93       delete ReturnBlock;
94     } else
95       EmitBlock(ReturnBlock);
96     return;
97   }
98 
99   // Otherwise, if the return block is the target of a single direct
100   // branch then we can just put the code in that block instead. This
101   // cleans up functions which started with a unified return block.
102   if (ReturnBlock->hasOneUse()) {
103     llvm::BranchInst *BI =
104       dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
105     if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
106       // Reset insertion point and delete the branch.
107       Builder.SetInsertPoint(BI->getParent());
108       BI->eraseFromParent();
109       delete ReturnBlock;
110       return;
111     }
112   }
113 
114   // FIXME: We are at an unreachable point, there is no reason to emit the block
115   // unless it has uses. However, we still need a place to put the debug
116   // region.end for now.
117 
118   EmitBlock(ReturnBlock);
119 }
120 
121 void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
122   assert(BreakContinueStack.empty() &&
123          "mismatched push/pop in break/continue stack!");
124   assert(BlockScopes.empty() &&
125          "did not remove all blocks from block scope map!");
126   assert(CleanupEntries.empty() &&
127          "mismatched push/pop in cleanup stack!");
128 
129   // Emit function epilog (to return).
130   EmitReturnBlock();
131 
132   EmitFunctionInstrumentation("__cyg_profile_func_exit");
133 
134   // Emit debug descriptor for function end.
135   if (CGDebugInfo *DI = getDebugInfo()) {
136     DI->setLocation(EndLoc);
137     DI->EmitRegionEnd(CurFn, Builder);
138   }
139 
140   EmitFunctionEpilog(*CurFnInfo, ReturnValue);
141   EmitEndEHSpec(CurCodeDecl);
142 
143   // If someone did an indirect goto, emit the indirect goto block at the end of
144   // the function.
145   if (IndirectBranch) {
146     EmitBlock(IndirectBranch->getParent());
147     Builder.ClearInsertionPoint();
148   }
149 
150   // Remove the AllocaInsertPt instruction, which is just a convenience for us.
151   llvm::Instruction *Ptr = AllocaInsertPt;
152   AllocaInsertPt = 0;
153   Ptr->eraseFromParent();
154 
155   // If someone took the address of a label but never did an indirect goto, we
156   // made a zero entry PHI node, which is illegal, zap it now.
157   if (IndirectBranch) {
158     llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
159     if (PN->getNumIncomingValues() == 0) {
160       PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
161       PN->eraseFromParent();
162     }
163   }
164 }
165 
166 /// ShouldInstrumentFunction - Return true if the current function should be
167 /// instrumented with __cyg_profile_func_* calls
168 bool CodeGenFunction::ShouldInstrumentFunction() {
169   if (!CGM.getCodeGenOpts().InstrumentFunctions)
170     return false;
171   if (CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
172     return false;
173   return true;
174 }
175 
176 /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
177 /// instrumentation function with the current function and the call site, if
178 /// function instrumentation is enabled.
179 void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
180   if (!ShouldInstrumentFunction())
181     return;
182 
183   const llvm::PointerType *PointerTy;
184   const llvm::FunctionType *FunctionTy;
185   std::vector<const llvm::Type*> ProfileFuncArgs;
186 
187   // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
188   PointerTy = llvm::Type::getInt8PtrTy(VMContext);
189   ProfileFuncArgs.push_back(PointerTy);
190   ProfileFuncArgs.push_back(PointerTy);
191   FunctionTy = llvm::FunctionType::get(
192     llvm::Type::getVoidTy(VMContext),
193     ProfileFuncArgs, false);
194 
195   llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
196   llvm::CallInst *CallSite = Builder.CreateCall(
197     CGM.getIntrinsic(llvm::Intrinsic::returnaddress, 0, 0),
198     llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0),
199     "callsite");
200 
201   Builder.CreateCall2(F,
202                       llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
203                       CallSite);
204 }
205 
206 void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
207                                     llvm::Function *Fn,
208                                     const FunctionArgList &Args,
209                                     SourceLocation StartLoc) {
210   const Decl *D = GD.getDecl();
211 
212   DidCallStackSave = false;
213   CurCodeDecl = CurFuncDecl = D;
214   FnRetTy = RetTy;
215   CurFn = Fn;
216   assert(CurFn->isDeclaration() && "Function already has body?");
217 
218   // Pass inline keyword to optimizer if it appears explicitly on any
219   // declaration.
220   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
221     for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
222            RE = FD->redecls_end(); RI != RE; ++RI)
223       if (RI->isInlineSpecified()) {
224         Fn->addFnAttr(llvm::Attribute::InlineHint);
225         break;
226       }
227 
228   llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
229 
230   // Create a marker to make it easy to insert allocas into the entryblock
231   // later.  Don't create this with the builder, because we don't want it
232   // folded.
233   llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext));
234   AllocaInsertPt = new llvm::BitCastInst(Undef,
235                                          llvm::Type::getInt32Ty(VMContext), "",
236                                          EntryBB);
237   if (Builder.isNamePreserving())
238     AllocaInsertPt->setName("allocapt");
239 
240   ReturnBlock = createBasicBlock("return");
241 
242   Builder.SetInsertPoint(EntryBB);
243 
244   QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0,
245                                                  false, false, 0, 0,
246                                                  /*FIXME?*/
247                                                  FunctionType::ExtInfo());
248 
249   // Emit subprogram debug descriptor.
250   if (CGDebugInfo *DI = getDebugInfo()) {
251     DI->setLocation(StartLoc);
252     DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
253   }
254 
255   EmitFunctionInstrumentation("__cyg_profile_func_enter");
256 
257   // FIXME: Leaked.
258   // CC info is ignored, hopefully?
259   CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args,
260                                               FunctionType::ExtInfo());
261 
262   if (RetTy->isVoidType()) {
263     // Void type; nothing to return.
264     ReturnValue = 0;
265   } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
266              hasAggregateLLVMType(CurFnInfo->getReturnType())) {
267     // Indirect aggregate return; emit returned value directly into sret slot.
268     // This reduces code size, and affects correctness in C++.
269     ReturnValue = CurFn->arg_begin();
270   } else {
271     ReturnValue = CreateIRTemp(RetTy, "retval");
272   }
273 
274   EmitStartEHSpec(CurCodeDecl);
275   EmitFunctionProlog(*CurFnInfo, CurFn, Args);
276 
277   if (CXXThisDecl)
278     CXXThisValue = Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
279   if (CXXVTTDecl)
280     CXXVTTValue = Builder.CreateLoad(LocalDeclMap[CXXVTTDecl], "vtt");
281 
282   // If any of the arguments have a variably modified type, make sure to
283   // emit the type size.
284   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
285        i != e; ++i) {
286     QualType Ty = i->second;
287 
288     if (Ty->isVariablyModifiedType())
289       EmitVLASize(Ty);
290   }
291 }
292 
293 void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
294   const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
295   assert(FD->getBody());
296   EmitStmt(FD->getBody());
297 }
298 
299 void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) {
300   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
301 
302   // Check if we should generate debug info for this function.
303   if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>())
304     DebugInfo = CGM.getDebugInfo();
305 
306   FunctionArgList Args;
307 
308   CurGD = GD;
309   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
310     if (MD->isInstance()) {
311       // Create the implicit 'this' decl.
312       // FIXME: I'm not entirely sure I like using a fake decl just for code
313       // generation. Maybe we can come up with a better way?
314       CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0,
315                                               FD->getLocation(),
316                                               &getContext().Idents.get("this"),
317                                               MD->getThisType(getContext()));
318       Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
319 
320       // Check if we need a VTT parameter as well.
321       if (CodeGenVTables::needsVTTParameter(GD)) {
322         // FIXME: The comment about using a fake decl above applies here too.
323         QualType T = getContext().getPointerType(getContext().VoidPtrTy);
324         CXXVTTDecl =
325           ImplicitParamDecl::Create(getContext(), 0, FD->getLocation(),
326                                     &getContext().Idents.get("vtt"), T);
327         Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType()));
328       }
329     }
330   }
331 
332   if (FD->getNumParams()) {
333     const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>();
334     assert(FProto && "Function def must have prototype!");
335 
336     for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
337       Args.push_back(std::make_pair(FD->getParamDecl(i),
338                                     FProto->getArgType(i)));
339   }
340 
341   SourceRange BodyRange;
342   if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
343 
344   // Emit the standard function prologue.
345   StartFunction(GD, FD->getResultType(), Fn, Args, BodyRange.getBegin());
346 
347   // Generate the body of the function.
348   if (isa<CXXDestructorDecl>(FD))
349     EmitDestructorBody(Args);
350   else if (isa<CXXConstructorDecl>(FD))
351     EmitConstructorBody(Args);
352   else
353     EmitFunctionBody(Args);
354 
355   // Emit the standard function epilogue.
356   FinishFunction(BodyRange.getEnd());
357 
358   // Destroy the 'this' declaration.
359   if (CXXThisDecl)
360     CXXThisDecl->Destroy(getContext());
361 
362   // Destroy the VTT declaration.
363   if (CXXVTTDecl)
364     CXXVTTDecl->Destroy(getContext());
365 }
366 
367 /// ContainsLabel - Return true if the statement contains a label in it.  If
368 /// this statement is not executed normally, it not containing a label means
369 /// that we can just remove the code.
370 bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
371   // Null statement, not a label!
372   if (S == 0) return false;
373 
374   // If this is a label, we have to emit the code, consider something like:
375   // if (0) {  ...  foo:  bar(); }  goto foo;
376   if (isa<LabelStmt>(S))
377     return true;
378 
379   // If this is a case/default statement, and we haven't seen a switch, we have
380   // to emit the code.
381   if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
382     return true;
383 
384   // If this is a switch statement, we want to ignore cases below it.
385   if (isa<SwitchStmt>(S))
386     IgnoreCaseStmts = true;
387 
388   // Scan subexpressions for verboten labels.
389   for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
390        I != E; ++I)
391     if (ContainsLabel(*I, IgnoreCaseStmts))
392       return true;
393 
394   return false;
395 }
396 
397 
398 /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
399 /// a constant, or if it does but contains a label, return 0.  If it constant
400 /// folds to 'true' and does not contain a label, return 1, if it constant folds
401 /// to 'false' and does not contain a label, return -1.
402 int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
403   // FIXME: Rename and handle conversion of other evaluatable things
404   // to bool.
405   Expr::EvalResult Result;
406   if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
407       Result.HasSideEffects)
408     return 0;  // Not foldable, not integer or not fully evaluatable.
409 
410   if (CodeGenFunction::ContainsLabel(Cond))
411     return 0;  // Contains a label.
412 
413   return Result.Val.getInt().getBoolValue() ? 1 : -1;
414 }
415 
416 
417 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
418 /// statement) to the specified blocks.  Based on the condition, this might try
419 /// to simplify the codegen of the conditional based on the branch.
420 ///
421 void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
422                                            llvm::BasicBlock *TrueBlock,
423                                            llvm::BasicBlock *FalseBlock) {
424   if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
425     return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
426 
427   if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
428     // Handle X && Y in a condition.
429     if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
430       // If we have "1 && X", simplify the code.  "0 && X" would have constant
431       // folded if the case was simple enough.
432       if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
433         // br(1 && X) -> br(X).
434         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
435       }
436 
437       // If we have "X && 1", simplify the code to use an uncond branch.
438       // "X && 0" would have been constant folded to 0.
439       if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
440         // br(X && 1) -> br(X).
441         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
442       }
443 
444       // Emit the LHS as a conditional.  If the LHS conditional is false, we
445       // want to jump to the FalseBlock.
446       llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
447       EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
448       EmitBlock(LHSTrue);
449 
450       // Any temporaries created here are conditional.
451       BeginConditionalBranch();
452       EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
453       EndConditionalBranch();
454 
455       return;
456     } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
457       // If we have "0 || X", simplify the code.  "1 || X" would have constant
458       // folded if the case was simple enough.
459       if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
460         // br(0 || X) -> br(X).
461         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
462       }
463 
464       // If we have "X || 0", simplify the code to use an uncond branch.
465       // "X || 1" would have been constant folded to 1.
466       if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
467         // br(X || 0) -> br(X).
468         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
469       }
470 
471       // Emit the LHS as a conditional.  If the LHS conditional is true, we
472       // want to jump to the TrueBlock.
473       llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
474       EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
475       EmitBlock(LHSFalse);
476 
477       // Any temporaries created here are conditional.
478       BeginConditionalBranch();
479       EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
480       EndConditionalBranch();
481 
482       return;
483     }
484   }
485 
486   if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
487     // br(!x, t, f) -> br(x, f, t)
488     if (CondUOp->getOpcode() == UnaryOperator::LNot)
489       return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
490   }
491 
492   if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
493     // Handle ?: operator.
494 
495     // Just ignore GNU ?: extension.
496     if (CondOp->getLHS()) {
497       // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
498       llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
499       llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
500       EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
501       EmitBlock(LHSBlock);
502       EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
503       EmitBlock(RHSBlock);
504       EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
505       return;
506     }
507   }
508 
509   // Emit the code with the fully general case.
510   llvm::Value *CondV = EvaluateExprAsBool(Cond);
511   Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
512 }
513 
514 /// ErrorUnsupported - Print out an error that codegen doesn't support the
515 /// specified stmt yet.
516 void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
517                                        bool OmitOnError) {
518   CGM.ErrorUnsupported(S, Type, OmitOnError);
519 }
520 
521 void
522 CodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
523   // If the type contains a pointer to data member we can't memset it to zero.
524   // Instead, create a null constant and copy it to the destination.
525   if (CGM.getTypes().ContainsPointerToDataMember(Ty)) {
526     llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
527 
528     llvm::GlobalVariable *NullVariable =
529       new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
530                                /*isConstant=*/true,
531                                llvm::GlobalVariable::PrivateLinkage,
532                                NullConstant, llvm::Twine());
533     EmitAggregateCopy(DestPtr, NullVariable, Ty, /*isVolatile=*/false);
534     return;
535   }
536 
537 
538   // Ignore empty classes in C++.
539   if (getContext().getLangOptions().CPlusPlus) {
540     if (const RecordType *RT = Ty->getAs<RecordType>()) {
541       if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
542         return;
543     }
544   }
545 
546   // Otherwise, just memset the whole thing to zero.  This is legal
547   // because in LLVM, all default initializers (other than the ones we just
548   // handled above) are guaranteed to have a bit pattern of all zeros.
549   const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
550   if (DestPtr->getType() != BP)
551     DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
552 
553   // Get size and alignment info for this aggregate.
554   std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
555 
556   // Don't bother emitting a zero-byte memset.
557   if (TypeInfo.first == 0)
558     return;
559 
560   // FIXME: Handle variable sized types.
561   const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext,
562                                                     LLVMPointerWidth);
563 
564   Builder.CreateCall5(CGM.getMemSetFn(BP, IntPtr), DestPtr,
565                  llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
566                       // TypeInfo.first describes size in bits.
567                       llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
568                       llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
569                                              TypeInfo.second/8),
570                       llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext),
571                                              0));
572 }
573 
574 llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) {
575   // Make sure that there is a block for the indirect goto.
576   if (IndirectBranch == 0)
577     GetIndirectGotoBlock();
578 
579   llvm::BasicBlock *BB = getBasicBlockForLabel(L);
580 
581   // Make sure the indirect branch includes all of the address-taken blocks.
582   IndirectBranch->addDestination(BB);
583   return llvm::BlockAddress::get(CurFn, BB);
584 }
585 
586 llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
587   // If we already made the indirect branch for indirect goto, return its block.
588   if (IndirectBranch) return IndirectBranch->getParent();
589 
590   CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
591 
592   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
593 
594   // Create the PHI node that indirect gotos will add entries to.
595   llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest");
596 
597   // Create the indirect branch instruction.
598   IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
599   return IndirectBranch->getParent();
600 }
601 
602 llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
603   llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
604 
605   assert(SizeEntry && "Did not emit size for type");
606   return SizeEntry;
607 }
608 
609 llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
610   assert(Ty->isVariablyModifiedType() &&
611          "Must pass variably modified type to EmitVLASizes!");
612 
613   EnsureInsertPoint();
614 
615   if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
616     llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
617 
618     if (!SizeEntry) {
619       const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
620 
621       // Get the element size;
622       QualType ElemTy = VAT->getElementType();
623       llvm::Value *ElemSize;
624       if (ElemTy->isVariableArrayType())
625         ElemSize = EmitVLASize(ElemTy);
626       else
627         ElemSize = llvm::ConstantInt::get(SizeTy,
628             getContext().getTypeSizeInChars(ElemTy).getQuantity());
629 
630       llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
631       NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
632 
633       SizeEntry = Builder.CreateMul(ElemSize, NumElements);
634     }
635 
636     return SizeEntry;
637   }
638 
639   if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
640     EmitVLASize(AT->getElementType());
641     return 0;
642   }
643 
644   const PointerType *PT = Ty->getAs<PointerType>();
645   assert(PT && "unknown VM type!");
646   EmitVLASize(PT->getPointeeType());
647   return 0;
648 }
649 
650 llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
651   if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
652     return EmitScalarExpr(E);
653   }
654   return EmitLValue(E).getAddress();
655 }
656 
657 void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock,
658                                        llvm::BasicBlock *CleanupExitBlock,
659                                        llvm::BasicBlock *PreviousInvokeDest,
660                                        bool EHOnly) {
661   CleanupEntries.push_back(CleanupEntry(CleanupEntryBlock, CleanupExitBlock,
662                                         PreviousInvokeDest, EHOnly));
663 }
664 
665 void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) {
666   assert(CleanupEntries.size() >= OldCleanupStackSize &&
667          "Cleanup stack mismatch!");
668 
669   while (CleanupEntries.size() > OldCleanupStackSize)
670     EmitCleanupBlock();
671 }
672 
673 CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() {
674   CleanupEntry &CE = CleanupEntries.back();
675 
676   llvm::BasicBlock *CleanupEntryBlock = CE.CleanupEntryBlock;
677 
678   std::vector<llvm::BasicBlock *> Blocks;
679   std::swap(Blocks, CE.Blocks);
680 
681   std::vector<llvm::BranchInst *> BranchFixups;
682   std::swap(BranchFixups, CE.BranchFixups);
683 
684   bool EHOnly = CE.EHOnly;
685 
686   setInvokeDest(CE.PreviousInvokeDest);
687 
688   CleanupEntries.pop_back();
689 
690   // Check if any branch fixups pointed to the scope we just popped. If so,
691   // we can remove them.
692   for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
693     llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
694     BlockScopeMap::iterator I = BlockScopes.find(Dest);
695 
696     if (I == BlockScopes.end())
697       continue;
698 
699     assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
700 
701     if (I->second == CleanupEntries.size()) {
702       // We don't need to do this branch fixup.
703       BranchFixups[i] = BranchFixups.back();
704       BranchFixups.pop_back();
705       i--;
706       e--;
707       continue;
708     }
709   }
710 
711   llvm::BasicBlock *SwitchBlock = CE.CleanupExitBlock;
712   llvm::BasicBlock *EndBlock = 0;
713   if (!BranchFixups.empty()) {
714     if (!SwitchBlock)
715       SwitchBlock = createBasicBlock("cleanup.switch");
716     EndBlock = createBasicBlock("cleanup.end");
717 
718     llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
719 
720     Builder.SetInsertPoint(SwitchBlock);
721 
722     llvm::Value *DestCodePtr
723       = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext),
724                          "cleanup.dst");
725     llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
726 
727     // Create a switch instruction to determine where to jump next.
728     llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
729                                                 BranchFixups.size());
730 
731     // Restore the current basic block (if any)
732     if (CurBB) {
733       Builder.SetInsertPoint(CurBB);
734 
735       // If we had a current basic block, we also need to emit an instruction
736       // to initialize the cleanup destination.
737       Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)),
738                           DestCodePtr);
739     } else
740       Builder.ClearInsertionPoint();
741 
742     for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
743       llvm::BranchInst *BI = BranchFixups[i];
744       llvm::BasicBlock *Dest = BI->getSuccessor(0);
745 
746       // Fixup the branch instruction to point to the cleanup block.
747       BI->setSuccessor(0, CleanupEntryBlock);
748 
749       if (CleanupEntries.empty()) {
750         llvm::ConstantInt *ID;
751 
752         // Check if we already have a destination for this block.
753         if (Dest == SI->getDefaultDest())
754           ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
755         else {
756           ID = SI->findCaseDest(Dest);
757           if (!ID) {
758             // No code found, get a new unique one by using the number of
759             // switch successors.
760             ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
761                                         SI->getNumSuccessors());
762             SI->addCase(ID, Dest);
763           }
764         }
765 
766         // Store the jump destination before the branch instruction.
767         new llvm::StoreInst(ID, DestCodePtr, BI);
768       } else {
769         // We need to jump through another cleanup block. Create a pad block
770         // with a branch instruction that jumps to the final destination and add
771         // it as a branch fixup to the current cleanup scope.
772 
773         // Create the pad block.
774         llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
775 
776         // Create a unique case ID.
777         llvm::ConstantInt *ID
778           = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
779                                    SI->getNumSuccessors());
780 
781         // Store the jump destination before the branch instruction.
782         new llvm::StoreInst(ID, DestCodePtr, BI);
783 
784         // Add it as the destination.
785         SI->addCase(ID, CleanupPad);
786 
787         // Create the branch to the final destination.
788         llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
789         CleanupPad->getInstList().push_back(BI);
790 
791         // And add it as a branch fixup.
792         CleanupEntries.back().BranchFixups.push_back(BI);
793       }
794     }
795   }
796 
797   // Remove all blocks from the block scope map.
798   for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
799     assert(BlockScopes.count(Blocks[i]) &&
800            "Did not find block in scope map!");
801 
802     BlockScopes.erase(Blocks[i]);
803   }
804 
805   return CleanupBlockInfo(CleanupEntryBlock, SwitchBlock, EndBlock, EHOnly);
806 }
807 
808 void CodeGenFunction::EmitCleanupBlock() {
809   CleanupBlockInfo Info = PopCleanupBlock();
810 
811   if (Info.EHOnly) {
812     // FIXME: Add this to the exceptional edge
813     if (Info.CleanupBlock->getNumUses() == 0)
814       delete Info.CleanupBlock;
815     return;
816   }
817 
818   //  Scrub debug location info.
819   for (llvm::BasicBlock::iterator LBI = Info.CleanupBlock->begin(),
820          LBE = Info.CleanupBlock->end(); LBI != LBE; ++LBI)
821     Builder.SetInstDebugLocation(LBI);
822 
823   llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
824   if (CurBB && !CurBB->getTerminator() &&
825       Info.CleanupBlock->getNumUses() == 0) {
826     CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList());
827     delete Info.CleanupBlock;
828   } else
829     EmitBlock(Info.CleanupBlock);
830 
831   if (Info.SwitchBlock)
832     EmitBlock(Info.SwitchBlock);
833   if (Info.EndBlock)
834     EmitBlock(Info.EndBlock);
835 }
836 
837 void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) {
838   assert(!CleanupEntries.empty() &&
839          "Trying to add branch fixup without cleanup block!");
840 
841   // FIXME: We could be more clever here and check if there's already a branch
842   // fixup for this destination and recycle it.
843   CleanupEntries.back().BranchFixups.push_back(BI);
844 }
845 
846 void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) {
847   if (!HaveInsertPoint())
848     return;
849 
850   llvm::BranchInst* BI = Builder.CreateBr(Dest);
851 
852   Builder.ClearInsertionPoint();
853 
854   // The stack is empty, no need to do any cleanup.
855   if (CleanupEntries.empty())
856     return;
857 
858   if (!Dest->getParent()) {
859     // We are trying to branch to a block that hasn't been inserted yet.
860     AddBranchFixup(BI);
861     return;
862   }
863 
864   BlockScopeMap::iterator I = BlockScopes.find(Dest);
865   if (I == BlockScopes.end()) {
866     // We are trying to jump to a block that is outside of any cleanup scope.
867     AddBranchFixup(BI);
868     return;
869   }
870 
871   assert(I->second < CleanupEntries.size() &&
872          "Trying to branch into cleanup region");
873 
874   if (I->second == CleanupEntries.size() - 1) {
875     // We have a branch to a block in the same scope.
876     return;
877   }
878 
879   AddBranchFixup(BI);
880 }
881