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