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