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" 22 using namespace clang; 23 using namespace CodeGen; 24 25 CodeGenFunction::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 32 ASTContext &CodeGenFunction::getContext() const { 33 return CGM.getContext(); 34 } 35 36 37 llvm::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 45 llvm::Constant * 46 CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { 47 return cast<llvm::Constant>(LocalDeclMap[BVD]); 48 } 49 50 llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) 51 { 52 return LocalDeclMap[VD]; 53 } 54 55 const llvm::Type *CodeGenFunction::ConvertType(QualType T) { 56 return CGM.getTypes().ConvertType(T); 57 } 58 59 bool CodeGenFunction::isObjCPointerType(QualType T) { 60 // All Objective-C types are pointers. 61 return T->isObjCInterfaceType() || 62 T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType(); 63 } 64 65 bool 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 73 void 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 112 void 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(FnRetTy, ReturnValue); 129 130 // Remove the AllocaInsertPt instruction, which is just a convenience for us. 131 AllocaInsertPt->eraseFromParent(); 132 AllocaInsertPt = 0; 133 } 134 135 void 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 EmitFunctionProlog(CurFn, FnRetTy, Args); 175 176 // If any of the arguments have a variably modified type, make sure to 177 // emit the type size. 178 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 179 i != e; ++i) { 180 QualType Ty = i->second; 181 182 if (Ty->isVariablyModifiedType()) 183 EmitVLASize(Ty); 184 } 185 } 186 187 void CodeGenFunction::GenerateCode(const FunctionDecl *FD, 188 llvm::Function *Fn) { 189 FunctionArgList Args; 190 if (FD->getNumParams()) { 191 const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto(); 192 assert(FProto && "Function def must have prototype!"); 193 194 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) 195 Args.push_back(std::make_pair(FD->getParamDecl(i), 196 FProto->getArgType(i))); 197 } 198 199 StartFunction(FD, FD->getResultType(), Fn, Args, 200 cast<CompoundStmt>(FD->getBody())->getLBracLoc()); 201 202 EmitStmt(FD->getBody()); 203 204 const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody()); 205 if (S) { 206 FinishFunction(S->getRBracLoc()); 207 } else { 208 FinishFunction(); 209 } 210 } 211 212 /// ContainsLabel - Return true if the statement contains a label in it. If 213 /// this statement is not executed normally, it not containing a label means 214 /// that we can just remove the code. 215 bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { 216 // Null statement, not a label! 217 if (S == 0) return false; 218 219 // If this is a label, we have to emit the code, consider something like: 220 // if (0) { ... foo: bar(); } goto foo; 221 if (isa<LabelStmt>(S)) 222 return true; 223 224 // If this is a case/default statement, and we haven't seen a switch, we have 225 // to emit the code. 226 if (isa<SwitchCase>(S) && !IgnoreCaseStmts) 227 return true; 228 229 // If this is a switch statement, we want to ignore cases below it. 230 if (isa<SwitchStmt>(S)) 231 IgnoreCaseStmts = true; 232 233 // Scan subexpressions for verboten labels. 234 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 235 I != E; ++I) 236 if (ContainsLabel(*I, IgnoreCaseStmts)) 237 return true; 238 239 return false; 240 } 241 242 243 /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to 244 /// a constant, or if it does but contains a label, return 0. If it constant 245 /// folds to 'true' and does not contain a label, return 1, if it constant folds 246 /// to 'false' and does not contain a label, return -1. 247 int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { 248 // FIXME: Rename and handle conversion of other evaluatable things 249 // to bool. 250 Expr::EvalResult Result; 251 if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || 252 Result.HasSideEffects) 253 return 0; // Not foldable, not integer or not fully evaluatable. 254 255 if (CodeGenFunction::ContainsLabel(Cond)) 256 return 0; // Contains a label. 257 258 return Result.Val.getInt().getBoolValue() ? 1 : -1; 259 } 260 261 262 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if 263 /// statement) to the specified blocks. Based on the condition, this might try 264 /// to simplify the codegen of the conditional based on the branch. 265 /// 266 void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, 267 llvm::BasicBlock *TrueBlock, 268 llvm::BasicBlock *FalseBlock) { 269 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) 270 return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); 271 272 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { 273 // Handle X && Y in a condition. 274 if (CondBOp->getOpcode() == BinaryOperator::LAnd) { 275 // If we have "1 && X", simplify the code. "0 && X" would have constant 276 // folded if the case was simple enough. 277 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { 278 // br(1 && X) -> br(X). 279 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 280 } 281 282 // If we have "X && 1", simplify the code to use an uncond branch. 283 // "X && 0" would have been constant folded to 0. 284 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { 285 // br(X && 1) -> br(X). 286 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 287 } 288 289 // Emit the LHS as a conditional. If the LHS conditional is false, we 290 // want to jump to the FalseBlock. 291 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); 292 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); 293 EmitBlock(LHSTrue); 294 295 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 296 return; 297 } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { 298 // If we have "0 || X", simplify the code. "1 || X" would have constant 299 // folded if the case was simple enough. 300 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { 301 // br(0 || X) -> br(X). 302 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 303 } 304 305 // If we have "X || 0", simplify the code to use an uncond branch. 306 // "X || 1" would have been constant folded to 1. 307 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { 308 // br(X || 0) -> br(X). 309 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 310 } 311 312 // Emit the LHS as a conditional. If the LHS conditional is true, we 313 // want to jump to the TrueBlock. 314 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); 315 EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); 316 EmitBlock(LHSFalse); 317 318 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 319 return; 320 } 321 } 322 323 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { 324 // br(!x, t, f) -> br(x, f, t) 325 if (CondUOp->getOpcode() == UnaryOperator::LNot) 326 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); 327 } 328 329 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { 330 // Handle ?: operator. 331 332 // Just ignore GNU ?: extension. 333 if (CondOp->getLHS()) { 334 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) 335 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); 336 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); 337 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); 338 EmitBlock(LHSBlock); 339 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); 340 EmitBlock(RHSBlock); 341 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); 342 return; 343 } 344 } 345 346 // Emit the code with the fully general case. 347 llvm::Value *CondV = EvaluateExprAsBool(Cond); 348 Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); 349 } 350 351 /// getCGRecordLayout - Return record layout info. 352 const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, 353 QualType Ty) { 354 const RecordType *RTy = Ty->getAsRecordType(); 355 assert (RTy && "Unexpected type. RecordType expected here."); 356 357 return CGT.getCGRecordLayout(RTy->getDecl()); 358 } 359 360 /// ErrorUnsupported - Print out an error that codegen doesn't support the 361 /// specified stmt yet. 362 void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, 363 bool OmitOnError) { 364 CGM.ErrorUnsupported(S, Type, OmitOnError); 365 } 366 367 unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { 368 // Use LabelIDs.size() as the new ID if one hasn't been assigned. 369 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; 370 } 371 372 void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) 373 { 374 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 375 if (DestPtr->getType() != BP) 376 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); 377 378 // Get size and alignment info for this aggregate. 379 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 380 381 // FIXME: Handle variable sized types. 382 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth); 383 384 Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, 385 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), 386 // TypeInfo.first describes size in bits. 387 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), 388 llvm::ConstantInt::get(llvm::Type::Int32Ty, 389 TypeInfo.second/8)); 390 } 391 392 void CodeGenFunction::EmitIndirectSwitches() { 393 llvm::BasicBlock *Default; 394 395 if (IndirectSwitches.empty()) 396 return; 397 398 if (!LabelIDs.empty()) { 399 Default = getBasicBlockForLabel(LabelIDs.begin()->first); 400 } else { 401 // No possible targets for indirect goto, just emit an infinite 402 // loop. 403 Default = createBasicBlock("indirectgoto.loop", CurFn); 404 llvm::BranchInst::Create(Default, Default); 405 } 406 407 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(), 408 e = IndirectSwitches.end(); i != e; ++i) { 409 llvm::SwitchInst *I = *i; 410 411 I->setSuccessor(0, Default); 412 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(), 413 LE = LabelIDs.end(); LI != LE; ++LI) { 414 I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty, 415 LI->second), 416 getBasicBlockForLabel(LI->first)); 417 } 418 } 419 } 420 421 llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) 422 { 423 // FIXME: This entire method is hardcoded for 32-bit X86. 424 425 const char *TargetPrefix = getContext().Target.getTargetPrefix(); 426 427 if (strcmp(TargetPrefix, "x86") != 0 || 428 getContext().Target.getPointerWidth(0) != 32) 429 return 0; 430 431 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 432 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); 433 434 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 435 "ap"); 436 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 437 llvm::Value *AddrTyped = 438 Builder.CreateBitCast(Addr, 439 llvm::PointerType::getUnqual(ConvertType(Ty))); 440 441 uint64_t SizeInBytes = getContext().getTypeSize(Ty) / 8; 442 const unsigned ArgumentSizeInBytes = 4; 443 if (SizeInBytes < ArgumentSizeInBytes) 444 SizeInBytes = ArgumentSizeInBytes; 445 446 llvm::Value *NextAddr = 447 Builder.CreateGEP(Addr, 448 llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes), 449 "ap.next"); 450 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 451 452 return AddrTyped; 453 } 454 455 456 llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) 457 { 458 llvm::Value *&SizeEntry = VLASizeMap[VAT]; 459 460 assert(SizeEntry && "Did not emit size for type"); 461 return SizeEntry; 462 } 463 464 llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) 465 { 466 assert(Ty->isVariablyModifiedType() && 467 "Must pass variably modified type to EmitVLASizes!"); 468 469 if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { 470 llvm::Value *&SizeEntry = VLASizeMap[VAT]; 471 472 if (!SizeEntry) { 473 // Get the element size; 474 llvm::Value *ElemSize; 475 476 QualType ElemTy = VAT->getElementType(); 477 478 if (ElemTy->isVariableArrayType()) 479 ElemSize = EmitVLASize(ElemTy); 480 else { 481 // FIXME: We use Int32Ty here because the alloca instruction takes a 482 // 32-bit integer. What should we do about overflow? 483 ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty, 484 getContext().getTypeSize(ElemTy) / 8); 485 } 486 487 llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); 488 489 SizeEntry = Builder.CreateMul(ElemSize, NumElements); 490 } 491 492 return SizeEntry; 493 } else if (const PointerType *PT = Ty->getAsPointerType()) 494 EmitVLASize(PT->getPointeeType()); 495 else { 496 assert(0 && "unknown VM type!"); 497 } 498 499 return 0; 500 } 501 502 llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { 503 if (CGM.getContext().getBuiltinVaListType()->isArrayType()) { 504 return EmitScalarExpr(E); 505 } 506 return EmitLValue(E).getAddress(); 507 } 508