CodeGenFunction.cpp revision 0e88aa7100da32acc63bc8a4dcb946ed517868f1
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 "CGCXXABI.h" 17#include "CGDebugInfo.h" 18#include "CGException.h" 19#include "clang/Basic/TargetInfo.h" 20#include "clang/AST/APValue.h" 21#include "clang/AST/ASTContext.h" 22#include "clang/AST/Decl.h" 23#include "clang/AST/DeclCXX.h" 24#include "clang/AST/StmtCXX.h" 25#include "clang/Frontend/CodeGenOptions.h" 26#include "llvm/Target/TargetData.h" 27#include "llvm/Intrinsics.h" 28using namespace clang; 29using namespace CodeGen; 30 31static void ResolveAllBranchFixups(CodeGenFunction &CGF, 32 llvm::SwitchInst *Switch, 33 llvm::BasicBlock *CleanupEntry); 34 35CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 36 : BlockFunction(cgm, *this, Builder), CGM(cgm), 37 Target(CGM.getContext().Target), 38 Builder(cgm.getModule().getContext()), 39 NormalCleanupDest(0), EHCleanupDest(0), NextCleanupDestIndex(1), 40 ExceptionSlot(0), DebugInfo(0), IndirectBranch(0), 41 SwitchInsn(0), CaseRangeBlock(0), 42 DidCallStackSave(false), UnreachableBlock(0), 43 CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0), 44 ConditionalBranchLevel(0), TerminateLandingPad(0), TerminateHandler(0), 45 TrapBB(0) { 46 47 // Get some frequently used types. 48 LLVMPointerWidth = Target.getPointerWidth(0); 49 llvm::LLVMContext &LLVMContext = CGM.getLLVMContext(); 50 IntPtrTy = llvm::IntegerType::get(LLVMContext, LLVMPointerWidth); 51 Int32Ty = llvm::Type::getInt32Ty(LLVMContext); 52 Int64Ty = llvm::Type::getInt64Ty(LLVMContext); 53 54 Exceptions = getContext().getLangOptions().Exceptions; 55 CatchUndefined = getContext().getLangOptions().CatchUndefined; 56 CGM.getCXXABI().getMangleContext().startNewFunction(); 57} 58 59ASTContext &CodeGenFunction::getContext() const { 60 return CGM.getContext(); 61} 62 63 64const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { 65 return CGM.getTypes().ConvertTypeForMem(T); 66} 67 68const llvm::Type *CodeGenFunction::ConvertType(QualType T) { 69 return CGM.getTypes().ConvertType(T); 70} 71 72bool CodeGenFunction::hasAggregateLLVMType(QualType T) { 73 return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() || 74 T->isObjCObjectType(); 75} 76 77void CodeGenFunction::EmitReturnBlock() { 78 // For cleanliness, we try to avoid emitting the return block for 79 // simple cases. 80 llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 81 82 if (CurBB) { 83 assert(!CurBB->getTerminator() && "Unexpected terminated block."); 84 85 // We have a valid insert point, reuse it if it is empty or there are no 86 // explicit jumps to the return block. 87 if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) { 88 ReturnBlock.getBlock()->replaceAllUsesWith(CurBB); 89 delete ReturnBlock.getBlock(); 90 } else 91 EmitBlock(ReturnBlock.getBlock()); 92 return; 93 } 94 95 // Otherwise, if the return block is the target of a single direct 96 // branch then we can just put the code in that block instead. This 97 // cleans up functions which started with a unified return block. 98 if (ReturnBlock.getBlock()->hasOneUse()) { 99 llvm::BranchInst *BI = 100 dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin()); 101 if (BI && BI->isUnconditional() && 102 BI->getSuccessor(0) == ReturnBlock.getBlock()) { 103 // Reset insertion point and delete the branch. 104 Builder.SetInsertPoint(BI->getParent()); 105 BI->eraseFromParent(); 106 delete ReturnBlock.getBlock(); 107 return; 108 } 109 } 110 111 // FIXME: We are at an unreachable point, there is no reason to emit the block 112 // unless it has uses. However, we still need a place to put the debug 113 // region.end for now. 114 115 EmitBlock(ReturnBlock.getBlock()); 116} 117 118static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) { 119 if (!BB) return; 120 if (!BB->use_empty()) 121 return CGF.CurFn->getBasicBlockList().push_back(BB); 122 delete BB; 123} 124 125void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { 126 assert(BreakContinueStack.empty() && 127 "mismatched push/pop in break/continue 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->EmitFunctionEnd(Builder); 138 } 139 140 EmitFunctionEpilog(*CurFnInfo); 141 EmitEndEHSpec(CurCodeDecl); 142 143 assert(EHStack.empty() && 144 "did not remove all scopes from cleanup stack!"); 145 146 // If someone did an indirect goto, emit the indirect goto block at the end of 147 // the function. 148 if (IndirectBranch) { 149 EmitBlock(IndirectBranch->getParent()); 150 Builder.ClearInsertionPoint(); 151 } 152 153 // Remove the AllocaInsertPt instruction, which is just a convenience for us. 154 llvm::Instruction *Ptr = AllocaInsertPt; 155 AllocaInsertPt = 0; 156 Ptr->eraseFromParent(); 157 158 // If someone took the address of a label but never did an indirect goto, we 159 // made a zero entry PHI node, which is illegal, zap it now. 160 if (IndirectBranch) { 161 llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); 162 if (PN->getNumIncomingValues() == 0) { 163 PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); 164 PN->eraseFromParent(); 165 } 166 } 167 168 EmitIfUsed(*this, RethrowBlock.getBlock()); 169 EmitIfUsed(*this, TerminateLandingPad); 170 EmitIfUsed(*this, TerminateHandler); 171 EmitIfUsed(*this, UnreachableBlock); 172 173 if (CGM.getCodeGenOpts().EmitDeclMetadata) 174 EmitDeclMetadata(); 175} 176 177/// ShouldInstrumentFunction - Return true if the current function should be 178/// instrumented with __cyg_profile_func_* calls 179bool CodeGenFunction::ShouldInstrumentFunction() { 180 if (!CGM.getCodeGenOpts().InstrumentFunctions) 181 return false; 182 if (CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) 183 return false; 184 return true; 185} 186 187/// EmitFunctionInstrumentation - Emit LLVM code to call the specified 188/// instrumentation function with the current function and the call site, if 189/// function instrumentation is enabled. 190void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) { 191 if (!ShouldInstrumentFunction()) 192 return; 193 194 const llvm::PointerType *PointerTy; 195 const llvm::FunctionType *FunctionTy; 196 std::vector<const llvm::Type*> ProfileFuncArgs; 197 198 // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site); 199 PointerTy = llvm::Type::getInt8PtrTy(VMContext); 200 ProfileFuncArgs.push_back(PointerTy); 201 ProfileFuncArgs.push_back(PointerTy); 202 FunctionTy = llvm::FunctionType::get( 203 llvm::Type::getVoidTy(VMContext), 204 ProfileFuncArgs, false); 205 206 llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn); 207 llvm::CallInst *CallSite = Builder.CreateCall( 208 CGM.getIntrinsic(llvm::Intrinsic::returnaddress, 0, 0), 209 llvm::ConstantInt::get(Int32Ty, 0), 210 "callsite"); 211 212 Builder.CreateCall2(F, 213 llvm::ConstantExpr::getBitCast(CurFn, PointerTy), 214 CallSite); 215} 216 217void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, 218 llvm::Function *Fn, 219 const FunctionArgList &Args, 220 SourceLocation StartLoc) { 221 const Decl *D = GD.getDecl(); 222 223 DidCallStackSave = false; 224 CurCodeDecl = CurFuncDecl = D; 225 FnRetTy = RetTy; 226 CurFn = Fn; 227 assert(CurFn->isDeclaration() && "Function already has body?"); 228 229 // Pass inline keyword to optimizer if it appears explicitly on any 230 // declaration. 231 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) 232 for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(), 233 RE = FD->redecls_end(); RI != RE; ++RI) 234 if (RI->isInlineSpecified()) { 235 Fn->addFnAttr(llvm::Attribute::InlineHint); 236 break; 237 } 238 239 llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); 240 241 // Create a marker to make it easy to insert allocas into the entryblock 242 // later. Don't create this with the builder, because we don't want it 243 // folded. 244 llvm::Value *Undef = llvm::UndefValue::get(Int32Ty); 245 AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB); 246 if (Builder.isNamePreserving()) 247 AllocaInsertPt->setName("allocapt"); 248 249 ReturnBlock = getJumpDestInCurrentScope("return"); 250 251 Builder.SetInsertPoint(EntryBB); 252 253 // Emit subprogram debug descriptor. 254 if (CGDebugInfo *DI = getDebugInfo()) { 255 // FIXME: what is going on here and why does it ignore all these 256 // interesting type properties? 257 QualType FnType = 258 getContext().getFunctionType(RetTy, 0, 0, 259 FunctionProtoType::ExtProtoInfo()); 260 261 DI->setLocation(StartLoc); 262 DI->EmitFunctionStart(GD, FnType, CurFn, Builder); 263 } 264 265 EmitFunctionInstrumentation("__cyg_profile_func_enter"); 266 267 // FIXME: Leaked. 268 // CC info is ignored, hopefully? 269 CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args, 270 FunctionType::ExtInfo()); 271 272 if (RetTy->isVoidType()) { 273 // Void type; nothing to return. 274 ReturnValue = 0; 275 } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && 276 hasAggregateLLVMType(CurFnInfo->getReturnType())) { 277 // Indirect aggregate return; emit returned value directly into sret slot. 278 // This reduces code size, and affects correctness in C++. 279 ReturnValue = CurFn->arg_begin(); 280 } else { 281 ReturnValue = CreateIRTemp(RetTy, "retval"); 282 } 283 284 EmitStartEHSpec(CurCodeDecl); 285 EmitFunctionProlog(*CurFnInfo, CurFn, Args); 286 287 if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) 288 CGM.getCXXABI().EmitInstanceFunctionProlog(*this); 289 290 // If any of the arguments have a variably modified type, make sure to 291 // emit the type size. 292 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 293 i != e; ++i) { 294 QualType Ty = i->second; 295 296 if (Ty->isVariablyModifiedType()) 297 EmitVLASize(Ty); 298 } 299} 300 301void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) { 302 const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl()); 303 assert(FD->getBody()); 304 EmitStmt(FD->getBody()); 305} 306 307/// Tries to mark the given function nounwind based on the 308/// non-existence of any throwing calls within it. We believe this is 309/// lightweight enough to do at -O0. 310static void TryMarkNoThrow(llvm::Function *F) { 311 // LLVM treats 'nounwind' on a function as part of the type, so we 312 // can't do this on functions that can be overwritten. 313 if (F->mayBeOverridden()) return; 314 315 for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) 316 for (llvm::BasicBlock::iterator 317 BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) 318 if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI)) 319 if (!Call->doesNotThrow()) 320 return; 321 F->setDoesNotThrow(true); 322} 323 324void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) { 325 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 326 327 // Check if we should generate debug info for this function. 328 if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>()) 329 DebugInfo = CGM.getDebugInfo(); 330 331 FunctionArgList Args; 332 QualType ResTy = FD->getResultType(); 333 334 CurGD = GD; 335 if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance()) 336 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args); 337 338 if (FD->getNumParams()) { 339 const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>(); 340 assert(FProto && "Function def must have prototype!"); 341 342 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) 343 Args.push_back(std::make_pair(FD->getParamDecl(i), 344 FProto->getArgType(i))); 345 } 346 347 SourceRange BodyRange; 348 if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange(); 349 350 // Emit the standard function prologue. 351 StartFunction(GD, ResTy, Fn, Args, BodyRange.getBegin()); 352 353 // Generate the body of the function. 354 if (isa<CXXDestructorDecl>(FD)) 355 EmitDestructorBody(Args); 356 else if (isa<CXXConstructorDecl>(FD)) 357 EmitConstructorBody(Args); 358 else 359 EmitFunctionBody(Args); 360 361 // Emit the standard function epilogue. 362 FinishFunction(BodyRange.getEnd()); 363 364 // If we haven't marked the function nothrow through other means, do 365 // a quick pass now to see if we can. 366 if (!CurFn->doesNotThrow()) 367 TryMarkNoThrow(CurFn); 368} 369 370/// ContainsLabel - Return true if the statement contains a label in it. If 371/// this statement is not executed normally, it not containing a label means 372/// that we can just remove the code. 373bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { 374 // Null statement, not a label! 375 if (S == 0) return false; 376 377 // If this is a label, we have to emit the code, consider something like: 378 // if (0) { ... foo: bar(); } goto foo; 379 if (isa<LabelStmt>(S)) 380 return true; 381 382 // If this is a case/default statement, and we haven't seen a switch, we have 383 // to emit the code. 384 if (isa<SwitchCase>(S) && !IgnoreCaseStmts) 385 return true; 386 387 // If this is a switch statement, we want to ignore cases below it. 388 if (isa<SwitchStmt>(S)) 389 IgnoreCaseStmts = true; 390 391 // Scan subexpressions for verboten labels. 392 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 393 I != E; ++I) 394 if (ContainsLabel(*I, IgnoreCaseStmts)) 395 return true; 396 397 return false; 398} 399 400 401/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to 402/// a constant, or if it does but contains a label, return 0. If it constant 403/// folds to 'true' and does not contain a label, return 1, if it constant folds 404/// to 'false' and does not contain a label, return -1. 405int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { 406 // FIXME: Rename and handle conversion of other evaluatable things 407 // to bool. 408 Expr::EvalResult Result; 409 if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || 410 Result.HasSideEffects) 411 return 0; // Not foldable, not integer or not fully evaluatable. 412 413 if (CodeGenFunction::ContainsLabel(Cond)) 414 return 0; // Contains a label. 415 416 return Result.Val.getInt().getBoolValue() ? 1 : -1; 417} 418 419 420/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if 421/// statement) to the specified blocks. Based on the condition, this might try 422/// to simplify the codegen of the conditional based on the branch. 423/// 424void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, 425 llvm::BasicBlock *TrueBlock, 426 llvm::BasicBlock *FalseBlock) { 427 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) 428 return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); 429 430 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { 431 // Handle X && Y in a condition. 432 if (CondBOp->getOpcode() == BO_LAnd) { 433 // If we have "1 && X", simplify the code. "0 && X" would have constant 434 // folded if the case was simple enough. 435 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { 436 // br(1 && X) -> br(X). 437 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 438 } 439 440 // If we have "X && 1", simplify the code to use an uncond branch. 441 // "X && 0" would have been constant folded to 0. 442 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { 443 // br(X && 1) -> br(X). 444 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 445 } 446 447 // Emit the LHS as a conditional. If the LHS conditional is false, we 448 // want to jump to the FalseBlock. 449 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); 450 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); 451 EmitBlock(LHSTrue); 452 453 // Any temporaries created here are conditional. 454 BeginConditionalBranch(); 455 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 456 EndConditionalBranch(); 457 458 return; 459 } else if (CondBOp->getOpcode() == BO_LOr) { 460 // If we have "0 || X", simplify the code. "1 || X" would have constant 461 // folded if the case was simple enough. 462 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { 463 // br(0 || X) -> br(X). 464 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 465 } 466 467 // If we have "X || 0", simplify the code to use an uncond branch. 468 // "X || 1" would have been constant folded to 1. 469 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { 470 // br(X || 0) -> br(X). 471 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 472 } 473 474 // Emit the LHS as a conditional. If the LHS conditional is true, we 475 // want to jump to the TrueBlock. 476 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); 477 EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); 478 EmitBlock(LHSFalse); 479 480 // Any temporaries created here are conditional. 481 BeginConditionalBranch(); 482 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 483 EndConditionalBranch(); 484 485 return; 486 } 487 } 488 489 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { 490 // br(!x, t, f) -> br(x, f, t) 491 if (CondUOp->getOpcode() == UO_LNot) 492 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); 493 } 494 495 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { 496 // Handle ?: operator. 497 498 // Just ignore GNU ?: extension. 499 if (CondOp->getLHS()) { 500 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) 501 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); 502 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); 503 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); 504 EmitBlock(LHSBlock); 505 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); 506 EmitBlock(RHSBlock); 507 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); 508 return; 509 } 510 } 511 512 // Emit the code with the fully general case. 513 llvm::Value *CondV = EvaluateExprAsBool(Cond); 514 Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); 515} 516 517/// ErrorUnsupported - Print out an error that codegen doesn't support the 518/// specified stmt yet. 519void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, 520 bool OmitOnError) { 521 CGM.ErrorUnsupported(S, Type, OmitOnError); 522} 523 524void 525CodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) { 526 // Ignore empty classes in C++. 527 if (getContext().getLangOptions().CPlusPlus) { 528 if (const RecordType *RT = Ty->getAs<RecordType>()) { 529 if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty()) 530 return; 531 } 532 } 533 534 // Cast the dest ptr to the appropriate i8 pointer type. 535 unsigned DestAS = 536 cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace(); 537 const llvm::Type *BP = 538 llvm::Type::getInt8PtrTy(VMContext, DestAS); 539 if (DestPtr->getType() != BP) 540 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); 541 542 // Get size and alignment info for this aggregate. 543 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 544 uint64_t Size = TypeInfo.first; 545 unsigned Align = TypeInfo.second; 546 547 // Don't bother emitting a zero-byte memset. 548 if (Size == 0) 549 return; 550 551 llvm::ConstantInt *SizeVal = llvm::ConstantInt::get(IntPtrTy, Size / 8); 552 llvm::ConstantInt *AlignVal = Builder.getInt32(Align / 8); 553 554 // If the type contains a pointer to data member we can't memset it to zero. 555 // Instead, create a null constant and copy it to the destination. 556 if (!CGM.getTypes().isZeroInitializable(Ty)) { 557 llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty); 558 559 llvm::GlobalVariable *NullVariable = 560 new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(), 561 /*isConstant=*/true, 562 llvm::GlobalVariable::PrivateLinkage, 563 NullConstant, llvm::Twine()); 564 llvm::Value *SrcPtr = 565 Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy()); 566 567 // FIXME: variable-size types? 568 569 // Get and call the appropriate llvm.memcpy overload. 570 llvm::Constant *Memcpy = 571 CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(), IntPtrTy); 572 Builder.CreateCall5(Memcpy, DestPtr, SrcPtr, SizeVal, AlignVal, 573 /*volatile*/ Builder.getFalse()); 574 return; 575 } 576 577 // Otherwise, just memset the whole thing to zero. This is legal 578 // because in LLVM, all default initializers (other than the ones we just 579 // handled above) are guaranteed to have a bit pattern of all zeros. 580 581 // FIXME: Handle variable sized types. 582 Builder.CreateCall5(CGM.getMemSetFn(BP, IntPtrTy), DestPtr, 583 Builder.getInt8(0), 584 SizeVal, AlignVal, /*volatile*/ Builder.getFalse()); 585} 586 587llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) { 588 // Make sure that there is a block for the indirect goto. 589 if (IndirectBranch == 0) 590 GetIndirectGotoBlock(); 591 592 llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock(); 593 594 // Make sure the indirect branch includes all of the address-taken blocks. 595 IndirectBranch->addDestination(BB); 596 return llvm::BlockAddress::get(CurFn, BB); 597} 598 599llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { 600 // If we already made the indirect branch for indirect goto, return its block. 601 if (IndirectBranch) return IndirectBranch->getParent(); 602 603 CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); 604 605 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); 606 607 // Create the PHI node that indirect gotos will add entries to. 608 llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest"); 609 610 // Create the indirect branch instruction. 611 IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); 612 return IndirectBranch->getParent(); 613} 614 615llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) { 616 llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; 617 618 assert(SizeEntry && "Did not emit size for type"); 619 return SizeEntry; 620} 621 622llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { 623 assert(Ty->isVariablyModifiedType() && 624 "Must pass variably modified type to EmitVLASizes!"); 625 626 EnsureInsertPoint(); 627 628 if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { 629 // unknown size indication requires no size computation. 630 if (!VAT->getSizeExpr()) 631 return 0; 632 llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; 633 634 if (!SizeEntry) { 635 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 636 637 // Get the element size; 638 QualType ElemTy = VAT->getElementType(); 639 llvm::Value *ElemSize; 640 if (ElemTy->isVariableArrayType()) 641 ElemSize = EmitVLASize(ElemTy); 642 else 643 ElemSize = llvm::ConstantInt::get(SizeTy, 644 getContext().getTypeSizeInChars(ElemTy).getQuantity()); 645 646 llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); 647 NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); 648 649 SizeEntry = Builder.CreateMul(ElemSize, NumElements); 650 } 651 652 return SizeEntry; 653 } 654 655 if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { 656 EmitVLASize(AT->getElementType()); 657 return 0; 658 } 659 660 if (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 661 EmitVLASize(PT->getInnerType()); 662 return 0; 663 } 664 665 const PointerType *PT = Ty->getAs<PointerType>(); 666 assert(PT && "unknown VM type!"); 667 EmitVLASize(PT->getPointeeType()); 668 return 0; 669} 670 671llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { 672 if (getContext().getBuiltinVaListType()->isArrayType()) 673 return EmitScalarExpr(E); 674 return EmitLValue(E).getAddress(); 675} 676 677/// Pops cleanup blocks until the given savepoint is reached. 678void CodeGenFunction::PopCleanupBlocks(EHScopeStack::stable_iterator Old) { 679 assert(Old.isValid()); 680 681 while (EHStack.stable_begin() != Old) { 682 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin()); 683 684 // As long as Old strictly encloses the scope's enclosing normal 685 // cleanup, we're going to emit another normal cleanup which 686 // fallthrough can propagate through. 687 bool FallThroughIsBranchThrough = 688 Old.strictlyEncloses(Scope.getEnclosingNormalCleanup()); 689 690 PopCleanupBlock(FallThroughIsBranchThrough); 691 } 692} 693 694static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF, 695 EHCleanupScope &Scope) { 696 assert(Scope.isNormalCleanup()); 697 llvm::BasicBlock *Entry = Scope.getNormalBlock(); 698 if (!Entry) { 699 Entry = CGF.createBasicBlock("cleanup"); 700 Scope.setNormalBlock(Entry); 701 } 702 return Entry; 703} 704 705static llvm::BasicBlock *CreateEHEntry(CodeGenFunction &CGF, 706 EHCleanupScope &Scope) { 707 assert(Scope.isEHCleanup()); 708 llvm::BasicBlock *Entry = Scope.getEHBlock(); 709 if (!Entry) { 710 Entry = CGF.createBasicBlock("eh.cleanup"); 711 Scope.setEHBlock(Entry); 712 } 713 return Entry; 714} 715 716/// Transitions the terminator of the given exit-block of a cleanup to 717/// be a cleanup switch. 718static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF, 719 llvm::BasicBlock *Block) { 720 // If it's a branch, turn it into a switch whose default 721 // destination is its original target. 722 llvm::TerminatorInst *Term = Block->getTerminator(); 723 assert(Term && "can't transition block without terminator"); 724 725 if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) { 726 assert(Br->isUnconditional()); 727 llvm::LoadInst *Load = 728 new llvm::LoadInst(CGF.getNormalCleanupDestSlot(), "cleanup.dest", Term); 729 llvm::SwitchInst *Switch = 730 llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block); 731 Br->eraseFromParent(); 732 return Switch; 733 } else { 734 return cast<llvm::SwitchInst>(Term); 735 } 736} 737 738/// Attempts to reduce a cleanup's entry block to a fallthrough. This 739/// is basically llvm::MergeBlockIntoPredecessor, except 740/// simplified/optimized for the tighter constraints on cleanup blocks. 741/// 742/// Returns the new block, whatever it is. 743static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF, 744 llvm::BasicBlock *Entry) { 745 llvm::BasicBlock *Pred = Entry->getSinglePredecessor(); 746 if (!Pred) return Entry; 747 748 llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator()); 749 if (!Br || Br->isConditional()) return Entry; 750 assert(Br->getSuccessor(0) == Entry); 751 752 // If we were previously inserting at the end of the cleanup entry 753 // block, we'll need to continue inserting at the end of the 754 // predecessor. 755 bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry; 756 assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end()); 757 758 // Kill the branch. 759 Br->eraseFromParent(); 760 761 // Merge the blocks. 762 Pred->getInstList().splice(Pred->end(), Entry->getInstList()); 763 764 // Kill the entry block. 765 Entry->eraseFromParent(); 766 767 if (WasInsertBlock) 768 CGF.Builder.SetInsertPoint(Pred); 769 770 return Pred; 771} 772 773static void EmitCleanup(CodeGenFunction &CGF, 774 EHScopeStack::Cleanup *Fn, 775 bool ForEH, 776 llvm::Value *ActiveFlag) { 777 // EH cleanups always occur within a terminate scope. 778 if (ForEH) CGF.EHStack.pushTerminate(); 779 780 // If there's an active flag, load it and skip the cleanup if it's 781 // false. 782 llvm::BasicBlock *ContBB = 0; 783 if (ActiveFlag) { 784 ContBB = CGF.createBasicBlock("cleanup.done"); 785 llvm::BasicBlock *CleanupBB = CGF.createBasicBlock("cleanup.action"); 786 llvm::Value *IsActive 787 = CGF.Builder.CreateLoad(ActiveFlag, "cleanup.is_active"); 788 CGF.Builder.CreateCondBr(IsActive, CleanupBB, ContBB); 789 CGF.EmitBlock(CleanupBB); 790 } 791 792 // Ask the cleanup to emit itself. 793 Fn->Emit(CGF, ForEH); 794 assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?"); 795 796 // Emit the continuation block if there was an active flag. 797 if (ActiveFlag) 798 CGF.EmitBlock(ContBB); 799 800 // Leave the terminate scope. 801 if (ForEH) CGF.EHStack.popTerminate(); 802} 803 804static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit, 805 llvm::BasicBlock *From, 806 llvm::BasicBlock *To) { 807 // Exit is the exit block of a cleanup, so it always terminates in 808 // an unconditional branch or a switch. 809 llvm::TerminatorInst *Term = Exit->getTerminator(); 810 811 if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) { 812 assert(Br->isUnconditional() && Br->getSuccessor(0) == From); 813 Br->setSuccessor(0, To); 814 } else { 815 llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term); 816 for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I) 817 if (Switch->getSuccessor(I) == From) 818 Switch->setSuccessor(I, To); 819 } 820} 821 822/// Pops a cleanup block. If the block includes a normal cleanup, the 823/// current insertion point is threaded through the cleanup, as are 824/// any branch fixups on the cleanup. 825void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) { 826 assert(!EHStack.empty() && "cleanup stack is empty!"); 827 assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!"); 828 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin()); 829 assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups()); 830 831 // Remember activation information. 832 bool IsActive = Scope.isActive(); 833 llvm::Value *NormalActiveFlag = 834 Scope.shouldTestFlagInNormalCleanup() ? Scope.getActiveFlag() : 0; 835 llvm::Value *EHActiveFlag = 836 Scope.shouldTestFlagInEHCleanup() ? Scope.getActiveFlag() : 0; 837 838 // Check whether we need an EH cleanup. This is only true if we've 839 // generated a lazy EH cleanup block. 840 bool RequiresEHCleanup = Scope.hasEHBranches(); 841 842 // Check the three conditions which might require a normal cleanup: 843 844 // - whether there are branch fix-ups through this cleanup 845 unsigned FixupDepth = Scope.getFixupDepth(); 846 bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth; 847 848 // - whether there are branch-throughs or branch-afters 849 bool HasExistingBranches = Scope.hasBranches(); 850 851 // - whether there's a fallthrough 852 llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock(); 853 bool HasFallthrough = (FallthroughSource != 0 && IsActive); 854 855 // Branch-through fall-throughs leave the insertion point set to the 856 // end of the last cleanup, which points to the current scope. The 857 // rest of IR gen doesn't need to worry about this; it only happens 858 // during the execution of PopCleanupBlocks(). 859 bool HasPrebranchedFallthrough = 860 (FallthroughSource && FallthroughSource->getTerminator()); 861 862 // If this is a normal cleanup, then having a prebranched 863 // fallthrough implies that the fallthrough source unconditionally 864 // jumps here. 865 assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough || 866 (Scope.getNormalBlock() && 867 FallthroughSource->getTerminator()->getSuccessor(0) 868 == Scope.getNormalBlock())); 869 870 bool RequiresNormalCleanup = false; 871 if (Scope.isNormalCleanup() && 872 (HasFixups || HasExistingBranches || HasFallthrough)) { 873 RequiresNormalCleanup = true; 874 } 875 876 // Even if we don't need the normal cleanup, we might still have 877 // prebranched fallthrough to worry about. 878 if (Scope.isNormalCleanup() && !RequiresNormalCleanup && 879 HasPrebranchedFallthrough) { 880 assert(!IsActive); 881 882 llvm::BasicBlock *NormalEntry = Scope.getNormalBlock(); 883 884 // If we're branching through this cleanup, just forward the 885 // prebranched fallthrough to the next cleanup, leaving the insert 886 // point in the old block. 887 if (FallthroughIsBranchThrough) { 888 EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup()); 889 llvm::BasicBlock *EnclosingEntry = 890 CreateNormalEntry(*this, cast<EHCleanupScope>(S)); 891 892 ForwardPrebranchedFallthrough(FallthroughSource, 893 NormalEntry, EnclosingEntry); 894 assert(NormalEntry->use_empty() && 895 "uses of entry remain after forwarding?"); 896 delete NormalEntry; 897 898 // Otherwise, we're branching out; just emit the next block. 899 } else { 900 EmitBlock(NormalEntry); 901 SimplifyCleanupEntry(*this, NormalEntry); 902 } 903 } 904 905 // If we don't need the cleanup at all, we're done. 906 if (!RequiresNormalCleanup && !RequiresEHCleanup) { 907 EHStack.popCleanup(); // safe because there are no fixups 908 assert(EHStack.getNumBranchFixups() == 0 || 909 EHStack.hasNormalCleanups()); 910 return; 911 } 912 913 // Copy the cleanup emission data out. Note that SmallVector 914 // guarantees maximal alignment for its buffer regardless of its 915 // type parameter. 916 llvm::SmallVector<char, 8*sizeof(void*)> CleanupBuffer; 917 CleanupBuffer.reserve(Scope.getCleanupSize()); 918 memcpy(CleanupBuffer.data(), 919 Scope.getCleanupBuffer(), Scope.getCleanupSize()); 920 CleanupBuffer.set_size(Scope.getCleanupSize()); 921 EHScopeStack::Cleanup *Fn = 922 reinterpret_cast<EHScopeStack::Cleanup*>(CleanupBuffer.data()); 923 924 // We want to emit the EH cleanup after the normal cleanup, but go 925 // ahead and do the setup for the EH cleanup while the scope is still 926 // alive. 927 llvm::BasicBlock *EHEntry = 0; 928 llvm::SmallVector<llvm::Instruction*, 2> EHInstsToAppend; 929 if (RequiresEHCleanup) { 930 EHEntry = CreateEHEntry(*this, Scope); 931 932 // Figure out the branch-through dest if necessary. 933 llvm::BasicBlock *EHBranchThroughDest = 0; 934 if (Scope.hasEHBranchThroughs()) { 935 assert(Scope.getEnclosingEHCleanup() != EHStack.stable_end()); 936 EHScope &S = *EHStack.find(Scope.getEnclosingEHCleanup()); 937 EHBranchThroughDest = CreateEHEntry(*this, cast<EHCleanupScope>(S)); 938 } 939 940 // If we have exactly one branch-after and no branch-throughs, we 941 // can dispatch it without a switch. 942 if (!Scope.hasEHBranchThroughs() && 943 Scope.getNumEHBranchAfters() == 1) { 944 assert(!EHBranchThroughDest); 945 946 // TODO: remove the spurious eh.cleanup.dest stores if this edge 947 // never went through any switches. 948 llvm::BasicBlock *BranchAfterDest = Scope.getEHBranchAfterBlock(0); 949 EHInstsToAppend.push_back(llvm::BranchInst::Create(BranchAfterDest)); 950 951 // Otherwise, if we have any branch-afters, we need a switch. 952 } else if (Scope.getNumEHBranchAfters()) { 953 // The default of the switch belongs to the branch-throughs if 954 // they exist. 955 llvm::BasicBlock *Default = 956 (EHBranchThroughDest ? EHBranchThroughDest : getUnreachableBlock()); 957 958 const unsigned SwitchCapacity = Scope.getNumEHBranchAfters(); 959 960 llvm::LoadInst *Load = 961 new llvm::LoadInst(getEHCleanupDestSlot(), "cleanup.dest"); 962 llvm::SwitchInst *Switch = 963 llvm::SwitchInst::Create(Load, Default, SwitchCapacity); 964 965 EHInstsToAppend.push_back(Load); 966 EHInstsToAppend.push_back(Switch); 967 968 for (unsigned I = 0, E = Scope.getNumEHBranchAfters(); I != E; ++I) 969 Switch->addCase(Scope.getEHBranchAfterIndex(I), 970 Scope.getEHBranchAfterBlock(I)); 971 972 // Otherwise, we have only branch-throughs; jump to the next EH 973 // cleanup. 974 } else { 975 assert(EHBranchThroughDest); 976 EHInstsToAppend.push_back(llvm::BranchInst::Create(EHBranchThroughDest)); 977 } 978 } 979 980 if (!RequiresNormalCleanup) { 981 EHStack.popCleanup(); 982 } else { 983 // If we have a fallthrough and no other need for the cleanup, 984 // emit it directly. 985 if (HasFallthrough && !HasPrebranchedFallthrough && 986 !HasFixups && !HasExistingBranches) { 987 988 // Fixups can cause us to optimistically create a normal block, 989 // only to later have no real uses for it. Just delete it in 990 // this case. 991 // TODO: we can potentially simplify all the uses after this. 992 if (Scope.getNormalBlock()) { 993 Scope.getNormalBlock()->replaceAllUsesWith(getUnreachableBlock()); 994 delete Scope.getNormalBlock(); 995 } 996 997 EHStack.popCleanup(); 998 999 EmitCleanup(*this, Fn, /*ForEH*/ false, NormalActiveFlag); 1000 1001 // Otherwise, the best approach is to thread everything through 1002 // the cleanup block and then try to clean up after ourselves. 1003 } else { 1004 // Force the entry block to exist. 1005 llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope); 1006 1007 // I. Set up the fallthrough edge in. 1008 1009 // If there's a fallthrough, we need to store the cleanup 1010 // destination index. For fall-throughs this is always zero. 1011 if (HasFallthrough) { 1012 if (!HasPrebranchedFallthrough) 1013 Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot()); 1014 1015 // Otherwise, clear the IP if we don't have fallthrough because 1016 // the cleanup is inactive. We don't need to save it because 1017 // it's still just FallthroughSource. 1018 } else if (FallthroughSource) { 1019 assert(!IsActive && "source without fallthrough for active cleanup"); 1020 Builder.ClearInsertionPoint(); 1021 } 1022 1023 // II. Emit the entry block. This implicitly branches to it if 1024 // we have fallthrough. All the fixups and existing branches 1025 // should already be branched to it. 1026 EmitBlock(NormalEntry); 1027 1028 // III. Figure out where we're going and build the cleanup 1029 // epilogue. 1030 1031 bool HasEnclosingCleanups = 1032 (Scope.getEnclosingNormalCleanup() != EHStack.stable_end()); 1033 1034 // Compute the branch-through dest if we need it: 1035 // - if there are branch-throughs threaded through the scope 1036 // - if fall-through is a branch-through 1037 // - if there are fixups that will be optimistically forwarded 1038 // to the enclosing cleanup 1039 llvm::BasicBlock *BranchThroughDest = 0; 1040 if (Scope.hasBranchThroughs() || 1041 (FallthroughSource && FallthroughIsBranchThrough) || 1042 (HasFixups && HasEnclosingCleanups)) { 1043 assert(HasEnclosingCleanups); 1044 EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup()); 1045 BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S)); 1046 } 1047 1048 llvm::BasicBlock *FallthroughDest = 0; 1049 llvm::SmallVector<llvm::Instruction*, 2> InstsToAppend; 1050 1051 // If there's exactly one branch-after and no other threads, 1052 // we can route it without a switch. 1053 if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough && 1054 Scope.getNumBranchAfters() == 1) { 1055 assert(!BranchThroughDest || !IsActive); 1056 1057 // TODO: clean up the possibly dead stores to the cleanup dest slot. 1058 llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0); 1059 InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter)); 1060 1061 // Build a switch-out if we need it: 1062 // - if there are branch-afters threaded through the scope 1063 // - if fall-through is a branch-after 1064 // - if there are fixups that have nowhere left to go and 1065 // so must be immediately resolved 1066 } else if (Scope.getNumBranchAfters() || 1067 (HasFallthrough && !FallthroughIsBranchThrough) || 1068 (HasFixups && !HasEnclosingCleanups)) { 1069 1070 llvm::BasicBlock *Default = 1071 (BranchThroughDest ? BranchThroughDest : getUnreachableBlock()); 1072 1073 // TODO: base this on the number of branch-afters and fixups 1074 const unsigned SwitchCapacity = 10; 1075 1076 llvm::LoadInst *Load = 1077 new llvm::LoadInst(getNormalCleanupDestSlot(), "cleanup.dest"); 1078 llvm::SwitchInst *Switch = 1079 llvm::SwitchInst::Create(Load, Default, SwitchCapacity); 1080 1081 InstsToAppend.push_back(Load); 1082 InstsToAppend.push_back(Switch); 1083 1084 // Branch-after fallthrough. 1085 if (FallthroughSource && !FallthroughIsBranchThrough) { 1086 FallthroughDest = createBasicBlock("cleanup.cont"); 1087 if (HasFallthrough) 1088 Switch->addCase(Builder.getInt32(0), FallthroughDest); 1089 } 1090 1091 for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) { 1092 Switch->addCase(Scope.getBranchAfterIndex(I), 1093 Scope.getBranchAfterBlock(I)); 1094 } 1095 1096 // If there aren't any enclosing cleanups, we can resolve all 1097 // the fixups now. 1098 if (HasFixups && !HasEnclosingCleanups) 1099 ResolveAllBranchFixups(*this, Switch, NormalEntry); 1100 } else { 1101 // We should always have a branch-through destination in this case. 1102 assert(BranchThroughDest); 1103 InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest)); 1104 } 1105 1106 // IV. Pop the cleanup and emit it. 1107 EHStack.popCleanup(); 1108 assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups); 1109 1110 EmitCleanup(*this, Fn, /*ForEH*/ false, NormalActiveFlag); 1111 1112 // Append the prepared cleanup prologue from above. 1113 llvm::BasicBlock *NormalExit = Builder.GetInsertBlock(); 1114 for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I) 1115 NormalExit->getInstList().push_back(InstsToAppend[I]); 1116 1117 // Optimistically hope that any fixups will continue falling through. 1118 for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups(); 1119 I < E; ++I) { 1120 BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I); 1121 if (!Fixup.Destination) continue; 1122 if (!Fixup.OptimisticBranchBlock) { 1123 new llvm::StoreInst(Builder.getInt32(Fixup.DestinationIndex), 1124 getNormalCleanupDestSlot(), 1125 Fixup.InitialBranch); 1126 Fixup.InitialBranch->setSuccessor(0, NormalEntry); 1127 } 1128 Fixup.OptimisticBranchBlock = NormalExit; 1129 } 1130 1131 // V. Set up the fallthrough edge out. 1132 1133 // Case 1: a fallthrough source exists but shouldn't branch to 1134 // the cleanup because the cleanup is inactive. 1135 if (!HasFallthrough && FallthroughSource) { 1136 assert(!IsActive); 1137 1138 // If we have a prebranched fallthrough, that needs to be 1139 // forwarded to the right block. 1140 if (HasPrebranchedFallthrough) { 1141 llvm::BasicBlock *Next; 1142 if (FallthroughIsBranchThrough) { 1143 Next = BranchThroughDest; 1144 assert(!FallthroughDest); 1145 } else { 1146 Next = FallthroughDest; 1147 } 1148 1149 ForwardPrebranchedFallthrough(FallthroughSource, NormalEntry, Next); 1150 } 1151 Builder.SetInsertPoint(FallthroughSource); 1152 1153 // Case 2: a fallthrough source exists and should branch to the 1154 // cleanup, but we're not supposed to branch through to the next 1155 // cleanup. 1156 } else if (HasFallthrough && FallthroughDest) { 1157 assert(!FallthroughIsBranchThrough); 1158 EmitBlock(FallthroughDest); 1159 1160 // Case 3: a fallthrough source exists and should branch to the 1161 // cleanup and then through to the next. 1162 } else if (HasFallthrough) { 1163 // Everything is already set up for this. 1164 1165 // Case 4: no fallthrough source exists. 1166 } else { 1167 Builder.ClearInsertionPoint(); 1168 } 1169 1170 // VI. Assorted cleaning. 1171 1172 // Check whether we can merge NormalEntry into a single predecessor. 1173 // This might invalidate (non-IR) pointers to NormalEntry. 1174 llvm::BasicBlock *NewNormalEntry = 1175 SimplifyCleanupEntry(*this, NormalEntry); 1176 1177 // If it did invalidate those pointers, and NormalEntry was the same 1178 // as NormalExit, go back and patch up the fixups. 1179 if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit) 1180 for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups(); 1181 I < E; ++I) 1182 CGF.EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry; 1183 } 1184 } 1185 1186 assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0); 1187 1188 // Emit the EH cleanup if required. 1189 if (RequiresEHCleanup) { 1190 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 1191 1192 EmitBlock(EHEntry); 1193 EmitCleanup(*this, Fn, /*ForEH*/ true, EHActiveFlag); 1194 1195 // Append the prepared cleanup prologue from above. 1196 llvm::BasicBlock *EHExit = Builder.GetInsertBlock(); 1197 for (unsigned I = 0, E = EHInstsToAppend.size(); I != E; ++I) 1198 EHExit->getInstList().push_back(EHInstsToAppend[I]); 1199 1200 Builder.restoreIP(SavedIP); 1201 1202 SimplifyCleanupEntry(*this, EHEntry); 1203 } 1204} 1205 1206/// Terminate the current block by emitting a branch which might leave 1207/// the current cleanup-protected scope. The target scope may not yet 1208/// be known, in which case this will require a fixup. 1209/// 1210/// As a side-effect, this method clears the insertion point. 1211void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) { 1212 assert(Dest.getScopeDepth().encloses(EHStack.getInnermostNormalCleanup()) 1213 && "stale jump destination"); 1214 1215 if (!HaveInsertPoint()) 1216 return; 1217 1218 // Create the branch. 1219 llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock()); 1220 1221 // Calculate the innermost active normal cleanup. 1222 EHScopeStack::stable_iterator 1223 TopCleanup = EHStack.getInnermostActiveNormalCleanup(); 1224 1225 // If we're not in an active normal cleanup scope, or if the 1226 // destination scope is within the innermost active normal cleanup 1227 // scope, we don't need to worry about fixups. 1228 if (TopCleanup == EHStack.stable_end() || 1229 TopCleanup.encloses(Dest.getScopeDepth())) { // works for invalid 1230 Builder.ClearInsertionPoint(); 1231 return; 1232 } 1233 1234 // If we can't resolve the destination cleanup scope, just add this 1235 // to the current cleanup scope as a branch fixup. 1236 if (!Dest.getScopeDepth().isValid()) { 1237 BranchFixup &Fixup = EHStack.addBranchFixup(); 1238 Fixup.Destination = Dest.getBlock(); 1239 Fixup.DestinationIndex = Dest.getDestIndex(); 1240 Fixup.InitialBranch = BI; 1241 Fixup.OptimisticBranchBlock = 0; 1242 1243 Builder.ClearInsertionPoint(); 1244 return; 1245 } 1246 1247 // Otherwise, thread through all the normal cleanups in scope. 1248 1249 // Store the index at the start. 1250 llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex()); 1251 new llvm::StoreInst(Index, getNormalCleanupDestSlot(), BI); 1252 1253 // Adjust BI to point to the first cleanup block. 1254 { 1255 EHCleanupScope &Scope = 1256 cast<EHCleanupScope>(*EHStack.find(TopCleanup)); 1257 BI->setSuccessor(0, CreateNormalEntry(*this, Scope)); 1258 } 1259 1260 // Add this destination to all the scopes involved. 1261 EHScopeStack::stable_iterator I = TopCleanup; 1262 EHScopeStack::stable_iterator E = Dest.getScopeDepth(); 1263 if (E.strictlyEncloses(I)) { 1264 while (true) { 1265 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I)); 1266 assert(Scope.isNormalCleanup()); 1267 I = Scope.getEnclosingNormalCleanup(); 1268 1269 // If this is the last cleanup we're propagating through, tell it 1270 // that there's a resolved jump moving through it. 1271 if (!E.strictlyEncloses(I)) { 1272 Scope.addBranchAfter(Index, Dest.getBlock()); 1273 break; 1274 } 1275 1276 // Otherwise, tell the scope that there's a jump propoagating 1277 // through it. If this isn't new information, all the rest of 1278 // the work has been done before. 1279 if (!Scope.addBranchThrough(Dest.getBlock())) 1280 break; 1281 } 1282 } 1283 1284 Builder.ClearInsertionPoint(); 1285} 1286 1287void CodeGenFunction::EmitBranchThroughEHCleanup(UnwindDest Dest) { 1288 // We should never get invalid scope depths for an UnwindDest; that 1289 // implies that the destination wasn't set up correctly. 1290 assert(Dest.getScopeDepth().isValid() && "invalid scope depth on EH dest?"); 1291 1292 if (!HaveInsertPoint()) 1293 return; 1294 1295 // Create the branch. 1296 llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock()); 1297 1298 // Calculate the innermost active cleanup. 1299 EHScopeStack::stable_iterator 1300 InnermostCleanup = EHStack.getInnermostActiveEHCleanup(); 1301 1302 // If the destination is in the same EH cleanup scope as us, we 1303 // don't need to thread through anything. 1304 if (InnermostCleanup.encloses(Dest.getScopeDepth())) { 1305 Builder.ClearInsertionPoint(); 1306 return; 1307 } 1308 assert(InnermostCleanup != EHStack.stable_end()); 1309 1310 // Store the index at the start. 1311 llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex()); 1312 new llvm::StoreInst(Index, getEHCleanupDestSlot(), BI); 1313 1314 // Adjust BI to point to the first cleanup block. 1315 { 1316 EHCleanupScope &Scope = 1317 cast<EHCleanupScope>(*EHStack.find(InnermostCleanup)); 1318 BI->setSuccessor(0, CreateEHEntry(*this, Scope)); 1319 } 1320 1321 // Add this destination to all the scopes involved. 1322 for (EHScopeStack::stable_iterator 1323 I = InnermostCleanup, E = Dest.getScopeDepth(); ; ) { 1324 assert(E.strictlyEncloses(I)); 1325 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I)); 1326 assert(Scope.isEHCleanup()); 1327 I = Scope.getEnclosingEHCleanup(); 1328 1329 // If this is the last cleanup we're propagating through, add this 1330 // as a branch-after. 1331 if (I == E) { 1332 Scope.addEHBranchAfter(Index, Dest.getBlock()); 1333 break; 1334 } 1335 1336 // Otherwise, add it as a branch-through. If this isn't new 1337 // information, all the rest of the work has been done before. 1338 if (!Scope.addEHBranchThrough(Dest.getBlock())) 1339 break; 1340 } 1341 1342 Builder.ClearInsertionPoint(); 1343} 1344 1345/// All the branch fixups on the EH stack have propagated out past the 1346/// outermost normal cleanup; resolve them all by adding cases to the 1347/// given switch instruction. 1348static void ResolveAllBranchFixups(CodeGenFunction &CGF, 1349 llvm::SwitchInst *Switch, 1350 llvm::BasicBlock *CleanupEntry) { 1351 llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded; 1352 1353 for (unsigned I = 0, E = CGF.EHStack.getNumBranchFixups(); I != E; ++I) { 1354 // Skip this fixup if its destination isn't set. 1355 BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I); 1356 if (Fixup.Destination == 0) continue; 1357 1358 // If there isn't an OptimisticBranchBlock, then InitialBranch is 1359 // still pointing directly to its destination; forward it to the 1360 // appropriate cleanup entry. This is required in the specific 1361 // case of 1362 // { std::string s; goto lbl; } 1363 // lbl: 1364 // i.e. where there's an unresolved fixup inside a single cleanup 1365 // entry which we're currently popping. 1366 if (Fixup.OptimisticBranchBlock == 0) { 1367 new llvm::StoreInst(CGF.Builder.getInt32(Fixup.DestinationIndex), 1368 CGF.getNormalCleanupDestSlot(), 1369 Fixup.InitialBranch); 1370 Fixup.InitialBranch->setSuccessor(0, CleanupEntry); 1371 } 1372 1373 // Don't add this case to the switch statement twice. 1374 if (!CasesAdded.insert(Fixup.Destination)) continue; 1375 1376 Switch->addCase(CGF.Builder.getInt32(Fixup.DestinationIndex), 1377 Fixup.Destination); 1378 } 1379 1380 CGF.EHStack.clearFixups(); 1381} 1382 1383void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) { 1384 assert(Block && "resolving a null target block"); 1385 if (!EHStack.getNumBranchFixups()) return; 1386 1387 assert(EHStack.hasNormalCleanups() && 1388 "branch fixups exist with no normal cleanups on stack"); 1389 1390 llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks; 1391 bool ResolvedAny = false; 1392 1393 for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) { 1394 // Skip this fixup if its destination doesn't match. 1395 BranchFixup &Fixup = EHStack.getBranchFixup(I); 1396 if (Fixup.Destination != Block) continue; 1397 1398 Fixup.Destination = 0; 1399 ResolvedAny = true; 1400 1401 // If it doesn't have an optimistic branch block, LatestBranch is 1402 // already pointing to the right place. 1403 llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock; 1404 if (!BranchBB) 1405 continue; 1406 1407 // Don't process the same optimistic branch block twice. 1408 if (!ModifiedOptimisticBlocks.insert(BranchBB)) 1409 continue; 1410 1411 llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB); 1412 1413 // Add a case to the switch. 1414 Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block); 1415 } 1416 1417 if (ResolvedAny) 1418 EHStack.popNullFixups(); 1419} 1420 1421static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack, 1422 EHScopeStack::stable_iterator C) { 1423 // If we needed a normal block for any reason, that counts. 1424 if (cast<EHCleanupScope>(*EHStack.find(C)).getNormalBlock()) 1425 return true; 1426 1427 // Check whether any enclosed cleanups were needed. 1428 for (EHScopeStack::stable_iterator 1429 I = EHStack.getInnermostNormalCleanup(); 1430 I != C; ) { 1431 assert(C.strictlyEncloses(I)); 1432 EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I)); 1433 if (S.getNormalBlock()) return true; 1434 I = S.getEnclosingNormalCleanup(); 1435 } 1436 1437 return false; 1438} 1439 1440static bool IsUsedAsEHCleanup(EHScopeStack &EHStack, 1441 EHScopeStack::stable_iterator C) { 1442 // If we needed an EH block for any reason, that counts. 1443 if (cast<EHCleanupScope>(*EHStack.find(C)).getEHBlock()) 1444 return true; 1445 1446 // Check whether any enclosed cleanups were needed. 1447 for (EHScopeStack::stable_iterator 1448 I = EHStack.getInnermostEHCleanup(); I != C; ) { 1449 assert(C.strictlyEncloses(I)); 1450 EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I)); 1451 if (S.getEHBlock()) return true; 1452 I = S.getEnclosingEHCleanup(); 1453 } 1454 1455 return false; 1456} 1457 1458enum ForActivation_t { 1459 ForActivation, 1460 ForDeactivation 1461}; 1462 1463/// The given cleanup block is changing activation state. Configure a 1464/// cleanup variable if necessary. 1465/// 1466/// It would be good if we had some way of determining if there were 1467/// extra uses *after* the change-over point. 1468static void SetupCleanupBlockActivation(CodeGenFunction &CGF, 1469 EHScopeStack::stable_iterator C, 1470 ForActivation_t Kind) { 1471 EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C)); 1472 1473 // We always need the flag if we're activating the cleanup, because 1474 // we have to assume that the current location doesn't necessarily 1475 // dominate all future uses of the cleanup. 1476 bool NeedFlag = (Kind == ForActivation); 1477 1478 // Calculate whether the cleanup was used: 1479 1480 // - as a normal cleanup 1481 if (Scope.isNormalCleanup() && IsUsedAsNormalCleanup(CGF.EHStack, C)) { 1482 Scope.setTestFlagInNormalCleanup(); 1483 NeedFlag = true; 1484 } 1485 1486 // - as an EH cleanup 1487 if (Scope.isEHCleanup() && IsUsedAsEHCleanup(CGF.EHStack, C)) { 1488 Scope.setTestFlagInEHCleanup(); 1489 NeedFlag = true; 1490 } 1491 1492 // If it hasn't yet been used as either, we're done. 1493 if (!NeedFlag) return; 1494 1495 llvm::AllocaInst *Var = Scope.getActiveFlag(); 1496 if (!Var) { 1497 Var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "cleanup.isactive"); 1498 Scope.setActiveFlag(Var); 1499 1500 // Initialize to true or false depending on whether it was 1501 // active up to this point. 1502 CGF.InitTempAlloca(Var, CGF.Builder.getInt1(Kind == ForDeactivation)); 1503 } 1504 1505 CGF.Builder.CreateStore(CGF.Builder.getInt1(Kind == ForActivation), Var); 1506} 1507 1508/// Activate a cleanup that was created in an inactivated state. 1509void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C) { 1510 assert(C != EHStack.stable_end() && "activating bottom of stack?"); 1511 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C)); 1512 assert(!Scope.isActive() && "double activation"); 1513 1514 SetupCleanupBlockActivation(*this, C, ForActivation); 1515 1516 Scope.setActive(true); 1517} 1518 1519/// Deactive a cleanup that was created in an active state. 1520void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C) { 1521 assert(C != EHStack.stable_end() && "deactivating bottom of stack?"); 1522 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C)); 1523 assert(Scope.isActive() && "double deactivation"); 1524 1525 // If it's the top of the stack, just pop it. 1526 if (C == EHStack.stable_begin()) { 1527 // If it's a normal cleanup, we need to pretend that the 1528 // fallthrough is unreachable. 1529 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 1530 PopCleanupBlock(); 1531 Builder.restoreIP(SavedIP); 1532 return; 1533 } 1534 1535 // Otherwise, follow the general case. 1536 SetupCleanupBlockActivation(*this, C, ForDeactivation); 1537 1538 Scope.setActive(false); 1539} 1540 1541llvm::Value *CodeGenFunction::getNormalCleanupDestSlot() { 1542 if (!NormalCleanupDest) 1543 NormalCleanupDest = 1544 CreateTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot"); 1545 return NormalCleanupDest; 1546} 1547 1548llvm::Value *CodeGenFunction::getEHCleanupDestSlot() { 1549 if (!EHCleanupDest) 1550 EHCleanupDest = 1551 CreateTempAlloca(Builder.getInt32Ty(), "eh.cleanup.dest.slot"); 1552 return EHCleanupDest; 1553} 1554 1555void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E, 1556 llvm::Constant *Init) { 1557 assert (Init && "Invalid DeclRefExpr initializer!"); 1558 if (CGDebugInfo *Dbg = getDebugInfo()) 1559 Dbg->EmitGlobalVariable(E->getDecl(), Init); 1560} 1561