1 //===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===// 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 contains code to emit Stmt nodes as LLVM code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenFunction.h" 15 #include "CGDebugInfo.h" 16 #include "CodeGenModule.h" 17 #include "TargetInfo.h" 18 #include "clang/AST/StmtVisitor.h" 19 #include "clang/Basic/PrettyStackTrace.h" 20 #include "clang/Basic/TargetInfo.h" 21 #include "clang/Sema/SemaDiagnostic.h" 22 #include "llvm/ADT/StringExtras.h" 23 #include "llvm/IR/CallSite.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/InlineAsm.h" 26 #include "llvm/IR/Intrinsics.h" 27 using namespace clang; 28 using namespace CodeGen; 29 30 //===----------------------------------------------------------------------===// 31 // Statement Emission 32 //===----------------------------------------------------------------------===// 33 34 void CodeGenFunction::EmitStopPoint(const Stmt *S) { 35 if (CGDebugInfo *DI = getDebugInfo()) { 36 SourceLocation Loc; 37 Loc = S->getLocStart(); 38 DI->EmitLocation(Builder, Loc); 39 40 LastStopPoint = Loc; 41 } 42 } 43 44 void CodeGenFunction::EmitStmt(const Stmt *S) { 45 assert(S && "Null statement?"); 46 PGO.setCurrentStmt(S); 47 48 // These statements have their own debug info handling. 49 if (EmitSimpleStmt(S)) 50 return; 51 52 // Check if we are generating unreachable code. 53 if (!HaveInsertPoint()) { 54 // If so, and the statement doesn't contain a label, then we do not need to 55 // generate actual code. This is safe because (1) the current point is 56 // unreachable, so we don't need to execute the code, and (2) we've already 57 // handled the statements which update internal data structures (like the 58 // local variable map) which could be used by subsequent statements. 59 if (!ContainsLabel(S)) { 60 // Verify that any decl statements were handled as simple, they may be in 61 // scope of subsequent reachable statements. 62 assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!"); 63 return; 64 } 65 66 // Otherwise, make a new block to hold the code. 67 EnsureInsertPoint(); 68 } 69 70 // Generate a stoppoint if we are emitting debug info. 71 EmitStopPoint(S); 72 73 switch (S->getStmtClass()) { 74 case Stmt::NoStmtClass: 75 case Stmt::CXXCatchStmtClass: 76 case Stmt::SEHExceptStmtClass: 77 case Stmt::SEHFinallyStmtClass: 78 case Stmt::MSDependentExistsStmtClass: 79 llvm_unreachable("invalid statement class to emit generically"); 80 case Stmt::NullStmtClass: 81 case Stmt::CompoundStmtClass: 82 case Stmt::DeclStmtClass: 83 case Stmt::LabelStmtClass: 84 case Stmt::AttributedStmtClass: 85 case Stmt::GotoStmtClass: 86 case Stmt::BreakStmtClass: 87 case Stmt::ContinueStmtClass: 88 case Stmt::DefaultStmtClass: 89 case Stmt::CaseStmtClass: 90 llvm_unreachable("should have emitted these statements as simple"); 91 92 #define STMT(Type, Base) 93 #define ABSTRACT_STMT(Op) 94 #define EXPR(Type, Base) \ 95 case Stmt::Type##Class: 96 #include "clang/AST/StmtNodes.inc" 97 { 98 // Remember the block we came in on. 99 llvm::BasicBlock *incoming = Builder.GetInsertBlock(); 100 assert(incoming && "expression emission must have an insertion point"); 101 102 EmitIgnoredExpr(cast<Expr>(S)); 103 104 llvm::BasicBlock *outgoing = Builder.GetInsertBlock(); 105 assert(outgoing && "expression emission cleared block!"); 106 107 // The expression emitters assume (reasonably!) that the insertion 108 // point is always set. To maintain that, the call-emission code 109 // for noreturn functions has to enter a new block with no 110 // predecessors. We want to kill that block and mark the current 111 // insertion point unreachable in the common case of a call like 112 // "exit();". Since expression emission doesn't otherwise create 113 // blocks with no predecessors, we can just test for that. 114 // However, we must be careful not to do this to our incoming 115 // block, because *statement* emission does sometimes create 116 // reachable blocks which will have no predecessors until later in 117 // the function. This occurs with, e.g., labels that are not 118 // reachable by fallthrough. 119 if (incoming != outgoing && outgoing->use_empty()) { 120 outgoing->eraseFromParent(); 121 Builder.ClearInsertionPoint(); 122 } 123 break; 124 } 125 126 case Stmt::IndirectGotoStmtClass: 127 EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break; 128 129 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; 130 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break; 131 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break; 132 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break; 133 134 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; 135 136 case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; 137 case Stmt::GCCAsmStmtClass: // Intentional fall-through. 138 case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; 139 case Stmt::CapturedStmtClass: { 140 const CapturedStmt *CS = cast<CapturedStmt>(S); 141 EmitCapturedStmt(*CS, CS->getCapturedRegionKind()); 142 } 143 break; 144 case Stmt::ObjCAtTryStmtClass: 145 EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S)); 146 break; 147 case Stmt::ObjCAtCatchStmtClass: 148 llvm_unreachable( 149 "@catch statements should be handled by EmitObjCAtTryStmt"); 150 case Stmt::ObjCAtFinallyStmtClass: 151 llvm_unreachable( 152 "@finally statements should be handled by EmitObjCAtTryStmt"); 153 case Stmt::ObjCAtThrowStmtClass: 154 EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S)); 155 break; 156 case Stmt::ObjCAtSynchronizedStmtClass: 157 EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S)); 158 break; 159 case Stmt::ObjCForCollectionStmtClass: 160 EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S)); 161 break; 162 case Stmt::ObjCAutoreleasePoolStmtClass: 163 EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S)); 164 break; 165 166 case Stmt::CXXTryStmtClass: 167 EmitCXXTryStmt(cast<CXXTryStmt>(*S)); 168 break; 169 case Stmt::CXXForRangeStmtClass: 170 EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S)); 171 break; 172 case Stmt::SEHTryStmtClass: 173 EmitSEHTryStmt(cast<SEHTryStmt>(*S)); 174 break; 175 case Stmt::OMPParallelDirectiveClass: 176 EmitOMPParallelDirective(cast<OMPParallelDirective>(*S)); 177 break; 178 case Stmt::OMPSimdDirectiveClass: 179 EmitOMPSimdDirective(cast<OMPSimdDirective>(*S)); 180 break; 181 } 182 } 183 184 bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) { 185 switch (S->getStmtClass()) { 186 default: return false; 187 case Stmt::NullStmtClass: break; 188 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break; 189 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break; 190 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break; 191 case Stmt::AttributedStmtClass: 192 EmitAttributedStmt(cast<AttributedStmt>(*S)); break; 193 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break; 194 case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break; 195 case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break; 196 case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break; 197 case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break; 198 } 199 200 return true; 201 } 202 203 /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true, 204 /// this captures the expression result of the last sub-statement and returns it 205 /// (for use by the statement expression extension). 206 llvm::Value* CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, 207 AggValueSlot AggSlot) { 208 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(), 209 "LLVM IR generation of compound statement ('{}')"); 210 211 // Keep track of the current cleanup stack depth, including debug scopes. 212 LexicalScope Scope(*this, S.getSourceRange()); 213 214 return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot); 215 } 216 217 llvm::Value* 218 CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S, 219 bool GetLast, 220 AggValueSlot AggSlot) { 221 222 for (CompoundStmt::const_body_iterator I = S.body_begin(), 223 E = S.body_end()-GetLast; I != E; ++I) 224 EmitStmt(*I); 225 226 llvm::Value *RetAlloca = nullptr; 227 if (GetLast) { 228 // We have to special case labels here. They are statements, but when put 229 // at the end of a statement expression, they yield the value of their 230 // subexpression. Handle this by walking through all labels we encounter, 231 // emitting them before we evaluate the subexpr. 232 const Stmt *LastStmt = S.body_back(); 233 while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) { 234 EmitLabel(LS->getDecl()); 235 LastStmt = LS->getSubStmt(); 236 } 237 238 EnsureInsertPoint(); 239 240 QualType ExprTy = cast<Expr>(LastStmt)->getType(); 241 if (hasAggregateEvaluationKind(ExprTy)) { 242 EmitAggExpr(cast<Expr>(LastStmt), AggSlot); 243 } else { 244 // We can't return an RValue here because there might be cleanups at 245 // the end of the StmtExpr. Because of that, we have to emit the result 246 // here into a temporary alloca. 247 RetAlloca = CreateMemTemp(ExprTy); 248 EmitAnyExprToMem(cast<Expr>(LastStmt), RetAlloca, Qualifiers(), 249 /*IsInit*/false); 250 } 251 252 } 253 254 return RetAlloca; 255 } 256 257 void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) { 258 llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator()); 259 260 // If there is a cleanup stack, then we it isn't worth trying to 261 // simplify this block (we would need to remove it from the scope map 262 // and cleanup entry). 263 if (!EHStack.empty()) 264 return; 265 266 // Can only simplify direct branches. 267 if (!BI || !BI->isUnconditional()) 268 return; 269 270 // Can only simplify empty blocks. 271 if (BI != BB->begin()) 272 return; 273 274 BB->replaceAllUsesWith(BI->getSuccessor(0)); 275 BI->eraseFromParent(); 276 BB->eraseFromParent(); 277 } 278 279 void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) { 280 llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 281 282 // Fall out of the current block (if necessary). 283 EmitBranch(BB); 284 285 if (IsFinished && BB->use_empty()) { 286 delete BB; 287 return; 288 } 289 290 // Place the block after the current block, if possible, or else at 291 // the end of the function. 292 if (CurBB && CurBB->getParent()) 293 CurFn->getBasicBlockList().insertAfter(CurBB, BB); 294 else 295 CurFn->getBasicBlockList().push_back(BB); 296 Builder.SetInsertPoint(BB); 297 } 298 299 void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) { 300 // Emit a branch from the current block to the target one if this 301 // was a real block. If this was just a fall-through block after a 302 // terminator, don't emit it. 303 llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 304 305 if (!CurBB || CurBB->getTerminator()) { 306 // If there is no insert point or the previous block is already 307 // terminated, don't touch it. 308 } else { 309 // Otherwise, create a fall-through branch. 310 Builder.CreateBr(Target); 311 } 312 313 Builder.ClearInsertionPoint(); 314 } 315 316 void CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) { 317 bool inserted = false; 318 for (llvm::User *u : block->users()) { 319 if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(u)) { 320 CurFn->getBasicBlockList().insertAfter(insn->getParent(), block); 321 inserted = true; 322 break; 323 } 324 } 325 326 if (!inserted) 327 CurFn->getBasicBlockList().push_back(block); 328 329 Builder.SetInsertPoint(block); 330 } 331 332 CodeGenFunction::JumpDest 333 CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) { 334 JumpDest &Dest = LabelMap[D]; 335 if (Dest.isValid()) return Dest; 336 337 // Create, but don't insert, the new block. 338 Dest = JumpDest(createBasicBlock(D->getName()), 339 EHScopeStack::stable_iterator::invalid(), 340 NextCleanupDestIndex++); 341 return Dest; 342 } 343 344 void CodeGenFunction::EmitLabel(const LabelDecl *D) { 345 // Add this label to the current lexical scope if we're within any 346 // normal cleanups. Jumps "in" to this label --- when permitted by 347 // the language --- may need to be routed around such cleanups. 348 if (EHStack.hasNormalCleanups() && CurLexicalScope) 349 CurLexicalScope->addLabel(D); 350 351 JumpDest &Dest = LabelMap[D]; 352 353 // If we didn't need a forward reference to this label, just go 354 // ahead and create a destination at the current scope. 355 if (!Dest.isValid()) { 356 Dest = getJumpDestInCurrentScope(D->getName()); 357 358 // Otherwise, we need to give this label a target depth and remove 359 // it from the branch-fixups list. 360 } else { 361 assert(!Dest.getScopeDepth().isValid() && "already emitted label!"); 362 Dest.setScopeDepth(EHStack.stable_begin()); 363 ResolveBranchFixups(Dest.getBlock()); 364 } 365 366 RegionCounter Cnt = getPGORegionCounter(D->getStmt()); 367 EmitBlock(Dest.getBlock()); 368 Cnt.beginRegion(Builder); 369 } 370 371 /// Change the cleanup scope of the labels in this lexical scope to 372 /// match the scope of the enclosing context. 373 void CodeGenFunction::LexicalScope::rescopeLabels() { 374 assert(!Labels.empty()); 375 EHScopeStack::stable_iterator innermostScope 376 = CGF.EHStack.getInnermostNormalCleanup(); 377 378 // Change the scope depth of all the labels. 379 for (SmallVectorImpl<const LabelDecl*>::const_iterator 380 i = Labels.begin(), e = Labels.end(); i != e; ++i) { 381 assert(CGF.LabelMap.count(*i)); 382 JumpDest &dest = CGF.LabelMap.find(*i)->second; 383 assert(dest.getScopeDepth().isValid()); 384 assert(innermostScope.encloses(dest.getScopeDepth())); 385 dest.setScopeDepth(innermostScope); 386 } 387 388 // Reparent the labels if the new scope also has cleanups. 389 if (innermostScope != EHScopeStack::stable_end() && ParentScope) { 390 ParentScope->Labels.append(Labels.begin(), Labels.end()); 391 } 392 } 393 394 395 void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { 396 EmitLabel(S.getDecl()); 397 EmitStmt(S.getSubStmt()); 398 } 399 400 void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { 401 EmitStmt(S.getSubStmt()); 402 } 403 404 void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { 405 // If this code is reachable then emit a stop point (if generating 406 // debug info). We have to do this ourselves because we are on the 407 // "simple" statement path. 408 if (HaveInsertPoint()) 409 EmitStopPoint(&S); 410 411 EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel())); 412 } 413 414 415 void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { 416 if (const LabelDecl *Target = S.getConstantTarget()) { 417 EmitBranchThroughCleanup(getJumpDestForLabel(Target)); 418 return; 419 } 420 421 // Ensure that we have an i8* for our PHI node. 422 llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()), 423 Int8PtrTy, "addr"); 424 llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 425 426 // Get the basic block for the indirect goto. 427 llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock(); 428 429 // The first instruction in the block has to be the PHI for the switch dest, 430 // add an entry for this branch. 431 cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB); 432 433 EmitBranch(IndGotoBB); 434 } 435 436 void CodeGenFunction::EmitIfStmt(const IfStmt &S) { 437 // C99 6.8.4.1: The first substatement is executed if the expression compares 438 // unequal to 0. The condition must be a scalar type. 439 LexicalScope ConditionScope(*this, S.getCond()->getSourceRange()); 440 RegionCounter Cnt = getPGORegionCounter(&S); 441 442 if (S.getConditionVariable()) 443 EmitAutoVarDecl(*S.getConditionVariable()); 444 445 // If the condition constant folds and can be elided, try to avoid emitting 446 // the condition and the dead arm of the if/else. 447 bool CondConstant; 448 if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant)) { 449 // Figure out which block (then or else) is executed. 450 const Stmt *Executed = S.getThen(); 451 const Stmt *Skipped = S.getElse(); 452 if (!CondConstant) // Condition false? 453 std::swap(Executed, Skipped); 454 455 // If the skipped block has no labels in it, just emit the executed block. 456 // This avoids emitting dead code and simplifies the CFG substantially. 457 if (!ContainsLabel(Skipped)) { 458 if (CondConstant) 459 Cnt.beginRegion(Builder); 460 if (Executed) { 461 RunCleanupsScope ExecutedScope(*this); 462 EmitStmt(Executed); 463 } 464 return; 465 } 466 } 467 468 // Otherwise, the condition did not fold, or we couldn't elide it. Just emit 469 // the conditional branch. 470 llvm::BasicBlock *ThenBlock = createBasicBlock("if.then"); 471 llvm::BasicBlock *ContBlock = createBasicBlock("if.end"); 472 llvm::BasicBlock *ElseBlock = ContBlock; 473 if (S.getElse()) 474 ElseBlock = createBasicBlock("if.else"); 475 476 EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock, Cnt.getCount()); 477 478 // Emit the 'then' code. 479 EmitBlock(ThenBlock); 480 Cnt.beginRegion(Builder); 481 { 482 RunCleanupsScope ThenScope(*this); 483 EmitStmt(S.getThen()); 484 } 485 EmitBranch(ContBlock); 486 487 // Emit the 'else' code if present. 488 if (const Stmt *Else = S.getElse()) { 489 // There is no need to emit line number for unconditional branch. 490 if (getDebugInfo()) 491 Builder.SetCurrentDebugLocation(llvm::DebugLoc()); 492 EmitBlock(ElseBlock); 493 { 494 RunCleanupsScope ElseScope(*this); 495 EmitStmt(Else); 496 } 497 // There is no need to emit line number for unconditional branch. 498 if (getDebugInfo()) 499 Builder.SetCurrentDebugLocation(llvm::DebugLoc()); 500 EmitBranch(ContBlock); 501 } 502 503 // Emit the continuation block for code after the if. 504 EmitBlock(ContBlock, true); 505 } 506 507 void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { 508 RegionCounter Cnt = getPGORegionCounter(&S); 509 510 // Emit the header for the loop, which will also become 511 // the continue target. 512 JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond"); 513 EmitBlock(LoopHeader.getBlock()); 514 515 LoopStack.push(LoopHeader.getBlock()); 516 517 // Create an exit block for when the condition fails, which will 518 // also become the break target. 519 JumpDest LoopExit = getJumpDestInCurrentScope("while.end"); 520 521 // Store the blocks to use for break and continue. 522 BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader)); 523 524 // C++ [stmt.while]p2: 525 // When the condition of a while statement is a declaration, the 526 // scope of the variable that is declared extends from its point 527 // of declaration (3.3.2) to the end of the while statement. 528 // [...] 529 // The object created in a condition is destroyed and created 530 // with each iteration of the loop. 531 RunCleanupsScope ConditionScope(*this); 532 533 if (S.getConditionVariable()) 534 EmitAutoVarDecl(*S.getConditionVariable()); 535 536 // Evaluate the conditional in the while header. C99 6.8.5.1: The 537 // evaluation of the controlling expression takes place before each 538 // execution of the loop body. 539 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 540 541 // while(1) is common, avoid extra exit blocks. Be sure 542 // to correctly handle break/continue though. 543 bool EmitBoolCondBranch = true; 544 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) 545 if (C->isOne()) 546 EmitBoolCondBranch = false; 547 548 // As long as the condition is true, go to the loop body. 549 llvm::BasicBlock *LoopBody = createBasicBlock("while.body"); 550 if (EmitBoolCondBranch) { 551 llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 552 if (ConditionScope.requiresCleanups()) 553 ExitBlock = createBasicBlock("while.exit"); 554 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, 555 PGO.createLoopWeights(S.getCond(), Cnt)); 556 557 if (ExitBlock != LoopExit.getBlock()) { 558 EmitBlock(ExitBlock); 559 EmitBranchThroughCleanup(LoopExit); 560 } 561 } 562 563 // Emit the loop body. We have to emit this in a cleanup scope 564 // because it might be a singleton DeclStmt. 565 { 566 RunCleanupsScope BodyScope(*this); 567 EmitBlock(LoopBody); 568 Cnt.beginRegion(Builder); 569 EmitStmt(S.getBody()); 570 } 571 572 BreakContinueStack.pop_back(); 573 574 // Immediately force cleanup. 575 ConditionScope.ForceCleanup(); 576 577 // Branch to the loop header again. 578 EmitBranch(LoopHeader.getBlock()); 579 580 LoopStack.pop(); 581 582 // Emit the exit block. 583 EmitBlock(LoopExit.getBlock(), true); 584 585 // The LoopHeader typically is just a branch if we skipped emitting 586 // a branch, try to erase it. 587 if (!EmitBoolCondBranch) 588 SimplifyForwardingBlocks(LoopHeader.getBlock()); 589 } 590 591 void CodeGenFunction::EmitDoStmt(const DoStmt &S) { 592 JumpDest LoopExit = getJumpDestInCurrentScope("do.end"); 593 JumpDest LoopCond = getJumpDestInCurrentScope("do.cond"); 594 595 RegionCounter Cnt = getPGORegionCounter(&S); 596 597 // Store the blocks to use for break and continue. 598 BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond)); 599 600 // Emit the body of the loop. 601 llvm::BasicBlock *LoopBody = createBasicBlock("do.body"); 602 603 LoopStack.push(LoopBody); 604 605 EmitBlockWithFallThrough(LoopBody, Cnt); 606 { 607 RunCleanupsScope BodyScope(*this); 608 EmitStmt(S.getBody()); 609 } 610 611 EmitBlock(LoopCond.getBlock()); 612 613 // C99 6.8.5.2: "The evaluation of the controlling expression takes place 614 // after each execution of the loop body." 615 616 // Evaluate the conditional in the while header. 617 // C99 6.8.5p2/p4: The first substatement is executed if the expression 618 // compares unequal to 0. The condition must be a scalar type. 619 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 620 621 BreakContinueStack.pop_back(); 622 623 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure 624 // to correctly handle break/continue though. 625 bool EmitBoolCondBranch = true; 626 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) 627 if (C->isZero()) 628 EmitBoolCondBranch = false; 629 630 // As long as the condition is true, iterate the loop. 631 if (EmitBoolCondBranch) 632 Builder.CreateCondBr(BoolCondVal, LoopBody, LoopExit.getBlock(), 633 PGO.createLoopWeights(S.getCond(), Cnt)); 634 635 LoopStack.pop(); 636 637 // Emit the exit block. 638 EmitBlock(LoopExit.getBlock()); 639 640 // The DoCond block typically is just a branch if we skipped 641 // emitting a branch, try to erase it. 642 if (!EmitBoolCondBranch) 643 SimplifyForwardingBlocks(LoopCond.getBlock()); 644 } 645 646 void CodeGenFunction::EmitForStmt(const ForStmt &S) { 647 JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); 648 649 RunCleanupsScope ForScope(*this); 650 651 CGDebugInfo *DI = getDebugInfo(); 652 if (DI) 653 DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 654 655 // Evaluate the first part before the loop. 656 if (S.getInit()) 657 EmitStmt(S.getInit()); 658 659 RegionCounter Cnt = getPGORegionCounter(&S); 660 661 // Start the loop with a block that tests the condition. 662 // If there's an increment, the continue scope will be overwritten 663 // later. 664 JumpDest Continue = getJumpDestInCurrentScope("for.cond"); 665 llvm::BasicBlock *CondBlock = Continue.getBlock(); 666 EmitBlock(CondBlock); 667 668 LoopStack.push(CondBlock); 669 670 // If the for loop doesn't have an increment we can just use the 671 // condition as the continue block. Otherwise we'll need to create 672 // a block for it (in the current scope, i.e. in the scope of the 673 // condition), and that we will become our continue block. 674 if (S.getInc()) 675 Continue = getJumpDestInCurrentScope("for.inc"); 676 677 // Store the blocks to use for break and continue. 678 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 679 680 // Create a cleanup scope for the condition variable cleanups. 681 RunCleanupsScope ConditionScope(*this); 682 683 if (S.getCond()) { 684 // If the for statement has a condition scope, emit the local variable 685 // declaration. 686 if (S.getConditionVariable()) { 687 EmitAutoVarDecl(*S.getConditionVariable()); 688 } 689 690 llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 691 // If there are any cleanups between here and the loop-exit scope, 692 // create a block to stage a loop exit along. 693 if (ForScope.requiresCleanups()) 694 ExitBlock = createBasicBlock("for.cond.cleanup"); 695 696 // As long as the condition is true, iterate the loop. 697 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 698 699 // C99 6.8.5p2/p4: The first substatement is executed if the expression 700 // compares unequal to 0. The condition must be a scalar type. 701 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 702 Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, 703 PGO.createLoopWeights(S.getCond(), Cnt)); 704 705 if (ExitBlock != LoopExit.getBlock()) { 706 EmitBlock(ExitBlock); 707 EmitBranchThroughCleanup(LoopExit); 708 } 709 710 EmitBlock(ForBody); 711 } else { 712 // Treat it as a non-zero constant. Don't even create a new block for the 713 // body, just fall into it. 714 } 715 Cnt.beginRegion(Builder); 716 717 { 718 // Create a separate cleanup scope for the body, in case it is not 719 // a compound statement. 720 RunCleanupsScope BodyScope(*this); 721 EmitStmt(S.getBody()); 722 } 723 724 // If there is an increment, emit it next. 725 if (S.getInc()) { 726 EmitBlock(Continue.getBlock()); 727 EmitStmt(S.getInc()); 728 } 729 730 BreakContinueStack.pop_back(); 731 732 ConditionScope.ForceCleanup(); 733 EmitBranch(CondBlock); 734 735 ForScope.ForceCleanup(); 736 737 if (DI) 738 DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 739 740 LoopStack.pop(); 741 742 // Emit the fall-through block. 743 EmitBlock(LoopExit.getBlock(), true); 744 } 745 746 void CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) { 747 JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); 748 749 RunCleanupsScope ForScope(*this); 750 751 CGDebugInfo *DI = getDebugInfo(); 752 if (DI) 753 DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 754 755 // Evaluate the first pieces before the loop. 756 EmitStmt(S.getRangeStmt()); 757 EmitStmt(S.getBeginEndStmt()); 758 759 RegionCounter Cnt = getPGORegionCounter(&S); 760 761 // Start the loop with a block that tests the condition. 762 // If there's an increment, the continue scope will be overwritten 763 // later. 764 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 765 EmitBlock(CondBlock); 766 767 LoopStack.push(CondBlock); 768 769 // If there are any cleanups between here and the loop-exit scope, 770 // create a block to stage a loop exit along. 771 llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 772 if (ForScope.requiresCleanups()) 773 ExitBlock = createBasicBlock("for.cond.cleanup"); 774 775 // The loop body, consisting of the specified body and the loop variable. 776 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 777 778 // The body is executed if the expression, contextually converted 779 // to bool, is true. 780 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 781 Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, 782 PGO.createLoopWeights(S.getCond(), Cnt)); 783 784 if (ExitBlock != LoopExit.getBlock()) { 785 EmitBlock(ExitBlock); 786 EmitBranchThroughCleanup(LoopExit); 787 } 788 789 EmitBlock(ForBody); 790 Cnt.beginRegion(Builder); 791 792 // Create a block for the increment. In case of a 'continue', we jump there. 793 JumpDest Continue = getJumpDestInCurrentScope("for.inc"); 794 795 // Store the blocks to use for break and continue. 796 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 797 798 { 799 // Create a separate cleanup scope for the loop variable and body. 800 RunCleanupsScope BodyScope(*this); 801 EmitStmt(S.getLoopVarStmt()); 802 EmitStmt(S.getBody()); 803 } 804 805 // If there is an increment, emit it next. 806 EmitBlock(Continue.getBlock()); 807 EmitStmt(S.getInc()); 808 809 BreakContinueStack.pop_back(); 810 811 EmitBranch(CondBlock); 812 813 ForScope.ForceCleanup(); 814 815 if (DI) 816 DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 817 818 LoopStack.pop(); 819 820 // Emit the fall-through block. 821 EmitBlock(LoopExit.getBlock(), true); 822 } 823 824 void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) { 825 if (RV.isScalar()) { 826 Builder.CreateStore(RV.getScalarVal(), ReturnValue); 827 } else if (RV.isAggregate()) { 828 EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty); 829 } else { 830 EmitStoreOfComplex(RV.getComplexVal(), 831 MakeNaturalAlignAddrLValue(ReturnValue, Ty), 832 /*init*/ true); 833 } 834 EmitBranchThroughCleanup(ReturnBlock); 835 } 836 837 /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand 838 /// if the function returns void, or may be missing one if the function returns 839 /// non-void. Fun stuff :). 840 void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { 841 // Emit the result value, even if unused, to evalute the side effects. 842 const Expr *RV = S.getRetValue(); 843 844 // Treat block literals in a return expression as if they appeared 845 // in their own scope. This permits a small, easily-implemented 846 // exception to our over-conservative rules about not jumping to 847 // statements following block literals with non-trivial cleanups. 848 RunCleanupsScope cleanupScope(*this); 849 if (const ExprWithCleanups *cleanups = 850 dyn_cast_or_null<ExprWithCleanups>(RV)) { 851 enterFullExpression(cleanups); 852 RV = cleanups->getSubExpr(); 853 } 854 855 // FIXME: Clean this up by using an LValue for ReturnTemp, 856 // EmitStoreThroughLValue, and EmitAnyExpr. 857 if (getLangOpts().ElideConstructors && 858 S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable()) { 859 // Apply the named return value optimization for this return statement, 860 // which means doing nothing: the appropriate result has already been 861 // constructed into the NRVO variable. 862 863 // If there is an NRVO flag for this variable, set it to 1 into indicate 864 // that the cleanup code should not destroy the variable. 865 if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()]) 866 Builder.CreateStore(Builder.getTrue(), NRVOFlag); 867 } else if (!ReturnValue || (RV && RV->getType()->isVoidType())) { 868 // Make sure not to return anything, but evaluate the expression 869 // for side effects. 870 if (RV) 871 EmitAnyExpr(RV); 872 } else if (!RV) { 873 // Do nothing (return value is left uninitialized) 874 } else if (FnRetTy->isReferenceType()) { 875 // If this function returns a reference, take the address of the expression 876 // rather than the value. 877 RValue Result = EmitReferenceBindingToExpr(RV); 878 Builder.CreateStore(Result.getScalarVal(), ReturnValue); 879 } else { 880 switch (getEvaluationKind(RV->getType())) { 881 case TEK_Scalar: 882 Builder.CreateStore(EmitScalarExpr(RV), ReturnValue); 883 break; 884 case TEK_Complex: 885 EmitComplexExprIntoLValue(RV, 886 MakeNaturalAlignAddrLValue(ReturnValue, RV->getType()), 887 /*isInit*/ true); 888 break; 889 case TEK_Aggregate: { 890 CharUnits Alignment = getContext().getTypeAlignInChars(RV->getType()); 891 EmitAggExpr(RV, AggValueSlot::forAddr(ReturnValue, Alignment, 892 Qualifiers(), 893 AggValueSlot::IsDestructed, 894 AggValueSlot::DoesNotNeedGCBarriers, 895 AggValueSlot::IsNotAliased)); 896 break; 897 } 898 } 899 } 900 901 ++NumReturnExprs; 902 if (!RV || RV->isEvaluatable(getContext())) 903 ++NumSimpleReturnExprs; 904 905 cleanupScope.ForceCleanup(); 906 EmitBranchThroughCleanup(ReturnBlock); 907 } 908 909 void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { 910 // As long as debug info is modeled with instructions, we have to ensure we 911 // have a place to insert here and write the stop point here. 912 if (HaveInsertPoint()) 913 EmitStopPoint(&S); 914 915 for (const auto *I : S.decls()) 916 EmitDecl(*I); 917 } 918 919 void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) { 920 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); 921 922 // If this code is reachable then emit a stop point (if generating 923 // debug info). We have to do this ourselves because we are on the 924 // "simple" statement path. 925 if (HaveInsertPoint()) 926 EmitStopPoint(&S); 927 928 EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock); 929 } 930 931 void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) { 932 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); 933 934 // If this code is reachable then emit a stop point (if generating 935 // debug info). We have to do this ourselves because we are on the 936 // "simple" statement path. 937 if (HaveInsertPoint()) 938 EmitStopPoint(&S); 939 940 EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock); 941 } 942 943 /// EmitCaseStmtRange - If case statement range is not too big then 944 /// add multiple cases to switch instruction, one for each value within 945 /// the range. If range is too big then emit "if" condition check. 946 void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) { 947 assert(S.getRHS() && "Expected RHS value in CaseStmt"); 948 949 llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext()); 950 llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext()); 951 952 RegionCounter CaseCnt = getPGORegionCounter(&S); 953 954 // Emit the code for this case. We do this first to make sure it is 955 // properly chained from our predecessor before generating the 956 // switch machinery to enter this block. 957 llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb"); 958 EmitBlockWithFallThrough(CaseDest, CaseCnt); 959 EmitStmt(S.getSubStmt()); 960 961 // If range is empty, do nothing. 962 if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS)) 963 return; 964 965 llvm::APInt Range = RHS - LHS; 966 // FIXME: parameters such as this should not be hardcoded. 967 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { 968 // Range is small enough to add multiple switch instruction cases. 969 uint64_t Total = CaseCnt.getCount(); 970 unsigned NCases = Range.getZExtValue() + 1; 971 // We only have one region counter for the entire set of cases here, so we 972 // need to divide the weights evenly between the generated cases, ensuring 973 // that the total weight is preserved. E.g., a weight of 5 over three cases 974 // will be distributed as weights of 2, 2, and 1. 975 uint64_t Weight = Total / NCases, Rem = Total % NCases; 976 for (unsigned I = 0; I != NCases; ++I) { 977 if (SwitchWeights) 978 SwitchWeights->push_back(Weight + (Rem ? 1 : 0)); 979 if (Rem) 980 Rem--; 981 SwitchInsn->addCase(Builder.getInt(LHS), CaseDest); 982 LHS++; 983 } 984 return; 985 } 986 987 // The range is too big. Emit "if" condition into a new block, 988 // making sure to save and restore the current insertion point. 989 llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock(); 990 991 // Push this test onto the chain of range checks (which terminates 992 // in the default basic block). The switch's default will be changed 993 // to the top of this chain after switch emission is complete. 994 llvm::BasicBlock *FalseDest = CaseRangeBlock; 995 CaseRangeBlock = createBasicBlock("sw.caserange"); 996 997 CurFn->getBasicBlockList().push_back(CaseRangeBlock); 998 Builder.SetInsertPoint(CaseRangeBlock); 999 1000 // Emit range check. 1001 llvm::Value *Diff = 1002 Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS)); 1003 llvm::Value *Cond = 1004 Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds"); 1005 1006 llvm::MDNode *Weights = nullptr; 1007 if (SwitchWeights) { 1008 uint64_t ThisCount = CaseCnt.getCount(); 1009 uint64_t DefaultCount = (*SwitchWeights)[0]; 1010 Weights = PGO.createBranchWeights(ThisCount, DefaultCount); 1011 1012 // Since we're chaining the switch default through each large case range, we 1013 // need to update the weight for the default, ie, the first case, to include 1014 // this case. 1015 (*SwitchWeights)[0] += ThisCount; 1016 } 1017 Builder.CreateCondBr(Cond, CaseDest, FalseDest, Weights); 1018 1019 // Restore the appropriate insertion point. 1020 if (RestoreBB) 1021 Builder.SetInsertPoint(RestoreBB); 1022 else 1023 Builder.ClearInsertionPoint(); 1024 } 1025 1026 void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { 1027 // If there is no enclosing switch instance that we're aware of, then this 1028 // case statement and its block can be elided. This situation only happens 1029 // when we've constant-folded the switch, are emitting the constant case, 1030 // and part of the constant case includes another case statement. For 1031 // instance: switch (4) { case 4: do { case 5: } while (1); } 1032 if (!SwitchInsn) { 1033 EmitStmt(S.getSubStmt()); 1034 return; 1035 } 1036 1037 // Handle case ranges. 1038 if (S.getRHS()) { 1039 EmitCaseStmtRange(S); 1040 return; 1041 } 1042 1043 RegionCounter CaseCnt = getPGORegionCounter(&S); 1044 llvm::ConstantInt *CaseVal = 1045 Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext())); 1046 1047 // If the body of the case is just a 'break', try to not emit an empty block. 1048 // If we're profiling or we're not optimizing, leave the block in for better 1049 // debug and coverage analysis. 1050 if (!CGM.getCodeGenOpts().ProfileInstrGenerate && 1051 CGM.getCodeGenOpts().OptimizationLevel > 0 && 1052 isa<BreakStmt>(S.getSubStmt())) { 1053 JumpDest Block = BreakContinueStack.back().BreakBlock; 1054 1055 // Only do this optimization if there are no cleanups that need emitting. 1056 if (isObviouslyBranchWithoutCleanups(Block)) { 1057 if (SwitchWeights) 1058 SwitchWeights->push_back(CaseCnt.getCount()); 1059 SwitchInsn->addCase(CaseVal, Block.getBlock()); 1060 1061 // If there was a fallthrough into this case, make sure to redirect it to 1062 // the end of the switch as well. 1063 if (Builder.GetInsertBlock()) { 1064 Builder.CreateBr(Block.getBlock()); 1065 Builder.ClearInsertionPoint(); 1066 } 1067 return; 1068 } 1069 } 1070 1071 llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb"); 1072 EmitBlockWithFallThrough(CaseDest, CaseCnt); 1073 if (SwitchWeights) 1074 SwitchWeights->push_back(CaseCnt.getCount()); 1075 SwitchInsn->addCase(CaseVal, CaseDest); 1076 1077 // Recursively emitting the statement is acceptable, but is not wonderful for 1078 // code where we have many case statements nested together, i.e.: 1079 // case 1: 1080 // case 2: 1081 // case 3: etc. 1082 // Handling this recursively will create a new block for each case statement 1083 // that falls through to the next case which is IR intensive. It also causes 1084 // deep recursion which can run into stack depth limitations. Handle 1085 // sequential non-range case statements specially. 1086 const CaseStmt *CurCase = &S; 1087 const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt()); 1088 1089 // Otherwise, iteratively add consecutive cases to this switch stmt. 1090 while (NextCase && NextCase->getRHS() == nullptr) { 1091 CurCase = NextCase; 1092 llvm::ConstantInt *CaseVal = 1093 Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext())); 1094 1095 CaseCnt = getPGORegionCounter(NextCase); 1096 if (SwitchWeights) 1097 SwitchWeights->push_back(CaseCnt.getCount()); 1098 if (CGM.getCodeGenOpts().ProfileInstrGenerate) { 1099 CaseDest = createBasicBlock("sw.bb"); 1100 EmitBlockWithFallThrough(CaseDest, CaseCnt); 1101 } 1102 1103 SwitchInsn->addCase(CaseVal, CaseDest); 1104 NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt()); 1105 } 1106 1107 // Normal default recursion for non-cases. 1108 EmitStmt(CurCase->getSubStmt()); 1109 } 1110 1111 void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) { 1112 llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest(); 1113 assert(DefaultBlock->empty() && 1114 "EmitDefaultStmt: Default block already defined?"); 1115 1116 RegionCounter Cnt = getPGORegionCounter(&S); 1117 EmitBlockWithFallThrough(DefaultBlock, Cnt); 1118 1119 EmitStmt(S.getSubStmt()); 1120 } 1121 1122 /// CollectStatementsForCase - Given the body of a 'switch' statement and a 1123 /// constant value that is being switched on, see if we can dead code eliminate 1124 /// the body of the switch to a simple series of statements to emit. Basically, 1125 /// on a switch (5) we want to find these statements: 1126 /// case 5: 1127 /// printf(...); <-- 1128 /// ++i; <-- 1129 /// break; 1130 /// 1131 /// and add them to the ResultStmts vector. If it is unsafe to do this 1132 /// transformation (for example, one of the elided statements contains a label 1133 /// that might be jumped to), return CSFC_Failure. If we handled it and 'S' 1134 /// should include statements after it (e.g. the printf() line is a substmt of 1135 /// the case) then return CSFC_FallThrough. If we handled it and found a break 1136 /// statement, then return CSFC_Success. 1137 /// 1138 /// If Case is non-null, then we are looking for the specified case, checking 1139 /// that nothing we jump over contains labels. If Case is null, then we found 1140 /// the case and are looking for the break. 1141 /// 1142 /// If the recursive walk actually finds our Case, then we set FoundCase to 1143 /// true. 1144 /// 1145 enum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success }; 1146 static CSFC_Result CollectStatementsForCase(const Stmt *S, 1147 const SwitchCase *Case, 1148 bool &FoundCase, 1149 SmallVectorImpl<const Stmt*> &ResultStmts) { 1150 // If this is a null statement, just succeed. 1151 if (!S) 1152 return Case ? CSFC_Success : CSFC_FallThrough; 1153 1154 // If this is the switchcase (case 4: or default) that we're looking for, then 1155 // we're in business. Just add the substatement. 1156 if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) { 1157 if (S == Case) { 1158 FoundCase = true; 1159 return CollectStatementsForCase(SC->getSubStmt(), nullptr, FoundCase, 1160 ResultStmts); 1161 } 1162 1163 // Otherwise, this is some other case or default statement, just ignore it. 1164 return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase, 1165 ResultStmts); 1166 } 1167 1168 // If we are in the live part of the code and we found our break statement, 1169 // return a success! 1170 if (!Case && isa<BreakStmt>(S)) 1171 return CSFC_Success; 1172 1173 // If this is a switch statement, then it might contain the SwitchCase, the 1174 // break, or neither. 1175 if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) { 1176 // Handle this as two cases: we might be looking for the SwitchCase (if so 1177 // the skipped statements must be skippable) or we might already have it. 1178 CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end(); 1179 if (Case) { 1180 // Keep track of whether we see a skipped declaration. The code could be 1181 // using the declaration even if it is skipped, so we can't optimize out 1182 // the decl if the kept statements might refer to it. 1183 bool HadSkippedDecl = false; 1184 1185 // If we're looking for the case, just see if we can skip each of the 1186 // substatements. 1187 for (; Case && I != E; ++I) { 1188 HadSkippedDecl |= isa<DeclStmt>(*I); 1189 1190 switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) { 1191 case CSFC_Failure: return CSFC_Failure; 1192 case CSFC_Success: 1193 // A successful result means that either 1) that the statement doesn't 1194 // have the case and is skippable, or 2) does contain the case value 1195 // and also contains the break to exit the switch. In the later case, 1196 // we just verify the rest of the statements are elidable. 1197 if (FoundCase) { 1198 // If we found the case and skipped declarations, we can't do the 1199 // optimization. 1200 if (HadSkippedDecl) 1201 return CSFC_Failure; 1202 1203 for (++I; I != E; ++I) 1204 if (CodeGenFunction::ContainsLabel(*I, true)) 1205 return CSFC_Failure; 1206 return CSFC_Success; 1207 } 1208 break; 1209 case CSFC_FallThrough: 1210 // If we have a fallthrough condition, then we must have found the 1211 // case started to include statements. Consider the rest of the 1212 // statements in the compound statement as candidates for inclusion. 1213 assert(FoundCase && "Didn't find case but returned fallthrough?"); 1214 // We recursively found Case, so we're not looking for it anymore. 1215 Case = nullptr; 1216 1217 // If we found the case and skipped declarations, we can't do the 1218 // optimization. 1219 if (HadSkippedDecl) 1220 return CSFC_Failure; 1221 break; 1222 } 1223 } 1224 } 1225 1226 // If we have statements in our range, then we know that the statements are 1227 // live and need to be added to the set of statements we're tracking. 1228 for (; I != E; ++I) { 1229 switch (CollectStatementsForCase(*I, nullptr, FoundCase, ResultStmts)) { 1230 case CSFC_Failure: return CSFC_Failure; 1231 case CSFC_FallThrough: 1232 // A fallthrough result means that the statement was simple and just 1233 // included in ResultStmt, keep adding them afterwards. 1234 break; 1235 case CSFC_Success: 1236 // A successful result means that we found the break statement and 1237 // stopped statement inclusion. We just ensure that any leftover stmts 1238 // are skippable and return success ourselves. 1239 for (++I; I != E; ++I) 1240 if (CodeGenFunction::ContainsLabel(*I, true)) 1241 return CSFC_Failure; 1242 return CSFC_Success; 1243 } 1244 } 1245 1246 return Case ? CSFC_Success : CSFC_FallThrough; 1247 } 1248 1249 // Okay, this is some other statement that we don't handle explicitly, like a 1250 // for statement or increment etc. If we are skipping over this statement, 1251 // just verify it doesn't have labels, which would make it invalid to elide. 1252 if (Case) { 1253 if (CodeGenFunction::ContainsLabel(S, true)) 1254 return CSFC_Failure; 1255 return CSFC_Success; 1256 } 1257 1258 // Otherwise, we want to include this statement. Everything is cool with that 1259 // so long as it doesn't contain a break out of the switch we're in. 1260 if (CodeGenFunction::containsBreak(S)) return CSFC_Failure; 1261 1262 // Otherwise, everything is great. Include the statement and tell the caller 1263 // that we fall through and include the next statement as well. 1264 ResultStmts.push_back(S); 1265 return CSFC_FallThrough; 1266 } 1267 1268 /// FindCaseStatementsForValue - Find the case statement being jumped to and 1269 /// then invoke CollectStatementsForCase to find the list of statements to emit 1270 /// for a switch on constant. See the comment above CollectStatementsForCase 1271 /// for more details. 1272 static bool FindCaseStatementsForValue(const SwitchStmt &S, 1273 const llvm::APSInt &ConstantCondValue, 1274 SmallVectorImpl<const Stmt*> &ResultStmts, 1275 ASTContext &C, 1276 const SwitchCase *&ResultCase) { 1277 // First step, find the switch case that is being branched to. We can do this 1278 // efficiently by scanning the SwitchCase list. 1279 const SwitchCase *Case = S.getSwitchCaseList(); 1280 const DefaultStmt *DefaultCase = nullptr; 1281 1282 for (; Case; Case = Case->getNextSwitchCase()) { 1283 // It's either a default or case. Just remember the default statement in 1284 // case we're not jumping to any numbered cases. 1285 if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) { 1286 DefaultCase = DS; 1287 continue; 1288 } 1289 1290 // Check to see if this case is the one we're looking for. 1291 const CaseStmt *CS = cast<CaseStmt>(Case); 1292 // Don't handle case ranges yet. 1293 if (CS->getRHS()) return false; 1294 1295 // If we found our case, remember it as 'case'. 1296 if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue) 1297 break; 1298 } 1299 1300 // If we didn't find a matching case, we use a default if it exists, or we 1301 // elide the whole switch body! 1302 if (!Case) { 1303 // It is safe to elide the body of the switch if it doesn't contain labels 1304 // etc. If it is safe, return successfully with an empty ResultStmts list. 1305 if (!DefaultCase) 1306 return !CodeGenFunction::ContainsLabel(&S); 1307 Case = DefaultCase; 1308 } 1309 1310 // Ok, we know which case is being jumped to, try to collect all the 1311 // statements that follow it. This can fail for a variety of reasons. Also, 1312 // check to see that the recursive walk actually found our case statement. 1313 // Insane cases like this can fail to find it in the recursive walk since we 1314 // don't handle every stmt kind: 1315 // switch (4) { 1316 // while (1) { 1317 // case 4: ... 1318 bool FoundCase = false; 1319 ResultCase = Case; 1320 return CollectStatementsForCase(S.getBody(), Case, FoundCase, 1321 ResultStmts) != CSFC_Failure && 1322 FoundCase; 1323 } 1324 1325 void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { 1326 // Handle nested switch statements. 1327 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; 1328 SmallVector<uint64_t, 16> *SavedSwitchWeights = SwitchWeights; 1329 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; 1330 1331 // See if we can constant fold the condition of the switch and therefore only 1332 // emit the live case statement (if any) of the switch. 1333 llvm::APSInt ConstantCondValue; 1334 if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) { 1335 SmallVector<const Stmt*, 4> CaseStmts; 1336 const SwitchCase *Case = nullptr; 1337 if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts, 1338 getContext(), Case)) { 1339 if (Case) { 1340 RegionCounter CaseCnt = getPGORegionCounter(Case); 1341 CaseCnt.beginRegion(Builder); 1342 } 1343 RunCleanupsScope ExecutedScope(*this); 1344 1345 // Emit the condition variable if needed inside the entire cleanup scope 1346 // used by this special case for constant folded switches. 1347 if (S.getConditionVariable()) 1348 EmitAutoVarDecl(*S.getConditionVariable()); 1349 1350 // At this point, we are no longer "within" a switch instance, so 1351 // we can temporarily enforce this to ensure that any embedded case 1352 // statements are not emitted. 1353 SwitchInsn = nullptr; 1354 1355 // Okay, we can dead code eliminate everything except this case. Emit the 1356 // specified series of statements and we're good. 1357 for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i) 1358 EmitStmt(CaseStmts[i]); 1359 RegionCounter ExitCnt = getPGORegionCounter(&S); 1360 ExitCnt.beginRegion(Builder); 1361 1362 // Now we want to restore the saved switch instance so that nested 1363 // switches continue to function properly 1364 SwitchInsn = SavedSwitchInsn; 1365 1366 return; 1367 } 1368 } 1369 1370 JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog"); 1371 1372 RunCleanupsScope ConditionScope(*this); 1373 if (S.getConditionVariable()) 1374 EmitAutoVarDecl(*S.getConditionVariable()); 1375 llvm::Value *CondV = EmitScalarExpr(S.getCond()); 1376 1377 // Create basic block to hold stuff that comes after switch 1378 // statement. We also need to create a default block now so that 1379 // explicit case ranges tests can have a place to jump to on 1380 // failure. 1381 llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default"); 1382 SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock); 1383 if (PGO.haveRegionCounts()) { 1384 // Walk the SwitchCase list to find how many there are. 1385 uint64_t DefaultCount = 0; 1386 unsigned NumCases = 0; 1387 for (const SwitchCase *Case = S.getSwitchCaseList(); 1388 Case; 1389 Case = Case->getNextSwitchCase()) { 1390 if (isa<DefaultStmt>(Case)) 1391 DefaultCount = getPGORegionCounter(Case).getCount(); 1392 NumCases += 1; 1393 } 1394 SwitchWeights = new SmallVector<uint64_t, 16>(); 1395 SwitchWeights->reserve(NumCases); 1396 // The default needs to be first. We store the edge count, so we already 1397 // know the right weight. 1398 SwitchWeights->push_back(DefaultCount); 1399 } 1400 CaseRangeBlock = DefaultBlock; 1401 1402 // Clear the insertion point to indicate we are in unreachable code. 1403 Builder.ClearInsertionPoint(); 1404 1405 // All break statements jump to NextBlock. If BreakContinueStack is non-empty 1406 // then reuse last ContinueBlock. 1407 JumpDest OuterContinue; 1408 if (!BreakContinueStack.empty()) 1409 OuterContinue = BreakContinueStack.back().ContinueBlock; 1410 1411 BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue)); 1412 1413 // Emit switch body. 1414 EmitStmt(S.getBody()); 1415 1416 BreakContinueStack.pop_back(); 1417 1418 // Update the default block in case explicit case range tests have 1419 // been chained on top. 1420 SwitchInsn->setDefaultDest(CaseRangeBlock); 1421 1422 // If a default was never emitted: 1423 if (!DefaultBlock->getParent()) { 1424 // If we have cleanups, emit the default block so that there's a 1425 // place to jump through the cleanups from. 1426 if (ConditionScope.requiresCleanups()) { 1427 EmitBlock(DefaultBlock); 1428 1429 // Otherwise, just forward the default block to the switch end. 1430 } else { 1431 DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock()); 1432 delete DefaultBlock; 1433 } 1434 } 1435 1436 ConditionScope.ForceCleanup(); 1437 1438 // Emit continuation. 1439 EmitBlock(SwitchExit.getBlock(), true); 1440 RegionCounter ExitCnt = getPGORegionCounter(&S); 1441 ExitCnt.beginRegion(Builder); 1442 1443 if (SwitchWeights) { 1444 assert(SwitchWeights->size() == 1 + SwitchInsn->getNumCases() && 1445 "switch weights do not match switch cases"); 1446 // If there's only one jump destination there's no sense weighting it. 1447 if (SwitchWeights->size() > 1) 1448 SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof, 1449 PGO.createBranchWeights(*SwitchWeights)); 1450 delete SwitchWeights; 1451 } 1452 SwitchInsn = SavedSwitchInsn; 1453 SwitchWeights = SavedSwitchWeights; 1454 CaseRangeBlock = SavedCRBlock; 1455 } 1456 1457 static std::string 1458 SimplifyConstraint(const char *Constraint, const TargetInfo &Target, 1459 SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=nullptr) { 1460 std::string Result; 1461 1462 while (*Constraint) { 1463 switch (*Constraint) { 1464 default: 1465 Result += Target.convertConstraint(Constraint); 1466 break; 1467 // Ignore these 1468 case '*': 1469 case '?': 1470 case '!': 1471 case '=': // Will see this and the following in mult-alt constraints. 1472 case '+': 1473 break; 1474 case '#': // Ignore the rest of the constraint alternative. 1475 while (Constraint[1] && Constraint[1] != ',') 1476 Constraint++; 1477 break; 1478 case ',': 1479 Result += "|"; 1480 break; 1481 case 'g': 1482 Result += "imr"; 1483 break; 1484 case '[': { 1485 assert(OutCons && 1486 "Must pass output names to constraints with a symbolic name"); 1487 unsigned Index; 1488 bool result = Target.resolveSymbolicName(Constraint, 1489 &(*OutCons)[0], 1490 OutCons->size(), Index); 1491 assert(result && "Could not resolve symbolic name"); (void)result; 1492 Result += llvm::utostr(Index); 1493 break; 1494 } 1495 } 1496 1497 Constraint++; 1498 } 1499 1500 return Result; 1501 } 1502 1503 /// AddVariableConstraints - Look at AsmExpr and if it is a variable declared 1504 /// as using a particular register add that as a constraint that will be used 1505 /// in this asm stmt. 1506 static std::string 1507 AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr, 1508 const TargetInfo &Target, CodeGenModule &CGM, 1509 const AsmStmt &Stmt) { 1510 const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr); 1511 if (!AsmDeclRef) 1512 return Constraint; 1513 const ValueDecl &Value = *AsmDeclRef->getDecl(); 1514 const VarDecl *Variable = dyn_cast<VarDecl>(&Value); 1515 if (!Variable) 1516 return Constraint; 1517 if (Variable->getStorageClass() != SC_Register) 1518 return Constraint; 1519 AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>(); 1520 if (!Attr) 1521 return Constraint; 1522 StringRef Register = Attr->getLabel(); 1523 assert(Target.isValidGCCRegisterName(Register)); 1524 // We're using validateOutputConstraint here because we only care if 1525 // this is a register constraint. 1526 TargetInfo::ConstraintInfo Info(Constraint, ""); 1527 if (Target.validateOutputConstraint(Info) && 1528 !Info.allowsRegister()) { 1529 CGM.ErrorUnsupported(&Stmt, "__asm__"); 1530 return Constraint; 1531 } 1532 // Canonicalize the register here before returning it. 1533 Register = Target.getNormalizedGCCRegisterName(Register); 1534 return "{" + Register.str() + "}"; 1535 } 1536 1537 llvm::Value* 1538 CodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info, 1539 LValue InputValue, QualType InputType, 1540 std::string &ConstraintStr, 1541 SourceLocation Loc) { 1542 llvm::Value *Arg; 1543 if (Info.allowsRegister() || !Info.allowsMemory()) { 1544 if (CodeGenFunction::hasScalarEvaluationKind(InputType)) { 1545 Arg = EmitLoadOfLValue(InputValue, Loc).getScalarVal(); 1546 } else { 1547 llvm::Type *Ty = ConvertType(InputType); 1548 uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty); 1549 if (Size <= 64 && llvm::isPowerOf2_64(Size)) { 1550 Ty = llvm::IntegerType::get(getLLVMContext(), Size); 1551 Ty = llvm::PointerType::getUnqual(Ty); 1552 1553 Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(), 1554 Ty)); 1555 } else { 1556 Arg = InputValue.getAddress(); 1557 ConstraintStr += '*'; 1558 } 1559 } 1560 } else { 1561 Arg = InputValue.getAddress(); 1562 ConstraintStr += '*'; 1563 } 1564 1565 return Arg; 1566 } 1567 1568 llvm::Value* CodeGenFunction::EmitAsmInput( 1569 const TargetInfo::ConstraintInfo &Info, 1570 const Expr *InputExpr, 1571 std::string &ConstraintStr) { 1572 if (Info.allowsRegister() || !Info.allowsMemory()) 1573 if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType())) 1574 return EmitScalarExpr(InputExpr); 1575 1576 InputExpr = InputExpr->IgnoreParenNoopCasts(getContext()); 1577 LValue Dest = EmitLValue(InputExpr); 1578 return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr, 1579 InputExpr->getExprLoc()); 1580 } 1581 1582 /// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline 1583 /// asm call instruction. The !srcloc MDNode contains a list of constant 1584 /// integers which are the source locations of the start of each line in the 1585 /// asm. 1586 static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str, 1587 CodeGenFunction &CGF) { 1588 SmallVector<llvm::Value *, 8> Locs; 1589 // Add the location of the first line to the MDNode. 1590 Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 1591 Str->getLocStart().getRawEncoding())); 1592 StringRef StrVal = Str->getString(); 1593 if (!StrVal.empty()) { 1594 const SourceManager &SM = CGF.CGM.getContext().getSourceManager(); 1595 const LangOptions &LangOpts = CGF.CGM.getLangOpts(); 1596 1597 // Add the location of the start of each subsequent line of the asm to the 1598 // MDNode. 1599 for (unsigned i = 0, e = StrVal.size()-1; i != e; ++i) { 1600 if (StrVal[i] != '\n') continue; 1601 SourceLocation LineLoc = Str->getLocationOfByte(i+1, SM, LangOpts, 1602 CGF.getTarget()); 1603 Locs.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 1604 LineLoc.getRawEncoding())); 1605 } 1606 } 1607 1608 return llvm::MDNode::get(CGF.getLLVMContext(), Locs); 1609 } 1610 1611 void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { 1612 // Assemble the final asm string. 1613 std::string AsmString = S.generateAsmString(getContext()); 1614 1615 // Get all the output and input constraints together. 1616 SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; 1617 SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; 1618 1619 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { 1620 StringRef Name; 1621 if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) 1622 Name = GAS->getOutputName(i); 1623 TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name); 1624 bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid; 1625 assert(IsValid && "Failed to parse output constraint"); 1626 OutputConstraintInfos.push_back(Info); 1627 } 1628 1629 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { 1630 StringRef Name; 1631 if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) 1632 Name = GAS->getInputName(i); 1633 TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name); 1634 bool IsValid = 1635 getTarget().validateInputConstraint(OutputConstraintInfos.data(), 1636 S.getNumOutputs(), Info); 1637 assert(IsValid && "Failed to parse input constraint"); (void)IsValid; 1638 InputConstraintInfos.push_back(Info); 1639 } 1640 1641 std::string Constraints; 1642 1643 std::vector<LValue> ResultRegDests; 1644 std::vector<QualType> ResultRegQualTys; 1645 std::vector<llvm::Type *> ResultRegTypes; 1646 std::vector<llvm::Type *> ResultTruncRegTypes; 1647 std::vector<llvm::Type *> ArgTypes; 1648 std::vector<llvm::Value*> Args; 1649 1650 // Keep track of inout constraints. 1651 std::string InOutConstraints; 1652 std::vector<llvm::Value*> InOutArgs; 1653 std::vector<llvm::Type*> InOutArgTypes; 1654 1655 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { 1656 TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; 1657 1658 // Simplify the output constraint. 1659 std::string OutputConstraint(S.getOutputConstraint(i)); 1660 OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, 1661 getTarget()); 1662 1663 const Expr *OutExpr = S.getOutputExpr(i); 1664 OutExpr = OutExpr->IgnoreParenNoopCasts(getContext()); 1665 1666 OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr, 1667 getTarget(), CGM, S); 1668 1669 LValue Dest = EmitLValue(OutExpr); 1670 if (!Constraints.empty()) 1671 Constraints += ','; 1672 1673 // If this is a register output, then make the inline asm return it 1674 // by-value. If this is a memory result, return the value by-reference. 1675 if (!Info.allowsMemory() && hasScalarEvaluationKind(OutExpr->getType())) { 1676 Constraints += "=" + OutputConstraint; 1677 ResultRegQualTys.push_back(OutExpr->getType()); 1678 ResultRegDests.push_back(Dest); 1679 ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType())); 1680 ResultTruncRegTypes.push_back(ResultRegTypes.back()); 1681 1682 // If this output is tied to an input, and if the input is larger, then 1683 // we need to set the actual result type of the inline asm node to be the 1684 // same as the input type. 1685 if (Info.hasMatchingInput()) { 1686 unsigned InputNo; 1687 for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) { 1688 TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo]; 1689 if (Input.hasTiedOperand() && Input.getTiedOperand() == i) 1690 break; 1691 } 1692 assert(InputNo != S.getNumInputs() && "Didn't find matching input!"); 1693 1694 QualType InputTy = S.getInputExpr(InputNo)->getType(); 1695 QualType OutputType = OutExpr->getType(); 1696 1697 uint64_t InputSize = getContext().getTypeSize(InputTy); 1698 if (getContext().getTypeSize(OutputType) < InputSize) { 1699 // Form the asm to return the value as a larger integer or fp type. 1700 ResultRegTypes.back() = ConvertType(InputTy); 1701 } 1702 } 1703 if (llvm::Type* AdjTy = 1704 getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, 1705 ResultRegTypes.back())) 1706 ResultRegTypes.back() = AdjTy; 1707 else { 1708 CGM.getDiags().Report(S.getAsmLoc(), 1709 diag::err_asm_invalid_type_in_input) 1710 << OutExpr->getType() << OutputConstraint; 1711 } 1712 } else { 1713 ArgTypes.push_back(Dest.getAddress()->getType()); 1714 Args.push_back(Dest.getAddress()); 1715 Constraints += "=*"; 1716 Constraints += OutputConstraint; 1717 } 1718 1719 if (Info.isReadWrite()) { 1720 InOutConstraints += ','; 1721 1722 const Expr *InputExpr = S.getOutputExpr(i); 1723 llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(), 1724 InOutConstraints, 1725 InputExpr->getExprLoc()); 1726 1727 if (llvm::Type* AdjTy = 1728 getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, 1729 Arg->getType())) 1730 Arg = Builder.CreateBitCast(Arg, AdjTy); 1731 1732 if (Info.allowsRegister()) 1733 InOutConstraints += llvm::utostr(i); 1734 else 1735 InOutConstraints += OutputConstraint; 1736 1737 InOutArgTypes.push_back(Arg->getType()); 1738 InOutArgs.push_back(Arg); 1739 } 1740 } 1741 1742 unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs(); 1743 1744 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { 1745 const Expr *InputExpr = S.getInputExpr(i); 1746 1747 TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; 1748 1749 if (!Constraints.empty()) 1750 Constraints += ','; 1751 1752 // Simplify the input constraint. 1753 std::string InputConstraint(S.getInputConstraint(i)); 1754 InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(), 1755 &OutputConstraintInfos); 1756 1757 InputConstraint = 1758 AddVariableConstraints(InputConstraint, 1759 *InputExpr->IgnoreParenNoopCasts(getContext()), 1760 getTarget(), CGM, S); 1761 1762 llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints); 1763 1764 // If this input argument is tied to a larger output result, extend the 1765 // input to be the same size as the output. The LLVM backend wants to see 1766 // the input and output of a matching constraint be the same size. Note 1767 // that GCC does not define what the top bits are here. We use zext because 1768 // that is usually cheaper, but LLVM IR should really get an anyext someday. 1769 if (Info.hasTiedOperand()) { 1770 unsigned Output = Info.getTiedOperand(); 1771 QualType OutputType = S.getOutputExpr(Output)->getType(); 1772 QualType InputTy = InputExpr->getType(); 1773 1774 if (getContext().getTypeSize(OutputType) > 1775 getContext().getTypeSize(InputTy)) { 1776 // Use ptrtoint as appropriate so that we can do our extension. 1777 if (isa<llvm::PointerType>(Arg->getType())) 1778 Arg = Builder.CreatePtrToInt(Arg, IntPtrTy); 1779 llvm::Type *OutputTy = ConvertType(OutputType); 1780 if (isa<llvm::IntegerType>(OutputTy)) 1781 Arg = Builder.CreateZExt(Arg, OutputTy); 1782 else if (isa<llvm::PointerType>(OutputTy)) 1783 Arg = Builder.CreateZExt(Arg, IntPtrTy); 1784 else { 1785 assert(OutputTy->isFloatingPointTy() && "Unexpected output type"); 1786 Arg = Builder.CreateFPExt(Arg, OutputTy); 1787 } 1788 } 1789 } 1790 if (llvm::Type* AdjTy = 1791 getTargetHooks().adjustInlineAsmType(*this, InputConstraint, 1792 Arg->getType())) 1793 Arg = Builder.CreateBitCast(Arg, AdjTy); 1794 else 1795 CGM.getDiags().Report(S.getAsmLoc(), diag::err_asm_invalid_type_in_input) 1796 << InputExpr->getType() << InputConstraint; 1797 1798 ArgTypes.push_back(Arg->getType()); 1799 Args.push_back(Arg); 1800 Constraints += InputConstraint; 1801 } 1802 1803 // Append the "input" part of inout constraints last. 1804 for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) { 1805 ArgTypes.push_back(InOutArgTypes[i]); 1806 Args.push_back(InOutArgs[i]); 1807 } 1808 Constraints += InOutConstraints; 1809 1810 // Clobbers 1811 for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) { 1812 StringRef Clobber = S.getClobber(i); 1813 1814 if (Clobber != "memory" && Clobber != "cc") 1815 Clobber = getTarget().getNormalizedGCCRegisterName(Clobber); 1816 1817 if (i != 0 || NumConstraints != 0) 1818 Constraints += ','; 1819 1820 Constraints += "~{"; 1821 Constraints += Clobber; 1822 Constraints += '}'; 1823 } 1824 1825 // Add machine specific clobbers 1826 std::string MachineClobbers = getTarget().getClobbers(); 1827 if (!MachineClobbers.empty()) { 1828 if (!Constraints.empty()) 1829 Constraints += ','; 1830 Constraints += MachineClobbers; 1831 } 1832 1833 llvm::Type *ResultType; 1834 if (ResultRegTypes.empty()) 1835 ResultType = VoidTy; 1836 else if (ResultRegTypes.size() == 1) 1837 ResultType = ResultRegTypes[0]; 1838 else 1839 ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes); 1840 1841 llvm::FunctionType *FTy = 1842 llvm::FunctionType::get(ResultType, ArgTypes, false); 1843 1844 bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0; 1845 llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ? 1846 llvm::InlineAsm::AD_Intel : llvm::InlineAsm::AD_ATT; 1847 llvm::InlineAsm *IA = 1848 llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect, 1849 /* IsAlignStack */ false, AsmDialect); 1850 llvm::CallInst *Result = Builder.CreateCall(IA, Args); 1851 Result->addAttribute(llvm::AttributeSet::FunctionIndex, 1852 llvm::Attribute::NoUnwind); 1853 1854 // Slap the source location of the inline asm into a !srcloc metadata on the 1855 // call. FIXME: Handle metadata for MS-style inline asms. 1856 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S)) 1857 Result->setMetadata("srcloc", getAsmSrcLocInfo(gccAsmStmt->getAsmString(), 1858 *this)); 1859 1860 // Extract all of the register value results from the asm. 1861 std::vector<llvm::Value*> RegResults; 1862 if (ResultRegTypes.size() == 1) { 1863 RegResults.push_back(Result); 1864 } else { 1865 for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) { 1866 llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult"); 1867 RegResults.push_back(Tmp); 1868 } 1869 } 1870 1871 for (unsigned i = 0, e = RegResults.size(); i != e; ++i) { 1872 llvm::Value *Tmp = RegResults[i]; 1873 1874 // If the result type of the LLVM IR asm doesn't match the result type of 1875 // the expression, do the conversion. 1876 if (ResultRegTypes[i] != ResultTruncRegTypes[i]) { 1877 llvm::Type *TruncTy = ResultTruncRegTypes[i]; 1878 1879 // Truncate the integer result to the right size, note that TruncTy can be 1880 // a pointer. 1881 if (TruncTy->isFloatingPointTy()) 1882 Tmp = Builder.CreateFPTrunc(Tmp, TruncTy); 1883 else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) { 1884 uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy); 1885 Tmp = Builder.CreateTrunc(Tmp, 1886 llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize)); 1887 Tmp = Builder.CreateIntToPtr(Tmp, TruncTy); 1888 } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) { 1889 uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType()); 1890 Tmp = Builder.CreatePtrToInt(Tmp, 1891 llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize)); 1892 Tmp = Builder.CreateTrunc(Tmp, TruncTy); 1893 } else if (TruncTy->isIntegerTy()) { 1894 Tmp = Builder.CreateTrunc(Tmp, TruncTy); 1895 } else if (TruncTy->isVectorTy()) { 1896 Tmp = Builder.CreateBitCast(Tmp, TruncTy); 1897 } 1898 } 1899 1900 EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]); 1901 } 1902 } 1903 1904 static LValue InitCapturedStruct(CodeGenFunction &CGF, const CapturedStmt &S) { 1905 const RecordDecl *RD = S.getCapturedRecordDecl(); 1906 QualType RecordTy = CGF.getContext().getRecordType(RD); 1907 1908 // Initialize the captured struct. 1909 LValue SlotLV = CGF.MakeNaturalAlignAddrLValue( 1910 CGF.CreateMemTemp(RecordTy, "agg.captured"), RecordTy); 1911 1912 RecordDecl::field_iterator CurField = RD->field_begin(); 1913 for (CapturedStmt::capture_init_iterator I = S.capture_init_begin(), 1914 E = S.capture_init_end(); 1915 I != E; ++I, ++CurField) { 1916 LValue LV = CGF.EmitLValueForFieldInitialization(SlotLV, *CurField); 1917 CGF.EmitInitializerForField(*CurField, LV, *I, ArrayRef<VarDecl *>()); 1918 } 1919 1920 return SlotLV; 1921 } 1922 1923 /// Generate an outlined function for the body of a CapturedStmt, store any 1924 /// captured variables into the captured struct, and call the outlined function. 1925 llvm::Function * 1926 CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) { 1927 const CapturedDecl *CD = S.getCapturedDecl(); 1928 const RecordDecl *RD = S.getCapturedRecordDecl(); 1929 assert(CD->hasBody() && "missing CapturedDecl body"); 1930 1931 LValue CapStruct = InitCapturedStruct(*this, S); 1932 1933 // Emit the CapturedDecl 1934 CodeGenFunction CGF(CGM, true); 1935 CGF.CapturedStmtInfo = new CGCapturedStmtInfo(S, K); 1936 llvm::Function *F = CGF.GenerateCapturedStmtFunction(CD, RD, S.getLocStart()); 1937 delete CGF.CapturedStmtInfo; 1938 1939 // Emit call to the helper function. 1940 EmitCallOrInvoke(F, CapStruct.getAddress()); 1941 1942 return F; 1943 } 1944 1945 llvm::Value * 1946 CodeGenFunction::GenerateCapturedStmtArgument(const CapturedStmt &S) { 1947 LValue CapStruct = InitCapturedStruct(*this, S); 1948 return CapStruct.getAddress(); 1949 } 1950 1951 /// Creates the outlined function for a CapturedStmt. 1952 llvm::Function * 1953 CodeGenFunction::GenerateCapturedStmtFunction(const CapturedDecl *CD, 1954 const RecordDecl *RD, 1955 SourceLocation Loc) { 1956 assert(CapturedStmtInfo && 1957 "CapturedStmtInfo should be set when generating the captured function"); 1958 1959 // Build the argument list. 1960 ASTContext &Ctx = CGM.getContext(); 1961 FunctionArgList Args; 1962 Args.append(CD->param_begin(), CD->param_end()); 1963 1964 // Create the function declaration. 1965 FunctionType::ExtInfo ExtInfo; 1966 const CGFunctionInfo &FuncInfo = 1967 CGM.getTypes().arrangeFreeFunctionDeclaration(Ctx.VoidTy, Args, ExtInfo, 1968 /*IsVariadic=*/false); 1969 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); 1970 1971 llvm::Function *F = 1972 llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, 1973 CapturedStmtInfo->getHelperName(), &CGM.getModule()); 1974 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); 1975 1976 // Generate the function. 1977 StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, 1978 CD->getLocation(), 1979 CD->getBody()->getLocStart()); 1980 1981 // Set the context parameter in CapturedStmtInfo. 1982 llvm::Value *DeclPtr = LocalDeclMap[CD->getContextParam()]; 1983 assert(DeclPtr && "missing context parameter for CapturedStmt"); 1984 CapturedStmtInfo->setContextValue(Builder.CreateLoad(DeclPtr)); 1985 1986 // If 'this' is captured, load it into CXXThisValue. 1987 if (CapturedStmtInfo->isCXXThisExprCaptured()) { 1988 FieldDecl *FD = CapturedStmtInfo->getThisFieldDecl(); 1989 LValue LV = MakeNaturalAlignAddrLValue(CapturedStmtInfo->getContextValue(), 1990 Ctx.getTagDeclType(RD)); 1991 LValue ThisLValue = EmitLValueForField(LV, FD); 1992 CXXThisValue = EmitLoadOfLValue(ThisLValue, Loc).getScalarVal(); 1993 } 1994 1995 PGO.assignRegionCounters(CD, F); 1996 CapturedStmtInfo->EmitBody(*this, CD->getBody()); 1997 FinishFunction(CD->getBodyRBrace()); 1998 PGO.emitInstrumentationData(); 1999 PGO.destroyRegionCounters(); 2000 2001 return F; 2002 } 2003