1 //===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This contains code to emit Stmt nodes as LLVM code. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CGDebugInfo.h" 14 #include "CGOpenMPRuntime.h" 15 #include "CodeGenFunction.h" 16 #include "CodeGenModule.h" 17 #include "TargetInfo.h" 18 #include "clang/AST/Attr.h" 19 #include "clang/AST/Expr.h" 20 #include "clang/AST/Stmt.h" 21 #include "clang/AST/StmtVisitor.h" 22 #include "clang/Basic/Builtins.h" 23 #include "clang/Basic/DiagnosticSema.h" 24 #include "clang/Basic/PrettyStackTrace.h" 25 #include "clang/Basic/SourceManager.h" 26 #include "clang/Basic/TargetInfo.h" 27 #include "llvm/ADT/SmallSet.h" 28 #include "llvm/ADT/StringExtras.h" 29 #include "llvm/IR/Assumptions.h" 30 #include "llvm/IR/DataLayout.h" 31 #include "llvm/IR/InlineAsm.h" 32 #include "llvm/IR/Intrinsics.h" 33 #include "llvm/IR/MDBuilder.h" 34 #include "llvm/Support/SaveAndRestore.h" 35 36 using namespace clang; 37 using namespace CodeGen; 38 39 //===----------------------------------------------------------------------===// 40 // Statement Emission 41 //===----------------------------------------------------------------------===// 42 43 void CodeGenFunction::EmitStopPoint(const Stmt *S) { 44 if (CGDebugInfo *DI = getDebugInfo()) { 45 SourceLocation Loc; 46 Loc = S->getBeginLoc(); 47 DI->EmitLocation(Builder, Loc); 48 49 LastStopPoint = Loc; 50 } 51 } 52 53 void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) { 54 assert(S && "Null statement?"); 55 PGO.setCurrentStmt(S); 56 57 // These statements have their own debug info handling. 58 if (EmitSimpleStmt(S, Attrs)) 59 return; 60 61 // Check if we are generating unreachable code. 62 if (!HaveInsertPoint()) { 63 // If so, and the statement doesn't contain a label, then we do not need to 64 // generate actual code. This is safe because (1) the current point is 65 // unreachable, so we don't need to execute the code, and (2) we've already 66 // handled the statements which update internal data structures (like the 67 // local variable map) which could be used by subsequent statements. 68 if (!ContainsLabel(S)) { 69 // Verify that any decl statements were handled as simple, they may be in 70 // scope of subsequent reachable statements. 71 assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!"); 72 return; 73 } 74 75 // Otherwise, make a new block to hold the code. 76 EnsureInsertPoint(); 77 } 78 79 // Generate a stoppoint if we are emitting debug info. 80 EmitStopPoint(S); 81 82 // Ignore all OpenMP directives except for simd if OpenMP with Simd is 83 // enabled. 84 if (getLangOpts().OpenMP && getLangOpts().OpenMPSimd) { 85 if (const auto *D = dyn_cast<OMPExecutableDirective>(S)) { 86 EmitSimpleOMPExecutableDirective(*D); 87 return; 88 } 89 } 90 91 switch (S->getStmtClass()) { 92 case Stmt::NoStmtClass: 93 case Stmt::CXXCatchStmtClass: 94 case Stmt::SEHExceptStmtClass: 95 case Stmt::SEHFinallyStmtClass: 96 case Stmt::MSDependentExistsStmtClass: 97 llvm_unreachable("invalid statement class to emit generically"); 98 case Stmt::NullStmtClass: 99 case Stmt::CompoundStmtClass: 100 case Stmt::DeclStmtClass: 101 case Stmt::LabelStmtClass: 102 case Stmt::AttributedStmtClass: 103 case Stmt::GotoStmtClass: 104 case Stmt::BreakStmtClass: 105 case Stmt::ContinueStmtClass: 106 case Stmt::DefaultStmtClass: 107 case Stmt::CaseStmtClass: 108 case Stmt::SEHLeaveStmtClass: 109 llvm_unreachable("should have emitted these statements as simple"); 110 111 #define STMT(Type, Base) 112 #define ABSTRACT_STMT(Op) 113 #define EXPR(Type, Base) \ 114 case Stmt::Type##Class: 115 #include "clang/AST/StmtNodes.inc" 116 { 117 // Remember the block we came in on. 118 llvm::BasicBlock *incoming = Builder.GetInsertBlock(); 119 assert(incoming && "expression emission must have an insertion point"); 120 121 EmitIgnoredExpr(cast<Expr>(S)); 122 123 llvm::BasicBlock *outgoing = Builder.GetInsertBlock(); 124 assert(outgoing && "expression emission cleared block!"); 125 126 // The expression emitters assume (reasonably!) that the insertion 127 // point is always set. To maintain that, the call-emission code 128 // for noreturn functions has to enter a new block with no 129 // predecessors. We want to kill that block and mark the current 130 // insertion point unreachable in the common case of a call like 131 // "exit();". Since expression emission doesn't otherwise create 132 // blocks with no predecessors, we can just test for that. 133 // However, we must be careful not to do this to our incoming 134 // block, because *statement* emission does sometimes create 135 // reachable blocks which will have no predecessors until later in 136 // the function. This occurs with, e.g., labels that are not 137 // reachable by fallthrough. 138 if (incoming != outgoing && outgoing->use_empty()) { 139 outgoing->eraseFromParent(); 140 Builder.ClearInsertionPoint(); 141 } 142 break; 143 } 144 145 case Stmt::IndirectGotoStmtClass: 146 EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break; 147 148 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; 149 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S), Attrs); break; 150 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S), Attrs); break; 151 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S), Attrs); break; 152 153 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; 154 155 case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; 156 case Stmt::GCCAsmStmtClass: // Intentional fall-through. 157 case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; 158 case Stmt::CoroutineBodyStmtClass: 159 EmitCoroutineBody(cast<CoroutineBodyStmt>(*S)); 160 break; 161 case Stmt::CoreturnStmtClass: 162 EmitCoreturnStmt(cast<CoreturnStmt>(*S)); 163 break; 164 case Stmt::CapturedStmtClass: { 165 const CapturedStmt *CS = cast<CapturedStmt>(S); 166 EmitCapturedStmt(*CS, CS->getCapturedRegionKind()); 167 } 168 break; 169 case Stmt::ObjCAtTryStmtClass: 170 EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S)); 171 break; 172 case Stmt::ObjCAtCatchStmtClass: 173 llvm_unreachable( 174 "@catch statements should be handled by EmitObjCAtTryStmt"); 175 case Stmt::ObjCAtFinallyStmtClass: 176 llvm_unreachable( 177 "@finally statements should be handled by EmitObjCAtTryStmt"); 178 case Stmt::ObjCAtThrowStmtClass: 179 EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S)); 180 break; 181 case Stmt::ObjCAtSynchronizedStmtClass: 182 EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S)); 183 break; 184 case Stmt::ObjCForCollectionStmtClass: 185 EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S)); 186 break; 187 case Stmt::ObjCAutoreleasePoolStmtClass: 188 EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S)); 189 break; 190 191 case Stmt::CXXTryStmtClass: 192 EmitCXXTryStmt(cast<CXXTryStmt>(*S)); 193 break; 194 case Stmt::CXXForRangeStmtClass: 195 EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S), Attrs); 196 break; 197 case Stmt::SEHTryStmtClass: 198 EmitSEHTryStmt(cast<SEHTryStmt>(*S)); 199 break; 200 case Stmt::OMPMetaDirectiveClass: 201 EmitOMPMetaDirective(cast<OMPMetaDirective>(*S)); 202 break; 203 case Stmt::OMPCanonicalLoopClass: 204 EmitOMPCanonicalLoop(cast<OMPCanonicalLoop>(S)); 205 break; 206 case Stmt::OMPParallelDirectiveClass: 207 EmitOMPParallelDirective(cast<OMPParallelDirective>(*S)); 208 break; 209 case Stmt::OMPSimdDirectiveClass: 210 EmitOMPSimdDirective(cast<OMPSimdDirective>(*S)); 211 break; 212 case Stmt::OMPTileDirectiveClass: 213 EmitOMPTileDirective(cast<OMPTileDirective>(*S)); 214 break; 215 case Stmt::OMPUnrollDirectiveClass: 216 EmitOMPUnrollDirective(cast<OMPUnrollDirective>(*S)); 217 break; 218 case Stmt::OMPForDirectiveClass: 219 EmitOMPForDirective(cast<OMPForDirective>(*S)); 220 break; 221 case Stmt::OMPForSimdDirectiveClass: 222 EmitOMPForSimdDirective(cast<OMPForSimdDirective>(*S)); 223 break; 224 case Stmt::OMPSectionsDirectiveClass: 225 EmitOMPSectionsDirective(cast<OMPSectionsDirective>(*S)); 226 break; 227 case Stmt::OMPSectionDirectiveClass: 228 EmitOMPSectionDirective(cast<OMPSectionDirective>(*S)); 229 break; 230 case Stmt::OMPSingleDirectiveClass: 231 EmitOMPSingleDirective(cast<OMPSingleDirective>(*S)); 232 break; 233 case Stmt::OMPMasterDirectiveClass: 234 EmitOMPMasterDirective(cast<OMPMasterDirective>(*S)); 235 break; 236 case Stmt::OMPCriticalDirectiveClass: 237 EmitOMPCriticalDirective(cast<OMPCriticalDirective>(*S)); 238 break; 239 case Stmt::OMPParallelForDirectiveClass: 240 EmitOMPParallelForDirective(cast<OMPParallelForDirective>(*S)); 241 break; 242 case Stmt::OMPParallelForSimdDirectiveClass: 243 EmitOMPParallelForSimdDirective(cast<OMPParallelForSimdDirective>(*S)); 244 break; 245 case Stmt::OMPParallelMasterDirectiveClass: 246 EmitOMPParallelMasterDirective(cast<OMPParallelMasterDirective>(*S)); 247 break; 248 case Stmt::OMPParallelSectionsDirectiveClass: 249 EmitOMPParallelSectionsDirective(cast<OMPParallelSectionsDirective>(*S)); 250 break; 251 case Stmt::OMPTaskDirectiveClass: 252 EmitOMPTaskDirective(cast<OMPTaskDirective>(*S)); 253 break; 254 case Stmt::OMPTaskyieldDirectiveClass: 255 EmitOMPTaskyieldDirective(cast<OMPTaskyieldDirective>(*S)); 256 break; 257 case Stmt::OMPBarrierDirectiveClass: 258 EmitOMPBarrierDirective(cast<OMPBarrierDirective>(*S)); 259 break; 260 case Stmt::OMPTaskwaitDirectiveClass: 261 EmitOMPTaskwaitDirective(cast<OMPTaskwaitDirective>(*S)); 262 break; 263 case Stmt::OMPTaskgroupDirectiveClass: 264 EmitOMPTaskgroupDirective(cast<OMPTaskgroupDirective>(*S)); 265 break; 266 case Stmt::OMPFlushDirectiveClass: 267 EmitOMPFlushDirective(cast<OMPFlushDirective>(*S)); 268 break; 269 case Stmt::OMPDepobjDirectiveClass: 270 EmitOMPDepobjDirective(cast<OMPDepobjDirective>(*S)); 271 break; 272 case Stmt::OMPScanDirectiveClass: 273 EmitOMPScanDirective(cast<OMPScanDirective>(*S)); 274 break; 275 case Stmt::OMPOrderedDirectiveClass: 276 EmitOMPOrderedDirective(cast<OMPOrderedDirective>(*S)); 277 break; 278 case Stmt::OMPAtomicDirectiveClass: 279 EmitOMPAtomicDirective(cast<OMPAtomicDirective>(*S)); 280 break; 281 case Stmt::OMPTargetDirectiveClass: 282 EmitOMPTargetDirective(cast<OMPTargetDirective>(*S)); 283 break; 284 case Stmt::OMPTeamsDirectiveClass: 285 EmitOMPTeamsDirective(cast<OMPTeamsDirective>(*S)); 286 break; 287 case Stmt::OMPCancellationPointDirectiveClass: 288 EmitOMPCancellationPointDirective(cast<OMPCancellationPointDirective>(*S)); 289 break; 290 case Stmt::OMPCancelDirectiveClass: 291 EmitOMPCancelDirective(cast<OMPCancelDirective>(*S)); 292 break; 293 case Stmt::OMPTargetDataDirectiveClass: 294 EmitOMPTargetDataDirective(cast<OMPTargetDataDirective>(*S)); 295 break; 296 case Stmt::OMPTargetEnterDataDirectiveClass: 297 EmitOMPTargetEnterDataDirective(cast<OMPTargetEnterDataDirective>(*S)); 298 break; 299 case Stmt::OMPTargetExitDataDirectiveClass: 300 EmitOMPTargetExitDataDirective(cast<OMPTargetExitDataDirective>(*S)); 301 break; 302 case Stmt::OMPTargetParallelDirectiveClass: 303 EmitOMPTargetParallelDirective(cast<OMPTargetParallelDirective>(*S)); 304 break; 305 case Stmt::OMPTargetParallelForDirectiveClass: 306 EmitOMPTargetParallelForDirective(cast<OMPTargetParallelForDirective>(*S)); 307 break; 308 case Stmt::OMPTaskLoopDirectiveClass: 309 EmitOMPTaskLoopDirective(cast<OMPTaskLoopDirective>(*S)); 310 break; 311 case Stmt::OMPTaskLoopSimdDirectiveClass: 312 EmitOMPTaskLoopSimdDirective(cast<OMPTaskLoopSimdDirective>(*S)); 313 break; 314 case Stmt::OMPMasterTaskLoopDirectiveClass: 315 EmitOMPMasterTaskLoopDirective(cast<OMPMasterTaskLoopDirective>(*S)); 316 break; 317 case Stmt::OMPMasterTaskLoopSimdDirectiveClass: 318 EmitOMPMasterTaskLoopSimdDirective( 319 cast<OMPMasterTaskLoopSimdDirective>(*S)); 320 break; 321 case Stmt::OMPParallelMasterTaskLoopDirectiveClass: 322 EmitOMPParallelMasterTaskLoopDirective( 323 cast<OMPParallelMasterTaskLoopDirective>(*S)); 324 break; 325 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass: 326 EmitOMPParallelMasterTaskLoopSimdDirective( 327 cast<OMPParallelMasterTaskLoopSimdDirective>(*S)); 328 break; 329 case Stmt::OMPDistributeDirectiveClass: 330 EmitOMPDistributeDirective(cast<OMPDistributeDirective>(*S)); 331 break; 332 case Stmt::OMPTargetUpdateDirectiveClass: 333 EmitOMPTargetUpdateDirective(cast<OMPTargetUpdateDirective>(*S)); 334 break; 335 case Stmt::OMPDistributeParallelForDirectiveClass: 336 EmitOMPDistributeParallelForDirective( 337 cast<OMPDistributeParallelForDirective>(*S)); 338 break; 339 case Stmt::OMPDistributeParallelForSimdDirectiveClass: 340 EmitOMPDistributeParallelForSimdDirective( 341 cast<OMPDistributeParallelForSimdDirective>(*S)); 342 break; 343 case Stmt::OMPDistributeSimdDirectiveClass: 344 EmitOMPDistributeSimdDirective(cast<OMPDistributeSimdDirective>(*S)); 345 break; 346 case Stmt::OMPTargetParallelForSimdDirectiveClass: 347 EmitOMPTargetParallelForSimdDirective( 348 cast<OMPTargetParallelForSimdDirective>(*S)); 349 break; 350 case Stmt::OMPTargetSimdDirectiveClass: 351 EmitOMPTargetSimdDirective(cast<OMPTargetSimdDirective>(*S)); 352 break; 353 case Stmt::OMPTeamsDistributeDirectiveClass: 354 EmitOMPTeamsDistributeDirective(cast<OMPTeamsDistributeDirective>(*S)); 355 break; 356 case Stmt::OMPTeamsDistributeSimdDirectiveClass: 357 EmitOMPTeamsDistributeSimdDirective( 358 cast<OMPTeamsDistributeSimdDirective>(*S)); 359 break; 360 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass: 361 EmitOMPTeamsDistributeParallelForSimdDirective( 362 cast<OMPTeamsDistributeParallelForSimdDirective>(*S)); 363 break; 364 case Stmt::OMPTeamsDistributeParallelForDirectiveClass: 365 EmitOMPTeamsDistributeParallelForDirective( 366 cast<OMPTeamsDistributeParallelForDirective>(*S)); 367 break; 368 case Stmt::OMPTargetTeamsDirectiveClass: 369 EmitOMPTargetTeamsDirective(cast<OMPTargetTeamsDirective>(*S)); 370 break; 371 case Stmt::OMPTargetTeamsDistributeDirectiveClass: 372 EmitOMPTargetTeamsDistributeDirective( 373 cast<OMPTargetTeamsDistributeDirective>(*S)); 374 break; 375 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass: 376 EmitOMPTargetTeamsDistributeParallelForDirective( 377 cast<OMPTargetTeamsDistributeParallelForDirective>(*S)); 378 break; 379 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass: 380 EmitOMPTargetTeamsDistributeParallelForSimdDirective( 381 cast<OMPTargetTeamsDistributeParallelForSimdDirective>(*S)); 382 break; 383 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass: 384 EmitOMPTargetTeamsDistributeSimdDirective( 385 cast<OMPTargetTeamsDistributeSimdDirective>(*S)); 386 break; 387 case Stmt::OMPInteropDirectiveClass: 388 EmitOMPInteropDirective(cast<OMPInteropDirective>(*S)); 389 break; 390 case Stmt::OMPDispatchDirectiveClass: 391 llvm_unreachable("Dispatch directive not supported yet."); 392 break; 393 case Stmt::OMPMaskedDirectiveClass: 394 EmitOMPMaskedDirective(cast<OMPMaskedDirective>(*S)); 395 break; 396 case Stmt::OMPGenericLoopDirectiveClass: 397 EmitOMPGenericLoopDirective(cast<OMPGenericLoopDirective>(*S)); 398 break; 399 case Stmt::OMPTeamsGenericLoopDirectiveClass: 400 llvm_unreachable("teams loop directive not supported yet."); 401 break; 402 case Stmt::OMPTargetTeamsGenericLoopDirectiveClass: 403 llvm_unreachable("target teams loop directive not supported yet."); 404 break; 405 case Stmt::OMPParallelGenericLoopDirectiveClass: 406 llvm_unreachable("parallel loop directive not supported yet."); 407 break; 408 } 409 } 410 411 bool CodeGenFunction::EmitSimpleStmt(const Stmt *S, 412 ArrayRef<const Attr *> Attrs) { 413 switch (S->getStmtClass()) { 414 default: 415 return false; 416 case Stmt::NullStmtClass: 417 break; 418 case Stmt::CompoundStmtClass: 419 EmitCompoundStmt(cast<CompoundStmt>(*S)); 420 break; 421 case Stmt::DeclStmtClass: 422 EmitDeclStmt(cast<DeclStmt>(*S)); 423 break; 424 case Stmt::LabelStmtClass: 425 EmitLabelStmt(cast<LabelStmt>(*S)); 426 break; 427 case Stmt::AttributedStmtClass: 428 EmitAttributedStmt(cast<AttributedStmt>(*S)); 429 break; 430 case Stmt::GotoStmtClass: 431 EmitGotoStmt(cast<GotoStmt>(*S)); 432 break; 433 case Stmt::BreakStmtClass: 434 EmitBreakStmt(cast<BreakStmt>(*S)); 435 break; 436 case Stmt::ContinueStmtClass: 437 EmitContinueStmt(cast<ContinueStmt>(*S)); 438 break; 439 case Stmt::DefaultStmtClass: 440 EmitDefaultStmt(cast<DefaultStmt>(*S), Attrs); 441 break; 442 case Stmt::CaseStmtClass: 443 EmitCaseStmt(cast<CaseStmt>(*S), Attrs); 444 break; 445 case Stmt::SEHLeaveStmtClass: 446 EmitSEHLeaveStmt(cast<SEHLeaveStmt>(*S)); 447 break; 448 } 449 return true; 450 } 451 452 /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true, 453 /// this captures the expression result of the last sub-statement and returns it 454 /// (for use by the statement expression extension). 455 Address CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, 456 AggValueSlot AggSlot) { 457 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(), 458 "LLVM IR generation of compound statement ('{}')"); 459 460 // Keep track of the current cleanup stack depth, including debug scopes. 461 LexicalScope Scope(*this, S.getSourceRange()); 462 463 return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot); 464 } 465 466 Address 467 CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S, 468 bool GetLast, 469 AggValueSlot AggSlot) { 470 471 const Stmt *ExprResult = S.getStmtExprResult(); 472 assert((!GetLast || (GetLast && ExprResult)) && 473 "If GetLast is true then the CompoundStmt must have a StmtExprResult"); 474 475 Address RetAlloca = Address::invalid(); 476 477 for (auto *CurStmt : S.body()) { 478 if (GetLast && ExprResult == CurStmt) { 479 // We have to special case labels here. They are statements, but when put 480 // at the end of a statement expression, they yield the value of their 481 // subexpression. Handle this by walking through all labels we encounter, 482 // emitting them before we evaluate the subexpr. 483 // Similar issues arise for attributed statements. 484 while (!isa<Expr>(ExprResult)) { 485 if (const auto *LS = dyn_cast<LabelStmt>(ExprResult)) { 486 EmitLabel(LS->getDecl()); 487 ExprResult = LS->getSubStmt(); 488 } else if (const auto *AS = dyn_cast<AttributedStmt>(ExprResult)) { 489 // FIXME: Update this if we ever have attributes that affect the 490 // semantics of an expression. 491 ExprResult = AS->getSubStmt(); 492 } else { 493 llvm_unreachable("unknown value statement"); 494 } 495 } 496 497 EnsureInsertPoint(); 498 499 const Expr *E = cast<Expr>(ExprResult); 500 QualType ExprTy = E->getType(); 501 if (hasAggregateEvaluationKind(ExprTy)) { 502 EmitAggExpr(E, AggSlot); 503 } else { 504 // We can't return an RValue here because there might be cleanups at 505 // the end of the StmtExpr. Because of that, we have to emit the result 506 // here into a temporary alloca. 507 RetAlloca = CreateMemTemp(ExprTy); 508 EmitAnyExprToMem(E, RetAlloca, Qualifiers(), 509 /*IsInit*/ false); 510 } 511 } else { 512 EmitStmt(CurStmt); 513 } 514 } 515 516 return RetAlloca; 517 } 518 519 void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) { 520 llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator()); 521 522 // If there is a cleanup stack, then we it isn't worth trying to 523 // simplify this block (we would need to remove it from the scope map 524 // and cleanup entry). 525 if (!EHStack.empty()) 526 return; 527 528 // Can only simplify direct branches. 529 if (!BI || !BI->isUnconditional()) 530 return; 531 532 // Can only simplify empty blocks. 533 if (BI->getIterator() != BB->begin()) 534 return; 535 536 BB->replaceAllUsesWith(BI->getSuccessor(0)); 537 BI->eraseFromParent(); 538 BB->eraseFromParent(); 539 } 540 541 void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) { 542 llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 543 544 // Fall out of the current block (if necessary). 545 EmitBranch(BB); 546 547 if (IsFinished && BB->use_empty()) { 548 delete BB; 549 return; 550 } 551 552 // Place the block after the current block, if possible, or else at 553 // the end of the function. 554 if (CurBB && CurBB->getParent()) 555 CurFn->getBasicBlockList().insertAfter(CurBB->getIterator(), BB); 556 else 557 CurFn->getBasicBlockList().push_back(BB); 558 Builder.SetInsertPoint(BB); 559 } 560 561 void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) { 562 // Emit a branch from the current block to the target one if this 563 // was a real block. If this was just a fall-through block after a 564 // terminator, don't emit it. 565 llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 566 567 if (!CurBB || CurBB->getTerminator()) { 568 // If there is no insert point or the previous block is already 569 // terminated, don't touch it. 570 } else { 571 // Otherwise, create a fall-through branch. 572 Builder.CreateBr(Target); 573 } 574 575 Builder.ClearInsertionPoint(); 576 } 577 578 void CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) { 579 bool inserted = false; 580 for (llvm::User *u : block->users()) { 581 if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(u)) { 582 CurFn->getBasicBlockList().insertAfter(insn->getParent()->getIterator(), 583 block); 584 inserted = true; 585 break; 586 } 587 } 588 589 if (!inserted) 590 CurFn->getBasicBlockList().push_back(block); 591 592 Builder.SetInsertPoint(block); 593 } 594 595 CodeGenFunction::JumpDest 596 CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) { 597 JumpDest &Dest = LabelMap[D]; 598 if (Dest.isValid()) return Dest; 599 600 // Create, but don't insert, the new block. 601 Dest = JumpDest(createBasicBlock(D->getName()), 602 EHScopeStack::stable_iterator::invalid(), 603 NextCleanupDestIndex++); 604 return Dest; 605 } 606 607 void CodeGenFunction::EmitLabel(const LabelDecl *D) { 608 // Add this label to the current lexical scope if we're within any 609 // normal cleanups. Jumps "in" to this label --- when permitted by 610 // the language --- may need to be routed around such cleanups. 611 if (EHStack.hasNormalCleanups() && CurLexicalScope) 612 CurLexicalScope->addLabel(D); 613 614 JumpDest &Dest = LabelMap[D]; 615 616 // If we didn't need a forward reference to this label, just go 617 // ahead and create a destination at the current scope. 618 if (!Dest.isValid()) { 619 Dest = getJumpDestInCurrentScope(D->getName()); 620 621 // Otherwise, we need to give this label a target depth and remove 622 // it from the branch-fixups list. 623 } else { 624 assert(!Dest.getScopeDepth().isValid() && "already emitted label!"); 625 Dest.setScopeDepth(EHStack.stable_begin()); 626 ResolveBranchFixups(Dest.getBlock()); 627 } 628 629 EmitBlock(Dest.getBlock()); 630 631 // Emit debug info for labels. 632 if (CGDebugInfo *DI = getDebugInfo()) { 633 if (CGM.getCodeGenOpts().hasReducedDebugInfo()) { 634 DI->setLocation(D->getLocation()); 635 DI->EmitLabel(D, Builder); 636 } 637 } 638 639 incrementProfileCounter(D->getStmt()); 640 } 641 642 /// Change the cleanup scope of the labels in this lexical scope to 643 /// match the scope of the enclosing context. 644 void CodeGenFunction::LexicalScope::rescopeLabels() { 645 assert(!Labels.empty()); 646 EHScopeStack::stable_iterator innermostScope 647 = CGF.EHStack.getInnermostNormalCleanup(); 648 649 // Change the scope depth of all the labels. 650 for (SmallVectorImpl<const LabelDecl*>::const_iterator 651 i = Labels.begin(), e = Labels.end(); i != e; ++i) { 652 assert(CGF.LabelMap.count(*i)); 653 JumpDest &dest = CGF.LabelMap.find(*i)->second; 654 assert(dest.getScopeDepth().isValid()); 655 assert(innermostScope.encloses(dest.getScopeDepth())); 656 dest.setScopeDepth(innermostScope); 657 } 658 659 // Reparent the labels if the new scope also has cleanups. 660 if (innermostScope != EHScopeStack::stable_end() && ParentScope) { 661 ParentScope->Labels.append(Labels.begin(), Labels.end()); 662 } 663 } 664 665 666 void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { 667 EmitLabel(S.getDecl()); 668 669 // IsEHa - emit eha.scope.begin if it's a side entry of a scope 670 if (getLangOpts().EHAsynch && S.isSideEntry()) 671 EmitSehCppScopeBegin(); 672 673 EmitStmt(S.getSubStmt()); 674 } 675 676 void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { 677 bool nomerge = false; 678 bool noinline = false; 679 bool alwaysinline = false; 680 const CallExpr *musttail = nullptr; 681 682 for (const auto *A : S.getAttrs()) { 683 switch (A->getKind()) { 684 default: 685 break; 686 case attr::NoMerge: 687 nomerge = true; 688 break; 689 case attr::NoInline: 690 noinline = true; 691 break; 692 case attr::AlwaysInline: 693 alwaysinline = true; 694 break; 695 case attr::MustTail: 696 const Stmt *Sub = S.getSubStmt(); 697 const ReturnStmt *R = cast<ReturnStmt>(Sub); 698 musttail = cast<CallExpr>(R->getRetValue()->IgnoreParens()); 699 break; 700 } 701 } 702 SaveAndRestore<bool> save_nomerge(InNoMergeAttributedStmt, nomerge); 703 SaveAndRestore<bool> save_noinline(InNoInlineAttributedStmt, noinline); 704 SaveAndRestore<bool> save_alwaysinline(InAlwaysInlineAttributedStmt, 705 alwaysinline); 706 SaveAndRestore<const CallExpr *> save_musttail(MustTailCall, musttail); 707 EmitStmt(S.getSubStmt(), S.getAttrs()); 708 } 709 710 void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { 711 // If this code is reachable then emit a stop point (if generating 712 // debug info). We have to do this ourselves because we are on the 713 // "simple" statement path. 714 if (HaveInsertPoint()) 715 EmitStopPoint(&S); 716 717 EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel())); 718 } 719 720 721 void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { 722 if (const LabelDecl *Target = S.getConstantTarget()) { 723 EmitBranchThroughCleanup(getJumpDestForLabel(Target)); 724 return; 725 } 726 727 // Ensure that we have an i8* for our PHI node. 728 llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()), 729 Int8PtrTy, "addr"); 730 llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 731 732 // Get the basic block for the indirect goto. 733 llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock(); 734 735 // The first instruction in the block has to be the PHI for the switch dest, 736 // add an entry for this branch. 737 cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB); 738 739 EmitBranch(IndGotoBB); 740 } 741 742 void CodeGenFunction::EmitIfStmt(const IfStmt &S) { 743 // The else branch of a consteval if statement is always the only branch that 744 // can be runtime evaluated. 745 if (S.isConsteval()) { 746 const Stmt *Executed = S.isNegatedConsteval() ? S.getThen() : S.getElse(); 747 if (Executed) { 748 RunCleanupsScope ExecutedScope(*this); 749 EmitStmt(Executed); 750 } 751 return; 752 } 753 754 // C99 6.8.4.1: The first substatement is executed if the expression compares 755 // unequal to 0. The condition must be a scalar type. 756 LexicalScope ConditionScope(*this, S.getCond()->getSourceRange()); 757 758 if (S.getInit()) 759 EmitStmt(S.getInit()); 760 761 if (S.getConditionVariable()) 762 EmitDecl(*S.getConditionVariable()); 763 764 // If the condition constant folds and can be elided, try to avoid emitting 765 // the condition and the dead arm of the if/else. 766 bool CondConstant; 767 if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant, 768 S.isConstexpr())) { 769 // Figure out which block (then or else) is executed. 770 const Stmt *Executed = S.getThen(); 771 const Stmt *Skipped = S.getElse(); 772 if (!CondConstant) // Condition false? 773 std::swap(Executed, Skipped); 774 775 // If the skipped block has no labels in it, just emit the executed block. 776 // This avoids emitting dead code and simplifies the CFG substantially. 777 if (S.isConstexpr() || !ContainsLabel(Skipped)) { 778 if (CondConstant) 779 incrementProfileCounter(&S); 780 if (Executed) { 781 RunCleanupsScope ExecutedScope(*this); 782 EmitStmt(Executed); 783 } 784 return; 785 } 786 } 787 788 // Otherwise, the condition did not fold, or we couldn't elide it. Just emit 789 // the conditional branch. 790 llvm::BasicBlock *ThenBlock = createBasicBlock("if.then"); 791 llvm::BasicBlock *ContBlock = createBasicBlock("if.end"); 792 llvm::BasicBlock *ElseBlock = ContBlock; 793 if (S.getElse()) 794 ElseBlock = createBasicBlock("if.else"); 795 796 // Prefer the PGO based weights over the likelihood attribute. 797 // When the build isn't optimized the metadata isn't used, so don't generate 798 // it. 799 Stmt::Likelihood LH = Stmt::LH_None; 800 uint64_t Count = getProfileCount(S.getThen()); 801 if (!Count && CGM.getCodeGenOpts().OptimizationLevel) 802 LH = Stmt::getLikelihood(S.getThen(), S.getElse()); 803 EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock, Count, LH); 804 805 // Emit the 'then' code. 806 EmitBlock(ThenBlock); 807 incrementProfileCounter(&S); 808 { 809 RunCleanupsScope ThenScope(*this); 810 EmitStmt(S.getThen()); 811 } 812 EmitBranch(ContBlock); 813 814 // Emit the 'else' code if present. 815 if (const Stmt *Else = S.getElse()) { 816 { 817 // There is no need to emit line number for an unconditional branch. 818 auto NL = ApplyDebugLocation::CreateEmpty(*this); 819 EmitBlock(ElseBlock); 820 } 821 { 822 RunCleanupsScope ElseScope(*this); 823 EmitStmt(Else); 824 } 825 { 826 // There is no need to emit line number for an unconditional branch. 827 auto NL = ApplyDebugLocation::CreateEmpty(*this); 828 EmitBranch(ContBlock); 829 } 830 } 831 832 // Emit the continuation block for code after the if. 833 EmitBlock(ContBlock, true); 834 } 835 836 void CodeGenFunction::EmitWhileStmt(const WhileStmt &S, 837 ArrayRef<const Attr *> WhileAttrs) { 838 // Emit the header for the loop, which will also become 839 // the continue target. 840 JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond"); 841 EmitBlock(LoopHeader.getBlock()); 842 843 // Create an exit block for when the condition fails, which will 844 // also become the break target. 845 JumpDest LoopExit = getJumpDestInCurrentScope("while.end"); 846 847 // Store the blocks to use for break and continue. 848 BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader)); 849 850 // C++ [stmt.while]p2: 851 // When the condition of a while statement is a declaration, the 852 // scope of the variable that is declared extends from its point 853 // of declaration (3.3.2) to the end of the while statement. 854 // [...] 855 // The object created in a condition is destroyed and created 856 // with each iteration of the loop. 857 RunCleanupsScope ConditionScope(*this); 858 859 if (S.getConditionVariable()) 860 EmitDecl(*S.getConditionVariable()); 861 862 // Evaluate the conditional in the while header. C99 6.8.5.1: The 863 // evaluation of the controlling expression takes place before each 864 // execution of the loop body. 865 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 866 867 // while(1) is common, avoid extra exit blocks. Be sure 868 // to correctly handle break/continue though. 869 llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal); 870 bool CondIsConstInt = C != nullptr; 871 bool EmitBoolCondBranch = !CondIsConstInt || !C->isOne(); 872 const SourceRange &R = S.getSourceRange(); 873 LoopStack.push(LoopHeader.getBlock(), CGM.getContext(), CGM.getCodeGenOpts(), 874 WhileAttrs, SourceLocToDebugLoc(R.getBegin()), 875 SourceLocToDebugLoc(R.getEnd()), 876 checkIfLoopMustProgress(CondIsConstInt)); 877 878 // As long as the condition is true, go to the loop body. 879 llvm::BasicBlock *LoopBody = createBasicBlock("while.body"); 880 if (EmitBoolCondBranch) { 881 llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 882 if (ConditionScope.requiresCleanups()) 883 ExitBlock = createBasicBlock("while.exit"); 884 llvm::MDNode *Weights = 885 createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())); 886 if (!Weights && CGM.getCodeGenOpts().OptimizationLevel) 887 BoolCondVal = emitCondLikelihoodViaExpectIntrinsic( 888 BoolCondVal, Stmt::getLikelihood(S.getBody())); 889 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, Weights); 890 891 if (ExitBlock != LoopExit.getBlock()) { 892 EmitBlock(ExitBlock); 893 EmitBranchThroughCleanup(LoopExit); 894 } 895 } else if (const Attr *A = Stmt::getLikelihoodAttr(S.getBody())) { 896 CGM.getDiags().Report(A->getLocation(), 897 diag::warn_attribute_has_no_effect_on_infinite_loop) 898 << A << A->getRange(); 899 CGM.getDiags().Report( 900 S.getWhileLoc(), 901 diag::note_attribute_has_no_effect_on_infinite_loop_here) 902 << SourceRange(S.getWhileLoc(), S.getRParenLoc()); 903 } 904 905 // Emit the loop body. We have to emit this in a cleanup scope 906 // because it might be a singleton DeclStmt. 907 { 908 RunCleanupsScope BodyScope(*this); 909 EmitBlock(LoopBody); 910 incrementProfileCounter(&S); 911 EmitStmt(S.getBody()); 912 } 913 914 BreakContinueStack.pop_back(); 915 916 // Immediately force cleanup. 917 ConditionScope.ForceCleanup(); 918 919 EmitStopPoint(&S); 920 // Branch to the loop header again. 921 EmitBranch(LoopHeader.getBlock()); 922 923 LoopStack.pop(); 924 925 // Emit the exit block. 926 EmitBlock(LoopExit.getBlock(), true); 927 928 // The LoopHeader typically is just a branch if we skipped emitting 929 // a branch, try to erase it. 930 if (!EmitBoolCondBranch) 931 SimplifyForwardingBlocks(LoopHeader.getBlock()); 932 } 933 934 void CodeGenFunction::EmitDoStmt(const DoStmt &S, 935 ArrayRef<const Attr *> DoAttrs) { 936 JumpDest LoopExit = getJumpDestInCurrentScope("do.end"); 937 JumpDest LoopCond = getJumpDestInCurrentScope("do.cond"); 938 939 uint64_t ParentCount = getCurrentProfileCount(); 940 941 // Store the blocks to use for break and continue. 942 BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond)); 943 944 // Emit the body of the loop. 945 llvm::BasicBlock *LoopBody = createBasicBlock("do.body"); 946 947 EmitBlockWithFallThrough(LoopBody, &S); 948 { 949 RunCleanupsScope BodyScope(*this); 950 EmitStmt(S.getBody()); 951 } 952 953 EmitBlock(LoopCond.getBlock()); 954 955 // C99 6.8.5.2: "The evaluation of the controlling expression takes place 956 // after each execution of the loop body." 957 958 // Evaluate the conditional in the while header. 959 // C99 6.8.5p2/p4: The first substatement is executed if the expression 960 // compares unequal to 0. The condition must be a scalar type. 961 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 962 963 BreakContinueStack.pop_back(); 964 965 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure 966 // to correctly handle break/continue though. 967 llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal); 968 bool CondIsConstInt = C; 969 bool EmitBoolCondBranch = !C || !C->isZero(); 970 971 const SourceRange &R = S.getSourceRange(); 972 LoopStack.push(LoopBody, CGM.getContext(), CGM.getCodeGenOpts(), DoAttrs, 973 SourceLocToDebugLoc(R.getBegin()), 974 SourceLocToDebugLoc(R.getEnd()), 975 checkIfLoopMustProgress(CondIsConstInt)); 976 977 // As long as the condition is true, iterate the loop. 978 if (EmitBoolCondBranch) { 979 uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount; 980 Builder.CreateCondBr( 981 BoolCondVal, LoopBody, LoopExit.getBlock(), 982 createProfileWeightsForLoop(S.getCond(), BackedgeCount)); 983 } 984 985 LoopStack.pop(); 986 987 // Emit the exit block. 988 EmitBlock(LoopExit.getBlock()); 989 990 // The DoCond block typically is just a branch if we skipped 991 // emitting a branch, try to erase it. 992 if (!EmitBoolCondBranch) 993 SimplifyForwardingBlocks(LoopCond.getBlock()); 994 } 995 996 void CodeGenFunction::EmitForStmt(const ForStmt &S, 997 ArrayRef<const Attr *> ForAttrs) { 998 JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); 999 1000 LexicalScope ForScope(*this, S.getSourceRange()); 1001 1002 // Evaluate the first part before the loop. 1003 if (S.getInit()) 1004 EmitStmt(S.getInit()); 1005 1006 // Start the loop with a block that tests the condition. 1007 // If there's an increment, the continue scope will be overwritten 1008 // later. 1009 JumpDest CondDest = getJumpDestInCurrentScope("for.cond"); 1010 llvm::BasicBlock *CondBlock = CondDest.getBlock(); 1011 EmitBlock(CondBlock); 1012 1013 Expr::EvalResult Result; 1014 bool CondIsConstInt = 1015 !S.getCond() || S.getCond()->EvaluateAsInt(Result, getContext()); 1016 1017 const SourceRange &R = S.getSourceRange(); 1018 LoopStack.push(CondBlock, CGM.getContext(), CGM.getCodeGenOpts(), ForAttrs, 1019 SourceLocToDebugLoc(R.getBegin()), 1020 SourceLocToDebugLoc(R.getEnd()), 1021 checkIfLoopMustProgress(CondIsConstInt)); 1022 1023 // Create a cleanup scope for the condition variable cleanups. 1024 LexicalScope ConditionScope(*this, S.getSourceRange()); 1025 1026 // If the for loop doesn't have an increment we can just use the condition as 1027 // the continue block. Otherwise, if there is no condition variable, we can 1028 // form the continue block now. If there is a condition variable, we can't 1029 // form the continue block until after we've emitted the condition, because 1030 // the condition is in scope in the increment, but Sema's jump diagnostics 1031 // ensure that there are no continues from the condition variable that jump 1032 // to the loop increment. 1033 JumpDest Continue; 1034 if (!S.getInc()) 1035 Continue = CondDest; 1036 else if (!S.getConditionVariable()) 1037 Continue = getJumpDestInCurrentScope("for.inc"); 1038 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1039 1040 if (S.getCond()) { 1041 // If the for statement has a condition scope, emit the local variable 1042 // declaration. 1043 if (S.getConditionVariable()) { 1044 EmitDecl(*S.getConditionVariable()); 1045 1046 // We have entered the condition variable's scope, so we're now able to 1047 // jump to the continue block. 1048 Continue = S.getInc() ? getJumpDestInCurrentScope("for.inc") : CondDest; 1049 BreakContinueStack.back().ContinueBlock = Continue; 1050 } 1051 1052 llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 1053 // If there are any cleanups between here and the loop-exit scope, 1054 // create a block to stage a loop exit along. 1055 if (ForScope.requiresCleanups()) 1056 ExitBlock = createBasicBlock("for.cond.cleanup"); 1057 1058 // As long as the condition is true, iterate the loop. 1059 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 1060 1061 // C99 6.8.5p2/p4: The first substatement is executed if the expression 1062 // compares unequal to 0. The condition must be a scalar type. 1063 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 1064 llvm::MDNode *Weights = 1065 createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())); 1066 if (!Weights && CGM.getCodeGenOpts().OptimizationLevel) 1067 BoolCondVal = emitCondLikelihoodViaExpectIntrinsic( 1068 BoolCondVal, Stmt::getLikelihood(S.getBody())); 1069 1070 Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights); 1071 1072 if (ExitBlock != LoopExit.getBlock()) { 1073 EmitBlock(ExitBlock); 1074 EmitBranchThroughCleanup(LoopExit); 1075 } 1076 1077 EmitBlock(ForBody); 1078 } else { 1079 // Treat it as a non-zero constant. Don't even create a new block for the 1080 // body, just fall into it. 1081 } 1082 incrementProfileCounter(&S); 1083 1084 { 1085 // Create a separate cleanup scope for the body, in case it is not 1086 // a compound statement. 1087 RunCleanupsScope BodyScope(*this); 1088 EmitStmt(S.getBody()); 1089 } 1090 1091 // If there is an increment, emit it next. 1092 if (S.getInc()) { 1093 EmitBlock(Continue.getBlock()); 1094 EmitStmt(S.getInc()); 1095 } 1096 1097 BreakContinueStack.pop_back(); 1098 1099 ConditionScope.ForceCleanup(); 1100 1101 EmitStopPoint(&S); 1102 EmitBranch(CondBlock); 1103 1104 ForScope.ForceCleanup(); 1105 1106 LoopStack.pop(); 1107 1108 // Emit the fall-through block. 1109 EmitBlock(LoopExit.getBlock(), true); 1110 } 1111 1112 void 1113 CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S, 1114 ArrayRef<const Attr *> ForAttrs) { 1115 JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); 1116 1117 LexicalScope ForScope(*this, S.getSourceRange()); 1118 1119 // Evaluate the first pieces before the loop. 1120 if (S.getInit()) 1121 EmitStmt(S.getInit()); 1122 EmitStmt(S.getRangeStmt()); 1123 EmitStmt(S.getBeginStmt()); 1124 EmitStmt(S.getEndStmt()); 1125 1126 // Start the loop with a block that tests the condition. 1127 // If there's an increment, the continue scope will be overwritten 1128 // later. 1129 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 1130 EmitBlock(CondBlock); 1131 1132 const SourceRange &R = S.getSourceRange(); 1133 LoopStack.push(CondBlock, CGM.getContext(), CGM.getCodeGenOpts(), ForAttrs, 1134 SourceLocToDebugLoc(R.getBegin()), 1135 SourceLocToDebugLoc(R.getEnd())); 1136 1137 // If there are any cleanups between here and the loop-exit scope, 1138 // create a block to stage a loop exit along. 1139 llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 1140 if (ForScope.requiresCleanups()) 1141 ExitBlock = createBasicBlock("for.cond.cleanup"); 1142 1143 // The loop body, consisting of the specified body and the loop variable. 1144 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 1145 1146 // The body is executed if the expression, contextually converted 1147 // to bool, is true. 1148 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); 1149 llvm::MDNode *Weights = 1150 createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())); 1151 if (!Weights && CGM.getCodeGenOpts().OptimizationLevel) 1152 BoolCondVal = emitCondLikelihoodViaExpectIntrinsic( 1153 BoolCondVal, Stmt::getLikelihood(S.getBody())); 1154 Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights); 1155 1156 if (ExitBlock != LoopExit.getBlock()) { 1157 EmitBlock(ExitBlock); 1158 EmitBranchThroughCleanup(LoopExit); 1159 } 1160 1161 EmitBlock(ForBody); 1162 incrementProfileCounter(&S); 1163 1164 // Create a block for the increment. In case of a 'continue', we jump there. 1165 JumpDest Continue = getJumpDestInCurrentScope("for.inc"); 1166 1167 // Store the blocks to use for break and continue. 1168 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1169 1170 { 1171 // Create a separate cleanup scope for the loop variable and body. 1172 LexicalScope BodyScope(*this, S.getSourceRange()); 1173 EmitStmt(S.getLoopVarStmt()); 1174 EmitStmt(S.getBody()); 1175 } 1176 1177 EmitStopPoint(&S); 1178 // If there is an increment, emit it next. 1179 EmitBlock(Continue.getBlock()); 1180 EmitStmt(S.getInc()); 1181 1182 BreakContinueStack.pop_back(); 1183 1184 EmitBranch(CondBlock); 1185 1186 ForScope.ForceCleanup(); 1187 1188 LoopStack.pop(); 1189 1190 // Emit the fall-through block. 1191 EmitBlock(LoopExit.getBlock(), true); 1192 } 1193 1194 void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) { 1195 if (RV.isScalar()) { 1196 Builder.CreateStore(RV.getScalarVal(), ReturnValue); 1197 } else if (RV.isAggregate()) { 1198 LValue Dest = MakeAddrLValue(ReturnValue, Ty); 1199 LValue Src = MakeAddrLValue(RV.getAggregateAddress(), Ty); 1200 EmitAggregateCopy(Dest, Src, Ty, getOverlapForReturnValue()); 1201 } else { 1202 EmitStoreOfComplex(RV.getComplexVal(), MakeAddrLValue(ReturnValue, Ty), 1203 /*init*/ true); 1204 } 1205 EmitBranchThroughCleanup(ReturnBlock); 1206 } 1207 1208 namespace { 1209 // RAII struct used to save and restore a return statment's result expression. 1210 struct SaveRetExprRAII { 1211 SaveRetExprRAII(const Expr *RetExpr, CodeGenFunction &CGF) 1212 : OldRetExpr(CGF.RetExpr), CGF(CGF) { 1213 CGF.RetExpr = RetExpr; 1214 } 1215 ~SaveRetExprRAII() { CGF.RetExpr = OldRetExpr; } 1216 const Expr *OldRetExpr; 1217 CodeGenFunction &CGF; 1218 }; 1219 } // namespace 1220 1221 /// If we have 'return f(...);', where both caller and callee are SwiftAsync, 1222 /// codegen it as 'tail call ...; ret void;'. 1223 static void makeTailCallIfSwiftAsync(const CallExpr *CE, CGBuilderTy &Builder, 1224 const CGFunctionInfo *CurFnInfo) { 1225 auto calleeQualType = CE->getCallee()->getType(); 1226 const FunctionType *calleeType = nullptr; 1227 if (calleeQualType->isFunctionPointerType() || 1228 calleeQualType->isFunctionReferenceType() || 1229 calleeQualType->isBlockPointerType() || 1230 calleeQualType->isMemberFunctionPointerType()) { 1231 calleeType = calleeQualType->getPointeeType()->castAs<FunctionType>(); 1232 } else if (auto *ty = dyn_cast<FunctionType>(calleeQualType)) { 1233 calleeType = ty; 1234 } else if (auto CMCE = dyn_cast<CXXMemberCallExpr>(CE)) { 1235 if (auto methodDecl = CMCE->getMethodDecl()) { 1236 // getMethodDecl() doesn't handle member pointers at the moment. 1237 calleeType = methodDecl->getType()->castAs<FunctionType>(); 1238 } else { 1239 return; 1240 } 1241 } else { 1242 return; 1243 } 1244 if (calleeType->getCallConv() == CallingConv::CC_SwiftAsync && 1245 (CurFnInfo->getASTCallingConvention() == CallingConv::CC_SwiftAsync)) { 1246 auto CI = cast<llvm::CallInst>(&Builder.GetInsertBlock()->back()); 1247 CI->setTailCallKind(llvm::CallInst::TCK_MustTail); 1248 Builder.CreateRetVoid(); 1249 Builder.ClearInsertionPoint(); 1250 } 1251 } 1252 1253 /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand 1254 /// if the function returns void, or may be missing one if the function returns 1255 /// non-void. Fun stuff :). 1256 void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { 1257 if (requiresReturnValueCheck()) { 1258 llvm::Constant *SLoc = EmitCheckSourceLocation(S.getBeginLoc()); 1259 auto *SLocPtr = 1260 new llvm::GlobalVariable(CGM.getModule(), SLoc->getType(), false, 1261 llvm::GlobalVariable::PrivateLinkage, SLoc); 1262 SLocPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 1263 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(SLocPtr); 1264 assert(ReturnLocation.isValid() && "No valid return location"); 1265 Builder.CreateStore(Builder.CreateBitCast(SLocPtr, Int8PtrTy), 1266 ReturnLocation); 1267 } 1268 1269 // Returning from an outlined SEH helper is UB, and we already warn on it. 1270 if (IsOutlinedSEHHelper) { 1271 Builder.CreateUnreachable(); 1272 Builder.ClearInsertionPoint(); 1273 } 1274 1275 // Emit the result value, even if unused, to evaluate the side effects. 1276 const Expr *RV = S.getRetValue(); 1277 1278 // Record the result expression of the return statement. The recorded 1279 // expression is used to determine whether a block capture's lifetime should 1280 // end at the end of the full expression as opposed to the end of the scope 1281 // enclosing the block expression. 1282 // 1283 // This permits a small, easily-implemented exception to our over-conservative 1284 // rules about not jumping to statements following block literals with 1285 // non-trivial cleanups. 1286 SaveRetExprRAII SaveRetExpr(RV, *this); 1287 1288 RunCleanupsScope cleanupScope(*this); 1289 if (const auto *EWC = dyn_cast_or_null<ExprWithCleanups>(RV)) 1290 RV = EWC->getSubExpr(); 1291 // FIXME: Clean this up by using an LValue for ReturnTemp, 1292 // EmitStoreThroughLValue, and EmitAnyExpr. 1293 // Check if the NRVO candidate was not globalized in OpenMP mode. 1294 if (getLangOpts().ElideConstructors && S.getNRVOCandidate() && 1295 S.getNRVOCandidate()->isNRVOVariable() && 1296 (!getLangOpts().OpenMP || 1297 !CGM.getOpenMPRuntime() 1298 .getAddressOfLocalVariable(*this, S.getNRVOCandidate()) 1299 .isValid())) { 1300 // Apply the named return value optimization for this return statement, 1301 // which means doing nothing: the appropriate result has already been 1302 // constructed into the NRVO variable. 1303 1304 // If there is an NRVO flag for this variable, set it to 1 into indicate 1305 // that the cleanup code should not destroy the variable. 1306 if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()]) 1307 Builder.CreateFlagStore(Builder.getTrue(), NRVOFlag); 1308 } else if (!ReturnValue.isValid() || (RV && RV->getType()->isVoidType())) { 1309 // Make sure not to return anything, but evaluate the expression 1310 // for side effects. 1311 if (RV) { 1312 EmitAnyExpr(RV); 1313 if (auto *CE = dyn_cast<CallExpr>(RV)) 1314 makeTailCallIfSwiftAsync(CE, Builder, CurFnInfo); 1315 } 1316 } else if (!RV) { 1317 // Do nothing (return value is left uninitialized) 1318 } else if (FnRetTy->isReferenceType()) { 1319 // If this function returns a reference, take the address of the expression 1320 // rather than the value. 1321 RValue Result = EmitReferenceBindingToExpr(RV); 1322 Builder.CreateStore(Result.getScalarVal(), ReturnValue); 1323 } else { 1324 switch (getEvaluationKind(RV->getType())) { 1325 case TEK_Scalar: 1326 Builder.CreateStore(EmitScalarExpr(RV), ReturnValue); 1327 break; 1328 case TEK_Complex: 1329 EmitComplexExprIntoLValue(RV, MakeAddrLValue(ReturnValue, RV->getType()), 1330 /*isInit*/ true); 1331 break; 1332 case TEK_Aggregate: 1333 EmitAggExpr(RV, AggValueSlot::forAddr( 1334 ReturnValue, Qualifiers(), 1335 AggValueSlot::IsDestructed, 1336 AggValueSlot::DoesNotNeedGCBarriers, 1337 AggValueSlot::IsNotAliased, 1338 getOverlapForReturnValue())); 1339 break; 1340 } 1341 } 1342 1343 ++NumReturnExprs; 1344 if (!RV || RV->isEvaluatable(getContext())) 1345 ++NumSimpleReturnExprs; 1346 1347 cleanupScope.ForceCleanup(); 1348 EmitBranchThroughCleanup(ReturnBlock); 1349 } 1350 1351 void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { 1352 // As long as debug info is modeled with instructions, we have to ensure we 1353 // have a place to insert here and write the stop point here. 1354 if (HaveInsertPoint()) 1355 EmitStopPoint(&S); 1356 1357 for (const auto *I : S.decls()) 1358 EmitDecl(*I); 1359 } 1360 1361 void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) { 1362 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); 1363 1364 // If this code is reachable then emit a stop point (if generating 1365 // debug info). We have to do this ourselves because we are on the 1366 // "simple" statement path. 1367 if (HaveInsertPoint()) 1368 EmitStopPoint(&S); 1369 1370 EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock); 1371 } 1372 1373 void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) { 1374 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); 1375 1376 // If this code is reachable then emit a stop point (if generating 1377 // debug info). We have to do this ourselves because we are on the 1378 // "simple" statement path. 1379 if (HaveInsertPoint()) 1380 EmitStopPoint(&S); 1381 1382 EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock); 1383 } 1384 1385 /// EmitCaseStmtRange - If case statement range is not too big then 1386 /// add multiple cases to switch instruction, one for each value within 1387 /// the range. If range is too big then emit "if" condition check. 1388 void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S, 1389 ArrayRef<const Attr *> Attrs) { 1390 assert(S.getRHS() && "Expected RHS value in CaseStmt"); 1391 1392 llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext()); 1393 llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext()); 1394 1395 // Emit the code for this case. We do this first to make sure it is 1396 // properly chained from our predecessor before generating the 1397 // switch machinery to enter this block. 1398 llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb"); 1399 EmitBlockWithFallThrough(CaseDest, &S); 1400 EmitStmt(S.getSubStmt()); 1401 1402 // If range is empty, do nothing. 1403 if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS)) 1404 return; 1405 1406 Stmt::Likelihood LH = Stmt::getLikelihood(Attrs); 1407 llvm::APInt Range = RHS - LHS; 1408 // FIXME: parameters such as this should not be hardcoded. 1409 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { 1410 // Range is small enough to add multiple switch instruction cases. 1411 uint64_t Total = getProfileCount(&S); 1412 unsigned NCases = Range.getZExtValue() + 1; 1413 // We only have one region counter for the entire set of cases here, so we 1414 // need to divide the weights evenly between the generated cases, ensuring 1415 // that the total weight is preserved. E.g., a weight of 5 over three cases 1416 // will be distributed as weights of 2, 2, and 1. 1417 uint64_t Weight = Total / NCases, Rem = Total % NCases; 1418 for (unsigned I = 0; I != NCases; ++I) { 1419 if (SwitchWeights) 1420 SwitchWeights->push_back(Weight + (Rem ? 1 : 0)); 1421 else if (SwitchLikelihood) 1422 SwitchLikelihood->push_back(LH); 1423 1424 if (Rem) 1425 Rem--; 1426 SwitchInsn->addCase(Builder.getInt(LHS), CaseDest); 1427 ++LHS; 1428 } 1429 return; 1430 } 1431 1432 // The range is too big. Emit "if" condition into a new block, 1433 // making sure to save and restore the current insertion point. 1434 llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock(); 1435 1436 // Push this test onto the chain of range checks (which terminates 1437 // in the default basic block). The switch's default will be changed 1438 // to the top of this chain after switch emission is complete. 1439 llvm::BasicBlock *FalseDest = CaseRangeBlock; 1440 CaseRangeBlock = createBasicBlock("sw.caserange"); 1441 1442 CurFn->getBasicBlockList().push_back(CaseRangeBlock); 1443 Builder.SetInsertPoint(CaseRangeBlock); 1444 1445 // Emit range check. 1446 llvm::Value *Diff = 1447 Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS)); 1448 llvm::Value *Cond = 1449 Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds"); 1450 1451 llvm::MDNode *Weights = nullptr; 1452 if (SwitchWeights) { 1453 uint64_t ThisCount = getProfileCount(&S); 1454 uint64_t DefaultCount = (*SwitchWeights)[0]; 1455 Weights = createProfileWeights(ThisCount, DefaultCount); 1456 1457 // Since we're chaining the switch default through each large case range, we 1458 // need to update the weight for the default, ie, the first case, to include 1459 // this case. 1460 (*SwitchWeights)[0] += ThisCount; 1461 } else if (SwitchLikelihood) 1462 Cond = emitCondLikelihoodViaExpectIntrinsic(Cond, LH); 1463 1464 Builder.CreateCondBr(Cond, CaseDest, FalseDest, Weights); 1465 1466 // Restore the appropriate insertion point. 1467 if (RestoreBB) 1468 Builder.SetInsertPoint(RestoreBB); 1469 else 1470 Builder.ClearInsertionPoint(); 1471 } 1472 1473 void CodeGenFunction::EmitCaseStmt(const CaseStmt &S, 1474 ArrayRef<const Attr *> Attrs) { 1475 // If there is no enclosing switch instance that we're aware of, then this 1476 // case statement and its block can be elided. This situation only happens 1477 // when we've constant-folded the switch, are emitting the constant case, 1478 // and part of the constant case includes another case statement. For 1479 // instance: switch (4) { case 4: do { case 5: } while (1); } 1480 if (!SwitchInsn) { 1481 EmitStmt(S.getSubStmt()); 1482 return; 1483 } 1484 1485 // Handle case ranges. 1486 if (S.getRHS()) { 1487 EmitCaseStmtRange(S, Attrs); 1488 return; 1489 } 1490 1491 llvm::ConstantInt *CaseVal = 1492 Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext())); 1493 if (SwitchLikelihood) 1494 SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs)); 1495 1496 // If the body of the case is just a 'break', try to not emit an empty block. 1497 // If we're profiling or we're not optimizing, leave the block in for better 1498 // debug and coverage analysis. 1499 if (!CGM.getCodeGenOpts().hasProfileClangInstr() && 1500 CGM.getCodeGenOpts().OptimizationLevel > 0 && 1501 isa<BreakStmt>(S.getSubStmt())) { 1502 JumpDest Block = BreakContinueStack.back().BreakBlock; 1503 1504 // Only do this optimization if there are no cleanups that need emitting. 1505 if (isObviouslyBranchWithoutCleanups(Block)) { 1506 if (SwitchWeights) 1507 SwitchWeights->push_back(getProfileCount(&S)); 1508 SwitchInsn->addCase(CaseVal, Block.getBlock()); 1509 1510 // If there was a fallthrough into this case, make sure to redirect it to 1511 // the end of the switch as well. 1512 if (Builder.GetInsertBlock()) { 1513 Builder.CreateBr(Block.getBlock()); 1514 Builder.ClearInsertionPoint(); 1515 } 1516 return; 1517 } 1518 } 1519 1520 llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb"); 1521 EmitBlockWithFallThrough(CaseDest, &S); 1522 if (SwitchWeights) 1523 SwitchWeights->push_back(getProfileCount(&S)); 1524 SwitchInsn->addCase(CaseVal, CaseDest); 1525 1526 // Recursively emitting the statement is acceptable, but is not wonderful for 1527 // code where we have many case statements nested together, i.e.: 1528 // case 1: 1529 // case 2: 1530 // case 3: etc. 1531 // Handling this recursively will create a new block for each case statement 1532 // that falls through to the next case which is IR intensive. It also causes 1533 // deep recursion which can run into stack depth limitations. Handle 1534 // sequential non-range case statements specially. 1535 // 1536 // TODO When the next case has a likelihood attribute the code returns to the 1537 // recursive algorithm. Maybe improve this case if it becomes common practice 1538 // to use a lot of attributes. 1539 const CaseStmt *CurCase = &S; 1540 const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt()); 1541 1542 // Otherwise, iteratively add consecutive cases to this switch stmt. 1543 while (NextCase && NextCase->getRHS() == nullptr) { 1544 CurCase = NextCase; 1545 llvm::ConstantInt *CaseVal = 1546 Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext())); 1547 1548 if (SwitchWeights) 1549 SwitchWeights->push_back(getProfileCount(NextCase)); 1550 if (CGM.getCodeGenOpts().hasProfileClangInstr()) { 1551 CaseDest = createBasicBlock("sw.bb"); 1552 EmitBlockWithFallThrough(CaseDest, CurCase); 1553 } 1554 // Since this loop is only executed when the CaseStmt has no attributes 1555 // use a hard-coded value. 1556 if (SwitchLikelihood) 1557 SwitchLikelihood->push_back(Stmt::LH_None); 1558 1559 SwitchInsn->addCase(CaseVal, CaseDest); 1560 NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt()); 1561 } 1562 1563 // Generate a stop point for debug info if the case statement is 1564 // followed by a default statement. A fallthrough case before a 1565 // default case gets its own branch target. 1566 if (CurCase->getSubStmt()->getStmtClass() == Stmt::DefaultStmtClass) 1567 EmitStopPoint(CurCase); 1568 1569 // Normal default recursion for non-cases. 1570 EmitStmt(CurCase->getSubStmt()); 1571 } 1572 1573 void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S, 1574 ArrayRef<const Attr *> Attrs) { 1575 // If there is no enclosing switch instance that we're aware of, then this 1576 // default statement can be elided. This situation only happens when we've 1577 // constant-folded the switch. 1578 if (!SwitchInsn) { 1579 EmitStmt(S.getSubStmt()); 1580 return; 1581 } 1582 1583 llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest(); 1584 assert(DefaultBlock->empty() && 1585 "EmitDefaultStmt: Default block already defined?"); 1586 1587 if (SwitchLikelihood) 1588 SwitchLikelihood->front() = Stmt::getLikelihood(Attrs); 1589 1590 EmitBlockWithFallThrough(DefaultBlock, &S); 1591 1592 EmitStmt(S.getSubStmt()); 1593 } 1594 1595 /// CollectStatementsForCase - Given the body of a 'switch' statement and a 1596 /// constant value that is being switched on, see if we can dead code eliminate 1597 /// the body of the switch to a simple series of statements to emit. Basically, 1598 /// on a switch (5) we want to find these statements: 1599 /// case 5: 1600 /// printf(...); <-- 1601 /// ++i; <-- 1602 /// break; 1603 /// 1604 /// and add them to the ResultStmts vector. If it is unsafe to do this 1605 /// transformation (for example, one of the elided statements contains a label 1606 /// that might be jumped to), return CSFC_Failure. If we handled it and 'S' 1607 /// should include statements after it (e.g. the printf() line is a substmt of 1608 /// the case) then return CSFC_FallThrough. If we handled it and found a break 1609 /// statement, then return CSFC_Success. 1610 /// 1611 /// If Case is non-null, then we are looking for the specified case, checking 1612 /// that nothing we jump over contains labels. If Case is null, then we found 1613 /// the case and are looking for the break. 1614 /// 1615 /// If the recursive walk actually finds our Case, then we set FoundCase to 1616 /// true. 1617 /// 1618 enum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success }; 1619 static CSFC_Result CollectStatementsForCase(const Stmt *S, 1620 const SwitchCase *Case, 1621 bool &FoundCase, 1622 SmallVectorImpl<const Stmt*> &ResultStmts) { 1623 // If this is a null statement, just succeed. 1624 if (!S) 1625 return Case ? CSFC_Success : CSFC_FallThrough; 1626 1627 // If this is the switchcase (case 4: or default) that we're looking for, then 1628 // we're in business. Just add the substatement. 1629 if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) { 1630 if (S == Case) { 1631 FoundCase = true; 1632 return CollectStatementsForCase(SC->getSubStmt(), nullptr, FoundCase, 1633 ResultStmts); 1634 } 1635 1636 // Otherwise, this is some other case or default statement, just ignore it. 1637 return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase, 1638 ResultStmts); 1639 } 1640 1641 // If we are in the live part of the code and we found our break statement, 1642 // return a success! 1643 if (!Case && isa<BreakStmt>(S)) 1644 return CSFC_Success; 1645 1646 // If this is a switch statement, then it might contain the SwitchCase, the 1647 // break, or neither. 1648 if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) { 1649 // Handle this as two cases: we might be looking for the SwitchCase (if so 1650 // the skipped statements must be skippable) or we might already have it. 1651 CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end(); 1652 bool StartedInLiveCode = FoundCase; 1653 unsigned StartSize = ResultStmts.size(); 1654 1655 // If we've not found the case yet, scan through looking for it. 1656 if (Case) { 1657 // Keep track of whether we see a skipped declaration. The code could be 1658 // using the declaration even if it is skipped, so we can't optimize out 1659 // the decl if the kept statements might refer to it. 1660 bool HadSkippedDecl = false; 1661 1662 // If we're looking for the case, just see if we can skip each of the 1663 // substatements. 1664 for (; Case && I != E; ++I) { 1665 HadSkippedDecl |= CodeGenFunction::mightAddDeclToScope(*I); 1666 1667 switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) { 1668 case CSFC_Failure: return CSFC_Failure; 1669 case CSFC_Success: 1670 // A successful result means that either 1) that the statement doesn't 1671 // have the case and is skippable, or 2) does contain the case value 1672 // and also contains the break to exit the switch. In the later case, 1673 // we just verify the rest of the statements are elidable. 1674 if (FoundCase) { 1675 // If we found the case and skipped declarations, we can't do the 1676 // optimization. 1677 if (HadSkippedDecl) 1678 return CSFC_Failure; 1679 1680 for (++I; I != E; ++I) 1681 if (CodeGenFunction::ContainsLabel(*I, true)) 1682 return CSFC_Failure; 1683 return CSFC_Success; 1684 } 1685 break; 1686 case CSFC_FallThrough: 1687 // If we have a fallthrough condition, then we must have found the 1688 // case started to include statements. Consider the rest of the 1689 // statements in the compound statement as candidates for inclusion. 1690 assert(FoundCase && "Didn't find case but returned fallthrough?"); 1691 // We recursively found Case, so we're not looking for it anymore. 1692 Case = nullptr; 1693 1694 // If we found the case and skipped declarations, we can't do the 1695 // optimization. 1696 if (HadSkippedDecl) 1697 return CSFC_Failure; 1698 break; 1699 } 1700 } 1701 1702 if (!FoundCase) 1703 return CSFC_Success; 1704 1705 assert(!HadSkippedDecl && "fallthrough after skipping decl"); 1706 } 1707 1708 // If we have statements in our range, then we know that the statements are 1709 // live and need to be added to the set of statements we're tracking. 1710 bool AnyDecls = false; 1711 for (; I != E; ++I) { 1712 AnyDecls |= CodeGenFunction::mightAddDeclToScope(*I); 1713 1714 switch (CollectStatementsForCase(*I, nullptr, FoundCase, ResultStmts)) { 1715 case CSFC_Failure: return CSFC_Failure; 1716 case CSFC_FallThrough: 1717 // A fallthrough result means that the statement was simple and just 1718 // included in ResultStmt, keep adding them afterwards. 1719 break; 1720 case CSFC_Success: 1721 // A successful result means that we found the break statement and 1722 // stopped statement inclusion. We just ensure that any leftover stmts 1723 // are skippable and return success ourselves. 1724 for (++I; I != E; ++I) 1725 if (CodeGenFunction::ContainsLabel(*I, true)) 1726 return CSFC_Failure; 1727 return CSFC_Success; 1728 } 1729 } 1730 1731 // If we're about to fall out of a scope without hitting a 'break;', we 1732 // can't perform the optimization if there were any decls in that scope 1733 // (we'd lose their end-of-lifetime). 1734 if (AnyDecls) { 1735 // If the entire compound statement was live, there's one more thing we 1736 // can try before giving up: emit the whole thing as a single statement. 1737 // We can do that unless the statement contains a 'break;'. 1738 // FIXME: Such a break must be at the end of a construct within this one. 1739 // We could emit this by just ignoring the BreakStmts entirely. 1740 if (StartedInLiveCode && !CodeGenFunction::containsBreak(S)) { 1741 ResultStmts.resize(StartSize); 1742 ResultStmts.push_back(S); 1743 } else { 1744 return CSFC_Failure; 1745 } 1746 } 1747 1748 return CSFC_FallThrough; 1749 } 1750 1751 // Okay, this is some other statement that we don't handle explicitly, like a 1752 // for statement or increment etc. If we are skipping over this statement, 1753 // just verify it doesn't have labels, which would make it invalid to elide. 1754 if (Case) { 1755 if (CodeGenFunction::ContainsLabel(S, true)) 1756 return CSFC_Failure; 1757 return CSFC_Success; 1758 } 1759 1760 // Otherwise, we want to include this statement. Everything is cool with that 1761 // so long as it doesn't contain a break out of the switch we're in. 1762 if (CodeGenFunction::containsBreak(S)) return CSFC_Failure; 1763 1764 // Otherwise, everything is great. Include the statement and tell the caller 1765 // that we fall through and include the next statement as well. 1766 ResultStmts.push_back(S); 1767 return CSFC_FallThrough; 1768 } 1769 1770 /// FindCaseStatementsForValue - Find the case statement being jumped to and 1771 /// then invoke CollectStatementsForCase to find the list of statements to emit 1772 /// for a switch on constant. See the comment above CollectStatementsForCase 1773 /// for more details. 1774 static bool FindCaseStatementsForValue(const SwitchStmt &S, 1775 const llvm::APSInt &ConstantCondValue, 1776 SmallVectorImpl<const Stmt*> &ResultStmts, 1777 ASTContext &C, 1778 const SwitchCase *&ResultCase) { 1779 // First step, find the switch case that is being branched to. We can do this 1780 // efficiently by scanning the SwitchCase list. 1781 const SwitchCase *Case = S.getSwitchCaseList(); 1782 const DefaultStmt *DefaultCase = nullptr; 1783 1784 for (; Case; Case = Case->getNextSwitchCase()) { 1785 // It's either a default or case. Just remember the default statement in 1786 // case we're not jumping to any numbered cases. 1787 if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) { 1788 DefaultCase = DS; 1789 continue; 1790 } 1791 1792 // Check to see if this case is the one we're looking for. 1793 const CaseStmt *CS = cast<CaseStmt>(Case); 1794 // Don't handle case ranges yet. 1795 if (CS->getRHS()) return false; 1796 1797 // If we found our case, remember it as 'case'. 1798 if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue) 1799 break; 1800 } 1801 1802 // If we didn't find a matching case, we use a default if it exists, or we 1803 // elide the whole switch body! 1804 if (!Case) { 1805 // It is safe to elide the body of the switch if it doesn't contain labels 1806 // etc. If it is safe, return successfully with an empty ResultStmts list. 1807 if (!DefaultCase) 1808 return !CodeGenFunction::ContainsLabel(&S); 1809 Case = DefaultCase; 1810 } 1811 1812 // Ok, we know which case is being jumped to, try to collect all the 1813 // statements that follow it. This can fail for a variety of reasons. Also, 1814 // check to see that the recursive walk actually found our case statement. 1815 // Insane cases like this can fail to find it in the recursive walk since we 1816 // don't handle every stmt kind: 1817 // switch (4) { 1818 // while (1) { 1819 // case 4: ... 1820 bool FoundCase = false; 1821 ResultCase = Case; 1822 return CollectStatementsForCase(S.getBody(), Case, FoundCase, 1823 ResultStmts) != CSFC_Failure && 1824 FoundCase; 1825 } 1826 1827 static Optional<SmallVector<uint64_t, 16>> 1828 getLikelihoodWeights(ArrayRef<Stmt::Likelihood> Likelihoods) { 1829 // Are there enough branches to weight them? 1830 if (Likelihoods.size() <= 1) 1831 return None; 1832 1833 uint64_t NumUnlikely = 0; 1834 uint64_t NumNone = 0; 1835 uint64_t NumLikely = 0; 1836 for (const auto LH : Likelihoods) { 1837 switch (LH) { 1838 case Stmt::LH_Unlikely: 1839 ++NumUnlikely; 1840 break; 1841 case Stmt::LH_None: 1842 ++NumNone; 1843 break; 1844 case Stmt::LH_Likely: 1845 ++NumLikely; 1846 break; 1847 } 1848 } 1849 1850 // Is there a likelihood attribute used? 1851 if (NumUnlikely == 0 && NumLikely == 0) 1852 return None; 1853 1854 // When multiple cases share the same code they can be combined during 1855 // optimization. In that case the weights of the branch will be the sum of 1856 // the individual weights. Make sure the combined sum of all neutral cases 1857 // doesn't exceed the value of a single likely attribute. 1858 // The additions both avoid divisions by 0 and make sure the weights of None 1859 // don't exceed the weight of Likely. 1860 const uint64_t Likely = INT32_MAX / (NumLikely + 2); 1861 const uint64_t None = Likely / (NumNone + 1); 1862 const uint64_t Unlikely = 0; 1863 1864 SmallVector<uint64_t, 16> Result; 1865 Result.reserve(Likelihoods.size()); 1866 for (const auto LH : Likelihoods) { 1867 switch (LH) { 1868 case Stmt::LH_Unlikely: 1869 Result.push_back(Unlikely); 1870 break; 1871 case Stmt::LH_None: 1872 Result.push_back(None); 1873 break; 1874 case Stmt::LH_Likely: 1875 Result.push_back(Likely); 1876 break; 1877 } 1878 } 1879 1880 return Result; 1881 } 1882 1883 void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { 1884 // Handle nested switch statements. 1885 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; 1886 SmallVector<uint64_t, 16> *SavedSwitchWeights = SwitchWeights; 1887 SmallVector<Stmt::Likelihood, 16> *SavedSwitchLikelihood = SwitchLikelihood; 1888 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; 1889 1890 // See if we can constant fold the condition of the switch and therefore only 1891 // emit the live case statement (if any) of the switch. 1892 llvm::APSInt ConstantCondValue; 1893 if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) { 1894 SmallVector<const Stmt*, 4> CaseStmts; 1895 const SwitchCase *Case = nullptr; 1896 if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts, 1897 getContext(), Case)) { 1898 if (Case) 1899 incrementProfileCounter(Case); 1900 RunCleanupsScope ExecutedScope(*this); 1901 1902 if (S.getInit()) 1903 EmitStmt(S.getInit()); 1904 1905 // Emit the condition variable if needed inside the entire cleanup scope 1906 // used by this special case for constant folded switches. 1907 if (S.getConditionVariable()) 1908 EmitDecl(*S.getConditionVariable()); 1909 1910 // At this point, we are no longer "within" a switch instance, so 1911 // we can temporarily enforce this to ensure that any embedded case 1912 // statements are not emitted. 1913 SwitchInsn = nullptr; 1914 1915 // Okay, we can dead code eliminate everything except this case. Emit the 1916 // specified series of statements and we're good. 1917 for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i) 1918 EmitStmt(CaseStmts[i]); 1919 incrementProfileCounter(&S); 1920 1921 // Now we want to restore the saved switch instance so that nested 1922 // switches continue to function properly 1923 SwitchInsn = SavedSwitchInsn; 1924 1925 return; 1926 } 1927 } 1928 1929 JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog"); 1930 1931 RunCleanupsScope ConditionScope(*this); 1932 1933 if (S.getInit()) 1934 EmitStmt(S.getInit()); 1935 1936 if (S.getConditionVariable()) 1937 EmitDecl(*S.getConditionVariable()); 1938 llvm::Value *CondV = EmitScalarExpr(S.getCond()); 1939 1940 // Create basic block to hold stuff that comes after switch 1941 // statement. We also need to create a default block now so that 1942 // explicit case ranges tests can have a place to jump to on 1943 // failure. 1944 llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default"); 1945 SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock); 1946 if (PGO.haveRegionCounts()) { 1947 // Walk the SwitchCase list to find how many there are. 1948 uint64_t DefaultCount = 0; 1949 unsigned NumCases = 0; 1950 for (const SwitchCase *Case = S.getSwitchCaseList(); 1951 Case; 1952 Case = Case->getNextSwitchCase()) { 1953 if (isa<DefaultStmt>(Case)) 1954 DefaultCount = getProfileCount(Case); 1955 NumCases += 1; 1956 } 1957 SwitchWeights = new SmallVector<uint64_t, 16>(); 1958 SwitchWeights->reserve(NumCases); 1959 // The default needs to be first. We store the edge count, so we already 1960 // know the right weight. 1961 SwitchWeights->push_back(DefaultCount); 1962 } else if (CGM.getCodeGenOpts().OptimizationLevel) { 1963 SwitchLikelihood = new SmallVector<Stmt::Likelihood, 16>(); 1964 // Initialize the default case. 1965 SwitchLikelihood->push_back(Stmt::LH_None); 1966 } 1967 1968 CaseRangeBlock = DefaultBlock; 1969 1970 // Clear the insertion point to indicate we are in unreachable code. 1971 Builder.ClearInsertionPoint(); 1972 1973 // All break statements jump to NextBlock. If BreakContinueStack is non-empty 1974 // then reuse last ContinueBlock. 1975 JumpDest OuterContinue; 1976 if (!BreakContinueStack.empty()) 1977 OuterContinue = BreakContinueStack.back().ContinueBlock; 1978 1979 BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue)); 1980 1981 // Emit switch body. 1982 EmitStmt(S.getBody()); 1983 1984 BreakContinueStack.pop_back(); 1985 1986 // Update the default block in case explicit case range tests have 1987 // been chained on top. 1988 SwitchInsn->setDefaultDest(CaseRangeBlock); 1989 1990 // If a default was never emitted: 1991 if (!DefaultBlock->getParent()) { 1992 // If we have cleanups, emit the default block so that there's a 1993 // place to jump through the cleanups from. 1994 if (ConditionScope.requiresCleanups()) { 1995 EmitBlock(DefaultBlock); 1996 1997 // Otherwise, just forward the default block to the switch end. 1998 } else { 1999 DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock()); 2000 delete DefaultBlock; 2001 } 2002 } 2003 2004 ConditionScope.ForceCleanup(); 2005 2006 // Emit continuation. 2007 EmitBlock(SwitchExit.getBlock(), true); 2008 incrementProfileCounter(&S); 2009 2010 // If the switch has a condition wrapped by __builtin_unpredictable, 2011 // create metadata that specifies that the switch is unpredictable. 2012 // Don't bother if not optimizing because that metadata would not be used. 2013 auto *Call = dyn_cast<CallExpr>(S.getCond()); 2014 if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) { 2015 auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl()); 2016 if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) { 2017 llvm::MDBuilder MDHelper(getLLVMContext()); 2018 SwitchInsn->setMetadata(llvm::LLVMContext::MD_unpredictable, 2019 MDHelper.createUnpredictable()); 2020 } 2021 } 2022 2023 if (SwitchWeights) { 2024 assert(SwitchWeights->size() == 1 + SwitchInsn->getNumCases() && 2025 "switch weights do not match switch cases"); 2026 // If there's only one jump destination there's no sense weighting it. 2027 if (SwitchWeights->size() > 1) 2028 SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof, 2029 createProfileWeights(*SwitchWeights)); 2030 delete SwitchWeights; 2031 } else if (SwitchLikelihood) { 2032 assert(SwitchLikelihood->size() == 1 + SwitchInsn->getNumCases() && 2033 "switch likelihoods do not match switch cases"); 2034 Optional<SmallVector<uint64_t, 16>> LHW = 2035 getLikelihoodWeights(*SwitchLikelihood); 2036 if (LHW) { 2037 llvm::MDBuilder MDHelper(CGM.getLLVMContext()); 2038 SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof, 2039 createProfileWeights(*LHW)); 2040 } 2041 delete SwitchLikelihood; 2042 } 2043 SwitchInsn = SavedSwitchInsn; 2044 SwitchWeights = SavedSwitchWeights; 2045 SwitchLikelihood = SavedSwitchLikelihood; 2046 CaseRangeBlock = SavedCRBlock; 2047 } 2048 2049 static std::string 2050 SimplifyConstraint(const char *Constraint, const TargetInfo &Target, 2051 SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=nullptr) { 2052 std::string Result; 2053 2054 while (*Constraint) { 2055 switch (*Constraint) { 2056 default: 2057 Result += Target.convertConstraint(Constraint); 2058 break; 2059 // Ignore these 2060 case '*': 2061 case '?': 2062 case '!': 2063 case '=': // Will see this and the following in mult-alt constraints. 2064 case '+': 2065 break; 2066 case '#': // Ignore the rest of the constraint alternative. 2067 while (Constraint[1] && Constraint[1] != ',') 2068 Constraint++; 2069 break; 2070 case '&': 2071 case '%': 2072 Result += *Constraint; 2073 while (Constraint[1] && Constraint[1] == *Constraint) 2074 Constraint++; 2075 break; 2076 case ',': 2077 Result += "|"; 2078 break; 2079 case 'g': 2080 Result += "imr"; 2081 break; 2082 case '[': { 2083 assert(OutCons && 2084 "Must pass output names to constraints with a symbolic name"); 2085 unsigned Index; 2086 bool result = Target.resolveSymbolicName(Constraint, *OutCons, Index); 2087 assert(result && "Could not resolve symbolic name"); (void)result; 2088 Result += llvm::utostr(Index); 2089 break; 2090 } 2091 } 2092 2093 Constraint++; 2094 } 2095 2096 return Result; 2097 } 2098 2099 /// AddVariableConstraints - Look at AsmExpr and if it is a variable declared 2100 /// as using a particular register add that as a constraint that will be used 2101 /// in this asm stmt. 2102 static std::string 2103 AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr, 2104 const TargetInfo &Target, CodeGenModule &CGM, 2105 const AsmStmt &Stmt, const bool EarlyClobber, 2106 std::string *GCCReg = nullptr) { 2107 const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr); 2108 if (!AsmDeclRef) 2109 return Constraint; 2110 const ValueDecl &Value = *AsmDeclRef->getDecl(); 2111 const VarDecl *Variable = dyn_cast<VarDecl>(&Value); 2112 if (!Variable) 2113 return Constraint; 2114 if (Variable->getStorageClass() != SC_Register) 2115 return Constraint; 2116 AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>(); 2117 if (!Attr) 2118 return Constraint; 2119 StringRef Register = Attr->getLabel(); 2120 assert(Target.isValidGCCRegisterName(Register)); 2121 // We're using validateOutputConstraint here because we only care if 2122 // this is a register constraint. 2123 TargetInfo::ConstraintInfo Info(Constraint, ""); 2124 if (Target.validateOutputConstraint(Info) && 2125 !Info.allowsRegister()) { 2126 CGM.ErrorUnsupported(&Stmt, "__asm__"); 2127 return Constraint; 2128 } 2129 // Canonicalize the register here before returning it. 2130 Register = Target.getNormalizedGCCRegisterName(Register); 2131 if (GCCReg != nullptr) 2132 *GCCReg = Register.str(); 2133 return (EarlyClobber ? "&{" : "{") + Register.str() + "}"; 2134 } 2135 2136 std::pair<llvm::Value*, llvm::Type *> CodeGenFunction::EmitAsmInputLValue( 2137 const TargetInfo::ConstraintInfo &Info, LValue InputValue, 2138 QualType InputType, std::string &ConstraintStr, SourceLocation Loc) { 2139 if (Info.allowsRegister() || !Info.allowsMemory()) { 2140 if (CodeGenFunction::hasScalarEvaluationKind(InputType)) 2141 return {EmitLoadOfLValue(InputValue, Loc).getScalarVal(), nullptr}; 2142 2143 llvm::Type *Ty = ConvertType(InputType); 2144 uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty); 2145 if ((Size <= 64 && llvm::isPowerOf2_64(Size)) || 2146 getTargetHooks().isScalarizableAsmOperand(*this, Ty)) { 2147 Ty = llvm::IntegerType::get(getLLVMContext(), Size); 2148 2149 return {Builder.CreateLoad(Builder.CreateElementBitCast( 2150 InputValue.getAddress(*this), Ty)), 2151 nullptr}; 2152 } 2153 } 2154 2155 Address Addr = InputValue.getAddress(*this); 2156 ConstraintStr += '*'; 2157 return {Addr.getPointer(), Addr.getElementType()}; 2158 } 2159 2160 std::pair<llvm::Value *, llvm::Type *> 2161 CodeGenFunction::EmitAsmInput(const TargetInfo::ConstraintInfo &Info, 2162 const Expr *InputExpr, 2163 std::string &ConstraintStr) { 2164 // If this can't be a register or memory, i.e., has to be a constant 2165 // (immediate or symbolic), try to emit it as such. 2166 if (!Info.allowsRegister() && !Info.allowsMemory()) { 2167 if (Info.requiresImmediateConstant()) { 2168 Expr::EvalResult EVResult; 2169 InputExpr->EvaluateAsRValue(EVResult, getContext(), true); 2170 2171 llvm::APSInt IntResult; 2172 if (EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(), 2173 getContext())) 2174 return {llvm::ConstantInt::get(getLLVMContext(), IntResult), nullptr}; 2175 } 2176 2177 Expr::EvalResult Result; 2178 if (InputExpr->EvaluateAsInt(Result, getContext())) 2179 return {llvm::ConstantInt::get(getLLVMContext(), Result.Val.getInt()), 2180 nullptr}; 2181 } 2182 2183 if (Info.allowsRegister() || !Info.allowsMemory()) 2184 if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType())) 2185 return {EmitScalarExpr(InputExpr), nullptr}; 2186 if (InputExpr->getStmtClass() == Expr::CXXThisExprClass) 2187 return {EmitScalarExpr(InputExpr), nullptr}; 2188 InputExpr = InputExpr->IgnoreParenNoopCasts(getContext()); 2189 LValue Dest = EmitLValue(InputExpr); 2190 return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr, 2191 InputExpr->getExprLoc()); 2192 } 2193 2194 /// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline 2195 /// asm call instruction. The !srcloc MDNode contains a list of constant 2196 /// integers which are the source locations of the start of each line in the 2197 /// asm. 2198 static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str, 2199 CodeGenFunction &CGF) { 2200 SmallVector<llvm::Metadata *, 8> Locs; 2201 // Add the location of the first line to the MDNode. 2202 Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( 2203 CGF.Int64Ty, Str->getBeginLoc().getRawEncoding()))); 2204 StringRef StrVal = Str->getString(); 2205 if (!StrVal.empty()) { 2206 const SourceManager &SM = CGF.CGM.getContext().getSourceManager(); 2207 const LangOptions &LangOpts = CGF.CGM.getLangOpts(); 2208 unsigned StartToken = 0; 2209 unsigned ByteOffset = 0; 2210 2211 // Add the location of the start of each subsequent line of the asm to the 2212 // MDNode. 2213 for (unsigned i = 0, e = StrVal.size() - 1; i != e; ++i) { 2214 if (StrVal[i] != '\n') continue; 2215 SourceLocation LineLoc = Str->getLocationOfByte( 2216 i + 1, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset); 2217 Locs.push_back(llvm::ConstantAsMetadata::get( 2218 llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding()))); 2219 } 2220 } 2221 2222 return llvm::MDNode::get(CGF.getLLVMContext(), Locs); 2223 } 2224 2225 static void UpdateAsmCallInst(llvm::CallBase &Result, bool HasSideEffect, 2226 bool HasUnwindClobber, bool ReadOnly, 2227 bool ReadNone, bool NoMerge, const AsmStmt &S, 2228 const std::vector<llvm::Type *> &ResultRegTypes, 2229 const std::vector<llvm::Type *> &ArgElemTypes, 2230 CodeGenFunction &CGF, 2231 std::vector<llvm::Value *> &RegResults) { 2232 if (!HasUnwindClobber) 2233 Result.addFnAttr(llvm::Attribute::NoUnwind); 2234 2235 if (NoMerge) 2236 Result.addFnAttr(llvm::Attribute::NoMerge); 2237 // Attach readnone and readonly attributes. 2238 if (!HasSideEffect) { 2239 if (ReadNone) 2240 Result.addFnAttr(llvm::Attribute::ReadNone); 2241 else if (ReadOnly) 2242 Result.addFnAttr(llvm::Attribute::ReadOnly); 2243 } 2244 2245 // Add elementtype attribute for indirect constraints. 2246 for (auto Pair : llvm::enumerate(ArgElemTypes)) { 2247 if (Pair.value()) { 2248 auto Attr = llvm::Attribute::get( 2249 CGF.getLLVMContext(), llvm::Attribute::ElementType, Pair.value()); 2250 Result.addParamAttr(Pair.index(), Attr); 2251 } 2252 } 2253 2254 // Slap the source location of the inline asm into a !srcloc metadata on the 2255 // call. 2256 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S)) 2257 Result.setMetadata("srcloc", 2258 getAsmSrcLocInfo(gccAsmStmt->getAsmString(), CGF)); 2259 else { 2260 // At least put the line number on MS inline asm blobs. 2261 llvm::Constant *Loc = 2262 llvm::ConstantInt::get(CGF.Int64Ty, S.getAsmLoc().getRawEncoding()); 2263 Result.setMetadata("srcloc", 2264 llvm::MDNode::get(CGF.getLLVMContext(), 2265 llvm::ConstantAsMetadata::get(Loc))); 2266 } 2267 2268 if (CGF.getLangOpts().assumeFunctionsAreConvergent()) 2269 // Conservatively, mark all inline asm blocks in CUDA or OpenCL as 2270 // convergent (meaning, they may call an intrinsically convergent op, such 2271 // as bar.sync, and so can't have certain optimizations applied around 2272 // them). 2273 Result.addFnAttr(llvm::Attribute::Convergent); 2274 // Extract all of the register value results from the asm. 2275 if (ResultRegTypes.size() == 1) { 2276 RegResults.push_back(&Result); 2277 } else { 2278 for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) { 2279 llvm::Value *Tmp = CGF.Builder.CreateExtractValue(&Result, i, "asmresult"); 2280 RegResults.push_back(Tmp); 2281 } 2282 } 2283 } 2284 2285 void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { 2286 // Assemble the final asm string. 2287 std::string AsmString = S.generateAsmString(getContext()); 2288 2289 // Get all the output and input constraints together. 2290 SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; 2291 SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; 2292 2293 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { 2294 StringRef Name; 2295 if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) 2296 Name = GAS->getOutputName(i); 2297 TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name); 2298 bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid; 2299 assert(IsValid && "Failed to parse output constraint"); 2300 OutputConstraintInfos.push_back(Info); 2301 } 2302 2303 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { 2304 StringRef Name; 2305 if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) 2306 Name = GAS->getInputName(i); 2307 TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name); 2308 bool IsValid = 2309 getTarget().validateInputConstraint(OutputConstraintInfos, Info); 2310 assert(IsValid && "Failed to parse input constraint"); (void)IsValid; 2311 InputConstraintInfos.push_back(Info); 2312 } 2313 2314 std::string Constraints; 2315 2316 std::vector<LValue> ResultRegDests; 2317 std::vector<QualType> ResultRegQualTys; 2318 std::vector<llvm::Type *> ResultRegTypes; 2319 std::vector<llvm::Type *> ResultTruncRegTypes; 2320 std::vector<llvm::Type *> ArgTypes; 2321 std::vector<llvm::Type *> ArgElemTypes; 2322 std::vector<llvm::Value*> Args; 2323 llvm::BitVector ResultTypeRequiresCast; 2324 2325 // Keep track of inout constraints. 2326 std::string InOutConstraints; 2327 std::vector<llvm::Value*> InOutArgs; 2328 std::vector<llvm::Type*> InOutArgTypes; 2329 std::vector<llvm::Type*> InOutArgElemTypes; 2330 2331 // Keep track of out constraints for tied input operand. 2332 std::vector<std::string> OutputConstraints; 2333 2334 // Keep track of defined physregs. 2335 llvm::SmallSet<std::string, 8> PhysRegOutputs; 2336 2337 // An inline asm can be marked readonly if it meets the following conditions: 2338 // - it doesn't have any sideeffects 2339 // - it doesn't clobber memory 2340 // - it doesn't return a value by-reference 2341 // It can be marked readnone if it doesn't have any input memory constraints 2342 // in addition to meeting the conditions listed above. 2343 bool ReadOnly = true, ReadNone = true; 2344 2345 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { 2346 TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; 2347 2348 // Simplify the output constraint. 2349 std::string OutputConstraint(S.getOutputConstraint(i)); 2350 OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, 2351 getTarget(), &OutputConstraintInfos); 2352 2353 const Expr *OutExpr = S.getOutputExpr(i); 2354 OutExpr = OutExpr->IgnoreParenNoopCasts(getContext()); 2355 2356 std::string GCCReg; 2357 OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr, 2358 getTarget(), CGM, S, 2359 Info.earlyClobber(), 2360 &GCCReg); 2361 // Give an error on multiple outputs to same physreg. 2362 if (!GCCReg.empty() && !PhysRegOutputs.insert(GCCReg).second) 2363 CGM.Error(S.getAsmLoc(), "multiple outputs to hard register: " + GCCReg); 2364 2365 OutputConstraints.push_back(OutputConstraint); 2366 LValue Dest = EmitLValue(OutExpr); 2367 if (!Constraints.empty()) 2368 Constraints += ','; 2369 2370 // If this is a register output, then make the inline asm return it 2371 // by-value. If this is a memory result, return the value by-reference. 2372 QualType QTy = OutExpr->getType(); 2373 const bool IsScalarOrAggregate = hasScalarEvaluationKind(QTy) || 2374 hasAggregateEvaluationKind(QTy); 2375 if (!Info.allowsMemory() && IsScalarOrAggregate) { 2376 2377 Constraints += "=" + OutputConstraint; 2378 ResultRegQualTys.push_back(QTy); 2379 ResultRegDests.push_back(Dest); 2380 2381 llvm::Type *Ty = ConvertTypeForMem(QTy); 2382 const bool RequiresCast = Info.allowsRegister() && 2383 (getTargetHooks().isScalarizableAsmOperand(*this, Ty) || 2384 Ty->isAggregateType()); 2385 2386 ResultTruncRegTypes.push_back(Ty); 2387 ResultTypeRequiresCast.push_back(RequiresCast); 2388 2389 if (RequiresCast) { 2390 unsigned Size = getContext().getTypeSize(QTy); 2391 Ty = llvm::IntegerType::get(getLLVMContext(), Size); 2392 } 2393 ResultRegTypes.push_back(Ty); 2394 // If this output is tied to an input, and if the input is larger, then 2395 // we need to set the actual result type of the inline asm node to be the 2396 // same as the input type. 2397 if (Info.hasMatchingInput()) { 2398 unsigned InputNo; 2399 for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) { 2400 TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo]; 2401 if (Input.hasTiedOperand() && Input.getTiedOperand() == i) 2402 break; 2403 } 2404 assert(InputNo != S.getNumInputs() && "Didn't find matching input!"); 2405 2406 QualType InputTy = S.getInputExpr(InputNo)->getType(); 2407 QualType OutputType = OutExpr->getType(); 2408 2409 uint64_t InputSize = getContext().getTypeSize(InputTy); 2410 if (getContext().getTypeSize(OutputType) < InputSize) { 2411 // Form the asm to return the value as a larger integer or fp type. 2412 ResultRegTypes.back() = ConvertType(InputTy); 2413 } 2414 } 2415 if (llvm::Type* AdjTy = 2416 getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, 2417 ResultRegTypes.back())) 2418 ResultRegTypes.back() = AdjTy; 2419 else { 2420 CGM.getDiags().Report(S.getAsmLoc(), 2421 diag::err_asm_invalid_type_in_input) 2422 << OutExpr->getType() << OutputConstraint; 2423 } 2424 2425 // Update largest vector width for any vector types. 2426 if (auto *VT = dyn_cast<llvm::VectorType>(ResultRegTypes.back())) 2427 LargestVectorWidth = 2428 std::max((uint64_t)LargestVectorWidth, 2429 VT->getPrimitiveSizeInBits().getKnownMinSize()); 2430 } else { 2431 Address DestAddr = Dest.getAddress(*this); 2432 // Matrix types in memory are represented by arrays, but accessed through 2433 // vector pointers, with the alignment specified on the access operation. 2434 // For inline assembly, update pointer arguments to use vector pointers. 2435 // Otherwise there will be a mis-match if the matrix is also an 2436 // input-argument which is represented as vector. 2437 if (isa<MatrixType>(OutExpr->getType().getCanonicalType())) 2438 DestAddr = Builder.CreateElementBitCast( 2439 DestAddr, ConvertType(OutExpr->getType())); 2440 2441 ArgTypes.push_back(DestAddr.getType()); 2442 ArgElemTypes.push_back(DestAddr.getElementType()); 2443 Args.push_back(DestAddr.getPointer()); 2444 Constraints += "=*"; 2445 Constraints += OutputConstraint; 2446 ReadOnly = ReadNone = false; 2447 } 2448 2449 if (Info.isReadWrite()) { 2450 InOutConstraints += ','; 2451 2452 const Expr *InputExpr = S.getOutputExpr(i); 2453 llvm::Value *Arg; 2454 llvm::Type *ArgElemType; 2455 std::tie(Arg, ArgElemType) = EmitAsmInputLValue( 2456 Info, Dest, InputExpr->getType(), InOutConstraints, 2457 InputExpr->getExprLoc()); 2458 2459 if (llvm::Type* AdjTy = 2460 getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, 2461 Arg->getType())) 2462 Arg = Builder.CreateBitCast(Arg, AdjTy); 2463 2464 // Update largest vector width for any vector types. 2465 if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType())) 2466 LargestVectorWidth = 2467 std::max((uint64_t)LargestVectorWidth, 2468 VT->getPrimitiveSizeInBits().getKnownMinSize()); 2469 // Only tie earlyclobber physregs. 2470 if (Info.allowsRegister() && (GCCReg.empty() || Info.earlyClobber())) 2471 InOutConstraints += llvm::utostr(i); 2472 else 2473 InOutConstraints += OutputConstraint; 2474 2475 InOutArgTypes.push_back(Arg->getType()); 2476 InOutArgElemTypes.push_back(ArgElemType); 2477 InOutArgs.push_back(Arg); 2478 } 2479 } 2480 2481 // If this is a Microsoft-style asm blob, store the return registers (EAX:EDX) 2482 // to the return value slot. Only do this when returning in registers. 2483 if (isa<MSAsmStmt>(&S)) { 2484 const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo(); 2485 if (RetAI.isDirect() || RetAI.isExtend()) { 2486 // Make a fake lvalue for the return value slot. 2487 LValue ReturnSlot = MakeAddrLValueWithoutTBAA(ReturnValue, FnRetTy); 2488 CGM.getTargetCodeGenInfo().addReturnRegisterOutputs( 2489 *this, ReturnSlot, Constraints, ResultRegTypes, ResultTruncRegTypes, 2490 ResultRegDests, AsmString, S.getNumOutputs()); 2491 SawAsmBlock = true; 2492 } 2493 } 2494 2495 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { 2496 const Expr *InputExpr = S.getInputExpr(i); 2497 2498 TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; 2499 2500 if (Info.allowsMemory()) 2501 ReadNone = false; 2502 2503 if (!Constraints.empty()) 2504 Constraints += ','; 2505 2506 // Simplify the input constraint. 2507 std::string InputConstraint(S.getInputConstraint(i)); 2508 InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(), 2509 &OutputConstraintInfos); 2510 2511 InputConstraint = AddVariableConstraints( 2512 InputConstraint, *InputExpr->IgnoreParenNoopCasts(getContext()), 2513 getTarget(), CGM, S, false /* No EarlyClobber */); 2514 2515 std::string ReplaceConstraint (InputConstraint); 2516 llvm::Value *Arg; 2517 llvm::Type *ArgElemType; 2518 std::tie(Arg, ArgElemType) = EmitAsmInput(Info, InputExpr, Constraints); 2519 2520 // If this input argument is tied to a larger output result, extend the 2521 // input to be the same size as the output. The LLVM backend wants to see 2522 // the input and output of a matching constraint be the same size. Note 2523 // that GCC does not define what the top bits are here. We use zext because 2524 // that is usually cheaper, but LLVM IR should really get an anyext someday. 2525 if (Info.hasTiedOperand()) { 2526 unsigned Output = Info.getTiedOperand(); 2527 QualType OutputType = S.getOutputExpr(Output)->getType(); 2528 QualType InputTy = InputExpr->getType(); 2529 2530 if (getContext().getTypeSize(OutputType) > 2531 getContext().getTypeSize(InputTy)) { 2532 // Use ptrtoint as appropriate so that we can do our extension. 2533 if (isa<llvm::PointerType>(Arg->getType())) 2534 Arg = Builder.CreatePtrToInt(Arg, IntPtrTy); 2535 llvm::Type *OutputTy = ConvertType(OutputType); 2536 if (isa<llvm::IntegerType>(OutputTy)) 2537 Arg = Builder.CreateZExt(Arg, OutputTy); 2538 else if (isa<llvm::PointerType>(OutputTy)) 2539 Arg = Builder.CreateZExt(Arg, IntPtrTy); 2540 else if (OutputTy->isFloatingPointTy()) 2541 Arg = Builder.CreateFPExt(Arg, OutputTy); 2542 } 2543 // Deal with the tied operands' constraint code in adjustInlineAsmType. 2544 ReplaceConstraint = OutputConstraints[Output]; 2545 } 2546 if (llvm::Type* AdjTy = 2547 getTargetHooks().adjustInlineAsmType(*this, ReplaceConstraint, 2548 Arg->getType())) 2549 Arg = Builder.CreateBitCast(Arg, AdjTy); 2550 else 2551 CGM.getDiags().Report(S.getAsmLoc(), diag::err_asm_invalid_type_in_input) 2552 << InputExpr->getType() << InputConstraint; 2553 2554 // Update largest vector width for any vector types. 2555 if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType())) 2556 LargestVectorWidth = 2557 std::max((uint64_t)LargestVectorWidth, 2558 VT->getPrimitiveSizeInBits().getKnownMinSize()); 2559 2560 ArgTypes.push_back(Arg->getType()); 2561 ArgElemTypes.push_back(ArgElemType); 2562 Args.push_back(Arg); 2563 Constraints += InputConstraint; 2564 } 2565 2566 // Append the "input" part of inout constraints. 2567 for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) { 2568 ArgTypes.push_back(InOutArgTypes[i]); 2569 ArgElemTypes.push_back(InOutArgElemTypes[i]); 2570 Args.push_back(InOutArgs[i]); 2571 } 2572 Constraints += InOutConstraints; 2573 2574 // Labels 2575 SmallVector<llvm::BasicBlock *, 16> Transfer; 2576 llvm::BasicBlock *Fallthrough = nullptr; 2577 bool IsGCCAsmGoto = false; 2578 if (const auto *GS = dyn_cast<GCCAsmStmt>(&S)) { 2579 IsGCCAsmGoto = GS->isAsmGoto(); 2580 if (IsGCCAsmGoto) { 2581 for (const auto *E : GS->labels()) { 2582 JumpDest Dest = getJumpDestForLabel(E->getLabel()); 2583 Transfer.push_back(Dest.getBlock()); 2584 llvm::BlockAddress *BA = 2585 llvm::BlockAddress::get(CurFn, Dest.getBlock()); 2586 Args.push_back(BA); 2587 ArgTypes.push_back(BA->getType()); 2588 ArgElemTypes.push_back(nullptr); 2589 if (!Constraints.empty()) 2590 Constraints += ','; 2591 Constraints += 'i'; 2592 } 2593 Fallthrough = createBasicBlock("asm.fallthrough"); 2594 } 2595 } 2596 2597 bool HasUnwindClobber = false; 2598 2599 // Clobbers 2600 for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) { 2601 StringRef Clobber = S.getClobber(i); 2602 2603 if (Clobber == "memory") 2604 ReadOnly = ReadNone = false; 2605 else if (Clobber == "unwind") { 2606 HasUnwindClobber = true; 2607 continue; 2608 } else if (Clobber != "cc") { 2609 Clobber = getTarget().getNormalizedGCCRegisterName(Clobber); 2610 if (CGM.getCodeGenOpts().StackClashProtector && 2611 getTarget().isSPRegName(Clobber)) { 2612 CGM.getDiags().Report(S.getAsmLoc(), 2613 diag::warn_stack_clash_protection_inline_asm); 2614 } 2615 } 2616 2617 if (isa<MSAsmStmt>(&S)) { 2618 if (Clobber == "eax" || Clobber == "edx") { 2619 if (Constraints.find("=&A") != std::string::npos) 2620 continue; 2621 std::string::size_type position1 = 2622 Constraints.find("={" + Clobber.str() + "}"); 2623 if (position1 != std::string::npos) { 2624 Constraints.insert(position1 + 1, "&"); 2625 continue; 2626 } 2627 std::string::size_type position2 = Constraints.find("=A"); 2628 if (position2 != std::string::npos) { 2629 Constraints.insert(position2 + 1, "&"); 2630 continue; 2631 } 2632 } 2633 } 2634 if (!Constraints.empty()) 2635 Constraints += ','; 2636 2637 Constraints += "~{"; 2638 Constraints += Clobber; 2639 Constraints += '}'; 2640 } 2641 2642 assert(!(HasUnwindClobber && IsGCCAsmGoto) && 2643 "unwind clobber can't be used with asm goto"); 2644 2645 // Add machine specific clobbers 2646 std::string MachineClobbers = getTarget().getClobbers(); 2647 if (!MachineClobbers.empty()) { 2648 if (!Constraints.empty()) 2649 Constraints += ','; 2650 Constraints += MachineClobbers; 2651 } 2652 2653 llvm::Type *ResultType; 2654 if (ResultRegTypes.empty()) 2655 ResultType = VoidTy; 2656 else if (ResultRegTypes.size() == 1) 2657 ResultType = ResultRegTypes[0]; 2658 else 2659 ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes); 2660 2661 llvm::FunctionType *FTy = 2662 llvm::FunctionType::get(ResultType, ArgTypes, false); 2663 2664 bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0; 2665 2666 llvm::InlineAsm::AsmDialect GnuAsmDialect = 2667 CGM.getCodeGenOpts().getInlineAsmDialect() == CodeGenOptions::IAD_ATT 2668 ? llvm::InlineAsm::AD_ATT 2669 : llvm::InlineAsm::AD_Intel; 2670 llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ? 2671 llvm::InlineAsm::AD_Intel : GnuAsmDialect; 2672 2673 llvm::InlineAsm *IA = llvm::InlineAsm::get( 2674 FTy, AsmString, Constraints, HasSideEffect, 2675 /* IsAlignStack */ false, AsmDialect, HasUnwindClobber); 2676 std::vector<llvm::Value*> RegResults; 2677 if (IsGCCAsmGoto) { 2678 llvm::CallBrInst *Result = 2679 Builder.CreateCallBr(IA, Fallthrough, Transfer, Args); 2680 EmitBlock(Fallthrough); 2681 UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, false, 2682 ReadOnly, ReadNone, InNoMergeAttributedStmt, S, 2683 ResultRegTypes, ArgElemTypes, *this, RegResults); 2684 } else if (HasUnwindClobber) { 2685 llvm::CallBase *Result = EmitCallOrInvoke(IA, Args, ""); 2686 UpdateAsmCallInst(*Result, HasSideEffect, true, ReadOnly, ReadNone, 2687 InNoMergeAttributedStmt, S, ResultRegTypes, ArgElemTypes, 2688 *this, RegResults); 2689 } else { 2690 llvm::CallInst *Result = 2691 Builder.CreateCall(IA, Args, getBundlesForFunclet(IA)); 2692 UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, false, 2693 ReadOnly, ReadNone, InNoMergeAttributedStmt, S, 2694 ResultRegTypes, ArgElemTypes, *this, RegResults); 2695 } 2696 2697 assert(RegResults.size() == ResultRegTypes.size()); 2698 assert(RegResults.size() == ResultTruncRegTypes.size()); 2699 assert(RegResults.size() == ResultRegDests.size()); 2700 // ResultRegDests can be also populated by addReturnRegisterOutputs() above, 2701 // in which case its size may grow. 2702 assert(ResultTypeRequiresCast.size() <= ResultRegDests.size()); 2703 for (unsigned i = 0, e = RegResults.size(); i != e; ++i) { 2704 llvm::Value *Tmp = RegResults[i]; 2705 llvm::Type *TruncTy = ResultTruncRegTypes[i]; 2706 2707 // If the result type of the LLVM IR asm doesn't match the result type of 2708 // the expression, do the conversion. 2709 if (ResultRegTypes[i] != ResultTruncRegTypes[i]) { 2710 2711 // Truncate the integer result to the right size, note that TruncTy can be 2712 // a pointer. 2713 if (TruncTy->isFloatingPointTy()) 2714 Tmp = Builder.CreateFPTrunc(Tmp, TruncTy); 2715 else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) { 2716 uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy); 2717 Tmp = Builder.CreateTrunc(Tmp, 2718 llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize)); 2719 Tmp = Builder.CreateIntToPtr(Tmp, TruncTy); 2720 } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) { 2721 uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType()); 2722 Tmp = Builder.CreatePtrToInt(Tmp, 2723 llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize)); 2724 Tmp = Builder.CreateTrunc(Tmp, TruncTy); 2725 } else if (TruncTy->isIntegerTy()) { 2726 Tmp = Builder.CreateZExtOrTrunc(Tmp, TruncTy); 2727 } else if (TruncTy->isVectorTy()) { 2728 Tmp = Builder.CreateBitCast(Tmp, TruncTy); 2729 } 2730 } 2731 2732 LValue Dest = ResultRegDests[i]; 2733 // ResultTypeRequiresCast elements correspond to the first 2734 // ResultTypeRequiresCast.size() elements of RegResults. 2735 if ((i < ResultTypeRequiresCast.size()) && ResultTypeRequiresCast[i]) { 2736 unsigned Size = getContext().getTypeSize(ResultRegQualTys[i]); 2737 Address A = Builder.CreateElementBitCast(Dest.getAddress(*this), 2738 ResultRegTypes[i]); 2739 if (getTargetHooks().isScalarizableAsmOperand(*this, TruncTy)) { 2740 Builder.CreateStore(Tmp, A); 2741 continue; 2742 } 2743 2744 QualType Ty = getContext().getIntTypeForBitwidth(Size, /*Signed*/ false); 2745 if (Ty.isNull()) { 2746 const Expr *OutExpr = S.getOutputExpr(i); 2747 CGM.Error( 2748 OutExpr->getExprLoc(), 2749 "impossible constraint in asm: can't store value into a register"); 2750 return; 2751 } 2752 Dest = MakeAddrLValue(A, Ty); 2753 } 2754 EmitStoreThroughLValue(RValue::get(Tmp), Dest); 2755 } 2756 } 2757 2758 LValue CodeGenFunction::InitCapturedStruct(const CapturedStmt &S) { 2759 const RecordDecl *RD = S.getCapturedRecordDecl(); 2760 QualType RecordTy = getContext().getRecordType(RD); 2761 2762 // Initialize the captured struct. 2763 LValue SlotLV = 2764 MakeAddrLValue(CreateMemTemp(RecordTy, "agg.captured"), RecordTy); 2765 2766 RecordDecl::field_iterator CurField = RD->field_begin(); 2767 for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), 2768 E = S.capture_init_end(); 2769 I != E; ++I, ++CurField) { 2770 LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField); 2771 if (CurField->hasCapturedVLAType()) { 2772 EmitLambdaVLACapture(CurField->getCapturedVLAType(), LV); 2773 } else { 2774 EmitInitializerForField(*CurField, LV, *I); 2775 } 2776 } 2777 2778 return SlotLV; 2779 } 2780 2781 /// Generate an outlined function for the body of a CapturedStmt, store any 2782 /// captured variables into the captured struct, and call the outlined function. 2783 llvm::Function * 2784 CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) { 2785 LValue CapStruct = InitCapturedStruct(S); 2786 2787 // Emit the CapturedDecl 2788 CodeGenFunction CGF(CGM, true); 2789 CGCapturedStmtRAII CapInfoRAII(CGF, new CGCapturedStmtInfo(S, K)); 2790 llvm::Function *F = CGF.GenerateCapturedStmtFunction(S); 2791 delete CGF.CapturedStmtInfo; 2792 2793 // Emit call to the helper function. 2794 EmitCallOrInvoke(F, CapStruct.getPointer(*this)); 2795 2796 return F; 2797 } 2798 2799 Address CodeGenFunction::GenerateCapturedStmtArgument(const CapturedStmt &S) { 2800 LValue CapStruct = InitCapturedStruct(S); 2801 return CapStruct.getAddress(*this); 2802 } 2803 2804 /// Creates the outlined function for a CapturedStmt. 2805 llvm::Function * 2806 CodeGenFunction::GenerateCapturedStmtFunction(const CapturedStmt &S) { 2807 assert(CapturedStmtInfo && 2808 "CapturedStmtInfo should be set when generating the captured function"); 2809 const CapturedDecl *CD = S.getCapturedDecl(); 2810 const RecordDecl *RD = S.getCapturedRecordDecl(); 2811 SourceLocation Loc = S.getBeginLoc(); 2812 assert(CD->hasBody() && "missing CapturedDecl body"); 2813 2814 // Build the argument list. 2815 ASTContext &Ctx = CGM.getContext(); 2816 FunctionArgList Args; 2817 Args.append(CD->param_begin(), CD->param_end()); 2818 2819 // Create the function declaration. 2820 const CGFunctionInfo &FuncInfo = 2821 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args); 2822 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); 2823 2824 llvm::Function *F = 2825 llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, 2826 CapturedStmtInfo->getHelperName(), &CGM.getModule()); 2827 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); 2828 if (CD->isNothrow()) 2829 F->addFnAttr(llvm::Attribute::NoUnwind); 2830 2831 // Generate the function. 2832 StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(), 2833 CD->getBody()->getBeginLoc()); 2834 // Set the context parameter in CapturedStmtInfo. 2835 Address DeclPtr = GetAddrOfLocalVar(CD->getContextParam()); 2836 CapturedStmtInfo->setContextValue(Builder.CreateLoad(DeclPtr)); 2837 2838 // Initialize variable-length arrays. 2839 LValue Base = MakeNaturalAlignAddrLValue(CapturedStmtInfo->getContextValue(), 2840 Ctx.getTagDeclType(RD)); 2841 for (auto *FD : RD->fields()) { 2842 if (FD->hasCapturedVLAType()) { 2843 auto *ExprArg = 2844 EmitLoadOfLValue(EmitLValueForField(Base, FD), S.getBeginLoc()) 2845 .getScalarVal(); 2846 auto VAT = FD->getCapturedVLAType(); 2847 VLASizeMap[VAT->getSizeExpr()] = ExprArg; 2848 } 2849 } 2850 2851 // If 'this' is captured, load it into CXXThisValue. 2852 if (CapturedStmtInfo->isCXXThisExprCaptured()) { 2853 FieldDecl *FD = CapturedStmtInfo->getThisFieldDecl(); 2854 LValue ThisLValue = EmitLValueForField(Base, FD); 2855 CXXThisValue = EmitLoadOfLValue(ThisLValue, Loc).getScalarVal(); 2856 } 2857 2858 PGO.assignRegionCounters(GlobalDecl(CD), F); 2859 CapturedStmtInfo->EmitBody(*this, CD->getBody()); 2860 FinishFunction(CD->getBodyRBrace()); 2861 2862 return F; 2863 } 2864