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