CodeGenFunction.cpp revision 88b5396b0897f28d22ae3debf4a0d97b33b6c362
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 "llvm/Support/CFG.h"
22using namespace clang;
23using namespace CodeGen;
24
25CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
26  : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
27    CaseRangeBlock(NULL) {
28    LLVMIntTy = ConvertType(getContext().IntTy);
29    LLVMPointerWidth = Target.getPointerWidth(0);
30}
31
32ASTContext &CodeGenFunction::getContext() const {
33  return CGM.getContext();
34}
35
36
37llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
38  llvm::BasicBlock *&BB = LabelMap[S];
39  if (BB) return BB;
40
41  // Create, but don't insert, the new block.
42  return BB = createBasicBlock(S->getName());
43}
44
45llvm::Constant *
46CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
47  return cast<llvm::Constant>(LocalDeclMap[BVD]);
48}
49
50llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD)
51{
52  return LocalDeclMap[VD];
53}
54
55const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
56  return CGM.getTypes().ConvertType(T);
57}
58
59bool CodeGenFunction::isObjCPointerType(QualType T) {
60  // All Objective-C types are pointers.
61  return T->isObjCInterfaceType() ||
62    T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
63}
64
65bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
66  // FIXME: Use positive checks instead of negative ones to be more
67  // robust in the face of extension.
68  return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
69    !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
70    !T->isBlockPointerType();
71}
72
73void CodeGenFunction::EmitReturnBlock() {
74  // For cleanliness, we try to avoid emitting the return block for
75  // simple cases.
76  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
77
78  if (CurBB) {
79    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
80
81    // We have a valid insert point, reuse it if there are no explicit
82    // jumps to the return block.
83    if (ReturnBlock->use_empty())
84      delete ReturnBlock;
85    else
86      EmitBlock(ReturnBlock);
87    return;
88  }
89
90  // Otherwise, if the return block is the target of a single direct
91  // branch then we can just put the code in that block instead. This
92  // cleans up functions which started with a unified return block.
93  if (ReturnBlock->hasOneUse()) {
94    llvm::BranchInst *BI =
95      dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
96    if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
97      // Reset insertion point and delete the branch.
98      Builder.SetInsertPoint(BI->getParent());
99      BI->eraseFromParent();
100      delete ReturnBlock;
101      return;
102    }
103  }
104
105  // FIXME: We are at an unreachable point, there is no reason to emit
106  // the block unless it has uses. However, we still need a place to
107  // put the debug region.end for now.
108
109  EmitBlock(ReturnBlock);
110}
111
112void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
113  // Finish emission of indirect switches.
114  EmitIndirectSwitches();
115
116  assert(BreakContinueStack.empty() &&
117         "mismatched push/pop in break/continue stack!");
118
119  // Emit function epilog (to return).
120  EmitReturnBlock();
121
122  // Emit debug descriptor for function end.
123  if (CGDebugInfo *DI = CGM.getDebugInfo()) {
124    DI->setLocation(EndLoc);
125    DI->EmitRegionEnd(CurFn, Builder);
126  }
127
128  EmitFunctionEpilog(*CurFnInfo, ReturnValue);
129
130  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
131  AllocaInsertPt->eraseFromParent();
132  AllocaInsertPt = 0;
133}
134
135void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
136                                    llvm::Function *Fn,
137                                    const FunctionArgList &Args,
138                                    SourceLocation StartLoc) {
139  CurFuncDecl = D;
140  FnRetTy = RetTy;
141  CurFn = Fn;
142  assert(CurFn->isDeclaration() && "Function already has body?");
143
144  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
145
146  // Create a marker to make it easy to insert allocas into the entryblock
147  // later.  Don't create this with the builder, because we don't want it
148  // folded.
149  llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
150  AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
151                                         EntryBB);
152
153  ReturnBlock = createBasicBlock("return");
154  ReturnValue = 0;
155  if (!RetTy->isVoidType())
156    ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
157
158  Builder.SetInsertPoint(EntryBB);
159
160  // Emit subprogram debug descriptor.
161  // FIXME: The cast here is a huge hack.
162  if (CGDebugInfo *DI = CGM.getDebugInfo()) {
163    DI->setLocation(StartLoc);
164    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
165      DI->EmitFunctionStart(FD->getIdentifier()->getName(),
166                            RetTy, CurFn, Builder);
167    } else {
168      // Just use LLVM function name.
169      DI->EmitFunctionStart(Fn->getName().c_str(),
170                            RetTy, CurFn, Builder);
171    }
172  }
173
174  // FIXME: Leaked.
175  CurFnInfo = new CGFunctionInfo(FnRetTy, Args);
176  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
177
178  // If any of the arguments have a variably modified type, make sure to
179  // emit the type size.
180  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
181       i != e; ++i) {
182    QualType Ty = i->second;
183
184    if (Ty->isVariablyModifiedType())
185      EmitVLASize(Ty);
186  }
187}
188
189void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
190                                   llvm::Function *Fn) {
191  FunctionArgList Args;
192  if (FD->getNumParams()) {
193    const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
194    assert(FProto && "Function def must have prototype!");
195
196    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
197      Args.push_back(std::make_pair(FD->getParamDecl(i),
198                                    FProto->getArgType(i)));
199  }
200
201  StartFunction(FD, FD->getResultType(), Fn, Args,
202                cast<CompoundStmt>(FD->getBody())->getLBracLoc());
203
204  EmitStmt(FD->getBody());
205
206  const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
207  if (S) {
208    FinishFunction(S->getRBracLoc());
209  } else {
210    FinishFunction();
211  }
212}
213
214/// ContainsLabel - Return true if the statement contains a label in it.  If
215/// this statement is not executed normally, it not containing a label means
216/// that we can just remove the code.
217bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
218  // Null statement, not a label!
219  if (S == 0) return false;
220
221  // If this is a label, we have to emit the code, consider something like:
222  // if (0) {  ...  foo:  bar(); }  goto foo;
223  if (isa<LabelStmt>(S))
224    return true;
225
226  // If this is a case/default statement, and we haven't seen a switch, we have
227  // to emit the code.
228  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
229    return true;
230
231  // If this is a switch statement, we want to ignore cases below it.
232  if (isa<SwitchStmt>(S))
233    IgnoreCaseStmts = true;
234
235  // Scan subexpressions for verboten labels.
236  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
237       I != E; ++I)
238    if (ContainsLabel(*I, IgnoreCaseStmts))
239      return true;
240
241  return false;
242}
243
244
245/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
246/// a constant, or if it does but contains a label, return 0.  If it constant
247/// folds to 'true' and does not contain a label, return 1, if it constant folds
248/// to 'false' and does not contain a label, return -1.
249int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
250  // FIXME: Rename and handle conversion of other evaluatable things
251  // to bool.
252  Expr::EvalResult Result;
253  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
254      Result.HasSideEffects)
255    return 0;  // Not foldable, not integer or not fully evaluatable.
256
257  if (CodeGenFunction::ContainsLabel(Cond))
258    return 0;  // Contains a label.
259
260  return Result.Val.getInt().getBoolValue() ? 1 : -1;
261}
262
263
264/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
265/// statement) to the specified blocks.  Based on the condition, this might try
266/// to simplify the codegen of the conditional based on the branch.
267///
268void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
269                                           llvm::BasicBlock *TrueBlock,
270                                           llvm::BasicBlock *FalseBlock) {
271  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
272    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
273
274  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
275    // Handle X && Y in a condition.
276    if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
277      // If we have "1 && X", simplify the code.  "0 && X" would have constant
278      // folded if the case was simple enough.
279      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
280        // br(1 && X) -> br(X).
281        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
282      }
283
284      // If we have "X && 1", simplify the code to use an uncond branch.
285      // "X && 0" would have been constant folded to 0.
286      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
287        // br(X && 1) -> br(X).
288        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
289      }
290
291      // Emit the LHS as a conditional.  If the LHS conditional is false, we
292      // want to jump to the FalseBlock.
293      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
294      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
295      EmitBlock(LHSTrue);
296
297      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
298      return;
299    } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
300      // If we have "0 || X", simplify the code.  "1 || X" would have constant
301      // folded if the case was simple enough.
302      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
303        // br(0 || X) -> br(X).
304        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
305      }
306
307      // If we have "X || 0", simplify the code to use an uncond branch.
308      // "X || 1" would have been constant folded to 1.
309      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
310        // br(X || 0) -> br(X).
311        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
312      }
313
314      // Emit the LHS as a conditional.  If the LHS conditional is true, we
315      // want to jump to the TrueBlock.
316      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
317      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
318      EmitBlock(LHSFalse);
319
320      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
321      return;
322    }
323  }
324
325  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
326    // br(!x, t, f) -> br(x, f, t)
327    if (CondUOp->getOpcode() == UnaryOperator::LNot)
328      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
329  }
330
331  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
332    // Handle ?: operator.
333
334    // Just ignore GNU ?: extension.
335    if (CondOp->getLHS()) {
336      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
337      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
338      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
339      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
340      EmitBlock(LHSBlock);
341      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
342      EmitBlock(RHSBlock);
343      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
344      return;
345    }
346  }
347
348  // Emit the code with the fully general case.
349  llvm::Value *CondV = EvaluateExprAsBool(Cond);
350  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
351}
352
353/// getCGRecordLayout - Return record layout info.
354const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
355                                                         QualType Ty) {
356  const RecordType *RTy = Ty->getAsRecordType();
357  assert (RTy && "Unexpected type. RecordType expected here.");
358
359  return CGT.getCGRecordLayout(RTy->getDecl());
360}
361
362/// ErrorUnsupported - Print out an error that codegen doesn't support the
363/// specified stmt yet.
364void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
365                                       bool OmitOnError) {
366  CGM.ErrorUnsupported(S, Type, OmitOnError);
367}
368
369unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
370  // Use LabelIDs.size() as the new ID if one hasn't been assigned.
371  return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
372}
373
374void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
375{
376  const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
377  if (DestPtr->getType() != BP)
378    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
379
380  // Get size and alignment info for this aggregate.
381  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
382
383  // FIXME: Handle variable sized types.
384  const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
385
386  Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
387                      llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
388                      // TypeInfo.first describes size in bits.
389                      llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
390                      llvm::ConstantInt::get(llvm::Type::Int32Ty,
391                                             TypeInfo.second/8));
392}
393
394void CodeGenFunction::EmitIndirectSwitches() {
395  llvm::BasicBlock *Default;
396
397  if (IndirectSwitches.empty())
398    return;
399
400  if (!LabelIDs.empty()) {
401    Default = getBasicBlockForLabel(LabelIDs.begin()->first);
402  } else {
403    // No possible targets for indirect goto, just emit an infinite
404    // loop.
405    Default = createBasicBlock("indirectgoto.loop", CurFn);
406    llvm::BranchInst::Create(Default, Default);
407  }
408
409  for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
410         e = IndirectSwitches.end(); i != e; ++i) {
411    llvm::SwitchInst *I = *i;
412
413    I->setSuccessor(0, Default);
414    for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
415           LE = LabelIDs.end(); LI != LE; ++LI) {
416      I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
417                                        LI->second),
418                 getBasicBlockForLabel(LI->first));
419    }
420  }
421}
422
423llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty)
424{
425  // FIXME: This entire method is hardcoded for 32-bit X86.
426
427  const char *TargetPrefix = getContext().Target.getTargetPrefix();
428
429  if (strcmp(TargetPrefix, "x86") != 0 ||
430      getContext().Target.getPointerWidth(0) != 32)
431    return 0;
432
433  const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
434  const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
435
436  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
437                                                       "ap");
438  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
439  llvm::Value *AddrTyped =
440    Builder.CreateBitCast(Addr,
441                          llvm::PointerType::getUnqual(ConvertType(Ty)));
442
443  uint64_t SizeInBytes = getContext().getTypeSize(Ty) / 8;
444  const unsigned ArgumentSizeInBytes = 4;
445  if (SizeInBytes < ArgumentSizeInBytes)
446    SizeInBytes = ArgumentSizeInBytes;
447
448  llvm::Value *NextAddr =
449    Builder.CreateGEP(Addr,
450                      llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes),
451                      "ap.next");
452  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
453
454  return AddrTyped;
455}
456
457
458llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
459{
460  llvm::Value *&SizeEntry = VLASizeMap[VAT];
461
462  assert(SizeEntry && "Did not emit size for type");
463  return SizeEntry;
464}
465
466llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
467{
468  assert(Ty->isVariablyModifiedType() &&
469         "Must pass variably modified type to EmitVLASizes!");
470
471  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
472    llvm::Value *&SizeEntry = VLASizeMap[VAT];
473
474    if (!SizeEntry) {
475      // Get the element size;
476      llvm::Value *ElemSize;
477
478      QualType ElemTy = VAT->getElementType();
479
480      if (ElemTy->isVariableArrayType())
481        ElemSize = EmitVLASize(ElemTy);
482      else {
483        // FIXME: We use Int32Ty here because the alloca instruction takes a
484        // 32-bit integer. What should we do about overflow?
485        ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty,
486                                          getContext().getTypeSize(ElemTy) / 8);
487      }
488
489      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
490
491      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
492    }
493
494    return SizeEntry;
495  } else if (const PointerType *PT = Ty->getAsPointerType())
496    EmitVLASize(PT->getPointeeType());
497  else {
498    assert(0 && "unknown VM type!");
499  }
500
501  return 0;
502}
503
504llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
505  if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
506    return EmitScalarExpr(E);
507  }
508  return EmitLValue(E).getAddress();
509}
510