1 //===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===// 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 is the internal per-function state used for llvm translation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H 14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H 15 16 #include "CGBuilder.h" 17 #include "CGDebugInfo.h" 18 #include "CGLoopInfo.h" 19 #include "CGValue.h" 20 #include "CodeGenModule.h" 21 #include "CodeGenPGO.h" 22 #include "EHScopeStack.h" 23 #include "VarBypassDetector.h" 24 #include "clang/AST/CharUnits.h" 25 #include "clang/AST/CurrentSourceLocExprScope.h" 26 #include "clang/AST/ExprCXX.h" 27 #include "clang/AST/ExprObjC.h" 28 #include "clang/AST/ExprOpenMP.h" 29 #include "clang/AST/StmtOpenMP.h" 30 #include "clang/AST/Type.h" 31 #include "clang/Basic/ABI.h" 32 #include "clang/Basic/CapturedStmt.h" 33 #include "clang/Basic/CodeGenOptions.h" 34 #include "clang/Basic/OpenMPKinds.h" 35 #include "clang/Basic/TargetInfo.h" 36 #include "llvm/ADT/ArrayRef.h" 37 #include "llvm/ADT/DenseMap.h" 38 #include "llvm/ADT/MapVector.h" 39 #include "llvm/ADT/SmallVector.h" 40 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 41 #include "llvm/IR/ValueHandle.h" 42 #include "llvm/Support/Debug.h" 43 #include "llvm/Transforms/Utils/SanitizerStats.h" 44 45 namespace llvm { 46 class BasicBlock; 47 class LLVMContext; 48 class MDNode; 49 class Module; 50 class SwitchInst; 51 class Twine; 52 class Value; 53 class CanonicalLoopInfo; 54 } 55 56 namespace clang { 57 class ASTContext; 58 class BlockDecl; 59 class CXXDestructorDecl; 60 class CXXForRangeStmt; 61 class CXXTryStmt; 62 class Decl; 63 class LabelDecl; 64 class EnumConstantDecl; 65 class FunctionDecl; 66 class FunctionProtoType; 67 class LabelStmt; 68 class ObjCContainerDecl; 69 class ObjCInterfaceDecl; 70 class ObjCIvarDecl; 71 class ObjCMethodDecl; 72 class ObjCImplementationDecl; 73 class ObjCPropertyImplDecl; 74 class TargetInfo; 75 class VarDecl; 76 class ObjCForCollectionStmt; 77 class ObjCAtTryStmt; 78 class ObjCAtThrowStmt; 79 class ObjCAtSynchronizedStmt; 80 class ObjCAutoreleasePoolStmt; 81 class OMPUseDevicePtrClause; 82 class OMPUseDeviceAddrClause; 83 class ReturnsNonNullAttr; 84 class SVETypeFlags; 85 class OMPExecutableDirective; 86 87 namespace analyze_os_log { 88 class OSLogBufferLayout; 89 } 90 91 namespace CodeGen { 92 class CodeGenTypes; 93 class CGCallee; 94 class CGFunctionInfo; 95 class CGRecordLayout; 96 class CGBlockInfo; 97 class CGCXXABI; 98 class BlockByrefHelpers; 99 class BlockByrefInfo; 100 class BlockFlags; 101 class BlockFieldFlags; 102 class RegionCodeGenTy; 103 class TargetCodeGenInfo; 104 struct OMPTaskDataTy; 105 struct CGCoroData; 106 107 /// The kind of evaluation to perform on values of a particular 108 /// type. Basically, is the code in CGExprScalar, CGExprComplex, or 109 /// CGExprAgg? 110 /// 111 /// TODO: should vectors maybe be split out into their own thing? 112 enum TypeEvaluationKind { 113 TEK_Scalar, 114 TEK_Complex, 115 TEK_Aggregate 116 }; 117 118 #define LIST_SANITIZER_CHECKS \ 119 SANITIZER_CHECK(AddOverflow, add_overflow, 0) \ 120 SANITIZER_CHECK(BuiltinUnreachable, builtin_unreachable, 0) \ 121 SANITIZER_CHECK(CFICheckFail, cfi_check_fail, 0) \ 122 SANITIZER_CHECK(DivremOverflow, divrem_overflow, 0) \ 123 SANITIZER_CHECK(DynamicTypeCacheMiss, dynamic_type_cache_miss, 0) \ 124 SANITIZER_CHECK(FloatCastOverflow, float_cast_overflow, 0) \ 125 SANITIZER_CHECK(FunctionTypeMismatch, function_type_mismatch, 1) \ 126 SANITIZER_CHECK(ImplicitConversion, implicit_conversion, 0) \ 127 SANITIZER_CHECK(InvalidBuiltin, invalid_builtin, 0) \ 128 SANITIZER_CHECK(InvalidObjCCast, invalid_objc_cast, 0) \ 129 SANITIZER_CHECK(LoadInvalidValue, load_invalid_value, 0) \ 130 SANITIZER_CHECK(MissingReturn, missing_return, 0) \ 131 SANITIZER_CHECK(MulOverflow, mul_overflow, 0) \ 132 SANITIZER_CHECK(NegateOverflow, negate_overflow, 0) \ 133 SANITIZER_CHECK(NullabilityArg, nullability_arg, 0) \ 134 SANITIZER_CHECK(NullabilityReturn, nullability_return, 1) \ 135 SANITIZER_CHECK(NonnullArg, nonnull_arg, 0) \ 136 SANITIZER_CHECK(NonnullReturn, nonnull_return, 1) \ 137 SANITIZER_CHECK(OutOfBounds, out_of_bounds, 0) \ 138 SANITIZER_CHECK(PointerOverflow, pointer_overflow, 0) \ 139 SANITIZER_CHECK(ShiftOutOfBounds, shift_out_of_bounds, 0) \ 140 SANITIZER_CHECK(SubOverflow, sub_overflow, 0) \ 141 SANITIZER_CHECK(TypeMismatch, type_mismatch, 1) \ 142 SANITIZER_CHECK(AlignmentAssumption, alignment_assumption, 0) \ 143 SANITIZER_CHECK(VLABoundNotPositive, vla_bound_not_positive, 0) 144 145 enum SanitizerHandler { 146 #define SANITIZER_CHECK(Enum, Name, Version) Enum, 147 LIST_SANITIZER_CHECKS 148 #undef SANITIZER_CHECK 149 }; 150 151 /// Helper class with most of the code for saving a value for a 152 /// conditional expression cleanup. 153 struct DominatingLLVMValue { 154 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type; 155 156 /// Answer whether the given value needs extra work to be saved. 157 static bool needsSaving(llvm::Value *value) { 158 // If it's not an instruction, we don't need to save. 159 if (!isa<llvm::Instruction>(value)) return false; 160 161 // If it's an instruction in the entry block, we don't need to save. 162 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent(); 163 return (block != &block->getParent()->getEntryBlock()); 164 } 165 166 static saved_type save(CodeGenFunction &CGF, llvm::Value *value); 167 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value); 168 }; 169 170 /// A partial specialization of DominatingValue for llvm::Values that 171 /// might be llvm::Instructions. 172 template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue { 173 typedef T *type; 174 static type restore(CodeGenFunction &CGF, saved_type value) { 175 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value)); 176 } 177 }; 178 179 /// A specialization of DominatingValue for Address. 180 template <> struct DominatingValue<Address> { 181 typedef Address type; 182 183 struct saved_type { 184 DominatingLLVMValue::saved_type SavedValue; 185 CharUnits Alignment; 186 }; 187 188 static bool needsSaving(type value) { 189 return DominatingLLVMValue::needsSaving(value.getPointer()); 190 } 191 static saved_type save(CodeGenFunction &CGF, type value) { 192 return { DominatingLLVMValue::save(CGF, value.getPointer()), 193 value.getAlignment() }; 194 } 195 static type restore(CodeGenFunction &CGF, saved_type value) { 196 return Address(DominatingLLVMValue::restore(CGF, value.SavedValue), 197 value.Alignment); 198 } 199 }; 200 201 /// A specialization of DominatingValue for RValue. 202 template <> struct DominatingValue<RValue> { 203 typedef RValue type; 204 class saved_type { 205 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral, 206 AggregateAddress, ComplexAddress }; 207 208 llvm::Value *Value; 209 unsigned K : 3; 210 unsigned Align : 29; 211 saved_type(llvm::Value *v, Kind k, unsigned a = 0) 212 : Value(v), K(k), Align(a) {} 213 214 public: 215 static bool needsSaving(RValue value); 216 static saved_type save(CodeGenFunction &CGF, RValue value); 217 RValue restore(CodeGenFunction &CGF); 218 219 // implementations in CGCleanup.cpp 220 }; 221 222 static bool needsSaving(type value) { 223 return saved_type::needsSaving(value); 224 } 225 static saved_type save(CodeGenFunction &CGF, type value) { 226 return saved_type::save(CGF, value); 227 } 228 static type restore(CodeGenFunction &CGF, saved_type value) { 229 return value.restore(CGF); 230 } 231 }; 232 233 /// CodeGenFunction - This class organizes the per-function state that is used 234 /// while generating LLVM code. 235 class CodeGenFunction : public CodeGenTypeCache { 236 CodeGenFunction(const CodeGenFunction &) = delete; 237 void operator=(const CodeGenFunction &) = delete; 238 239 friend class CGCXXABI; 240 public: 241 /// A jump destination is an abstract label, branching to which may 242 /// require a jump out through normal cleanups. 243 struct JumpDest { 244 JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {} 245 JumpDest(llvm::BasicBlock *Block, 246 EHScopeStack::stable_iterator Depth, 247 unsigned Index) 248 : Block(Block), ScopeDepth(Depth), Index(Index) {} 249 250 bool isValid() const { return Block != nullptr; } 251 llvm::BasicBlock *getBlock() const { return Block; } 252 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; } 253 unsigned getDestIndex() const { return Index; } 254 255 // This should be used cautiously. 256 void setScopeDepth(EHScopeStack::stable_iterator depth) { 257 ScopeDepth = depth; 258 } 259 260 private: 261 llvm::BasicBlock *Block; 262 EHScopeStack::stable_iterator ScopeDepth; 263 unsigned Index; 264 }; 265 266 CodeGenModule &CGM; // Per-module state. 267 const TargetInfo &Target; 268 269 // For EH/SEH outlined funclets, this field points to parent's CGF 270 CodeGenFunction *ParentCGF = nullptr; 271 272 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy; 273 LoopInfoStack LoopStack; 274 CGBuilderTy Builder; 275 276 // Stores variables for which we can't generate correct lifetime markers 277 // because of jumps. 278 VarBypassDetector Bypasses; 279 280 /// List of recently emitted OMPCanonicalLoops. 281 /// 282 /// Since OMPCanonicalLoops are nested inside other statements (in particular 283 /// CapturedStmt generated by OMPExecutableDirective and non-perfectly nested 284 /// loops), we cannot directly call OMPEmitOMPCanonicalLoop and receive its 285 /// llvm::CanonicalLoopInfo. Instead, we call EmitStmt and any 286 /// OMPEmitOMPCanonicalLoop called by it will add its CanonicalLoopInfo to 287 /// this stack when done. Entering a new loop requires clearing this list; it 288 /// either means we start parsing a new loop nest (in which case the previous 289 /// loop nest goes out of scope) or a second loop in the same level in which 290 /// case it would be ambiguous into which of the two (or more) loops the loop 291 /// nest would extend. 292 SmallVector<llvm::CanonicalLoopInfo *, 4> OMPLoopNestStack; 293 294 /// Number of nested loop to be consumed by the last surrounding 295 /// loop-associated directive. 296 int ExpectedOMPLoopDepth = 0; 297 298 // CodeGen lambda for loops and support for ordered clause 299 typedef llvm::function_ref<void(CodeGenFunction &, const OMPLoopDirective &, 300 JumpDest)> 301 CodeGenLoopTy; 302 typedef llvm::function_ref<void(CodeGenFunction &, SourceLocation, 303 const unsigned, const bool)> 304 CodeGenOrderedTy; 305 306 // Codegen lambda for loop bounds in worksharing loop constructs 307 typedef llvm::function_ref<std::pair<LValue, LValue>( 308 CodeGenFunction &, const OMPExecutableDirective &S)> 309 CodeGenLoopBoundsTy; 310 311 // Codegen lambda for loop bounds in dispatch-based loop implementation 312 typedef llvm::function_ref<std::pair<llvm::Value *, llvm::Value *>( 313 CodeGenFunction &, const OMPExecutableDirective &S, Address LB, 314 Address UB)> 315 CodeGenDispatchBoundsTy; 316 317 /// CGBuilder insert helper. This function is called after an 318 /// instruction is created using Builder. 319 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, 320 llvm::BasicBlock *BB, 321 llvm::BasicBlock::iterator InsertPt) const; 322 323 /// CurFuncDecl - Holds the Decl for the current outermost 324 /// non-closure context. 325 const Decl *CurFuncDecl; 326 /// CurCodeDecl - This is the inner-most code context, which includes blocks. 327 const Decl *CurCodeDecl; 328 const CGFunctionInfo *CurFnInfo; 329 QualType FnRetTy; 330 llvm::Function *CurFn = nullptr; 331 332 /// Save Parameter Decl for coroutine. 333 llvm::SmallVector<const ParmVarDecl *, 4> FnArgs; 334 335 // Holds coroutine data if the current function is a coroutine. We use a 336 // wrapper to manage its lifetime, so that we don't have to define CGCoroData 337 // in this header. 338 struct CGCoroInfo { 339 std::unique_ptr<CGCoroData> Data; 340 CGCoroInfo(); 341 ~CGCoroInfo(); 342 }; 343 CGCoroInfo CurCoro; 344 345 bool isCoroutine() const { 346 return CurCoro.Data != nullptr; 347 } 348 349 /// CurGD - The GlobalDecl for the current function being compiled. 350 GlobalDecl CurGD; 351 352 /// PrologueCleanupDepth - The cleanup depth enclosing all the 353 /// cleanups associated with the parameters. 354 EHScopeStack::stable_iterator PrologueCleanupDepth; 355 356 /// ReturnBlock - Unified return block. 357 JumpDest ReturnBlock; 358 359 /// ReturnValue - The temporary alloca to hold the return 360 /// value. This is invalid iff the function has no return value. 361 Address ReturnValue = Address::invalid(); 362 363 /// ReturnValuePointer - The temporary alloca to hold a pointer to sret. 364 /// This is invalid if sret is not in use. 365 Address ReturnValuePointer = Address::invalid(); 366 367 /// If a return statement is being visited, this holds the return statment's 368 /// result expression. 369 const Expr *RetExpr = nullptr; 370 371 /// Return true if a label was seen in the current scope. 372 bool hasLabelBeenSeenInCurrentScope() const { 373 if (CurLexicalScope) 374 return CurLexicalScope->hasLabels(); 375 return !LabelMap.empty(); 376 } 377 378 /// AllocaInsertPoint - This is an instruction in the entry block before which 379 /// we prefer to insert allocas. 380 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt; 381 382 private: 383 /// PostAllocaInsertPt - This is a place in the prologue where code can be 384 /// inserted that will be dominated by all the static allocas. This helps 385 /// achieve two things: 386 /// 1. Contiguity of all static allocas (within the prologue) is maintained. 387 /// 2. All other prologue code (which are dominated by static allocas) do 388 /// appear in the source order immediately after all static allocas. 389 /// 390 /// PostAllocaInsertPt will be lazily created when it is *really* required. 391 llvm::AssertingVH<llvm::Instruction> PostAllocaInsertPt = nullptr; 392 393 public: 394 /// Return PostAllocaInsertPt. If it is not yet created, then insert it 395 /// immediately after AllocaInsertPt. 396 llvm::Instruction *getPostAllocaInsertPoint() { 397 if (!PostAllocaInsertPt) { 398 assert(AllocaInsertPt && 399 "Expected static alloca insertion point at function prologue"); 400 assert(AllocaInsertPt->getParent()->isEntryBlock() && 401 "EBB should be entry block of the current code gen function"); 402 PostAllocaInsertPt = AllocaInsertPt->clone(); 403 PostAllocaInsertPt->setName("postallocapt"); 404 PostAllocaInsertPt->insertAfter(AllocaInsertPt); 405 } 406 407 return PostAllocaInsertPt; 408 } 409 410 /// API for captured statement code generation. 411 class CGCapturedStmtInfo { 412 public: 413 explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default) 414 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {} 415 explicit CGCapturedStmtInfo(const CapturedStmt &S, 416 CapturedRegionKind K = CR_Default) 417 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) { 418 419 RecordDecl::field_iterator Field = 420 S.getCapturedRecordDecl()->field_begin(); 421 for (CapturedStmt::const_capture_iterator I = S.capture_begin(), 422 E = S.capture_end(); 423 I != E; ++I, ++Field) { 424 if (I->capturesThis()) 425 CXXThisFieldDecl = *Field; 426 else if (I->capturesVariable()) 427 CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field; 428 else if (I->capturesVariableByCopy()) 429 CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field; 430 } 431 } 432 433 virtual ~CGCapturedStmtInfo(); 434 435 CapturedRegionKind getKind() const { return Kind; } 436 437 virtual void setContextValue(llvm::Value *V) { ThisValue = V; } 438 // Retrieve the value of the context parameter. 439 virtual llvm::Value *getContextValue() const { return ThisValue; } 440 441 /// Lookup the captured field decl for a variable. 442 virtual const FieldDecl *lookup(const VarDecl *VD) const { 443 return CaptureFields.lookup(VD->getCanonicalDecl()); 444 } 445 446 bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; } 447 virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; } 448 449 static bool classof(const CGCapturedStmtInfo *) { 450 return true; 451 } 452 453 /// Emit the captured statement body. 454 virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) { 455 CGF.incrementProfileCounter(S); 456 CGF.EmitStmt(S); 457 } 458 459 /// Get the name of the capture helper. 460 virtual StringRef getHelperName() const { return "__captured_stmt"; } 461 462 private: 463 /// The kind of captured statement being generated. 464 CapturedRegionKind Kind; 465 466 /// Keep the map between VarDecl and FieldDecl. 467 llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields; 468 469 /// The base address of the captured record, passed in as the first 470 /// argument of the parallel region function. 471 llvm::Value *ThisValue; 472 473 /// Captured 'this' type. 474 FieldDecl *CXXThisFieldDecl; 475 }; 476 CGCapturedStmtInfo *CapturedStmtInfo = nullptr; 477 478 /// RAII for correct setting/restoring of CapturedStmtInfo. 479 class CGCapturedStmtRAII { 480 private: 481 CodeGenFunction &CGF; 482 CGCapturedStmtInfo *PrevCapturedStmtInfo; 483 public: 484 CGCapturedStmtRAII(CodeGenFunction &CGF, 485 CGCapturedStmtInfo *NewCapturedStmtInfo) 486 : CGF(CGF), PrevCapturedStmtInfo(CGF.CapturedStmtInfo) { 487 CGF.CapturedStmtInfo = NewCapturedStmtInfo; 488 } 489 ~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; } 490 }; 491 492 /// An abstract representation of regular/ObjC call/message targets. 493 class AbstractCallee { 494 /// The function declaration of the callee. 495 const Decl *CalleeDecl; 496 497 public: 498 AbstractCallee() : CalleeDecl(nullptr) {} 499 AbstractCallee(const FunctionDecl *FD) : CalleeDecl(FD) {} 500 AbstractCallee(const ObjCMethodDecl *OMD) : CalleeDecl(OMD) {} 501 bool hasFunctionDecl() const { 502 return isa_and_nonnull<FunctionDecl>(CalleeDecl); 503 } 504 const Decl *getDecl() const { return CalleeDecl; } 505 unsigned getNumParams() const { 506 if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl)) 507 return FD->getNumParams(); 508 return cast<ObjCMethodDecl>(CalleeDecl)->param_size(); 509 } 510 const ParmVarDecl *getParamDecl(unsigned I) const { 511 if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl)) 512 return FD->getParamDecl(I); 513 return *(cast<ObjCMethodDecl>(CalleeDecl)->param_begin() + I); 514 } 515 }; 516 517 /// Sanitizers enabled for this function. 518 SanitizerSet SanOpts; 519 520 /// True if CodeGen currently emits code implementing sanitizer checks. 521 bool IsSanitizerScope = false; 522 523 /// RAII object to set/unset CodeGenFunction::IsSanitizerScope. 524 class SanitizerScope { 525 CodeGenFunction *CGF; 526 public: 527 SanitizerScope(CodeGenFunction *CGF); 528 ~SanitizerScope(); 529 }; 530 531 /// In C++, whether we are code generating a thunk. This controls whether we 532 /// should emit cleanups. 533 bool CurFuncIsThunk = false; 534 535 /// In ARC, whether we should autorelease the return value. 536 bool AutoreleaseResult = false; 537 538 /// Whether we processed a Microsoft-style asm block during CodeGen. These can 539 /// potentially set the return value. 540 bool SawAsmBlock = false; 541 542 const NamedDecl *CurSEHParent = nullptr; 543 544 /// True if the current function is an outlined SEH helper. This can be a 545 /// finally block or filter expression. 546 bool IsOutlinedSEHHelper = false; 547 548 /// True if CodeGen currently emits code inside presereved access index 549 /// region. 550 bool IsInPreservedAIRegion = false; 551 552 /// True if the current statement has nomerge attribute. 553 bool InNoMergeAttributedStmt = false; 554 555 // The CallExpr within the current statement that the musttail attribute 556 // applies to. nullptr if there is no 'musttail' on the current statement. 557 const CallExpr *MustTailCall = nullptr; 558 559 /// Returns true if a function must make progress, which means the 560 /// mustprogress attribute can be added. 561 bool checkIfFunctionMustProgress() { 562 if (CGM.getCodeGenOpts().getFiniteLoops() == 563 CodeGenOptions::FiniteLoopsKind::Never) 564 return false; 565 566 // C++11 and later guarantees that a thread eventually will do one of the 567 // following (6.9.2.3.1 in C++11): 568 // - terminate, 569 // - make a call to a library I/O function, 570 // - perform an access through a volatile glvalue, or 571 // - perform a synchronization operation or an atomic operation. 572 // 573 // Hence each function is 'mustprogress' in C++11 or later. 574 return getLangOpts().CPlusPlus11; 575 } 576 577 /// Returns true if a loop must make progress, which means the mustprogress 578 /// attribute can be added. \p HasConstantCond indicates whether the branch 579 /// condition is a known constant. 580 bool checkIfLoopMustProgress(bool HasConstantCond) { 581 if (CGM.getCodeGenOpts().getFiniteLoops() == 582 CodeGenOptions::FiniteLoopsKind::Always) 583 return true; 584 if (CGM.getCodeGenOpts().getFiniteLoops() == 585 CodeGenOptions::FiniteLoopsKind::Never) 586 return false; 587 588 // If the containing function must make progress, loops also must make 589 // progress (as in C++11 and later). 590 if (checkIfFunctionMustProgress()) 591 return true; 592 593 // Now apply rules for plain C (see 6.8.5.6 in C11). 594 // Loops with constant conditions do not have to make progress in any C 595 // version. 596 if (HasConstantCond) 597 return false; 598 599 // Loops with non-constant conditions must make progress in C11 and later. 600 return getLangOpts().C11; 601 } 602 603 const CodeGen::CGBlockInfo *BlockInfo = nullptr; 604 llvm::Value *BlockPointer = nullptr; 605 606 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; 607 FieldDecl *LambdaThisCaptureField = nullptr; 608 609 /// A mapping from NRVO variables to the flags used to indicate 610 /// when the NRVO has been applied to this variable. 611 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags; 612 613 EHScopeStack EHStack; 614 llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack; 615 llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack; 616 617 llvm::Instruction *CurrentFuncletPad = nullptr; 618 619 class CallLifetimeEnd final : public EHScopeStack::Cleanup { 620 bool isRedundantBeforeReturn() override { return true; } 621 622 llvm::Value *Addr; 623 llvm::Value *Size; 624 625 public: 626 CallLifetimeEnd(Address addr, llvm::Value *size) 627 : Addr(addr.getPointer()), Size(size) {} 628 629 void Emit(CodeGenFunction &CGF, Flags flags) override { 630 CGF.EmitLifetimeEnd(Size, Addr); 631 } 632 }; 633 634 /// Header for data within LifetimeExtendedCleanupStack. 635 struct LifetimeExtendedCleanupHeader { 636 /// The size of the following cleanup object. 637 unsigned Size; 638 /// The kind of cleanup to push: a value from the CleanupKind enumeration. 639 unsigned Kind : 31; 640 /// Whether this is a conditional cleanup. 641 unsigned IsConditional : 1; 642 643 size_t getSize() const { return Size; } 644 CleanupKind getKind() const { return (CleanupKind)Kind; } 645 bool isConditional() const { return IsConditional; } 646 }; 647 648 /// i32s containing the indexes of the cleanup destinations. 649 Address NormalCleanupDest = Address::invalid(); 650 651 unsigned NextCleanupDestIndex = 1; 652 653 /// EHResumeBlock - Unified block containing a call to llvm.eh.resume. 654 llvm::BasicBlock *EHResumeBlock = nullptr; 655 656 /// The exception slot. All landing pads write the current exception pointer 657 /// into this alloca. 658 llvm::Value *ExceptionSlot = nullptr; 659 660 /// The selector slot. Under the MandatoryCleanup model, all landing pads 661 /// write the current selector value into this alloca. 662 llvm::AllocaInst *EHSelectorSlot = nullptr; 663 664 /// A stack of exception code slots. Entering an __except block pushes a slot 665 /// on the stack and leaving pops one. The __exception_code() intrinsic loads 666 /// a value from the top of the stack. 667 SmallVector<Address, 1> SEHCodeSlotStack; 668 669 /// Value returned by __exception_info intrinsic. 670 llvm::Value *SEHInfo = nullptr; 671 672 /// Emits a landing pad for the current EH stack. 673 llvm::BasicBlock *EmitLandingPad(); 674 675 llvm::BasicBlock *getInvokeDestImpl(); 676 677 /// Parent loop-based directive for scan directive. 678 const OMPExecutableDirective *OMPParentLoopDirectiveForScan = nullptr; 679 llvm::BasicBlock *OMPBeforeScanBlock = nullptr; 680 llvm::BasicBlock *OMPAfterScanBlock = nullptr; 681 llvm::BasicBlock *OMPScanExitBlock = nullptr; 682 llvm::BasicBlock *OMPScanDispatch = nullptr; 683 bool OMPFirstScanLoop = false; 684 685 /// Manages parent directive for scan directives. 686 class ParentLoopDirectiveForScanRegion { 687 CodeGenFunction &CGF; 688 const OMPExecutableDirective *ParentLoopDirectiveForScan; 689 690 public: 691 ParentLoopDirectiveForScanRegion( 692 CodeGenFunction &CGF, 693 const OMPExecutableDirective &ParentLoopDirectiveForScan) 694 : CGF(CGF), 695 ParentLoopDirectiveForScan(CGF.OMPParentLoopDirectiveForScan) { 696 CGF.OMPParentLoopDirectiveForScan = &ParentLoopDirectiveForScan; 697 } 698 ~ParentLoopDirectiveForScanRegion() { 699 CGF.OMPParentLoopDirectiveForScan = ParentLoopDirectiveForScan; 700 } 701 }; 702 703 template <class T> 704 typename DominatingValue<T>::saved_type saveValueInCond(T value) { 705 return DominatingValue<T>::save(*this, value); 706 } 707 708 class CGFPOptionsRAII { 709 public: 710 CGFPOptionsRAII(CodeGenFunction &CGF, FPOptions FPFeatures); 711 CGFPOptionsRAII(CodeGenFunction &CGF, const Expr *E); 712 ~CGFPOptionsRAII(); 713 714 private: 715 void ConstructorHelper(FPOptions FPFeatures); 716 CodeGenFunction &CGF; 717 FPOptions OldFPFeatures; 718 llvm::fp::ExceptionBehavior OldExcept; 719 llvm::RoundingMode OldRounding; 720 Optional<CGBuilderTy::FastMathFlagGuard> FMFGuard; 721 }; 722 FPOptions CurFPFeatures; 723 724 public: 725 /// ObjCEHValueStack - Stack of Objective-C exception values, used for 726 /// rethrows. 727 SmallVector<llvm::Value*, 8> ObjCEHValueStack; 728 729 /// A class controlling the emission of a finally block. 730 class FinallyInfo { 731 /// Where the catchall's edge through the cleanup should go. 732 JumpDest RethrowDest; 733 734 /// A function to call to enter the catch. 735 llvm::FunctionCallee BeginCatchFn; 736 737 /// An i1 variable indicating whether or not the @finally is 738 /// running for an exception. 739 llvm::AllocaInst *ForEHVar; 740 741 /// An i8* variable into which the exception pointer to rethrow 742 /// has been saved. 743 llvm::AllocaInst *SavedExnVar; 744 745 public: 746 void enter(CodeGenFunction &CGF, const Stmt *Finally, 747 llvm::FunctionCallee beginCatchFn, 748 llvm::FunctionCallee endCatchFn, llvm::FunctionCallee rethrowFn); 749 void exit(CodeGenFunction &CGF); 750 }; 751 752 /// Returns true inside SEH __try blocks. 753 bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); } 754 755 /// Returns true while emitting a cleanuppad. 756 bool isCleanupPadScope() const { 757 return CurrentFuncletPad && isa<llvm::CleanupPadInst>(CurrentFuncletPad); 758 } 759 760 /// pushFullExprCleanup - Push a cleanup to be run at the end of the 761 /// current full-expression. Safe against the possibility that 762 /// we're currently inside a conditionally-evaluated expression. 763 template <class T, class... As> 764 void pushFullExprCleanup(CleanupKind kind, As... A) { 765 // If we're not in a conditional branch, or if none of the 766 // arguments requires saving, then use the unconditional cleanup. 767 if (!isInConditionalBranch()) 768 return EHStack.pushCleanup<T>(kind, A...); 769 770 // Stash values in a tuple so we can guarantee the order of saves. 771 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple; 772 SavedTuple Saved{saveValueInCond(A)...}; 773 774 typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType; 775 EHStack.pushCleanupTuple<CleanupType>(kind, Saved); 776 initFullExprCleanup(); 777 } 778 779 /// Queue a cleanup to be pushed after finishing the current full-expression, 780 /// potentially with an active flag. 781 template <class T, class... As> 782 void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) { 783 if (!isInConditionalBranch()) 784 return pushCleanupAfterFullExprWithActiveFlag<T>(Kind, Address::invalid(), 785 A...); 786 787 Address ActiveFlag = createCleanupActiveFlag(); 788 assert(!DominatingValue<Address>::needsSaving(ActiveFlag) && 789 "cleanup active flag should never need saving"); 790 791 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple; 792 SavedTuple Saved{saveValueInCond(A)...}; 793 794 typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType; 795 pushCleanupAfterFullExprWithActiveFlag<CleanupType>(Kind, ActiveFlag, Saved); 796 } 797 798 template <class T, class... As> 799 void pushCleanupAfterFullExprWithActiveFlag(CleanupKind Kind, 800 Address ActiveFlag, As... A) { 801 LifetimeExtendedCleanupHeader Header = {sizeof(T), Kind, 802 ActiveFlag.isValid()}; 803 804 size_t OldSize = LifetimeExtendedCleanupStack.size(); 805 LifetimeExtendedCleanupStack.resize( 806 LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size + 807 (Header.IsConditional ? sizeof(ActiveFlag) : 0)); 808 809 static_assert(sizeof(Header) % alignof(T) == 0, 810 "Cleanup will be allocated on misaligned address"); 811 char *Buffer = &LifetimeExtendedCleanupStack[OldSize]; 812 new (Buffer) LifetimeExtendedCleanupHeader(Header); 813 new (Buffer + sizeof(Header)) T(A...); 814 if (Header.IsConditional) 815 new (Buffer + sizeof(Header) + sizeof(T)) Address(ActiveFlag); 816 } 817 818 /// Set up the last cleanup that was pushed as a conditional 819 /// full-expression cleanup. 820 void initFullExprCleanup() { 821 initFullExprCleanupWithFlag(createCleanupActiveFlag()); 822 } 823 824 void initFullExprCleanupWithFlag(Address ActiveFlag); 825 Address createCleanupActiveFlag(); 826 827 /// PushDestructorCleanup - Push a cleanup to call the 828 /// complete-object destructor of an object of the given type at the 829 /// given address. Does nothing if T is not a C++ class type with a 830 /// non-trivial destructor. 831 void PushDestructorCleanup(QualType T, Address Addr); 832 833 /// PushDestructorCleanup - Push a cleanup to call the 834 /// complete-object variant of the given destructor on the object at 835 /// the given address. 836 void PushDestructorCleanup(const CXXDestructorDecl *Dtor, QualType T, 837 Address Addr); 838 839 /// PopCleanupBlock - Will pop the cleanup entry on the stack and 840 /// process all branch fixups. 841 void PopCleanupBlock(bool FallThroughIsBranchThrough = false); 842 843 /// DeactivateCleanupBlock - Deactivates the given cleanup block. 844 /// The block cannot be reactivated. Pops it if it's the top of the 845 /// stack. 846 /// 847 /// \param DominatingIP - An instruction which is known to 848 /// dominate the current IP (if set) and which lies along 849 /// all paths of execution between the current IP and the 850 /// the point at which the cleanup comes into scope. 851 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, 852 llvm::Instruction *DominatingIP); 853 854 /// ActivateCleanupBlock - Activates an initially-inactive cleanup. 855 /// Cannot be used to resurrect a deactivated cleanup. 856 /// 857 /// \param DominatingIP - An instruction which is known to 858 /// dominate the current IP (if set) and which lies along 859 /// all paths of execution between the current IP and the 860 /// the point at which the cleanup comes into scope. 861 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, 862 llvm::Instruction *DominatingIP); 863 864 /// Enters a new scope for capturing cleanups, all of which 865 /// will be executed once the scope is exited. 866 class RunCleanupsScope { 867 EHScopeStack::stable_iterator CleanupStackDepth, OldCleanupScopeDepth; 868 size_t LifetimeExtendedCleanupStackSize; 869 bool OldDidCallStackSave; 870 protected: 871 bool PerformCleanup; 872 private: 873 874 RunCleanupsScope(const RunCleanupsScope &) = delete; 875 void operator=(const RunCleanupsScope &) = delete; 876 877 protected: 878 CodeGenFunction& CGF; 879 880 public: 881 /// Enter a new cleanup scope. 882 explicit RunCleanupsScope(CodeGenFunction &CGF) 883 : PerformCleanup(true), CGF(CGF) 884 { 885 CleanupStackDepth = CGF.EHStack.stable_begin(); 886 LifetimeExtendedCleanupStackSize = 887 CGF.LifetimeExtendedCleanupStack.size(); 888 OldDidCallStackSave = CGF.DidCallStackSave; 889 CGF.DidCallStackSave = false; 890 OldCleanupScopeDepth = CGF.CurrentCleanupScopeDepth; 891 CGF.CurrentCleanupScopeDepth = CleanupStackDepth; 892 } 893 894 /// Exit this cleanup scope, emitting any accumulated cleanups. 895 ~RunCleanupsScope() { 896 if (PerformCleanup) 897 ForceCleanup(); 898 } 899 900 /// Determine whether this scope requires any cleanups. 901 bool requiresCleanups() const { 902 return CGF.EHStack.stable_begin() != CleanupStackDepth; 903 } 904 905 /// Force the emission of cleanups now, instead of waiting 906 /// until this object is destroyed. 907 /// \param ValuesToReload - A list of values that need to be available at 908 /// the insertion point after cleanup emission. If cleanup emission created 909 /// a shared cleanup block, these value pointers will be rewritten. 910 /// Otherwise, they not will be modified. 911 void ForceCleanup(std::initializer_list<llvm::Value**> ValuesToReload = {}) { 912 assert(PerformCleanup && "Already forced cleanup"); 913 CGF.DidCallStackSave = OldDidCallStackSave; 914 CGF.PopCleanupBlocks(CleanupStackDepth, LifetimeExtendedCleanupStackSize, 915 ValuesToReload); 916 PerformCleanup = false; 917 CGF.CurrentCleanupScopeDepth = OldCleanupScopeDepth; 918 } 919 }; 920 921 // Cleanup stack depth of the RunCleanupsScope that was pushed most recently. 922 EHScopeStack::stable_iterator CurrentCleanupScopeDepth = 923 EHScopeStack::stable_end(); 924 925 class LexicalScope : public RunCleanupsScope { 926 SourceRange Range; 927 SmallVector<const LabelDecl*, 4> Labels; 928 LexicalScope *ParentScope; 929 930 LexicalScope(const LexicalScope &) = delete; 931 void operator=(const LexicalScope &) = delete; 932 933 public: 934 /// Enter a new cleanup scope. 935 explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range) 936 : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) { 937 CGF.CurLexicalScope = this; 938 if (CGDebugInfo *DI = CGF.getDebugInfo()) 939 DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin()); 940 } 941 942 void addLabel(const LabelDecl *label) { 943 assert(PerformCleanup && "adding label to dead scope?"); 944 Labels.push_back(label); 945 } 946 947 /// Exit this cleanup scope, emitting any accumulated 948 /// cleanups. 949 ~LexicalScope() { 950 if (CGDebugInfo *DI = CGF.getDebugInfo()) 951 DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd()); 952 953 // If we should perform a cleanup, force them now. Note that 954 // this ends the cleanup scope before rescoping any labels. 955 if (PerformCleanup) { 956 ApplyDebugLocation DL(CGF, Range.getEnd()); 957 ForceCleanup(); 958 } 959 } 960 961 /// Force the emission of cleanups now, instead of waiting 962 /// until this object is destroyed. 963 void ForceCleanup() { 964 CGF.CurLexicalScope = ParentScope; 965 RunCleanupsScope::ForceCleanup(); 966 967 if (!Labels.empty()) 968 rescopeLabels(); 969 } 970 971 bool hasLabels() const { 972 return !Labels.empty(); 973 } 974 975 void rescopeLabels(); 976 }; 977 978 typedef llvm::DenseMap<const Decl *, Address> DeclMapTy; 979 980 /// The class used to assign some variables some temporarily addresses. 981 class OMPMapVars { 982 DeclMapTy SavedLocals; 983 DeclMapTy SavedTempAddresses; 984 OMPMapVars(const OMPMapVars &) = delete; 985 void operator=(const OMPMapVars &) = delete; 986 987 public: 988 explicit OMPMapVars() = default; 989 ~OMPMapVars() { 990 assert(SavedLocals.empty() && "Did not restored original addresses."); 991 }; 992 993 /// Sets the address of the variable \p LocalVD to be \p TempAddr in 994 /// function \p CGF. 995 /// \return true if at least one variable was set already, false otherwise. 996 bool setVarAddr(CodeGenFunction &CGF, const VarDecl *LocalVD, 997 Address TempAddr) { 998 LocalVD = LocalVD->getCanonicalDecl(); 999 // Only save it once. 1000 if (SavedLocals.count(LocalVD)) return false; 1001 1002 // Copy the existing local entry to SavedLocals. 1003 auto it = CGF.LocalDeclMap.find(LocalVD); 1004 if (it != CGF.LocalDeclMap.end()) 1005 SavedLocals.try_emplace(LocalVD, it->second); 1006 else 1007 SavedLocals.try_emplace(LocalVD, Address::invalid()); 1008 1009 // Generate the private entry. 1010 QualType VarTy = LocalVD->getType(); 1011 if (VarTy->isReferenceType()) { 1012 Address Temp = CGF.CreateMemTemp(VarTy); 1013 CGF.Builder.CreateStore(TempAddr.getPointer(), Temp); 1014 TempAddr = Temp; 1015 } 1016 SavedTempAddresses.try_emplace(LocalVD, TempAddr); 1017 1018 return true; 1019 } 1020 1021 /// Applies new addresses to the list of the variables. 1022 /// \return true if at least one variable is using new address, false 1023 /// otherwise. 1024 bool apply(CodeGenFunction &CGF) { 1025 copyInto(SavedTempAddresses, CGF.LocalDeclMap); 1026 SavedTempAddresses.clear(); 1027 return !SavedLocals.empty(); 1028 } 1029 1030 /// Restores original addresses of the variables. 1031 void restore(CodeGenFunction &CGF) { 1032 if (!SavedLocals.empty()) { 1033 copyInto(SavedLocals, CGF.LocalDeclMap); 1034 SavedLocals.clear(); 1035 } 1036 } 1037 1038 private: 1039 /// Copy all the entries in the source map over the corresponding 1040 /// entries in the destination, which must exist. 1041 static void copyInto(const DeclMapTy &Src, DeclMapTy &Dest) { 1042 for (auto &Pair : Src) { 1043 if (!Pair.second.isValid()) { 1044 Dest.erase(Pair.first); 1045 continue; 1046 } 1047 1048 auto I = Dest.find(Pair.first); 1049 if (I != Dest.end()) 1050 I->second = Pair.second; 1051 else 1052 Dest.insert(Pair); 1053 } 1054 } 1055 }; 1056 1057 /// The scope used to remap some variables as private in the OpenMP loop body 1058 /// (or other captured region emitted without outlining), and to restore old 1059 /// vars back on exit. 1060 class OMPPrivateScope : public RunCleanupsScope { 1061 OMPMapVars MappedVars; 1062 OMPPrivateScope(const OMPPrivateScope &) = delete; 1063 void operator=(const OMPPrivateScope &) = delete; 1064 1065 public: 1066 /// Enter a new OpenMP private scope. 1067 explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {} 1068 1069 /// Registers \p LocalVD variable as a private and apply \p PrivateGen 1070 /// function for it to generate corresponding private variable. \p 1071 /// PrivateGen returns an address of the generated private variable. 1072 /// \return true if the variable is registered as private, false if it has 1073 /// been privatized already. 1074 bool addPrivate(const VarDecl *LocalVD, 1075 const llvm::function_ref<Address()> PrivateGen) { 1076 assert(PerformCleanup && "adding private to dead scope"); 1077 return MappedVars.setVarAddr(CGF, LocalVD, PrivateGen()); 1078 } 1079 1080 /// Privatizes local variables previously registered as private. 1081 /// Registration is separate from the actual privatization to allow 1082 /// initializers use values of the original variables, not the private one. 1083 /// This is important, for example, if the private variable is a class 1084 /// variable initialized by a constructor that references other private 1085 /// variables. But at initialization original variables must be used, not 1086 /// private copies. 1087 /// \return true if at least one variable was privatized, false otherwise. 1088 bool Privatize() { return MappedVars.apply(CGF); } 1089 1090 void ForceCleanup() { 1091 RunCleanupsScope::ForceCleanup(); 1092 MappedVars.restore(CGF); 1093 } 1094 1095 /// Exit scope - all the mapped variables are restored. 1096 ~OMPPrivateScope() { 1097 if (PerformCleanup) 1098 ForceCleanup(); 1099 } 1100 1101 /// Checks if the global variable is captured in current function. 1102 bool isGlobalVarCaptured(const VarDecl *VD) const { 1103 VD = VD->getCanonicalDecl(); 1104 return !VD->isLocalVarDeclOrParm() && CGF.LocalDeclMap.count(VD) > 0; 1105 } 1106 }; 1107 1108 /// Save/restore original map of previously emitted local vars in case when we 1109 /// need to duplicate emission of the same code several times in the same 1110 /// function for OpenMP code. 1111 class OMPLocalDeclMapRAII { 1112 CodeGenFunction &CGF; 1113 DeclMapTy SavedMap; 1114 1115 public: 1116 OMPLocalDeclMapRAII(CodeGenFunction &CGF) 1117 : CGF(CGF), SavedMap(CGF.LocalDeclMap) {} 1118 ~OMPLocalDeclMapRAII() { SavedMap.swap(CGF.LocalDeclMap); } 1119 }; 1120 1121 /// Takes the old cleanup stack size and emits the cleanup blocks 1122 /// that have been added. 1123 void 1124 PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize, 1125 std::initializer_list<llvm::Value **> ValuesToReload = {}); 1126 1127 /// Takes the old cleanup stack size and emits the cleanup blocks 1128 /// that have been added, then adds all lifetime-extended cleanups from 1129 /// the given position to the stack. 1130 void 1131 PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize, 1132 size_t OldLifetimeExtendedStackSize, 1133 std::initializer_list<llvm::Value **> ValuesToReload = {}); 1134 1135 void ResolveBranchFixups(llvm::BasicBlock *Target); 1136 1137 /// The given basic block lies in the current EH scope, but may be a 1138 /// target of a potentially scope-crossing jump; get a stable handle 1139 /// to which we can perform this jump later. 1140 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) { 1141 return JumpDest(Target, 1142 EHStack.getInnermostNormalCleanup(), 1143 NextCleanupDestIndex++); 1144 } 1145 1146 /// The given basic block lies in the current EH scope, but may be a 1147 /// target of a potentially scope-crossing jump; get a stable handle 1148 /// to which we can perform this jump later. 1149 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) { 1150 return getJumpDestInCurrentScope(createBasicBlock(Name)); 1151 } 1152 1153 /// EmitBranchThroughCleanup - Emit a branch from the current insert 1154 /// block through the normal cleanup handling code (if any) and then 1155 /// on to \arg Dest. 1156 void EmitBranchThroughCleanup(JumpDest Dest); 1157 1158 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the 1159 /// specified destination obviously has no cleanups to run. 'false' is always 1160 /// a conservatively correct answer for this method. 1161 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const; 1162 1163 /// popCatchScope - Pops the catch scope at the top of the EHScope 1164 /// stack, emitting any required code (other than the catch handlers 1165 /// themselves). 1166 void popCatchScope(); 1167 1168 llvm::BasicBlock *getEHResumeBlock(bool isCleanup); 1169 llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope); 1170 llvm::BasicBlock * 1171 getFuncletEHDispatchBlock(EHScopeStack::stable_iterator scope); 1172 1173 /// An object to manage conditionally-evaluated expressions. 1174 class ConditionalEvaluation { 1175 llvm::BasicBlock *StartBB; 1176 1177 public: 1178 ConditionalEvaluation(CodeGenFunction &CGF) 1179 : StartBB(CGF.Builder.GetInsertBlock()) {} 1180 1181 void begin(CodeGenFunction &CGF) { 1182 assert(CGF.OutermostConditional != this); 1183 if (!CGF.OutermostConditional) 1184 CGF.OutermostConditional = this; 1185 } 1186 1187 void end(CodeGenFunction &CGF) { 1188 assert(CGF.OutermostConditional != nullptr); 1189 if (CGF.OutermostConditional == this) 1190 CGF.OutermostConditional = nullptr; 1191 } 1192 1193 /// Returns a block which will be executed prior to each 1194 /// evaluation of the conditional code. 1195 llvm::BasicBlock *getStartingBlock() const { 1196 return StartBB; 1197 } 1198 }; 1199 1200 /// isInConditionalBranch - Return true if we're currently emitting 1201 /// one branch or the other of a conditional expression. 1202 bool isInConditionalBranch() const { return OutermostConditional != nullptr; } 1203 1204 void setBeforeOutermostConditional(llvm::Value *value, Address addr) { 1205 assert(isInConditionalBranch()); 1206 llvm::BasicBlock *block = OutermostConditional->getStartingBlock(); 1207 auto store = new llvm::StoreInst(value, addr.getPointer(), &block->back()); 1208 store->setAlignment(addr.getAlignment().getAsAlign()); 1209 } 1210 1211 /// An RAII object to record that we're evaluating a statement 1212 /// expression. 1213 class StmtExprEvaluation { 1214 CodeGenFunction &CGF; 1215 1216 /// We have to save the outermost conditional: cleanups in a 1217 /// statement expression aren't conditional just because the 1218 /// StmtExpr is. 1219 ConditionalEvaluation *SavedOutermostConditional; 1220 1221 public: 1222 StmtExprEvaluation(CodeGenFunction &CGF) 1223 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) { 1224 CGF.OutermostConditional = nullptr; 1225 } 1226 1227 ~StmtExprEvaluation() { 1228 CGF.OutermostConditional = SavedOutermostConditional; 1229 CGF.EnsureInsertPoint(); 1230 } 1231 }; 1232 1233 /// An object which temporarily prevents a value from being 1234 /// destroyed by aggressive peephole optimizations that assume that 1235 /// all uses of a value have been realized in the IR. 1236 class PeepholeProtection { 1237 llvm::Instruction *Inst; 1238 friend class CodeGenFunction; 1239 1240 public: 1241 PeepholeProtection() : Inst(nullptr) {} 1242 }; 1243 1244 /// A non-RAII class containing all the information about a bound 1245 /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for 1246 /// this which makes individual mappings very simple; using this 1247 /// class directly is useful when you have a variable number of 1248 /// opaque values or don't want the RAII functionality for some 1249 /// reason. 1250 class OpaqueValueMappingData { 1251 const OpaqueValueExpr *OpaqueValue; 1252 bool BoundLValue; 1253 CodeGenFunction::PeepholeProtection Protection; 1254 1255 OpaqueValueMappingData(const OpaqueValueExpr *ov, 1256 bool boundLValue) 1257 : OpaqueValue(ov), BoundLValue(boundLValue) {} 1258 public: 1259 OpaqueValueMappingData() : OpaqueValue(nullptr) {} 1260 1261 static bool shouldBindAsLValue(const Expr *expr) { 1262 // gl-values should be bound as l-values for obvious reasons. 1263 // Records should be bound as l-values because IR generation 1264 // always keeps them in memory. Expressions of function type 1265 // act exactly like l-values but are formally required to be 1266 // r-values in C. 1267 return expr->isGLValue() || 1268 expr->getType()->isFunctionType() || 1269 hasAggregateEvaluationKind(expr->getType()); 1270 } 1271 1272 static OpaqueValueMappingData bind(CodeGenFunction &CGF, 1273 const OpaqueValueExpr *ov, 1274 const Expr *e) { 1275 if (shouldBindAsLValue(ov)) 1276 return bind(CGF, ov, CGF.EmitLValue(e)); 1277 return bind(CGF, ov, CGF.EmitAnyExpr(e)); 1278 } 1279 1280 static OpaqueValueMappingData bind(CodeGenFunction &CGF, 1281 const OpaqueValueExpr *ov, 1282 const LValue &lv) { 1283 assert(shouldBindAsLValue(ov)); 1284 CGF.OpaqueLValues.insert(std::make_pair(ov, lv)); 1285 return OpaqueValueMappingData(ov, true); 1286 } 1287 1288 static OpaqueValueMappingData bind(CodeGenFunction &CGF, 1289 const OpaqueValueExpr *ov, 1290 const RValue &rv) { 1291 assert(!shouldBindAsLValue(ov)); 1292 CGF.OpaqueRValues.insert(std::make_pair(ov, rv)); 1293 1294 OpaqueValueMappingData data(ov, false); 1295 1296 // Work around an extremely aggressive peephole optimization in 1297 // EmitScalarConversion which assumes that all other uses of a 1298 // value are extant. 1299 data.Protection = CGF.protectFromPeepholes(rv); 1300 1301 return data; 1302 } 1303 1304 bool isValid() const { return OpaqueValue != nullptr; } 1305 void clear() { OpaqueValue = nullptr; } 1306 1307 void unbind(CodeGenFunction &CGF) { 1308 assert(OpaqueValue && "no data to unbind!"); 1309 1310 if (BoundLValue) { 1311 CGF.OpaqueLValues.erase(OpaqueValue); 1312 } else { 1313 CGF.OpaqueRValues.erase(OpaqueValue); 1314 CGF.unprotectFromPeepholes(Protection); 1315 } 1316 } 1317 }; 1318 1319 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr. 1320 class OpaqueValueMapping { 1321 CodeGenFunction &CGF; 1322 OpaqueValueMappingData Data; 1323 1324 public: 1325 static bool shouldBindAsLValue(const Expr *expr) { 1326 return OpaqueValueMappingData::shouldBindAsLValue(expr); 1327 } 1328 1329 /// Build the opaque value mapping for the given conditional 1330 /// operator if it's the GNU ?: extension. This is a common 1331 /// enough pattern that the convenience operator is really 1332 /// helpful. 1333 /// 1334 OpaqueValueMapping(CodeGenFunction &CGF, 1335 const AbstractConditionalOperator *op) : CGF(CGF) { 1336 if (isa<ConditionalOperator>(op)) 1337 // Leave Data empty. 1338 return; 1339 1340 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op); 1341 Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(), 1342 e->getCommon()); 1343 } 1344 1345 /// Build the opaque value mapping for an OpaqueValueExpr whose source 1346 /// expression is set to the expression the OVE represents. 1347 OpaqueValueMapping(CodeGenFunction &CGF, const OpaqueValueExpr *OV) 1348 : CGF(CGF) { 1349 if (OV) { 1350 assert(OV->getSourceExpr() && "wrong form of OpaqueValueMapping used " 1351 "for OVE with no source expression"); 1352 Data = OpaqueValueMappingData::bind(CGF, OV, OV->getSourceExpr()); 1353 } 1354 } 1355 1356 OpaqueValueMapping(CodeGenFunction &CGF, 1357 const OpaqueValueExpr *opaqueValue, 1358 LValue lvalue) 1359 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) { 1360 } 1361 1362 OpaqueValueMapping(CodeGenFunction &CGF, 1363 const OpaqueValueExpr *opaqueValue, 1364 RValue rvalue) 1365 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) { 1366 } 1367 1368 void pop() { 1369 Data.unbind(CGF); 1370 Data.clear(); 1371 } 1372 1373 ~OpaqueValueMapping() { 1374 if (Data.isValid()) Data.unbind(CGF); 1375 } 1376 }; 1377 1378 private: 1379 CGDebugInfo *DebugInfo; 1380 /// Used to create unique names for artificial VLA size debug info variables. 1381 unsigned VLAExprCounter = 0; 1382 bool DisableDebugInfo = false; 1383 1384 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid 1385 /// calling llvm.stacksave for multiple VLAs in the same scope. 1386 bool DidCallStackSave = false; 1387 1388 /// IndirectBranch - The first time an indirect goto is seen we create a block 1389 /// with an indirect branch. Every time we see the address of a label taken, 1390 /// we add the label to the indirect goto. Every subsequent indirect goto is 1391 /// codegen'd as a jump to the IndirectBranch's basic block. 1392 llvm::IndirectBrInst *IndirectBranch = nullptr; 1393 1394 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C 1395 /// decls. 1396 DeclMapTy LocalDeclMap; 1397 1398 // Keep track of the cleanups for callee-destructed parameters pushed to the 1399 // cleanup stack so that they can be deactivated later. 1400 llvm::DenseMap<const ParmVarDecl *, EHScopeStack::stable_iterator> 1401 CalleeDestructedParamCleanups; 1402 1403 /// SizeArguments - If a ParmVarDecl had the pass_object_size attribute, this 1404 /// will contain a mapping from said ParmVarDecl to its implicit "object_size" 1405 /// parameter. 1406 llvm::SmallDenseMap<const ParmVarDecl *, const ImplicitParamDecl *, 2> 1407 SizeArguments; 1408 1409 /// Track escaped local variables with auto storage. Used during SEH 1410 /// outlining to produce a call to llvm.localescape. 1411 llvm::DenseMap<llvm::AllocaInst *, int> EscapedLocals; 1412 1413 /// LabelMap - This keeps track of the LLVM basic block for each C label. 1414 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap; 1415 1416 // BreakContinueStack - This keeps track of where break and continue 1417 // statements should jump to. 1418 struct BreakContinue { 1419 BreakContinue(JumpDest Break, JumpDest Continue) 1420 : BreakBlock(Break), ContinueBlock(Continue) {} 1421 1422 JumpDest BreakBlock; 1423 JumpDest ContinueBlock; 1424 }; 1425 SmallVector<BreakContinue, 8> BreakContinueStack; 1426 1427 /// Handles cancellation exit points in OpenMP-related constructs. 1428 class OpenMPCancelExitStack { 1429 /// Tracks cancellation exit point and join point for cancel-related exit 1430 /// and normal exit. 1431 struct CancelExit { 1432 CancelExit() = default; 1433 CancelExit(OpenMPDirectiveKind Kind, JumpDest ExitBlock, 1434 JumpDest ContBlock) 1435 : Kind(Kind), ExitBlock(ExitBlock), ContBlock(ContBlock) {} 1436 OpenMPDirectiveKind Kind = llvm::omp::OMPD_unknown; 1437 /// true if the exit block has been emitted already by the special 1438 /// emitExit() call, false if the default codegen is used. 1439 bool HasBeenEmitted = false; 1440 JumpDest ExitBlock; 1441 JumpDest ContBlock; 1442 }; 1443 1444 SmallVector<CancelExit, 8> Stack; 1445 1446 public: 1447 OpenMPCancelExitStack() : Stack(1) {} 1448 ~OpenMPCancelExitStack() = default; 1449 /// Fetches the exit block for the current OpenMP construct. 1450 JumpDest getExitBlock() const { return Stack.back().ExitBlock; } 1451 /// Emits exit block with special codegen procedure specific for the related 1452 /// OpenMP construct + emits code for normal construct cleanup. 1453 void emitExit(CodeGenFunction &CGF, OpenMPDirectiveKind Kind, 1454 const llvm::function_ref<void(CodeGenFunction &)> CodeGen) { 1455 if (Stack.back().Kind == Kind && getExitBlock().isValid()) { 1456 assert(CGF.getOMPCancelDestination(Kind).isValid()); 1457 assert(CGF.HaveInsertPoint()); 1458 assert(!Stack.back().HasBeenEmitted); 1459 auto IP = CGF.Builder.saveAndClearIP(); 1460 CGF.EmitBlock(Stack.back().ExitBlock.getBlock()); 1461 CodeGen(CGF); 1462 CGF.EmitBranch(Stack.back().ContBlock.getBlock()); 1463 CGF.Builder.restoreIP(IP); 1464 Stack.back().HasBeenEmitted = true; 1465 } 1466 CodeGen(CGF); 1467 } 1468 /// Enter the cancel supporting \a Kind construct. 1469 /// \param Kind OpenMP directive that supports cancel constructs. 1470 /// \param HasCancel true, if the construct has inner cancel directive, 1471 /// false otherwise. 1472 void enter(CodeGenFunction &CGF, OpenMPDirectiveKind Kind, bool HasCancel) { 1473 Stack.push_back({Kind, 1474 HasCancel ? CGF.getJumpDestInCurrentScope("cancel.exit") 1475 : JumpDest(), 1476 HasCancel ? CGF.getJumpDestInCurrentScope("cancel.cont") 1477 : JumpDest()}); 1478 } 1479 /// Emits default exit point for the cancel construct (if the special one 1480 /// has not be used) + join point for cancel/normal exits. 1481 void exit(CodeGenFunction &CGF) { 1482 if (getExitBlock().isValid()) { 1483 assert(CGF.getOMPCancelDestination(Stack.back().Kind).isValid()); 1484 bool HaveIP = CGF.HaveInsertPoint(); 1485 if (!Stack.back().HasBeenEmitted) { 1486 if (HaveIP) 1487 CGF.EmitBranchThroughCleanup(Stack.back().ContBlock); 1488 CGF.EmitBlock(Stack.back().ExitBlock.getBlock()); 1489 CGF.EmitBranchThroughCleanup(Stack.back().ContBlock); 1490 } 1491 CGF.EmitBlock(Stack.back().ContBlock.getBlock()); 1492 if (!HaveIP) { 1493 CGF.Builder.CreateUnreachable(); 1494 CGF.Builder.ClearInsertionPoint(); 1495 } 1496 } 1497 Stack.pop_back(); 1498 } 1499 }; 1500 OpenMPCancelExitStack OMPCancelStack; 1501 1502 /// Lower the Likelihood knowledge about the \p Cond via llvm.expect intrin. 1503 llvm::Value *emitCondLikelihoodViaExpectIntrinsic(llvm::Value *Cond, 1504 Stmt::Likelihood LH); 1505 1506 CodeGenPGO PGO; 1507 1508 /// Calculate branch weights appropriate for PGO data 1509 llvm::MDNode *createProfileWeights(uint64_t TrueCount, 1510 uint64_t FalseCount) const; 1511 llvm::MDNode *createProfileWeights(ArrayRef<uint64_t> Weights) const; 1512 llvm::MDNode *createProfileWeightsForLoop(const Stmt *Cond, 1513 uint64_t LoopCount) const; 1514 1515 public: 1516 /// Increment the profiler's counter for the given statement by \p StepV. 1517 /// If \p StepV is null, the default increment is 1. 1518 void incrementProfileCounter(const Stmt *S, llvm::Value *StepV = nullptr) { 1519 if (CGM.getCodeGenOpts().hasProfileClangInstr() && 1520 !CurFn->hasFnAttribute(llvm::Attribute::NoProfile)) 1521 PGO.emitCounterIncrement(Builder, S, StepV); 1522 PGO.setCurrentStmt(S); 1523 } 1524 1525 /// Get the profiler's count for the given statement. 1526 uint64_t getProfileCount(const Stmt *S) { 1527 Optional<uint64_t> Count = PGO.getStmtCount(S); 1528 if (!Count.hasValue()) 1529 return 0; 1530 return *Count; 1531 } 1532 1533 /// Set the profiler's current count. 1534 void setCurrentProfileCount(uint64_t Count) { 1535 PGO.setCurrentRegionCount(Count); 1536 } 1537 1538 /// Get the profiler's current count. This is generally the count for the most 1539 /// recently incremented counter. 1540 uint64_t getCurrentProfileCount() { 1541 return PGO.getCurrentRegionCount(); 1542 } 1543 1544 private: 1545 1546 /// SwitchInsn - This is nearest current switch instruction. It is null if 1547 /// current context is not in a switch. 1548 llvm::SwitchInst *SwitchInsn = nullptr; 1549 /// The branch weights of SwitchInsn when doing instrumentation based PGO. 1550 SmallVector<uint64_t, 16> *SwitchWeights = nullptr; 1551 1552 /// The likelihood attributes of the SwitchCase. 1553 SmallVector<Stmt::Likelihood, 16> *SwitchLikelihood = nullptr; 1554 1555 /// CaseRangeBlock - This block holds if condition check for last case 1556 /// statement range in current switch instruction. 1557 llvm::BasicBlock *CaseRangeBlock = nullptr; 1558 1559 /// OpaqueLValues - Keeps track of the current set of opaque value 1560 /// expressions. 1561 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues; 1562 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues; 1563 1564 // VLASizeMap - This keeps track of the associated size for each VLA type. 1565 // We track this by the size expression rather than the type itself because 1566 // in certain situations, like a const qualifier applied to an VLA typedef, 1567 // multiple VLA types can share the same size expression. 1568 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we 1569 // enter/leave scopes. 1570 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap; 1571 1572 /// A block containing a single 'unreachable' instruction. Created 1573 /// lazily by getUnreachableBlock(). 1574 llvm::BasicBlock *UnreachableBlock = nullptr; 1575 1576 /// Counts of the number return expressions in the function. 1577 unsigned NumReturnExprs = 0; 1578 1579 /// Count the number of simple (constant) return expressions in the function. 1580 unsigned NumSimpleReturnExprs = 0; 1581 1582 /// The last regular (non-return) debug location (breakpoint) in the function. 1583 SourceLocation LastStopPoint; 1584 1585 public: 1586 /// Source location information about the default argument or member 1587 /// initializer expression we're evaluating, if any. 1588 CurrentSourceLocExprScope CurSourceLocExprScope; 1589 using SourceLocExprScopeGuard = 1590 CurrentSourceLocExprScope::SourceLocExprScopeGuard; 1591 1592 /// A scope within which we are constructing the fields of an object which 1593 /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use 1594 /// if we need to evaluate a CXXDefaultInitExpr within the evaluation. 1595 class FieldConstructionScope { 1596 public: 1597 FieldConstructionScope(CodeGenFunction &CGF, Address This) 1598 : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) { 1599 CGF.CXXDefaultInitExprThis = This; 1600 } 1601 ~FieldConstructionScope() { 1602 CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis; 1603 } 1604 1605 private: 1606 CodeGenFunction &CGF; 1607 Address OldCXXDefaultInitExprThis; 1608 }; 1609 1610 /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this' 1611 /// is overridden to be the object under construction. 1612 class CXXDefaultInitExprScope { 1613 public: 1614 CXXDefaultInitExprScope(CodeGenFunction &CGF, const CXXDefaultInitExpr *E) 1615 : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue), 1616 OldCXXThisAlignment(CGF.CXXThisAlignment), 1617 SourceLocScope(E, CGF.CurSourceLocExprScope) { 1618 CGF.CXXThisValue = CGF.CXXDefaultInitExprThis.getPointer(); 1619 CGF.CXXThisAlignment = CGF.CXXDefaultInitExprThis.getAlignment(); 1620 } 1621 ~CXXDefaultInitExprScope() { 1622 CGF.CXXThisValue = OldCXXThisValue; 1623 CGF.CXXThisAlignment = OldCXXThisAlignment; 1624 } 1625 1626 public: 1627 CodeGenFunction &CGF; 1628 llvm::Value *OldCXXThisValue; 1629 CharUnits OldCXXThisAlignment; 1630 SourceLocExprScopeGuard SourceLocScope; 1631 }; 1632 1633 struct CXXDefaultArgExprScope : SourceLocExprScopeGuard { 1634 CXXDefaultArgExprScope(CodeGenFunction &CGF, const CXXDefaultArgExpr *E) 1635 : SourceLocExprScopeGuard(E, CGF.CurSourceLocExprScope) {} 1636 }; 1637 1638 /// The scope of an ArrayInitLoopExpr. Within this scope, the value of the 1639 /// current loop index is overridden. 1640 class ArrayInitLoopExprScope { 1641 public: 1642 ArrayInitLoopExprScope(CodeGenFunction &CGF, llvm::Value *Index) 1643 : CGF(CGF), OldArrayInitIndex(CGF.ArrayInitIndex) { 1644 CGF.ArrayInitIndex = Index; 1645 } 1646 ~ArrayInitLoopExprScope() { 1647 CGF.ArrayInitIndex = OldArrayInitIndex; 1648 } 1649 1650 private: 1651 CodeGenFunction &CGF; 1652 llvm::Value *OldArrayInitIndex; 1653 }; 1654 1655 class InlinedInheritingConstructorScope { 1656 public: 1657 InlinedInheritingConstructorScope(CodeGenFunction &CGF, GlobalDecl GD) 1658 : CGF(CGF), OldCurGD(CGF.CurGD), OldCurFuncDecl(CGF.CurFuncDecl), 1659 OldCurCodeDecl(CGF.CurCodeDecl), 1660 OldCXXABIThisDecl(CGF.CXXABIThisDecl), 1661 OldCXXABIThisValue(CGF.CXXABIThisValue), 1662 OldCXXThisValue(CGF.CXXThisValue), 1663 OldCXXABIThisAlignment(CGF.CXXABIThisAlignment), 1664 OldCXXThisAlignment(CGF.CXXThisAlignment), 1665 OldReturnValue(CGF.ReturnValue), OldFnRetTy(CGF.FnRetTy), 1666 OldCXXInheritedCtorInitExprArgs( 1667 std::move(CGF.CXXInheritedCtorInitExprArgs)) { 1668 CGF.CurGD = GD; 1669 CGF.CurFuncDecl = CGF.CurCodeDecl = 1670 cast<CXXConstructorDecl>(GD.getDecl()); 1671 CGF.CXXABIThisDecl = nullptr; 1672 CGF.CXXABIThisValue = nullptr; 1673 CGF.CXXThisValue = nullptr; 1674 CGF.CXXABIThisAlignment = CharUnits(); 1675 CGF.CXXThisAlignment = CharUnits(); 1676 CGF.ReturnValue = Address::invalid(); 1677 CGF.FnRetTy = QualType(); 1678 CGF.CXXInheritedCtorInitExprArgs.clear(); 1679 } 1680 ~InlinedInheritingConstructorScope() { 1681 CGF.CurGD = OldCurGD; 1682 CGF.CurFuncDecl = OldCurFuncDecl; 1683 CGF.CurCodeDecl = OldCurCodeDecl; 1684 CGF.CXXABIThisDecl = OldCXXABIThisDecl; 1685 CGF.CXXABIThisValue = OldCXXABIThisValue; 1686 CGF.CXXThisValue = OldCXXThisValue; 1687 CGF.CXXABIThisAlignment = OldCXXABIThisAlignment; 1688 CGF.CXXThisAlignment = OldCXXThisAlignment; 1689 CGF.ReturnValue = OldReturnValue; 1690 CGF.FnRetTy = OldFnRetTy; 1691 CGF.CXXInheritedCtorInitExprArgs = 1692 std::move(OldCXXInheritedCtorInitExprArgs); 1693 } 1694 1695 private: 1696 CodeGenFunction &CGF; 1697 GlobalDecl OldCurGD; 1698 const Decl *OldCurFuncDecl; 1699 const Decl *OldCurCodeDecl; 1700 ImplicitParamDecl *OldCXXABIThisDecl; 1701 llvm::Value *OldCXXABIThisValue; 1702 llvm::Value *OldCXXThisValue; 1703 CharUnits OldCXXABIThisAlignment; 1704 CharUnits OldCXXThisAlignment; 1705 Address OldReturnValue; 1706 QualType OldFnRetTy; 1707 CallArgList OldCXXInheritedCtorInitExprArgs; 1708 }; 1709 1710 // Helper class for the OpenMP IR Builder. Allows reusability of code used for 1711 // region body, and finalization codegen callbacks. This will class will also 1712 // contain privatization functions used by the privatization call backs 1713 // 1714 // TODO: this is temporary class for things that are being moved out of 1715 // CGOpenMPRuntime, new versions of current CodeGenFunction methods, or 1716 // utility function for use with the OMPBuilder. Once that move to use the 1717 // OMPBuilder is done, everything here will either become part of CodeGenFunc. 1718 // directly, or a new helper class that will contain functions used by both 1719 // this and the OMPBuilder 1720 1721 struct OMPBuilderCBHelpers { 1722 1723 OMPBuilderCBHelpers() = delete; 1724 OMPBuilderCBHelpers(const OMPBuilderCBHelpers &) = delete; 1725 OMPBuilderCBHelpers &operator=(const OMPBuilderCBHelpers &) = delete; 1726 1727 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 1728 1729 /// Cleanup action for allocate support. 1730 class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup { 1731 1732 private: 1733 llvm::CallInst *RTLFnCI; 1734 1735 public: 1736 OMPAllocateCleanupTy(llvm::CallInst *RLFnCI) : RTLFnCI(RLFnCI) { 1737 RLFnCI->removeFromParent(); 1738 } 1739 1740 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { 1741 if (!CGF.HaveInsertPoint()) 1742 return; 1743 CGF.Builder.Insert(RTLFnCI); 1744 } 1745 }; 1746 1747 /// Returns address of the threadprivate variable for the current 1748 /// thread. This Also create any necessary OMP runtime calls. 1749 /// 1750 /// \param VD VarDecl for Threadprivate variable. 1751 /// \param VDAddr Address of the Vardecl 1752 /// \param Loc The location where the barrier directive was encountered 1753 static Address getAddrOfThreadPrivate(CodeGenFunction &CGF, 1754 const VarDecl *VD, Address VDAddr, 1755 SourceLocation Loc); 1756 1757 /// Gets the OpenMP-specific address of the local variable /p VD. 1758 static Address getAddressOfLocalVariable(CodeGenFunction &CGF, 1759 const VarDecl *VD); 1760 /// Get the platform-specific name separator. 1761 /// \param Parts different parts of the final name that needs separation 1762 /// \param FirstSeparator First separator used between the initial two 1763 /// parts of the name. 1764 /// \param Separator separator used between all of the rest consecutinve 1765 /// parts of the name 1766 static std::string getNameWithSeparators(ArrayRef<StringRef> Parts, 1767 StringRef FirstSeparator = ".", 1768 StringRef Separator = "."); 1769 /// Emit the Finalization for an OMP region 1770 /// \param CGF The Codegen function this belongs to 1771 /// \param IP Insertion point for generating the finalization code. 1772 static void FinalizeOMPRegion(CodeGenFunction &CGF, InsertPointTy IP) { 1773 CGBuilderTy::InsertPointGuard IPG(CGF.Builder); 1774 assert(IP.getBlock()->end() != IP.getPoint() && 1775 "OpenMP IR Builder should cause terminated block!"); 1776 1777 llvm::BasicBlock *IPBB = IP.getBlock(); 1778 llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor(); 1779 assert(DestBB && "Finalization block should have one successor!"); 1780 1781 // erase and replace with cleanup branch. 1782 IPBB->getTerminator()->eraseFromParent(); 1783 CGF.Builder.SetInsertPoint(IPBB); 1784 CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB); 1785 CGF.EmitBranchThroughCleanup(Dest); 1786 } 1787 1788 /// Emit the body of an OMP region 1789 /// \param CGF The Codegen function this belongs to 1790 /// \param RegionBodyStmt The body statement for the OpenMP region being 1791 /// generated 1792 /// \param CodeGenIP Insertion point for generating the body code. 1793 /// \param FiniBB The finalization basic block 1794 static void EmitOMPRegionBody(CodeGenFunction &CGF, 1795 const Stmt *RegionBodyStmt, 1796 InsertPointTy CodeGenIP, 1797 llvm::BasicBlock &FiniBB) { 1798 llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock(); 1799 if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator()) 1800 CodeGenIPBBTI->eraseFromParent(); 1801 1802 CGF.Builder.SetInsertPoint(CodeGenIPBB); 1803 1804 CGF.EmitStmt(RegionBodyStmt); 1805 1806 if (CGF.Builder.saveIP().isSet()) 1807 CGF.Builder.CreateBr(&FiniBB); 1808 } 1809 1810 static void EmitCaptureStmt(CodeGenFunction &CGF, InsertPointTy CodeGenIP, 1811 llvm::BasicBlock &FiniBB, llvm::Function *Fn, 1812 ArrayRef<llvm::Value *> Args) { 1813 llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock(); 1814 if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator()) 1815 CodeGenIPBBTI->eraseFromParent(); 1816 1817 CGF.Builder.SetInsertPoint(CodeGenIPBB); 1818 1819 if (Fn->doesNotThrow()) 1820 CGF.EmitNounwindRuntimeCall(Fn, Args); 1821 else 1822 CGF.EmitRuntimeCall(Fn, Args); 1823 1824 if (CGF.Builder.saveIP().isSet()) 1825 CGF.Builder.CreateBr(&FiniBB); 1826 } 1827 1828 /// RAII for preserving necessary info during Outlined region body codegen. 1829 class OutlinedRegionBodyRAII { 1830 1831 llvm::AssertingVH<llvm::Instruction> OldAllocaIP; 1832 CodeGenFunction::JumpDest OldReturnBlock; 1833 CGBuilderTy::InsertPoint IP; 1834 CodeGenFunction &CGF; 1835 1836 public: 1837 OutlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP, 1838 llvm::BasicBlock &RetBB) 1839 : CGF(cgf) { 1840 assert(AllocaIP.isSet() && 1841 "Must specify Insertion point for allocas of outlined function"); 1842 OldAllocaIP = CGF.AllocaInsertPt; 1843 CGF.AllocaInsertPt = &*AllocaIP.getPoint(); 1844 IP = CGF.Builder.saveIP(); 1845 1846 OldReturnBlock = CGF.ReturnBlock; 1847 CGF.ReturnBlock = CGF.getJumpDestInCurrentScope(&RetBB); 1848 } 1849 1850 ~OutlinedRegionBodyRAII() { 1851 CGF.AllocaInsertPt = OldAllocaIP; 1852 CGF.ReturnBlock = OldReturnBlock; 1853 CGF.Builder.restoreIP(IP); 1854 } 1855 }; 1856 1857 /// RAII for preserving necessary info during inlined region body codegen. 1858 class InlinedRegionBodyRAII { 1859 1860 llvm::AssertingVH<llvm::Instruction> OldAllocaIP; 1861 CodeGenFunction &CGF; 1862 1863 public: 1864 InlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP, 1865 llvm::BasicBlock &FiniBB) 1866 : CGF(cgf) { 1867 // Alloca insertion block should be in the entry block of the containing 1868 // function so it expects an empty AllocaIP in which case will reuse the 1869 // old alloca insertion point, or a new AllocaIP in the same block as 1870 // the old one 1871 assert((!AllocaIP.isSet() || 1872 CGF.AllocaInsertPt->getParent() == AllocaIP.getBlock()) && 1873 "Insertion point should be in the entry block of containing " 1874 "function!"); 1875 OldAllocaIP = CGF.AllocaInsertPt; 1876 if (AllocaIP.isSet()) 1877 CGF.AllocaInsertPt = &*AllocaIP.getPoint(); 1878 1879 // TODO: Remove the call, after making sure the counter is not used by 1880 // the EHStack. 1881 // Since this is an inlined region, it should not modify the 1882 // ReturnBlock, and should reuse the one for the enclosing outlined 1883 // region. So, the JumpDest being return by the function is discarded 1884 (void)CGF.getJumpDestInCurrentScope(&FiniBB); 1885 } 1886 1887 ~InlinedRegionBodyRAII() { CGF.AllocaInsertPt = OldAllocaIP; } 1888 }; 1889 }; 1890 1891 private: 1892 /// CXXThisDecl - When generating code for a C++ member function, 1893 /// this will hold the implicit 'this' declaration. 1894 ImplicitParamDecl *CXXABIThisDecl = nullptr; 1895 llvm::Value *CXXABIThisValue = nullptr; 1896 llvm::Value *CXXThisValue = nullptr; 1897 CharUnits CXXABIThisAlignment; 1898 CharUnits CXXThisAlignment; 1899 1900 /// The value of 'this' to use when evaluating CXXDefaultInitExprs within 1901 /// this expression. 1902 Address CXXDefaultInitExprThis = Address::invalid(); 1903 1904 /// The current array initialization index when evaluating an 1905 /// ArrayInitIndexExpr within an ArrayInitLoopExpr. 1906 llvm::Value *ArrayInitIndex = nullptr; 1907 1908 /// The values of function arguments to use when evaluating 1909 /// CXXInheritedCtorInitExprs within this context. 1910 CallArgList CXXInheritedCtorInitExprArgs; 1911 1912 /// CXXStructorImplicitParamDecl - When generating code for a constructor or 1913 /// destructor, this will hold the implicit argument (e.g. VTT). 1914 ImplicitParamDecl *CXXStructorImplicitParamDecl = nullptr; 1915 llvm::Value *CXXStructorImplicitParamValue = nullptr; 1916 1917 /// OutermostConditional - Points to the outermost active 1918 /// conditional control. This is used so that we know if a 1919 /// temporary should be destroyed conditionally. 1920 ConditionalEvaluation *OutermostConditional = nullptr; 1921 1922 /// The current lexical scope. 1923 LexicalScope *CurLexicalScope = nullptr; 1924 1925 /// The current source location that should be used for exception 1926 /// handling code. 1927 SourceLocation CurEHLocation; 1928 1929 /// BlockByrefInfos - For each __block variable, contains 1930 /// information about the layout of the variable. 1931 llvm::DenseMap<const ValueDecl *, BlockByrefInfo> BlockByrefInfos; 1932 1933 /// Used by -fsanitize=nullability-return to determine whether the return 1934 /// value can be checked. 1935 llvm::Value *RetValNullabilityPrecondition = nullptr; 1936 1937 /// Check if -fsanitize=nullability-return instrumentation is required for 1938 /// this function. 1939 bool requiresReturnValueNullabilityCheck() const { 1940 return RetValNullabilityPrecondition; 1941 } 1942 1943 /// Used to store precise source locations for return statements by the 1944 /// runtime return value checks. 1945 Address ReturnLocation = Address::invalid(); 1946 1947 /// Check if the return value of this function requires sanitization. 1948 bool requiresReturnValueCheck() const; 1949 1950 llvm::BasicBlock *TerminateLandingPad = nullptr; 1951 llvm::BasicBlock *TerminateHandler = nullptr; 1952 llvm::SmallVector<llvm::BasicBlock *, 2> TrapBBs; 1953 1954 /// Terminate funclets keyed by parent funclet pad. 1955 llvm::MapVector<llvm::Value *, llvm::BasicBlock *> TerminateFunclets; 1956 1957 /// Largest vector width used in ths function. Will be used to create a 1958 /// function attribute. 1959 unsigned LargestVectorWidth = 0; 1960 1961 /// True if we need emit the life-time markers. This is initially set in 1962 /// the constructor, but could be overwritten to true if this is a coroutine. 1963 bool ShouldEmitLifetimeMarkers; 1964 1965 /// Add OpenCL kernel arg metadata and the kernel attribute metadata to 1966 /// the function metadata. 1967 void EmitOpenCLKernelMetadata(const FunctionDecl *FD, 1968 llvm::Function *Fn); 1969 1970 public: 1971 CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false); 1972 ~CodeGenFunction(); 1973 1974 CodeGenTypes &getTypes() const { return CGM.getTypes(); } 1975 ASTContext &getContext() const { return CGM.getContext(); } 1976 CGDebugInfo *getDebugInfo() { 1977 if (DisableDebugInfo) 1978 return nullptr; 1979 return DebugInfo; 1980 } 1981 void disableDebugInfo() { DisableDebugInfo = true; } 1982 void enableDebugInfo() { DisableDebugInfo = false; } 1983 1984 bool shouldUseFusedARCCalls() { 1985 return CGM.getCodeGenOpts().OptimizationLevel == 0; 1986 } 1987 1988 const LangOptions &getLangOpts() const { return CGM.getLangOpts(); } 1989 1990 /// Returns a pointer to the function's exception object and selector slot, 1991 /// which is assigned in every landing pad. 1992 Address getExceptionSlot(); 1993 Address getEHSelectorSlot(); 1994 1995 /// Returns the contents of the function's exception object and selector 1996 /// slots. 1997 llvm::Value *getExceptionFromSlot(); 1998 llvm::Value *getSelectorFromSlot(); 1999 2000 Address getNormalCleanupDestSlot(); 2001 2002 llvm::BasicBlock *getUnreachableBlock() { 2003 if (!UnreachableBlock) { 2004 UnreachableBlock = createBasicBlock("unreachable"); 2005 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock); 2006 } 2007 return UnreachableBlock; 2008 } 2009 2010 llvm::BasicBlock *getInvokeDest() { 2011 if (!EHStack.requiresLandingPad()) return nullptr; 2012 return getInvokeDestImpl(); 2013 } 2014 2015 bool currentFunctionUsesSEHTry() const { return CurSEHParent != nullptr; } 2016 2017 const TargetInfo &getTarget() const { return Target; } 2018 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); } 2019 const TargetCodeGenInfo &getTargetHooks() const { 2020 return CGM.getTargetCodeGenInfo(); 2021 } 2022 2023 //===--------------------------------------------------------------------===// 2024 // Cleanups 2025 //===--------------------------------------------------------------------===// 2026 2027 typedef void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty); 2028 2029 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, 2030 Address arrayEndPointer, 2031 QualType elementType, 2032 CharUnits elementAlignment, 2033 Destroyer *destroyer); 2034 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, 2035 llvm::Value *arrayEnd, 2036 QualType elementType, 2037 CharUnits elementAlignment, 2038 Destroyer *destroyer); 2039 2040 void pushDestroy(QualType::DestructionKind dtorKind, 2041 Address addr, QualType type); 2042 void pushEHDestroy(QualType::DestructionKind dtorKind, 2043 Address addr, QualType type); 2044 void pushDestroy(CleanupKind kind, Address addr, QualType type, 2045 Destroyer *destroyer, bool useEHCleanupForArray); 2046 void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, 2047 QualType type, Destroyer *destroyer, 2048 bool useEHCleanupForArray); 2049 void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, 2050 llvm::Value *CompletePtr, 2051 QualType ElementType); 2052 void pushStackRestore(CleanupKind kind, Address SPMem); 2053 void emitDestroy(Address addr, QualType type, Destroyer *destroyer, 2054 bool useEHCleanupForArray); 2055 llvm::Function *generateDestroyHelper(Address addr, QualType type, 2056 Destroyer *destroyer, 2057 bool useEHCleanupForArray, 2058 const VarDecl *VD); 2059 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end, 2060 QualType elementType, CharUnits elementAlign, 2061 Destroyer *destroyer, 2062 bool checkZeroLength, bool useEHCleanup); 2063 2064 Destroyer *getDestroyer(QualType::DestructionKind destructionKind); 2065 2066 /// Determines whether an EH cleanup is required to destroy a type 2067 /// with the given destruction kind. 2068 bool needsEHCleanup(QualType::DestructionKind kind) { 2069 switch (kind) { 2070 case QualType::DK_none: 2071 return false; 2072 case QualType::DK_cxx_destructor: 2073 case QualType::DK_objc_weak_lifetime: 2074 case QualType::DK_nontrivial_c_struct: 2075 return getLangOpts().Exceptions; 2076 case QualType::DK_objc_strong_lifetime: 2077 return getLangOpts().Exceptions && 2078 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions; 2079 } 2080 llvm_unreachable("bad destruction kind"); 2081 } 2082 2083 CleanupKind getCleanupKind(QualType::DestructionKind kind) { 2084 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup); 2085 } 2086 2087 //===--------------------------------------------------------------------===// 2088 // Objective-C 2089 //===--------------------------------------------------------------------===// 2090 2091 void GenerateObjCMethod(const ObjCMethodDecl *OMD); 2092 2093 void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD); 2094 2095 /// GenerateObjCGetter - Synthesize an Objective-C property getter function. 2096 void GenerateObjCGetter(ObjCImplementationDecl *IMP, 2097 const ObjCPropertyImplDecl *PID); 2098 void generateObjCGetterBody(const ObjCImplementationDecl *classImpl, 2099 const ObjCPropertyImplDecl *propImpl, 2100 const ObjCMethodDecl *GetterMothodDecl, 2101 llvm::Constant *AtomicHelperFn); 2102 2103 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, 2104 ObjCMethodDecl *MD, bool ctor); 2105 2106 /// GenerateObjCSetter - Synthesize an Objective-C property setter function 2107 /// for the given property. 2108 void GenerateObjCSetter(ObjCImplementationDecl *IMP, 2109 const ObjCPropertyImplDecl *PID); 2110 void generateObjCSetterBody(const ObjCImplementationDecl *classImpl, 2111 const ObjCPropertyImplDecl *propImpl, 2112 llvm::Constant *AtomicHelperFn); 2113 2114 //===--------------------------------------------------------------------===// 2115 // Block Bits 2116 //===--------------------------------------------------------------------===// 2117 2118 /// Emit block literal. 2119 /// \return an LLVM value which is a pointer to a struct which contains 2120 /// information about the block, including the block invoke function, the 2121 /// captured variables, etc. 2122 llvm::Value *EmitBlockLiteral(const BlockExpr *); 2123 2124 llvm::Function *GenerateBlockFunction(GlobalDecl GD, 2125 const CGBlockInfo &Info, 2126 const DeclMapTy &ldm, 2127 bool IsLambdaConversionToBlock, 2128 bool BuildGlobalBlock); 2129 2130 /// Check if \p T is a C++ class that has a destructor that can throw. 2131 static bool cxxDestructorCanThrow(QualType T); 2132 2133 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo); 2134 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo); 2135 llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction( 2136 const ObjCPropertyImplDecl *PID); 2137 llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction( 2138 const ObjCPropertyImplDecl *PID); 2139 llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty); 2140 2141 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags, 2142 bool CanThrow); 2143 2144 class AutoVarEmission; 2145 2146 void emitByrefStructureInit(const AutoVarEmission &emission); 2147 2148 /// Enter a cleanup to destroy a __block variable. Note that this 2149 /// cleanup should be a no-op if the variable hasn't left the stack 2150 /// yet; if a cleanup is required for the variable itself, that needs 2151 /// to be done externally. 2152 /// 2153 /// \param Kind Cleanup kind. 2154 /// 2155 /// \param Addr When \p LoadBlockVarAddr is false, the address of the __block 2156 /// structure that will be passed to _Block_object_dispose. When 2157 /// \p LoadBlockVarAddr is true, the address of the field of the block 2158 /// structure that holds the address of the __block structure. 2159 /// 2160 /// \param Flags The flag that will be passed to _Block_object_dispose. 2161 /// 2162 /// \param LoadBlockVarAddr Indicates whether we need to emit a load from 2163 /// \p Addr to get the address of the __block structure. 2164 void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags, 2165 bool LoadBlockVarAddr, bool CanThrow); 2166 2167 void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum, 2168 llvm::Value *ptr); 2169 2170 Address LoadBlockStruct(); 2171 Address GetAddrOfBlockDecl(const VarDecl *var); 2172 2173 /// BuildBlockByrefAddress - Computes the location of the 2174 /// data in a variable which is declared as __block. 2175 Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V, 2176 bool followForward = true); 2177 Address emitBlockByrefAddress(Address baseAddr, 2178 const BlockByrefInfo &info, 2179 bool followForward, 2180 const llvm::Twine &name); 2181 2182 const BlockByrefInfo &getBlockByrefInfo(const VarDecl *var); 2183 2184 QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args); 2185 2186 void GenerateCode(GlobalDecl GD, llvm::Function *Fn, 2187 const CGFunctionInfo &FnInfo); 2188 2189 /// Annotate the function with an attribute that disables TSan checking at 2190 /// runtime. 2191 void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn); 2192 2193 /// Emit code for the start of a function. 2194 /// \param Loc The location to be associated with the function. 2195 /// \param StartLoc The location of the function body. 2196 void StartFunction(GlobalDecl GD, 2197 QualType RetTy, 2198 llvm::Function *Fn, 2199 const CGFunctionInfo &FnInfo, 2200 const FunctionArgList &Args, 2201 SourceLocation Loc = SourceLocation(), 2202 SourceLocation StartLoc = SourceLocation()); 2203 2204 static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor); 2205 2206 void EmitConstructorBody(FunctionArgList &Args); 2207 void EmitDestructorBody(FunctionArgList &Args); 2208 void emitImplicitAssignmentOperatorBody(FunctionArgList &Args); 2209 void EmitFunctionBody(const Stmt *Body); 2210 void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S); 2211 2212 void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator, 2213 CallArgList &CallArgs); 2214 void EmitLambdaBlockInvokeBody(); 2215 void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD); 2216 void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD); 2217 void EmitLambdaVLACapture(const VariableArrayType *VAT, LValue LV) { 2218 EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV); 2219 } 2220 void EmitAsanPrologueOrEpilogue(bool Prologue); 2221 2222 /// Emit the unified return block, trying to avoid its emission when 2223 /// possible. 2224 /// \return The debug location of the user written return statement if the 2225 /// return block is is avoided. 2226 llvm::DebugLoc EmitReturnBlock(); 2227 2228 /// FinishFunction - Complete IR generation of the current function. It is 2229 /// legal to call this function even if there is no current insertion point. 2230 void FinishFunction(SourceLocation EndLoc=SourceLocation()); 2231 2232 void StartThunk(llvm::Function *Fn, GlobalDecl GD, 2233 const CGFunctionInfo &FnInfo, bool IsUnprototyped); 2234 2235 void EmitCallAndReturnForThunk(llvm::FunctionCallee Callee, 2236 const ThunkInfo *Thunk, bool IsUnprototyped); 2237 2238 void FinishThunk(); 2239 2240 /// Emit a musttail call for a thunk with a potentially adjusted this pointer. 2241 void EmitMustTailThunk(GlobalDecl GD, llvm::Value *AdjustedThisPtr, 2242 llvm::FunctionCallee Callee); 2243 2244 /// Generate a thunk for the given method. 2245 void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo, 2246 GlobalDecl GD, const ThunkInfo &Thunk, 2247 bool IsUnprototyped); 2248 2249 llvm::Function *GenerateVarArgsThunk(llvm::Function *Fn, 2250 const CGFunctionInfo &FnInfo, 2251 GlobalDecl GD, const ThunkInfo &Thunk); 2252 2253 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type, 2254 FunctionArgList &Args); 2255 2256 void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init); 2257 2258 /// Struct with all information about dynamic [sub]class needed to set vptr. 2259 struct VPtr { 2260 BaseSubobject Base; 2261 const CXXRecordDecl *NearestVBase; 2262 CharUnits OffsetFromNearestVBase; 2263 const CXXRecordDecl *VTableClass; 2264 }; 2265 2266 /// Initialize the vtable pointer of the given subobject. 2267 void InitializeVTablePointer(const VPtr &vptr); 2268 2269 typedef llvm::SmallVector<VPtr, 4> VPtrsVector; 2270 2271 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; 2272 VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass); 2273 2274 void getVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase, 2275 CharUnits OffsetFromNearestVBase, 2276 bool BaseIsNonVirtualPrimaryBase, 2277 const CXXRecordDecl *VTableClass, 2278 VisitedVirtualBasesSetTy &VBases, VPtrsVector &vptrs); 2279 2280 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl); 2281 2282 /// GetVTablePtr - Return the Value of the vtable pointer member pointed 2283 /// to by This. 2284 llvm::Value *GetVTablePtr(Address This, llvm::Type *VTableTy, 2285 const CXXRecordDecl *VTableClass); 2286 2287 enum CFITypeCheckKind { 2288 CFITCK_VCall, 2289 CFITCK_NVCall, 2290 CFITCK_DerivedCast, 2291 CFITCK_UnrelatedCast, 2292 CFITCK_ICall, 2293 CFITCK_NVMFCall, 2294 CFITCK_VMFCall, 2295 }; 2296 2297 /// Derived is the presumed address of an object of type T after a 2298 /// cast. If T is a polymorphic class type, emit a check that the virtual 2299 /// table for Derived belongs to a class derived from T. 2300 void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived, 2301 bool MayBeNull, CFITypeCheckKind TCK, 2302 SourceLocation Loc); 2303 2304 /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable. 2305 /// If vptr CFI is enabled, emit a check that VTable is valid. 2306 void EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable, 2307 CFITypeCheckKind TCK, SourceLocation Loc); 2308 2309 /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for 2310 /// RD using llvm.type.test. 2311 void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable, 2312 CFITypeCheckKind TCK, SourceLocation Loc); 2313 2314 /// If whole-program virtual table optimization is enabled, emit an assumption 2315 /// that VTable is a member of RD's type identifier. Or, if vptr CFI is 2316 /// enabled, emit a check that VTable is a member of RD's type identifier. 2317 void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD, 2318 llvm::Value *VTable, SourceLocation Loc); 2319 2320 /// Returns whether we should perform a type checked load when loading a 2321 /// virtual function for virtual calls to members of RD. This is generally 2322 /// true when both vcall CFI and whole-program-vtables are enabled. 2323 bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD); 2324 2325 /// Emit a type checked load from the given vtable. 2326 llvm::Value *EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable, 2327 uint64_t VTableByteOffset); 2328 2329 /// EnterDtorCleanups - Enter the cleanups necessary to complete the 2330 /// given phase of destruction for a destructor. The end result 2331 /// should call destructors on members and base classes in reverse 2332 /// order of their construction. 2333 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type); 2334 2335 /// ShouldInstrumentFunction - Return true if the current function should be 2336 /// instrumented with __cyg_profile_func_* calls 2337 bool ShouldInstrumentFunction(); 2338 2339 /// ShouldSkipSanitizerInstrumentation - Return true if the current function 2340 /// should not be instrumented with sanitizers. 2341 bool ShouldSkipSanitizerInstrumentation(); 2342 2343 /// ShouldXRayInstrument - Return true if the current function should be 2344 /// instrumented with XRay nop sleds. 2345 bool ShouldXRayInstrumentFunction() const; 2346 2347 /// AlwaysEmitXRayCustomEvents - Return true if we must unconditionally emit 2348 /// XRay custom event handling calls. 2349 bool AlwaysEmitXRayCustomEvents() const; 2350 2351 /// AlwaysEmitXRayTypedEvents - Return true if clang must unconditionally emit 2352 /// XRay typed event handling calls. 2353 bool AlwaysEmitXRayTypedEvents() const; 2354 2355 /// Encode an address into a form suitable for use in a function prologue. 2356 llvm::Constant *EncodeAddrForUseInPrologue(llvm::Function *F, 2357 llvm::Constant *Addr); 2358 2359 /// Decode an address used in a function prologue, encoded by \c 2360 /// EncodeAddrForUseInPrologue. 2361 llvm::Value *DecodeAddrUsedInPrologue(llvm::Value *F, 2362 llvm::Value *EncodedAddr); 2363 2364 /// EmitFunctionProlog - Emit the target specific LLVM code to load the 2365 /// arguments for the given function. This is also responsible for naming the 2366 /// LLVM function arguments. 2367 void EmitFunctionProlog(const CGFunctionInfo &FI, 2368 llvm::Function *Fn, 2369 const FunctionArgList &Args); 2370 2371 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the 2372 /// given temporary. 2373 void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc, 2374 SourceLocation EndLoc); 2375 2376 /// Emit a test that checks if the return value \p RV is nonnull. 2377 void EmitReturnValueCheck(llvm::Value *RV); 2378 2379 /// EmitStartEHSpec - Emit the start of the exception spec. 2380 void EmitStartEHSpec(const Decl *D); 2381 2382 /// EmitEndEHSpec - Emit the end of the exception spec. 2383 void EmitEndEHSpec(const Decl *D); 2384 2385 /// getTerminateLandingPad - Return a landing pad that just calls terminate. 2386 llvm::BasicBlock *getTerminateLandingPad(); 2387 2388 /// getTerminateLandingPad - Return a cleanup funclet that just calls 2389 /// terminate. 2390 llvm::BasicBlock *getTerminateFunclet(); 2391 2392 /// getTerminateHandler - Return a handler (not a landing pad, just 2393 /// a catch handler) that just calls terminate. This is used when 2394 /// a terminate scope encloses a try. 2395 llvm::BasicBlock *getTerminateHandler(); 2396 2397 llvm::Type *ConvertTypeForMem(QualType T); 2398 llvm::Type *ConvertType(QualType T); 2399 llvm::Type *ConvertType(const TypeDecl *T) { 2400 return ConvertType(getContext().getTypeDeclType(T)); 2401 } 2402 2403 /// LoadObjCSelf - Load the value of self. This function is only valid while 2404 /// generating code for an Objective-C method. 2405 llvm::Value *LoadObjCSelf(); 2406 2407 /// TypeOfSelfObject - Return type of object that this self represents. 2408 QualType TypeOfSelfObject(); 2409 2410 /// getEvaluationKind - Return the TypeEvaluationKind of QualType \c T. 2411 static TypeEvaluationKind getEvaluationKind(QualType T); 2412 2413 static bool hasScalarEvaluationKind(QualType T) { 2414 return getEvaluationKind(T) == TEK_Scalar; 2415 } 2416 2417 static bool hasAggregateEvaluationKind(QualType T) { 2418 return getEvaluationKind(T) == TEK_Aggregate; 2419 } 2420 2421 /// createBasicBlock - Create an LLVM basic block. 2422 llvm::BasicBlock *createBasicBlock(const Twine &name = "", 2423 llvm::Function *parent = nullptr, 2424 llvm::BasicBlock *before = nullptr) { 2425 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before); 2426 } 2427 2428 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified 2429 /// label maps to. 2430 JumpDest getJumpDestForLabel(const LabelDecl *S); 2431 2432 /// SimplifyForwardingBlocks - If the given basic block is only a branch to 2433 /// another basic block, simplify it. This assumes that no other code could 2434 /// potentially reference the basic block. 2435 void SimplifyForwardingBlocks(llvm::BasicBlock *BB); 2436 2437 /// EmitBlock - Emit the given block \arg BB and set it as the insert point, 2438 /// adding a fall-through branch from the current insert block if 2439 /// necessary. It is legal to call this function even if there is no current 2440 /// insertion point. 2441 /// 2442 /// IsFinished - If true, indicates that the caller has finished emitting 2443 /// branches to the given block and does not expect to emit code into it. This 2444 /// means the block can be ignored if it is unreachable. 2445 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false); 2446 2447 /// EmitBlockAfterUses - Emit the given block somewhere hopefully 2448 /// near its uses, and leave the insertion point in it. 2449 void EmitBlockAfterUses(llvm::BasicBlock *BB); 2450 2451 /// EmitBranch - Emit a branch to the specified basic block from the current 2452 /// insert block, taking care to avoid creation of branches from dummy 2453 /// blocks. It is legal to call this function even if there is no current 2454 /// insertion point. 2455 /// 2456 /// This function clears the current insertion point. The caller should follow 2457 /// calls to this function with calls to Emit*Block prior to generation new 2458 /// code. 2459 void EmitBranch(llvm::BasicBlock *Block); 2460 2461 /// HaveInsertPoint - True if an insertion point is defined. If not, this 2462 /// indicates that the current code being emitted is unreachable. 2463 bool HaveInsertPoint() const { 2464 return Builder.GetInsertBlock() != nullptr; 2465 } 2466 2467 /// EnsureInsertPoint - Ensure that an insertion point is defined so that 2468 /// emitted IR has a place to go. Note that by definition, if this function 2469 /// creates a block then that block is unreachable; callers may do better to 2470 /// detect when no insertion point is defined and simply skip IR generation. 2471 void EnsureInsertPoint() { 2472 if (!HaveInsertPoint()) 2473 EmitBlock(createBasicBlock()); 2474 } 2475 2476 /// ErrorUnsupported - Print out an error that codegen doesn't support the 2477 /// specified stmt yet. 2478 void ErrorUnsupported(const Stmt *S, const char *Type); 2479 2480 //===--------------------------------------------------------------------===// 2481 // Helpers 2482 //===--------------------------------------------------------------------===// 2483 2484 LValue MakeAddrLValue(Address Addr, QualType T, 2485 AlignmentSource Source = AlignmentSource::Type) { 2486 return LValue::MakeAddr(Addr, T, getContext(), LValueBaseInfo(Source), 2487 CGM.getTBAAAccessInfo(T)); 2488 } 2489 2490 LValue MakeAddrLValue(Address Addr, QualType T, LValueBaseInfo BaseInfo, 2491 TBAAAccessInfo TBAAInfo) { 2492 return LValue::MakeAddr(Addr, T, getContext(), BaseInfo, TBAAInfo); 2493 } 2494 2495 LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment, 2496 AlignmentSource Source = AlignmentSource::Type) { 2497 return LValue::MakeAddr(Address(V, Alignment), T, getContext(), 2498 LValueBaseInfo(Source), CGM.getTBAAAccessInfo(T)); 2499 } 2500 2501 LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment, 2502 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) { 2503 return LValue::MakeAddr(Address(V, Alignment), T, getContext(), 2504 BaseInfo, TBAAInfo); 2505 } 2506 2507 LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T); 2508 LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T); 2509 2510 Address EmitLoadOfReference(LValue RefLVal, 2511 LValueBaseInfo *PointeeBaseInfo = nullptr, 2512 TBAAAccessInfo *PointeeTBAAInfo = nullptr); 2513 LValue EmitLoadOfReferenceLValue(LValue RefLVal); 2514 LValue EmitLoadOfReferenceLValue(Address RefAddr, QualType RefTy, 2515 AlignmentSource Source = 2516 AlignmentSource::Type) { 2517 LValue RefLVal = MakeAddrLValue(RefAddr, RefTy, LValueBaseInfo(Source), 2518 CGM.getTBAAAccessInfo(RefTy)); 2519 return EmitLoadOfReferenceLValue(RefLVal); 2520 } 2521 2522 Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy, 2523 LValueBaseInfo *BaseInfo = nullptr, 2524 TBAAAccessInfo *TBAAInfo = nullptr); 2525 LValue EmitLoadOfPointerLValue(Address Ptr, const PointerType *PtrTy); 2526 2527 /// CreateTempAlloca - This creates an alloca and inserts it into the entry 2528 /// block if \p ArraySize is nullptr, otherwise inserts it at the current 2529 /// insertion point of the builder. The caller is responsible for setting an 2530 /// appropriate alignment on 2531 /// the alloca. 2532 /// 2533 /// \p ArraySize is the number of array elements to be allocated if it 2534 /// is not nullptr. 2535 /// 2536 /// LangAS::Default is the address space of pointers to local variables and 2537 /// temporaries, as exposed in the source language. In certain 2538 /// configurations, this is not the same as the alloca address space, and a 2539 /// cast is needed to lift the pointer from the alloca AS into 2540 /// LangAS::Default. This can happen when the target uses a restricted 2541 /// address space for the stack but the source language requires 2542 /// LangAS::Default to be a generic address space. The latter condition is 2543 /// common for most programming languages; OpenCL is an exception in that 2544 /// LangAS::Default is the private address space, which naturally maps 2545 /// to the stack. 2546 /// 2547 /// Because the address of a temporary is often exposed to the program in 2548 /// various ways, this function will perform the cast. The original alloca 2549 /// instruction is returned through \p Alloca if it is not nullptr. 2550 /// 2551 /// The cast is not performaed in CreateTempAllocaWithoutCast. This is 2552 /// more efficient if the caller knows that the address will not be exposed. 2553 llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty, const Twine &Name = "tmp", 2554 llvm::Value *ArraySize = nullptr); 2555 Address CreateTempAlloca(llvm::Type *Ty, CharUnits align, 2556 const Twine &Name = "tmp", 2557 llvm::Value *ArraySize = nullptr, 2558 Address *Alloca = nullptr); 2559 Address CreateTempAllocaWithoutCast(llvm::Type *Ty, CharUnits align, 2560 const Twine &Name = "tmp", 2561 llvm::Value *ArraySize = nullptr); 2562 2563 /// CreateDefaultAlignedTempAlloca - This creates an alloca with the 2564 /// default ABI alignment of the given LLVM type. 2565 /// 2566 /// IMPORTANT NOTE: This is *not* generally the right alignment for 2567 /// any given AST type that happens to have been lowered to the 2568 /// given IR type. This should only ever be used for function-local, 2569 /// IR-driven manipulations like saving and restoring a value. Do 2570 /// not hand this address off to arbitrary IRGen routines, and especially 2571 /// do not pass it as an argument to a function that might expect a 2572 /// properly ABI-aligned value. 2573 Address CreateDefaultAlignTempAlloca(llvm::Type *Ty, 2574 const Twine &Name = "tmp"); 2575 2576 /// CreateIRTemp - Create a temporary IR object of the given type, with 2577 /// appropriate alignment. This routine should only be used when an temporary 2578 /// value needs to be stored into an alloca (for example, to avoid explicit 2579 /// PHI construction), but the type is the IR type, not the type appropriate 2580 /// for storing in memory. 2581 /// 2582 /// That is, this is exactly equivalent to CreateMemTemp, but calling 2583 /// ConvertType instead of ConvertTypeForMem. 2584 Address CreateIRTemp(QualType T, const Twine &Name = "tmp"); 2585 2586 /// CreateMemTemp - Create a temporary memory object of the given type, with 2587 /// appropriate alignmen and cast it to the default address space. Returns 2588 /// the original alloca instruction by \p Alloca if it is not nullptr. 2589 Address CreateMemTemp(QualType T, const Twine &Name = "tmp", 2590 Address *Alloca = nullptr); 2591 Address CreateMemTemp(QualType T, CharUnits Align, const Twine &Name = "tmp", 2592 Address *Alloca = nullptr); 2593 2594 /// CreateMemTemp - Create a temporary memory object of the given type, with 2595 /// appropriate alignmen without casting it to the default address space. 2596 Address CreateMemTempWithoutCast(QualType T, const Twine &Name = "tmp"); 2597 Address CreateMemTempWithoutCast(QualType T, CharUnits Align, 2598 const Twine &Name = "tmp"); 2599 2600 /// CreateAggTemp - Create a temporary memory object for the given 2601 /// aggregate type. 2602 AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp", 2603 Address *Alloca = nullptr) { 2604 return AggValueSlot::forAddr(CreateMemTemp(T, Name, Alloca), 2605 T.getQualifiers(), 2606 AggValueSlot::IsNotDestructed, 2607 AggValueSlot::DoesNotNeedGCBarriers, 2608 AggValueSlot::IsNotAliased, 2609 AggValueSlot::DoesNotOverlap); 2610 } 2611 2612 /// Emit a cast to void* in the appropriate address space. 2613 llvm::Value *EmitCastToVoidPtr(llvm::Value *value); 2614 2615 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified 2616 /// expression and compare the result against zero, returning an Int1Ty value. 2617 llvm::Value *EvaluateExprAsBool(const Expr *E); 2618 2619 /// EmitIgnoredExpr - Emit an expression in a context which ignores the result. 2620 void EmitIgnoredExpr(const Expr *E); 2621 2622 /// EmitAnyExpr - Emit code to compute the specified expression which can have 2623 /// any type. The result is returned as an RValue struct. If this is an 2624 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where 2625 /// the result should be returned. 2626 /// 2627 /// \param ignoreResult True if the resulting value isn't used. 2628 RValue EmitAnyExpr(const Expr *E, 2629 AggValueSlot aggSlot = AggValueSlot::ignored(), 2630 bool ignoreResult = false); 2631 2632 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address 2633 // or the value of the expression, depending on how va_list is defined. 2634 Address EmitVAListRef(const Expr *E); 2635 2636 /// Emit a "reference" to a __builtin_ms_va_list; this is 2637 /// always the value of the expression, because a __builtin_ms_va_list is a 2638 /// pointer to a char. 2639 Address EmitMSVAListRef(const Expr *E); 2640 2641 /// EmitAnyExprToTemp - Similarly to EmitAnyExpr(), however, the result will 2642 /// always be accessible even if no aggregate location is provided. 2643 RValue EmitAnyExprToTemp(const Expr *E); 2644 2645 /// EmitAnyExprToMem - Emits the code necessary to evaluate an 2646 /// arbitrary expression into the given memory location. 2647 void EmitAnyExprToMem(const Expr *E, Address Location, 2648 Qualifiers Quals, bool IsInitializer); 2649 2650 void EmitAnyExprToExn(const Expr *E, Address Addr); 2651 2652 /// EmitExprAsInit - Emits the code necessary to initialize a 2653 /// location in memory with the given initializer. 2654 void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, 2655 bool capturedByInit); 2656 2657 /// hasVolatileMember - returns true if aggregate type has a volatile 2658 /// member. 2659 bool hasVolatileMember(QualType T) { 2660 if (const RecordType *RT = T->getAs<RecordType>()) { 2661 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); 2662 return RD->hasVolatileMember(); 2663 } 2664 return false; 2665 } 2666 2667 /// Determine whether a return value slot may overlap some other object. 2668 AggValueSlot::Overlap_t getOverlapForReturnValue() { 2669 // FIXME: Assuming no overlap here breaks guaranteed copy elision for base 2670 // class subobjects. These cases may need to be revisited depending on the 2671 // resolution of the relevant core issue. 2672 return AggValueSlot::DoesNotOverlap; 2673 } 2674 2675 /// Determine whether a field initialization may overlap some other object. 2676 AggValueSlot::Overlap_t getOverlapForFieldInit(const FieldDecl *FD); 2677 2678 /// Determine whether a base class initialization may overlap some other 2679 /// object. 2680 AggValueSlot::Overlap_t getOverlapForBaseInit(const CXXRecordDecl *RD, 2681 const CXXRecordDecl *BaseRD, 2682 bool IsVirtual); 2683 2684 /// Emit an aggregate assignment. 2685 void EmitAggregateAssign(LValue Dest, LValue Src, QualType EltTy) { 2686 bool IsVolatile = hasVolatileMember(EltTy); 2687 EmitAggregateCopy(Dest, Src, EltTy, AggValueSlot::MayOverlap, IsVolatile); 2688 } 2689 2690 void EmitAggregateCopyCtor(LValue Dest, LValue Src, 2691 AggValueSlot::Overlap_t MayOverlap) { 2692 EmitAggregateCopy(Dest, Src, Src.getType(), MayOverlap); 2693 } 2694 2695 /// EmitAggregateCopy - Emit an aggregate copy. 2696 /// 2697 /// \param isVolatile \c true iff either the source or the destination is 2698 /// volatile. 2699 /// \param MayOverlap Whether the tail padding of the destination might be 2700 /// occupied by some other object. More efficient code can often be 2701 /// generated if not. 2702 void EmitAggregateCopy(LValue Dest, LValue Src, QualType EltTy, 2703 AggValueSlot::Overlap_t MayOverlap, 2704 bool isVolatile = false); 2705 2706 /// GetAddrOfLocalVar - Return the address of a local variable. 2707 Address GetAddrOfLocalVar(const VarDecl *VD) { 2708 auto it = LocalDeclMap.find(VD); 2709 assert(it != LocalDeclMap.end() && 2710 "Invalid argument to GetAddrOfLocalVar(), no decl!"); 2711 return it->second; 2712 } 2713 2714 /// Given an opaque value expression, return its LValue mapping if it exists, 2715 /// otherwise create one. 2716 LValue getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e); 2717 2718 /// Given an opaque value expression, return its RValue mapping if it exists, 2719 /// otherwise create one. 2720 RValue getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e); 2721 2722 /// Get the index of the current ArrayInitLoopExpr, if any. 2723 llvm::Value *getArrayInitIndex() { return ArrayInitIndex; } 2724 2725 /// getAccessedFieldNo - Given an encoded value and a result number, return 2726 /// the input field number being accessed. 2727 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts); 2728 2729 llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L); 2730 llvm::BasicBlock *GetIndirectGotoBlock(); 2731 2732 /// Check if \p E is a C++ "this" pointer wrapped in value-preserving casts. 2733 static bool IsWrappedCXXThis(const Expr *E); 2734 2735 /// EmitNullInitialization - Generate code to set a value of the given type to 2736 /// null, If the type contains data member pointers, they will be initialized 2737 /// to -1 in accordance with the Itanium C++ ABI. 2738 void EmitNullInitialization(Address DestPtr, QualType Ty); 2739 2740 /// Emits a call to an LLVM variable-argument intrinsic, either 2741 /// \c llvm.va_start or \c llvm.va_end. 2742 /// \param ArgValue A reference to the \c va_list as emitted by either 2743 /// \c EmitVAListRef or \c EmitMSVAListRef. 2744 /// \param IsStart If \c true, emits a call to \c llvm.va_start; otherwise, 2745 /// calls \c llvm.va_end. 2746 llvm::Value *EmitVAStartEnd(llvm::Value *ArgValue, bool IsStart); 2747 2748 /// Generate code to get an argument from the passed in pointer 2749 /// and update it accordingly. 2750 /// \param VE The \c VAArgExpr for which to generate code. 2751 /// \param VAListAddr Receives a reference to the \c va_list as emitted by 2752 /// either \c EmitVAListRef or \c EmitMSVAListRef. 2753 /// \returns A pointer to the argument. 2754 // FIXME: We should be able to get rid of this method and use the va_arg 2755 // instruction in LLVM instead once it works well enough. 2756 Address EmitVAArg(VAArgExpr *VE, Address &VAListAddr); 2757 2758 /// emitArrayLength - Compute the length of an array, even if it's a 2759 /// VLA, and drill down to the base element type. 2760 llvm::Value *emitArrayLength(const ArrayType *arrayType, 2761 QualType &baseType, 2762 Address &addr); 2763 2764 /// EmitVLASize - Capture all the sizes for the VLA expressions in 2765 /// the given variably-modified type and store them in the VLASizeMap. 2766 /// 2767 /// This function can be called with a null (unreachable) insert point. 2768 void EmitVariablyModifiedType(QualType Ty); 2769 2770 struct VlaSizePair { 2771 llvm::Value *NumElts; 2772 QualType Type; 2773 2774 VlaSizePair(llvm::Value *NE, QualType T) : NumElts(NE), Type(T) {} 2775 }; 2776 2777 /// Return the number of elements for a single dimension 2778 /// for the given array type. 2779 VlaSizePair getVLAElements1D(const VariableArrayType *vla); 2780 VlaSizePair getVLAElements1D(QualType vla); 2781 2782 /// Returns an LLVM value that corresponds to the size, 2783 /// in non-variably-sized elements, of a variable length array type, 2784 /// plus that largest non-variably-sized element type. Assumes that 2785 /// the type has already been emitted with EmitVariablyModifiedType. 2786 VlaSizePair getVLASize(const VariableArrayType *vla); 2787 VlaSizePair getVLASize(QualType vla); 2788 2789 /// LoadCXXThis - Load the value of 'this'. This function is only valid while 2790 /// generating code for an C++ member function. 2791 llvm::Value *LoadCXXThis() { 2792 assert(CXXThisValue && "no 'this' value for this function"); 2793 return CXXThisValue; 2794 } 2795 Address LoadCXXThisAddress(); 2796 2797 /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have 2798 /// virtual bases. 2799 // FIXME: Every place that calls LoadCXXVTT is something 2800 // that needs to be abstracted properly. 2801 llvm::Value *LoadCXXVTT() { 2802 assert(CXXStructorImplicitParamValue && "no VTT value for this function"); 2803 return CXXStructorImplicitParamValue; 2804 } 2805 2806 /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a 2807 /// complete class to the given direct base. 2808 Address 2809 GetAddressOfDirectBaseInCompleteClass(Address Value, 2810 const CXXRecordDecl *Derived, 2811 const CXXRecordDecl *Base, 2812 bool BaseIsVirtual); 2813 2814 static bool ShouldNullCheckClassCastValue(const CastExpr *Cast); 2815 2816 /// GetAddressOfBaseClass - This function will add the necessary delta to the 2817 /// load of 'this' and returns address of the base class. 2818 Address GetAddressOfBaseClass(Address Value, 2819 const CXXRecordDecl *Derived, 2820 CastExpr::path_const_iterator PathBegin, 2821 CastExpr::path_const_iterator PathEnd, 2822 bool NullCheckValue, SourceLocation Loc); 2823 2824 Address GetAddressOfDerivedClass(Address Value, 2825 const CXXRecordDecl *Derived, 2826 CastExpr::path_const_iterator PathBegin, 2827 CastExpr::path_const_iterator PathEnd, 2828 bool NullCheckValue); 2829 2830 /// GetVTTParameter - Return the VTT parameter that should be passed to a 2831 /// base constructor/destructor with virtual bases. 2832 /// FIXME: VTTs are Itanium ABI-specific, so the definition should move 2833 /// to ItaniumCXXABI.cpp together with all the references to VTT. 2834 llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase, 2835 bool Delegating); 2836 2837 void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, 2838 CXXCtorType CtorType, 2839 const FunctionArgList &Args, 2840 SourceLocation Loc); 2841 // It's important not to confuse this and the previous function. Delegating 2842 // constructors are the C++0x feature. The constructor delegate optimization 2843 // is used to reduce duplication in the base and complete consturctors where 2844 // they are substantially the same. 2845 void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, 2846 const FunctionArgList &Args); 2847 2848 /// Emit a call to an inheriting constructor (that is, one that invokes a 2849 /// constructor inherited from a base class) by inlining its definition. This 2850 /// is necessary if the ABI does not support forwarding the arguments to the 2851 /// base class constructor (because they're variadic or similar). 2852 void EmitInlinedInheritingCXXConstructorCall(const CXXConstructorDecl *Ctor, 2853 CXXCtorType CtorType, 2854 bool ForVirtualBase, 2855 bool Delegating, 2856 CallArgList &Args); 2857 2858 /// Emit a call to a constructor inherited from a base class, passing the 2859 /// current constructor's arguments along unmodified (without even making 2860 /// a copy). 2861 void EmitInheritedCXXConstructorCall(const CXXConstructorDecl *D, 2862 bool ForVirtualBase, Address This, 2863 bool InheritedFromVBase, 2864 const CXXInheritedCtorInitExpr *E); 2865 2866 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, 2867 bool ForVirtualBase, bool Delegating, 2868 AggValueSlot ThisAVS, const CXXConstructExpr *E); 2869 2870 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, 2871 bool ForVirtualBase, bool Delegating, 2872 Address This, CallArgList &Args, 2873 AggValueSlot::Overlap_t Overlap, 2874 SourceLocation Loc, bool NewPointerIsChecked); 2875 2876 /// Emit assumption load for all bases. Requires to be be called only on 2877 /// most-derived class and not under construction of the object. 2878 void EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, Address This); 2879 2880 /// Emit assumption that vptr load == global vtable. 2881 void EmitVTableAssumptionLoad(const VPtr &vptr, Address This); 2882 2883 void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, 2884 Address This, Address Src, 2885 const CXXConstructExpr *E); 2886 2887 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, 2888 const ArrayType *ArrayTy, 2889 Address ArrayPtr, 2890 const CXXConstructExpr *E, 2891 bool NewPointerIsChecked, 2892 bool ZeroInitialization = false); 2893 2894 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, 2895 llvm::Value *NumElements, 2896 Address ArrayPtr, 2897 const CXXConstructExpr *E, 2898 bool NewPointerIsChecked, 2899 bool ZeroInitialization = false); 2900 2901 static Destroyer destroyCXXObject; 2902 2903 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, 2904 bool ForVirtualBase, bool Delegating, Address This, 2905 QualType ThisTy); 2906 2907 void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType, 2908 llvm::Type *ElementTy, Address NewPtr, 2909 llvm::Value *NumElements, 2910 llvm::Value *AllocSizeWithoutCookie); 2911 2912 void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType, 2913 Address Ptr); 2914 2915 void EmitSehCppScopeBegin(); 2916 void EmitSehCppScopeEnd(); 2917 void EmitSehTryScopeBegin(); 2918 void EmitSehTryScopeEnd(); 2919 2920 llvm::Value *EmitLifetimeStart(llvm::TypeSize Size, llvm::Value *Addr); 2921 void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr); 2922 2923 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E); 2924 void EmitCXXDeleteExpr(const CXXDeleteExpr *E); 2925 2926 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, 2927 QualType DeleteTy, llvm::Value *NumElements = nullptr, 2928 CharUnits CookieSize = CharUnits()); 2929 2930 RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, 2931 const CallExpr *TheCallExpr, bool IsDelete); 2932 2933 llvm::Value *EmitCXXTypeidExpr(const CXXTypeidExpr *E); 2934 llvm::Value *EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE); 2935 Address EmitCXXUuidofExpr(const CXXUuidofExpr *E); 2936 2937 /// Situations in which we might emit a check for the suitability of a 2938 /// pointer or glvalue. Needs to be kept in sync with ubsan_handlers.cpp in 2939 /// compiler-rt. 2940 enum TypeCheckKind { 2941 /// Checking the operand of a load. Must be suitably sized and aligned. 2942 TCK_Load, 2943 /// Checking the destination of a store. Must be suitably sized and aligned. 2944 TCK_Store, 2945 /// Checking the bound value in a reference binding. Must be suitably sized 2946 /// and aligned, but is not required to refer to an object (until the 2947 /// reference is used), per core issue 453. 2948 TCK_ReferenceBinding, 2949 /// Checking the object expression in a non-static data member access. Must 2950 /// be an object within its lifetime. 2951 TCK_MemberAccess, 2952 /// Checking the 'this' pointer for a call to a non-static member function. 2953 /// Must be an object within its lifetime. 2954 TCK_MemberCall, 2955 /// Checking the 'this' pointer for a constructor call. 2956 TCK_ConstructorCall, 2957 /// Checking the operand of a static_cast to a derived pointer type. Must be 2958 /// null or an object within its lifetime. 2959 TCK_DowncastPointer, 2960 /// Checking the operand of a static_cast to a derived reference type. Must 2961 /// be an object within its lifetime. 2962 TCK_DowncastReference, 2963 /// Checking the operand of a cast to a base object. Must be suitably sized 2964 /// and aligned. 2965 TCK_Upcast, 2966 /// Checking the operand of a cast to a virtual base object. Must be an 2967 /// object within its lifetime. 2968 TCK_UpcastToVirtualBase, 2969 /// Checking the value assigned to a _Nonnull pointer. Must not be null. 2970 TCK_NonnullAssign, 2971 /// Checking the operand of a dynamic_cast or a typeid expression. Must be 2972 /// null or an object within its lifetime. 2973 TCK_DynamicOperation 2974 }; 2975 2976 /// Determine whether the pointer type check \p TCK permits null pointers. 2977 static bool isNullPointerAllowed(TypeCheckKind TCK); 2978 2979 /// Determine whether the pointer type check \p TCK requires a vptr check. 2980 static bool isVptrCheckRequired(TypeCheckKind TCK, QualType Ty); 2981 2982 /// Whether any type-checking sanitizers are enabled. If \c false, 2983 /// calls to EmitTypeCheck can be skipped. 2984 bool sanitizePerformTypeCheck() const; 2985 2986 /// Emit a check that \p V is the address of storage of the 2987 /// appropriate size and alignment for an object of type \p Type 2988 /// (or if ArraySize is provided, for an array of that bound). 2989 void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V, 2990 QualType Type, CharUnits Alignment = CharUnits::Zero(), 2991 SanitizerSet SkippedChecks = SanitizerSet(), 2992 llvm::Value *ArraySize = nullptr); 2993 2994 /// Emit a check that \p Base points into an array object, which 2995 /// we can access at index \p Index. \p Accessed should be \c false if we 2996 /// this expression is used as an lvalue, for instance in "&Arr[Idx]". 2997 void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index, 2998 QualType IndexType, bool Accessed); 2999 3000 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 3001 bool isInc, bool isPre); 3002 ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, 3003 bool isInc, bool isPre); 3004 3005 /// Converts Location to a DebugLoc, if debug information is enabled. 3006 llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Location); 3007 3008 /// Get the record field index as represented in debug info. 3009 unsigned getDebugInfoFIndex(const RecordDecl *Rec, unsigned FieldIndex); 3010 3011 3012 //===--------------------------------------------------------------------===// 3013 // Declaration Emission 3014 //===--------------------------------------------------------------------===// 3015 3016 /// EmitDecl - Emit a declaration. 3017 /// 3018 /// This function can be called with a null (unreachable) insert point. 3019 void EmitDecl(const Decl &D); 3020 3021 /// EmitVarDecl - Emit a local variable declaration. 3022 /// 3023 /// This function can be called with a null (unreachable) insert point. 3024 void EmitVarDecl(const VarDecl &D); 3025 3026 void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, 3027 bool capturedByInit); 3028 3029 typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D, 3030 llvm::Value *Address); 3031 3032 /// Determine whether the given initializer is trivial in the sense 3033 /// that it requires no code to be generated. 3034 bool isTrivialInitializer(const Expr *Init); 3035 3036 /// EmitAutoVarDecl - Emit an auto variable declaration. 3037 /// 3038 /// This function can be called with a null (unreachable) insert point. 3039 void EmitAutoVarDecl(const VarDecl &D); 3040 3041 class AutoVarEmission { 3042 friend class CodeGenFunction; 3043 3044 const VarDecl *Variable; 3045 3046 /// The address of the alloca for languages with explicit address space 3047 /// (e.g. OpenCL) or alloca casted to generic pointer for address space 3048 /// agnostic languages (e.g. C++). Invalid if the variable was emitted 3049 /// as a global constant. 3050 Address Addr; 3051 3052 llvm::Value *NRVOFlag; 3053 3054 /// True if the variable is a __block variable that is captured by an 3055 /// escaping block. 3056 bool IsEscapingByRef; 3057 3058 /// True if the variable is of aggregate type and has a constant 3059 /// initializer. 3060 bool IsConstantAggregate; 3061 3062 /// Non-null if we should use lifetime annotations. 3063 llvm::Value *SizeForLifetimeMarkers; 3064 3065 /// Address with original alloca instruction. Invalid if the variable was 3066 /// emitted as a global constant. 3067 Address AllocaAddr; 3068 3069 struct Invalid {}; 3070 AutoVarEmission(Invalid) 3071 : Variable(nullptr), Addr(Address::invalid()), 3072 AllocaAddr(Address::invalid()) {} 3073 3074 AutoVarEmission(const VarDecl &variable) 3075 : Variable(&variable), Addr(Address::invalid()), NRVOFlag(nullptr), 3076 IsEscapingByRef(false), IsConstantAggregate(false), 3077 SizeForLifetimeMarkers(nullptr), AllocaAddr(Address::invalid()) {} 3078 3079 bool wasEmittedAsGlobal() const { return !Addr.isValid(); } 3080 3081 public: 3082 static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); } 3083 3084 bool useLifetimeMarkers() const { 3085 return SizeForLifetimeMarkers != nullptr; 3086 } 3087 llvm::Value *getSizeForLifetimeMarkers() const { 3088 assert(useLifetimeMarkers()); 3089 return SizeForLifetimeMarkers; 3090 } 3091 3092 /// Returns the raw, allocated address, which is not necessarily 3093 /// the address of the object itself. It is casted to default 3094 /// address space for address space agnostic languages. 3095 Address getAllocatedAddress() const { 3096 return Addr; 3097 } 3098 3099 /// Returns the address for the original alloca instruction. 3100 Address getOriginalAllocatedAddress() const { return AllocaAddr; } 3101 3102 /// Returns the address of the object within this declaration. 3103 /// Note that this does not chase the forwarding pointer for 3104 /// __block decls. 3105 Address getObjectAddress(CodeGenFunction &CGF) const { 3106 if (!IsEscapingByRef) return Addr; 3107 3108 return CGF.emitBlockByrefAddress(Addr, Variable, /*forward*/ false); 3109 } 3110 }; 3111 AutoVarEmission EmitAutoVarAlloca(const VarDecl &var); 3112 void EmitAutoVarInit(const AutoVarEmission &emission); 3113 void EmitAutoVarCleanups(const AutoVarEmission &emission); 3114 void emitAutoVarTypeCleanup(const AutoVarEmission &emission, 3115 QualType::DestructionKind dtorKind); 3116 3117 /// Emits the alloca and debug information for the size expressions for each 3118 /// dimension of an array. It registers the association of its (1-dimensional) 3119 /// QualTypes and size expression's debug node, so that CGDebugInfo can 3120 /// reference this node when creating the DISubrange object to describe the 3121 /// array types. 3122 void EmitAndRegisterVariableArrayDimensions(CGDebugInfo *DI, 3123 const VarDecl &D, 3124 bool EmitDebugInfo); 3125 3126 void EmitStaticVarDecl(const VarDecl &D, 3127 llvm::GlobalValue::LinkageTypes Linkage); 3128 3129 class ParamValue { 3130 llvm::Value *Value; 3131 unsigned Alignment; 3132 ParamValue(llvm::Value *V, unsigned A) : Value(V), Alignment(A) {} 3133 public: 3134 static ParamValue forDirect(llvm::Value *value) { 3135 return ParamValue(value, 0); 3136 } 3137 static ParamValue forIndirect(Address addr) { 3138 assert(!addr.getAlignment().isZero()); 3139 return ParamValue(addr.getPointer(), addr.getAlignment().getQuantity()); 3140 } 3141 3142 bool isIndirect() const { return Alignment != 0; } 3143 llvm::Value *getAnyValue() const { return Value; } 3144 3145 llvm::Value *getDirectValue() const { 3146 assert(!isIndirect()); 3147 return Value; 3148 } 3149 3150 Address getIndirectAddress() const { 3151 assert(isIndirect()); 3152 return Address(Value, CharUnits::fromQuantity(Alignment)); 3153 } 3154 }; 3155 3156 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl. 3157 void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo); 3158 3159 /// protectFromPeepholes - Protect a value that we're intending to 3160 /// store to the side, but which will probably be used later, from 3161 /// aggressive peepholing optimizations that might delete it. 3162 /// 3163 /// Pass the result to unprotectFromPeepholes to declare that 3164 /// protection is no longer required. 3165 /// 3166 /// There's no particular reason why this shouldn't apply to 3167 /// l-values, it's just that no existing peepholes work on pointers. 3168 PeepholeProtection protectFromPeepholes(RValue rvalue); 3169 void unprotectFromPeepholes(PeepholeProtection protection); 3170 3171 void emitAlignmentAssumptionCheck(llvm::Value *Ptr, QualType Ty, 3172 SourceLocation Loc, 3173 SourceLocation AssumptionLoc, 3174 llvm::Value *Alignment, 3175 llvm::Value *OffsetValue, 3176 llvm::Value *TheCheck, 3177 llvm::Instruction *Assumption); 3178 3179 void emitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty, 3180 SourceLocation Loc, SourceLocation AssumptionLoc, 3181 llvm::Value *Alignment, 3182 llvm::Value *OffsetValue = nullptr); 3183 3184 void emitAlignmentAssumption(llvm::Value *PtrValue, const Expr *E, 3185 SourceLocation AssumptionLoc, 3186 llvm::Value *Alignment, 3187 llvm::Value *OffsetValue = nullptr); 3188 3189 //===--------------------------------------------------------------------===// 3190 // Statement Emission 3191 //===--------------------------------------------------------------------===// 3192 3193 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info. 3194 void EmitStopPoint(const Stmt *S); 3195 3196 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call 3197 /// this function even if there is no current insertion point. 3198 /// 3199 /// This function may clear the current insertion point; callers should use 3200 /// EnsureInsertPoint if they wish to subsequently generate code without first 3201 /// calling EmitBlock, EmitBranch, or EmitStmt. 3202 void EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs = None); 3203 3204 /// EmitSimpleStmt - Try to emit a "simple" statement which does not 3205 /// necessarily require an insertion point or debug information; typically 3206 /// because the statement amounts to a jump or a container of other 3207 /// statements. 3208 /// 3209 /// \return True if the statement was handled. 3210 bool EmitSimpleStmt(const Stmt *S, ArrayRef<const Attr *> Attrs); 3211 3212 Address EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, 3213 AggValueSlot AVS = AggValueSlot::ignored()); 3214 Address EmitCompoundStmtWithoutScope(const CompoundStmt &S, 3215 bool GetLast = false, 3216 AggValueSlot AVS = 3217 AggValueSlot::ignored()); 3218 3219 /// EmitLabel - Emit the block for the given label. It is legal to call this 3220 /// function even if there is no current insertion point. 3221 void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt. 3222 3223 void EmitLabelStmt(const LabelStmt &S); 3224 void EmitAttributedStmt(const AttributedStmt &S); 3225 void EmitGotoStmt(const GotoStmt &S); 3226 void EmitIndirectGotoStmt(const IndirectGotoStmt &S); 3227 void EmitIfStmt(const IfStmt &S); 3228 3229 void EmitWhileStmt(const WhileStmt &S, 3230 ArrayRef<const Attr *> Attrs = None); 3231 void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None); 3232 void EmitForStmt(const ForStmt &S, 3233 ArrayRef<const Attr *> Attrs = None); 3234 void EmitReturnStmt(const ReturnStmt &S); 3235 void EmitDeclStmt(const DeclStmt &S); 3236 void EmitBreakStmt(const BreakStmt &S); 3237 void EmitContinueStmt(const ContinueStmt &S); 3238 void EmitSwitchStmt(const SwitchStmt &S); 3239 void EmitDefaultStmt(const DefaultStmt &S, ArrayRef<const Attr *> Attrs); 3240 void EmitCaseStmt(const CaseStmt &S, ArrayRef<const Attr *> Attrs); 3241 void EmitCaseStmtRange(const CaseStmt &S, ArrayRef<const Attr *> Attrs); 3242 void EmitAsmStmt(const AsmStmt &S); 3243 3244 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S); 3245 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S); 3246 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S); 3247 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S); 3248 void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S); 3249 3250 void EmitCoroutineBody(const CoroutineBodyStmt &S); 3251 void EmitCoreturnStmt(const CoreturnStmt &S); 3252 RValue EmitCoawaitExpr(const CoawaitExpr &E, 3253 AggValueSlot aggSlot = AggValueSlot::ignored(), 3254 bool ignoreResult = false); 3255 LValue EmitCoawaitLValue(const CoawaitExpr *E); 3256 RValue EmitCoyieldExpr(const CoyieldExpr &E, 3257 AggValueSlot aggSlot = AggValueSlot::ignored(), 3258 bool ignoreResult = false); 3259 LValue EmitCoyieldLValue(const CoyieldExpr *E); 3260 RValue EmitCoroutineIntrinsic(const CallExpr *E, unsigned int IID); 3261 3262 void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); 3263 void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); 3264 3265 void EmitCXXTryStmt(const CXXTryStmt &S); 3266 void EmitSEHTryStmt(const SEHTryStmt &S); 3267 void EmitSEHLeaveStmt(const SEHLeaveStmt &S); 3268 void EnterSEHTryStmt(const SEHTryStmt &S); 3269 void ExitSEHTryStmt(const SEHTryStmt &S); 3270 void VolatilizeTryBlocks(llvm::BasicBlock *BB, 3271 llvm::SmallPtrSet<llvm::BasicBlock *, 10> &V); 3272 3273 void pushSEHCleanup(CleanupKind kind, 3274 llvm::Function *FinallyFunc); 3275 void startOutlinedSEHHelper(CodeGenFunction &ParentCGF, bool IsFilter, 3276 const Stmt *OutlinedStmt); 3277 3278 llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF, 3279 const SEHExceptStmt &Except); 3280 3281 llvm::Function *GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF, 3282 const SEHFinallyStmt &Finally); 3283 3284 void EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF, 3285 llvm::Value *ParentFP, 3286 llvm::Value *EntryEBP); 3287 llvm::Value *EmitSEHExceptionCode(); 3288 llvm::Value *EmitSEHExceptionInfo(); 3289 llvm::Value *EmitSEHAbnormalTermination(); 3290 3291 /// Emit simple code for OpenMP directives in Simd-only mode. 3292 void EmitSimpleOMPExecutableDirective(const OMPExecutableDirective &D); 3293 3294 /// Scan the outlined statement for captures from the parent function. For 3295 /// each capture, mark the capture as escaped and emit a call to 3296 /// llvm.localrecover. Insert the localrecover result into the LocalDeclMap. 3297 void EmitCapturedLocals(CodeGenFunction &ParentCGF, const Stmt *OutlinedStmt, 3298 bool IsFilter); 3299 3300 /// Recovers the address of a local in a parent function. ParentVar is the 3301 /// address of the variable used in the immediate parent function. It can 3302 /// either be an alloca or a call to llvm.localrecover if there are nested 3303 /// outlined functions. ParentFP is the frame pointer of the outermost parent 3304 /// frame. 3305 Address recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF, 3306 Address ParentVar, 3307 llvm::Value *ParentFP); 3308 3309 void EmitCXXForRangeStmt(const CXXForRangeStmt &S, 3310 ArrayRef<const Attr *> Attrs = None); 3311 3312 /// Controls insertion of cancellation exit blocks in worksharing constructs. 3313 class OMPCancelStackRAII { 3314 CodeGenFunction &CGF; 3315 3316 public: 3317 OMPCancelStackRAII(CodeGenFunction &CGF, OpenMPDirectiveKind Kind, 3318 bool HasCancel) 3319 : CGF(CGF) { 3320 CGF.OMPCancelStack.enter(CGF, Kind, HasCancel); 3321 } 3322 ~OMPCancelStackRAII() { CGF.OMPCancelStack.exit(CGF); } 3323 }; 3324 3325 /// Returns calculated size of the specified type. 3326 llvm::Value *getTypeSize(QualType Ty); 3327 LValue InitCapturedStruct(const CapturedStmt &S); 3328 llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K); 3329 llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S); 3330 Address GenerateCapturedStmtArgument(const CapturedStmt &S); 3331 llvm::Function *GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S, 3332 SourceLocation Loc); 3333 void GenerateOpenMPCapturedVars(const CapturedStmt &S, 3334 SmallVectorImpl<llvm::Value *> &CapturedVars); 3335 void emitOMPSimpleStore(LValue LVal, RValue RVal, QualType RValTy, 3336 SourceLocation Loc); 3337 /// Perform element by element copying of arrays with type \a 3338 /// OriginalType from \a SrcAddr to \a DestAddr using copying procedure 3339 /// generated by \a CopyGen. 3340 /// 3341 /// \param DestAddr Address of the destination array. 3342 /// \param SrcAddr Address of the source array. 3343 /// \param OriginalType Type of destination and source arrays. 3344 /// \param CopyGen Copying procedure that copies value of single array element 3345 /// to another single array element. 3346 void EmitOMPAggregateAssign( 3347 Address DestAddr, Address SrcAddr, QualType OriginalType, 3348 const llvm::function_ref<void(Address, Address)> CopyGen); 3349 /// Emit proper copying of data from one variable to another. 3350 /// 3351 /// \param OriginalType Original type of the copied variables. 3352 /// \param DestAddr Destination address. 3353 /// \param SrcAddr Source address. 3354 /// \param DestVD Destination variable used in \a CopyExpr (for arrays, has 3355 /// type of the base array element). 3356 /// \param SrcVD Source variable used in \a CopyExpr (for arrays, has type of 3357 /// the base array element). 3358 /// \param Copy Actual copygin expression for copying data from \a SrcVD to \a 3359 /// DestVD. 3360 void EmitOMPCopy(QualType OriginalType, 3361 Address DestAddr, Address SrcAddr, 3362 const VarDecl *DestVD, const VarDecl *SrcVD, 3363 const Expr *Copy); 3364 /// Emit atomic update code for constructs: \a X = \a X \a BO \a E or 3365 /// \a X = \a E \a BO \a E. 3366 /// 3367 /// \param X Value to be updated. 3368 /// \param E Update value. 3369 /// \param BO Binary operation for update operation. 3370 /// \param IsXLHSInRHSPart true if \a X is LHS in RHS part of the update 3371 /// expression, false otherwise. 3372 /// \param AO Atomic ordering of the generated atomic instructions. 3373 /// \param CommonGen Code generator for complex expressions that cannot be 3374 /// expressed through atomicrmw instruction. 3375 /// \returns <true, OldAtomicValue> if simple 'atomicrmw' instruction was 3376 /// generated, <false, RValue::get(nullptr)> otherwise. 3377 std::pair<bool, RValue> EmitOMPAtomicSimpleUpdateExpr( 3378 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, 3379 llvm::AtomicOrdering AO, SourceLocation Loc, 3380 const llvm::function_ref<RValue(RValue)> CommonGen); 3381 bool EmitOMPFirstprivateClause(const OMPExecutableDirective &D, 3382 OMPPrivateScope &PrivateScope); 3383 void EmitOMPPrivateClause(const OMPExecutableDirective &D, 3384 OMPPrivateScope &PrivateScope); 3385 void EmitOMPUseDevicePtrClause( 3386 const OMPUseDevicePtrClause &C, OMPPrivateScope &PrivateScope, 3387 const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap); 3388 void EmitOMPUseDeviceAddrClause( 3389 const OMPUseDeviceAddrClause &C, OMPPrivateScope &PrivateScope, 3390 const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap); 3391 /// Emit code for copyin clause in \a D directive. The next code is 3392 /// generated at the start of outlined functions for directives: 3393 /// \code 3394 /// threadprivate_var1 = master_threadprivate_var1; 3395 /// operator=(threadprivate_var2, master_threadprivate_var2); 3396 /// ... 3397 /// __kmpc_barrier(&loc, global_tid); 3398 /// \endcode 3399 /// 3400 /// \param D OpenMP directive possibly with 'copyin' clause(s). 3401 /// \returns true if at least one copyin variable is found, false otherwise. 3402 bool EmitOMPCopyinClause(const OMPExecutableDirective &D); 3403 /// Emit initial code for lastprivate variables. If some variable is 3404 /// not also firstprivate, then the default initialization is used. Otherwise 3405 /// initialization of this variable is performed by EmitOMPFirstprivateClause 3406 /// method. 3407 /// 3408 /// \param D Directive that may have 'lastprivate' directives. 3409 /// \param PrivateScope Private scope for capturing lastprivate variables for 3410 /// proper codegen in internal captured statement. 3411 /// 3412 /// \returns true if there is at least one lastprivate variable, false 3413 /// otherwise. 3414 bool EmitOMPLastprivateClauseInit(const OMPExecutableDirective &D, 3415 OMPPrivateScope &PrivateScope); 3416 /// Emit final copying of lastprivate values to original variables at 3417 /// the end of the worksharing or simd directive. 3418 /// 3419 /// \param D Directive that has at least one 'lastprivate' directives. 3420 /// \param IsLastIterCond Boolean condition that must be set to 'i1 true' if 3421 /// it is the last iteration of the loop code in associated directive, or to 3422 /// 'i1 false' otherwise. If this item is nullptr, no final check is required. 3423 void EmitOMPLastprivateClauseFinal(const OMPExecutableDirective &D, 3424 bool NoFinals, 3425 llvm::Value *IsLastIterCond = nullptr); 3426 /// Emit initial code for linear clauses. 3427 void EmitOMPLinearClause(const OMPLoopDirective &D, 3428 CodeGenFunction::OMPPrivateScope &PrivateScope); 3429 /// Emit final code for linear clauses. 3430 /// \param CondGen Optional conditional code for final part of codegen for 3431 /// linear clause. 3432 void EmitOMPLinearClauseFinal( 3433 const OMPLoopDirective &D, 3434 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen); 3435 /// Emit initial code for reduction variables. Creates reduction copies 3436 /// and initializes them with the values according to OpenMP standard. 3437 /// 3438 /// \param D Directive (possibly) with the 'reduction' clause. 3439 /// \param PrivateScope Private scope for capturing reduction variables for 3440 /// proper codegen in internal captured statement. 3441 /// 3442 void EmitOMPReductionClauseInit(const OMPExecutableDirective &D, 3443 OMPPrivateScope &PrivateScope, 3444 bool ForInscan = false); 3445 /// Emit final update of reduction values to original variables at 3446 /// the end of the directive. 3447 /// 3448 /// \param D Directive that has at least one 'reduction' directives. 3449 /// \param ReductionKind The kind of reduction to perform. 3450 void EmitOMPReductionClauseFinal(const OMPExecutableDirective &D, 3451 const OpenMPDirectiveKind ReductionKind); 3452 /// Emit initial code for linear variables. Creates private copies 3453 /// and initializes them with the values according to OpenMP standard. 3454 /// 3455 /// \param D Directive (possibly) with the 'linear' clause. 3456 /// \return true if at least one linear variable is found that should be 3457 /// initialized with the value of the original variable, false otherwise. 3458 bool EmitOMPLinearClauseInit(const OMPLoopDirective &D); 3459 3460 typedef const llvm::function_ref<void(CodeGenFunction & /*CGF*/, 3461 llvm::Function * /*OutlinedFn*/, 3462 const OMPTaskDataTy & /*Data*/)> 3463 TaskGenTy; 3464 void EmitOMPTaskBasedDirective(const OMPExecutableDirective &S, 3465 const OpenMPDirectiveKind CapturedRegion, 3466 const RegionCodeGenTy &BodyGen, 3467 const TaskGenTy &TaskGen, OMPTaskDataTy &Data); 3468 struct OMPTargetDataInfo { 3469 Address BasePointersArray = Address::invalid(); 3470 Address PointersArray = Address::invalid(); 3471 Address SizesArray = Address::invalid(); 3472 Address MappersArray = Address::invalid(); 3473 unsigned NumberOfTargetItems = 0; 3474 explicit OMPTargetDataInfo() = default; 3475 OMPTargetDataInfo(Address BasePointersArray, Address PointersArray, 3476 Address SizesArray, Address MappersArray, 3477 unsigned NumberOfTargetItems) 3478 : BasePointersArray(BasePointersArray), PointersArray(PointersArray), 3479 SizesArray(SizesArray), MappersArray(MappersArray), 3480 NumberOfTargetItems(NumberOfTargetItems) {} 3481 }; 3482 void EmitOMPTargetTaskBasedDirective(const OMPExecutableDirective &S, 3483 const RegionCodeGenTy &BodyGen, 3484 OMPTargetDataInfo &InputInfo); 3485 3486 void EmitOMPMetaDirective(const OMPMetaDirective &S); 3487 void EmitOMPParallelDirective(const OMPParallelDirective &S); 3488 void EmitOMPSimdDirective(const OMPSimdDirective &S); 3489 void EmitOMPTileDirective(const OMPTileDirective &S); 3490 void EmitOMPUnrollDirective(const OMPUnrollDirective &S); 3491 void EmitOMPForDirective(const OMPForDirective &S); 3492 void EmitOMPForSimdDirective(const OMPForSimdDirective &S); 3493 void EmitOMPSectionsDirective(const OMPSectionsDirective &S); 3494 void EmitOMPSectionDirective(const OMPSectionDirective &S); 3495 void EmitOMPSingleDirective(const OMPSingleDirective &S); 3496 void EmitOMPMasterDirective(const OMPMasterDirective &S); 3497 void EmitOMPMaskedDirective(const OMPMaskedDirective &S); 3498 void EmitOMPCriticalDirective(const OMPCriticalDirective &S); 3499 void EmitOMPParallelForDirective(const OMPParallelForDirective &S); 3500 void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S); 3501 void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S); 3502 void EmitOMPParallelMasterDirective(const OMPParallelMasterDirective &S); 3503 void EmitOMPTaskDirective(const OMPTaskDirective &S); 3504 void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S); 3505 void EmitOMPBarrierDirective(const OMPBarrierDirective &S); 3506 void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S); 3507 void EmitOMPTaskgroupDirective(const OMPTaskgroupDirective &S); 3508 void EmitOMPFlushDirective(const OMPFlushDirective &S); 3509 void EmitOMPDepobjDirective(const OMPDepobjDirective &S); 3510 void EmitOMPScanDirective(const OMPScanDirective &S); 3511 void EmitOMPOrderedDirective(const OMPOrderedDirective &S); 3512 void EmitOMPAtomicDirective(const OMPAtomicDirective &S); 3513 void EmitOMPTargetDirective(const OMPTargetDirective &S); 3514 void EmitOMPTargetDataDirective(const OMPTargetDataDirective &S); 3515 void EmitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective &S); 3516 void EmitOMPTargetExitDataDirective(const OMPTargetExitDataDirective &S); 3517 void EmitOMPTargetUpdateDirective(const OMPTargetUpdateDirective &S); 3518 void EmitOMPTargetParallelDirective(const OMPTargetParallelDirective &S); 3519 void 3520 EmitOMPTargetParallelForDirective(const OMPTargetParallelForDirective &S); 3521 void EmitOMPTeamsDirective(const OMPTeamsDirective &S); 3522 void 3523 EmitOMPCancellationPointDirective(const OMPCancellationPointDirective &S); 3524 void EmitOMPCancelDirective(const OMPCancelDirective &S); 3525 void EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S); 3526 void EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S); 3527 void EmitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective &S); 3528 void EmitOMPMasterTaskLoopDirective(const OMPMasterTaskLoopDirective &S); 3529 void 3530 EmitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective &S); 3531 void EmitOMPParallelMasterTaskLoopDirective( 3532 const OMPParallelMasterTaskLoopDirective &S); 3533 void EmitOMPParallelMasterTaskLoopSimdDirective( 3534 const OMPParallelMasterTaskLoopSimdDirective &S); 3535 void EmitOMPDistributeDirective(const OMPDistributeDirective &S); 3536 void EmitOMPDistributeParallelForDirective( 3537 const OMPDistributeParallelForDirective &S); 3538 void EmitOMPDistributeParallelForSimdDirective( 3539 const OMPDistributeParallelForSimdDirective &S); 3540 void EmitOMPDistributeSimdDirective(const OMPDistributeSimdDirective &S); 3541 void EmitOMPTargetParallelForSimdDirective( 3542 const OMPTargetParallelForSimdDirective &S); 3543 void EmitOMPTargetSimdDirective(const OMPTargetSimdDirective &S); 3544 void EmitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective &S); 3545 void 3546 EmitOMPTeamsDistributeSimdDirective(const OMPTeamsDistributeSimdDirective &S); 3547 void EmitOMPTeamsDistributeParallelForSimdDirective( 3548 const OMPTeamsDistributeParallelForSimdDirective &S); 3549 void EmitOMPTeamsDistributeParallelForDirective( 3550 const OMPTeamsDistributeParallelForDirective &S); 3551 void EmitOMPTargetTeamsDirective(const OMPTargetTeamsDirective &S); 3552 void EmitOMPTargetTeamsDistributeDirective( 3553 const OMPTargetTeamsDistributeDirective &S); 3554 void EmitOMPTargetTeamsDistributeParallelForDirective( 3555 const OMPTargetTeamsDistributeParallelForDirective &S); 3556 void EmitOMPTargetTeamsDistributeParallelForSimdDirective( 3557 const OMPTargetTeamsDistributeParallelForSimdDirective &S); 3558 void EmitOMPTargetTeamsDistributeSimdDirective( 3559 const OMPTargetTeamsDistributeSimdDirective &S); 3560 void EmitOMPGenericLoopDirective(const OMPGenericLoopDirective &S); 3561 3562 /// Emit device code for the target directive. 3563 static void EmitOMPTargetDeviceFunction(CodeGenModule &CGM, 3564 StringRef ParentName, 3565 const OMPTargetDirective &S); 3566 static void 3567 EmitOMPTargetParallelDeviceFunction(CodeGenModule &CGM, StringRef ParentName, 3568 const OMPTargetParallelDirective &S); 3569 /// Emit device code for the target parallel for directive. 3570 static void EmitOMPTargetParallelForDeviceFunction( 3571 CodeGenModule &CGM, StringRef ParentName, 3572 const OMPTargetParallelForDirective &S); 3573 /// Emit device code for the target parallel for simd directive. 3574 static void EmitOMPTargetParallelForSimdDeviceFunction( 3575 CodeGenModule &CGM, StringRef ParentName, 3576 const OMPTargetParallelForSimdDirective &S); 3577 /// Emit device code for the target teams directive. 3578 static void 3579 EmitOMPTargetTeamsDeviceFunction(CodeGenModule &CGM, StringRef ParentName, 3580 const OMPTargetTeamsDirective &S); 3581 /// Emit device code for the target teams distribute directive. 3582 static void EmitOMPTargetTeamsDistributeDeviceFunction( 3583 CodeGenModule &CGM, StringRef ParentName, 3584 const OMPTargetTeamsDistributeDirective &S); 3585 /// Emit device code for the target teams distribute simd directive. 3586 static void EmitOMPTargetTeamsDistributeSimdDeviceFunction( 3587 CodeGenModule &CGM, StringRef ParentName, 3588 const OMPTargetTeamsDistributeSimdDirective &S); 3589 /// Emit device code for the target simd directive. 3590 static void EmitOMPTargetSimdDeviceFunction(CodeGenModule &CGM, 3591 StringRef ParentName, 3592 const OMPTargetSimdDirective &S); 3593 /// Emit device code for the target teams distribute parallel for simd 3594 /// directive. 3595 static void EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( 3596 CodeGenModule &CGM, StringRef ParentName, 3597 const OMPTargetTeamsDistributeParallelForSimdDirective &S); 3598 3599 static void EmitOMPTargetTeamsDistributeParallelForDeviceFunction( 3600 CodeGenModule &CGM, StringRef ParentName, 3601 const OMPTargetTeamsDistributeParallelForDirective &S); 3602 3603 /// Emit the Stmt \p S and return its topmost canonical loop, if any. 3604 /// TODO: The \p Depth paramter is not yet implemented and must be 1. In the 3605 /// future it is meant to be the number of loops expected in the loop nests 3606 /// (usually specified by the "collapse" clause) that are collapsed to a 3607 /// single loop by this function. 3608 llvm::CanonicalLoopInfo *EmitOMPCollapsedCanonicalLoopNest(const Stmt *S, 3609 int Depth); 3610 3611 /// Emit an OMPCanonicalLoop using the OpenMPIRBuilder. 3612 void EmitOMPCanonicalLoop(const OMPCanonicalLoop *S); 3613 3614 /// Emit inner loop of the worksharing/simd construct. 3615 /// 3616 /// \param S Directive, for which the inner loop must be emitted. 3617 /// \param RequiresCleanup true, if directive has some associated private 3618 /// variables. 3619 /// \param LoopCond Bollean condition for loop continuation. 3620 /// \param IncExpr Increment expression for loop control variable. 3621 /// \param BodyGen Generator for the inner body of the inner loop. 3622 /// \param PostIncGen Genrator for post-increment code (required for ordered 3623 /// loop directvies). 3624 void EmitOMPInnerLoop( 3625 const OMPExecutableDirective &S, bool RequiresCleanup, 3626 const Expr *LoopCond, const Expr *IncExpr, 3627 const llvm::function_ref<void(CodeGenFunction &)> BodyGen, 3628 const llvm::function_ref<void(CodeGenFunction &)> PostIncGen); 3629 3630 JumpDest getOMPCancelDestination(OpenMPDirectiveKind Kind); 3631 /// Emit initial code for loop counters of loop-based directives. 3632 void EmitOMPPrivateLoopCounters(const OMPLoopDirective &S, 3633 OMPPrivateScope &LoopScope); 3634 3635 /// Helper for the OpenMP loop directives. 3636 void EmitOMPLoopBody(const OMPLoopDirective &D, JumpDest LoopExit); 3637 3638 /// Emit code for the worksharing loop-based directive. 3639 /// \return true, if this construct has any lastprivate clause, false - 3640 /// otherwise. 3641 bool EmitOMPWorksharingLoop(const OMPLoopDirective &S, Expr *EUB, 3642 const CodeGenLoopBoundsTy &CodeGenLoopBounds, 3643 const CodeGenDispatchBoundsTy &CGDispatchBounds); 3644 3645 /// Emit code for the distribute loop-based directive. 3646 void EmitOMPDistributeLoop(const OMPLoopDirective &S, 3647 const CodeGenLoopTy &CodeGenLoop, Expr *IncExpr); 3648 3649 /// Helpers for the OpenMP loop directives. 3650 void EmitOMPSimdInit(const OMPLoopDirective &D); 3651 void EmitOMPSimdFinal( 3652 const OMPLoopDirective &D, 3653 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen); 3654 3655 /// Emits the lvalue for the expression with possibly captured variable. 3656 LValue EmitOMPSharedLValue(const Expr *E); 3657 3658 private: 3659 /// Helpers for blocks. 3660 llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info); 3661 3662 /// struct with the values to be passed to the OpenMP loop-related functions 3663 struct OMPLoopArguments { 3664 /// loop lower bound 3665 Address LB = Address::invalid(); 3666 /// loop upper bound 3667 Address UB = Address::invalid(); 3668 /// loop stride 3669 Address ST = Address::invalid(); 3670 /// isLastIteration argument for runtime functions 3671 Address IL = Address::invalid(); 3672 /// Chunk value generated by sema 3673 llvm::Value *Chunk = nullptr; 3674 /// EnsureUpperBound 3675 Expr *EUB = nullptr; 3676 /// IncrementExpression 3677 Expr *IncExpr = nullptr; 3678 /// Loop initialization 3679 Expr *Init = nullptr; 3680 /// Loop exit condition 3681 Expr *Cond = nullptr; 3682 /// Update of LB after a whole chunk has been executed 3683 Expr *NextLB = nullptr; 3684 /// Update of UB after a whole chunk has been executed 3685 Expr *NextUB = nullptr; 3686 OMPLoopArguments() = default; 3687 OMPLoopArguments(Address LB, Address UB, Address ST, Address IL, 3688 llvm::Value *Chunk = nullptr, Expr *EUB = nullptr, 3689 Expr *IncExpr = nullptr, Expr *Init = nullptr, 3690 Expr *Cond = nullptr, Expr *NextLB = nullptr, 3691 Expr *NextUB = nullptr) 3692 : LB(LB), UB(UB), ST(ST), IL(IL), Chunk(Chunk), EUB(EUB), 3693 IncExpr(IncExpr), Init(Init), Cond(Cond), NextLB(NextLB), 3694 NextUB(NextUB) {} 3695 }; 3696 void EmitOMPOuterLoop(bool DynamicOrOrdered, bool IsMonotonic, 3697 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, 3698 const OMPLoopArguments &LoopArgs, 3699 const CodeGenLoopTy &CodeGenLoop, 3700 const CodeGenOrderedTy &CodeGenOrdered); 3701 void EmitOMPForOuterLoop(const OpenMPScheduleTy &ScheduleKind, 3702 bool IsMonotonic, const OMPLoopDirective &S, 3703 OMPPrivateScope &LoopScope, bool Ordered, 3704 const OMPLoopArguments &LoopArgs, 3705 const CodeGenDispatchBoundsTy &CGDispatchBounds); 3706 void EmitOMPDistributeOuterLoop(OpenMPDistScheduleClauseKind ScheduleKind, 3707 const OMPLoopDirective &S, 3708 OMPPrivateScope &LoopScope, 3709 const OMPLoopArguments &LoopArgs, 3710 const CodeGenLoopTy &CodeGenLoopContent); 3711 /// Emit code for sections directive. 3712 void EmitSections(const OMPExecutableDirective &S); 3713 3714 public: 3715 3716 //===--------------------------------------------------------------------===// 3717 // LValue Expression Emission 3718 //===--------------------------------------------------------------------===// 3719 3720 /// Create a check that a scalar RValue is non-null. 3721 llvm::Value *EmitNonNullRValueCheck(RValue RV, QualType T); 3722 3723 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type. 3724 RValue GetUndefRValue(QualType Ty); 3725 3726 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E 3727 /// and issue an ErrorUnsupported style diagnostic (using the 3728 /// provided Name). 3729 RValue EmitUnsupportedRValue(const Expr *E, 3730 const char *Name); 3731 3732 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue 3733 /// an ErrorUnsupported style diagnostic (using the provided Name). 3734 LValue EmitUnsupportedLValue(const Expr *E, 3735 const char *Name); 3736 3737 /// EmitLValue - Emit code to compute a designator that specifies the location 3738 /// of the expression. 3739 /// 3740 /// This can return one of two things: a simple address or a bitfield 3741 /// reference. In either case, the LLVM Value* in the LValue structure is 3742 /// guaranteed to be an LLVM pointer type. 3743 /// 3744 /// If this returns a bitfield reference, nothing about the pointee type of 3745 /// the LLVM value is known: For example, it may not be a pointer to an 3746 /// integer. 3747 /// 3748 /// If this returns a normal address, and if the lvalue's C type is fixed 3749 /// size, this method guarantees that the returned pointer type will point to 3750 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a 3751 /// variable length type, this is not possible. 3752 /// 3753 LValue EmitLValue(const Expr *E); 3754 3755 /// Same as EmitLValue but additionally we generate checking code to 3756 /// guard against undefined behavior. This is only suitable when we know 3757 /// that the address will be used to access the object. 3758 LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK); 3759 3760 RValue convertTempToRValue(Address addr, QualType type, 3761 SourceLocation Loc); 3762 3763 void EmitAtomicInit(Expr *E, LValue lvalue); 3764 3765 bool LValueIsSuitableForInlineAtomic(LValue Src); 3766 3767 RValue EmitAtomicLoad(LValue LV, SourceLocation SL, 3768 AggValueSlot Slot = AggValueSlot::ignored()); 3769 3770 RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc, 3771 llvm::AtomicOrdering AO, bool IsVolatile = false, 3772 AggValueSlot slot = AggValueSlot::ignored()); 3773 3774 void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit); 3775 3776 void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO, 3777 bool IsVolatile, bool isInit); 3778 3779 std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange( 3780 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc, 3781 llvm::AtomicOrdering Success = 3782 llvm::AtomicOrdering::SequentiallyConsistent, 3783 llvm::AtomicOrdering Failure = 3784 llvm::AtomicOrdering::SequentiallyConsistent, 3785 bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored()); 3786 3787 void EmitAtomicUpdate(LValue LVal, llvm::AtomicOrdering AO, 3788 const llvm::function_ref<RValue(RValue)> &UpdateOp, 3789 bool IsVolatile); 3790 3791 /// EmitToMemory - Change a scalar value from its value 3792 /// representation to its in-memory representation. 3793 llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty); 3794 3795 /// EmitFromMemory - Change a scalar value from its memory 3796 /// representation to its value representation. 3797 llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty); 3798 3799 /// Check if the scalar \p Value is within the valid range for the given 3800 /// type \p Ty. 3801 /// 3802 /// Returns true if a check is needed (even if the range is unknown). 3803 bool EmitScalarRangeCheck(llvm::Value *Value, QualType Ty, 3804 SourceLocation Loc); 3805 3806 /// EmitLoadOfScalar - Load a scalar value from an address, taking 3807 /// care to appropriately convert from the memory representation to 3808 /// the LLVM value representation. 3809 llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, 3810 SourceLocation Loc, 3811 AlignmentSource Source = AlignmentSource::Type, 3812 bool isNontemporal = false) { 3813 return EmitLoadOfScalar(Addr, Volatile, Ty, Loc, LValueBaseInfo(Source), 3814 CGM.getTBAAAccessInfo(Ty), isNontemporal); 3815 } 3816 3817 llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, 3818 SourceLocation Loc, LValueBaseInfo BaseInfo, 3819 TBAAAccessInfo TBAAInfo, 3820 bool isNontemporal = false); 3821 3822 /// EmitLoadOfScalar - Load a scalar value from an address, taking 3823 /// care to appropriately convert from the memory representation to 3824 /// the LLVM value representation. The l-value must be a simple 3825 /// l-value. 3826 llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc); 3827 3828 /// EmitStoreOfScalar - Store a scalar value to an address, taking 3829 /// care to appropriately convert from the memory representation to 3830 /// the LLVM value representation. 3831 void EmitStoreOfScalar(llvm::Value *Value, Address Addr, 3832 bool Volatile, QualType Ty, 3833 AlignmentSource Source = AlignmentSource::Type, 3834 bool isInit = false, bool isNontemporal = false) { 3835 EmitStoreOfScalar(Value, Addr, Volatile, Ty, LValueBaseInfo(Source), 3836 CGM.getTBAAAccessInfo(Ty), isInit, isNontemporal); 3837 } 3838 3839 void EmitStoreOfScalar(llvm::Value *Value, Address Addr, 3840 bool Volatile, QualType Ty, 3841 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo, 3842 bool isInit = false, bool isNontemporal = false); 3843 3844 /// EmitStoreOfScalar - Store a scalar value to an address, taking 3845 /// care to appropriately convert from the memory representation to 3846 /// the LLVM value representation. The l-value must be a simple 3847 /// l-value. The isInit flag indicates whether this is an initialization. 3848 /// If so, atomic qualifiers are ignored and the store is always non-atomic. 3849 void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false); 3850 3851 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, 3852 /// this method emits the address of the lvalue, then loads the result as an 3853 /// rvalue, returning the rvalue. 3854 RValue EmitLoadOfLValue(LValue V, SourceLocation Loc); 3855 RValue EmitLoadOfExtVectorElementLValue(LValue V); 3856 RValue EmitLoadOfBitfieldLValue(LValue LV, SourceLocation Loc); 3857 RValue EmitLoadOfGlobalRegLValue(LValue LV); 3858 3859 /// EmitStoreThroughLValue - Store the specified rvalue into the specified 3860 /// lvalue, where both are guaranteed to the have the same type, and that type 3861 /// is 'Ty'. 3862 void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false); 3863 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst); 3864 void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst); 3865 3866 /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints 3867 /// as EmitStoreThroughLValue. 3868 /// 3869 /// \param Result [out] - If non-null, this will be set to a Value* for the 3870 /// bit-field contents after the store, appropriate for use as the result of 3871 /// an assignment to the bit-field. 3872 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, 3873 llvm::Value **Result=nullptr); 3874 3875 /// Emit an l-value for an assignment (simple or compound) of complex type. 3876 LValue EmitComplexAssignmentLValue(const BinaryOperator *E); 3877 LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E); 3878 LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E, 3879 llvm::Value *&Result); 3880 3881 // Note: only available for agg return types 3882 LValue EmitBinaryOperatorLValue(const BinaryOperator *E); 3883 LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E); 3884 // Note: only available for agg return types 3885 LValue EmitCallExprLValue(const CallExpr *E); 3886 // Note: only available for agg return types 3887 LValue EmitVAArgExprLValue(const VAArgExpr *E); 3888 LValue EmitDeclRefLValue(const DeclRefExpr *E); 3889 LValue EmitStringLiteralLValue(const StringLiteral *E); 3890 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E); 3891 LValue EmitPredefinedLValue(const PredefinedExpr *E); 3892 LValue EmitUnaryOpLValue(const UnaryOperator *E); 3893 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E, 3894 bool Accessed = false); 3895 LValue EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E); 3896 LValue EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, 3897 bool IsLowerBound = true); 3898 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); 3899 LValue EmitMemberExpr(const MemberExpr *E); 3900 LValue EmitObjCIsaExpr(const ObjCIsaExpr *E); 3901 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E); 3902 LValue EmitInitListLValue(const InitListExpr *E); 3903 LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E); 3904 LValue EmitCastLValue(const CastExpr *E); 3905 LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); 3906 LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e); 3907 3908 Address EmitExtVectorElementLValue(LValue V); 3909 3910 RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc); 3911 3912 Address EmitArrayToPointerDecay(const Expr *Array, 3913 LValueBaseInfo *BaseInfo = nullptr, 3914 TBAAAccessInfo *TBAAInfo = nullptr); 3915 3916 class ConstantEmission { 3917 llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference; 3918 ConstantEmission(llvm::Constant *C, bool isReference) 3919 : ValueAndIsReference(C, isReference) {} 3920 public: 3921 ConstantEmission() {} 3922 static ConstantEmission forReference(llvm::Constant *C) { 3923 return ConstantEmission(C, true); 3924 } 3925 static ConstantEmission forValue(llvm::Constant *C) { 3926 return ConstantEmission(C, false); 3927 } 3928 3929 explicit operator bool() const { 3930 return ValueAndIsReference.getOpaqueValue() != nullptr; 3931 } 3932 3933 bool isReference() const { return ValueAndIsReference.getInt(); } 3934 LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const { 3935 assert(isReference()); 3936 return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(), 3937 refExpr->getType()); 3938 } 3939 3940 llvm::Constant *getValue() const { 3941 assert(!isReference()); 3942 return ValueAndIsReference.getPointer(); 3943 } 3944 }; 3945 3946 ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr); 3947 ConstantEmission tryEmitAsConstant(const MemberExpr *ME); 3948 llvm::Value *emitScalarConstant(const ConstantEmission &Constant, Expr *E); 3949 3950 RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e, 3951 AggValueSlot slot = AggValueSlot::ignored()); 3952 LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e); 3953 3954 llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface, 3955 const ObjCIvarDecl *Ivar); 3956 LValue EmitLValueForField(LValue Base, const FieldDecl* Field); 3957 LValue EmitLValueForLambdaField(const FieldDecl *Field); 3958 3959 /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that 3960 /// if the Field is a reference, this will return the address of the reference 3961 /// and not the address of the value stored in the reference. 3962 LValue EmitLValueForFieldInitialization(LValue Base, 3963 const FieldDecl* Field); 3964 3965 LValue EmitLValueForIvar(QualType ObjectTy, 3966 llvm::Value* Base, const ObjCIvarDecl *Ivar, 3967 unsigned CVRQualifiers); 3968 3969 LValue EmitCXXConstructLValue(const CXXConstructExpr *E); 3970 LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E); 3971 LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E); 3972 LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E); 3973 3974 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E); 3975 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); 3976 LValue EmitStmtExprLValue(const StmtExpr *E); 3977 LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E); 3978 LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E); 3979 void EmitDeclRefExprDbgValue(const DeclRefExpr *E, const APValue &Init); 3980 3981 //===--------------------------------------------------------------------===// 3982 // Scalar Expression Emission 3983 //===--------------------------------------------------------------------===// 3984 3985 /// EmitCall - Generate a call of the given function, expecting the given 3986 /// result type, and using the given argument list which specifies both the 3987 /// LLVM arguments and the types they were derived from. 3988 RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, 3989 ReturnValueSlot ReturnValue, const CallArgList &Args, 3990 llvm::CallBase **callOrInvoke, bool IsMustTail, 3991 SourceLocation Loc); 3992 RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, 3993 ReturnValueSlot ReturnValue, const CallArgList &Args, 3994 llvm::CallBase **callOrInvoke = nullptr, 3995 bool IsMustTail = false) { 3996 return EmitCall(CallInfo, Callee, ReturnValue, Args, callOrInvoke, 3997 IsMustTail, SourceLocation()); 3998 } 3999 RValue EmitCall(QualType FnType, const CGCallee &Callee, const CallExpr *E, 4000 ReturnValueSlot ReturnValue, llvm::Value *Chain = nullptr); 4001 RValue EmitCallExpr(const CallExpr *E, 4002 ReturnValueSlot ReturnValue = ReturnValueSlot()); 4003 RValue EmitSimpleCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue); 4004 CGCallee EmitCallee(const Expr *E); 4005 4006 void checkTargetFeatures(const CallExpr *E, const FunctionDecl *TargetDecl); 4007 void checkTargetFeatures(SourceLocation Loc, const FunctionDecl *TargetDecl); 4008 4009 llvm::CallInst *EmitRuntimeCall(llvm::FunctionCallee callee, 4010 const Twine &name = ""); 4011 llvm::CallInst *EmitRuntimeCall(llvm::FunctionCallee callee, 4012 ArrayRef<llvm::Value *> args, 4013 const Twine &name = ""); 4014 llvm::CallInst *EmitNounwindRuntimeCall(llvm::FunctionCallee callee, 4015 const Twine &name = ""); 4016 llvm::CallInst *EmitNounwindRuntimeCall(llvm::FunctionCallee callee, 4017 ArrayRef<llvm::Value *> args, 4018 const Twine &name = ""); 4019 4020 SmallVector<llvm::OperandBundleDef, 1> 4021 getBundlesForFunclet(llvm::Value *Callee); 4022 4023 llvm::CallBase *EmitCallOrInvoke(llvm::FunctionCallee Callee, 4024 ArrayRef<llvm::Value *> Args, 4025 const Twine &Name = ""); 4026 llvm::CallBase *EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, 4027 ArrayRef<llvm::Value *> args, 4028 const Twine &name = ""); 4029 llvm::CallBase *EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, 4030 const Twine &name = ""); 4031 void EmitNoreturnRuntimeCallOrInvoke(llvm::FunctionCallee callee, 4032 ArrayRef<llvm::Value *> args); 4033 4034 CGCallee BuildAppleKextVirtualCall(const CXXMethodDecl *MD, 4035 NestedNameSpecifier *Qual, 4036 llvm::Type *Ty); 4037 4038 CGCallee BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD, 4039 CXXDtorType Type, 4040 const CXXRecordDecl *RD); 4041 4042 // Return the copy constructor name with the prefix "__copy_constructor_" 4043 // removed. 4044 static std::string getNonTrivialCopyConstructorStr(QualType QT, 4045 CharUnits Alignment, 4046 bool IsVolatile, 4047 ASTContext &Ctx); 4048 4049 // Return the destructor name with the prefix "__destructor_" removed. 4050 static std::string getNonTrivialDestructorStr(QualType QT, 4051 CharUnits Alignment, 4052 bool IsVolatile, 4053 ASTContext &Ctx); 4054 4055 // These functions emit calls to the special functions of non-trivial C 4056 // structs. 4057 void defaultInitNonTrivialCStructVar(LValue Dst); 4058 void callCStructDefaultConstructor(LValue Dst); 4059 void callCStructDestructor(LValue Dst); 4060 void callCStructCopyConstructor(LValue Dst, LValue Src); 4061 void callCStructMoveConstructor(LValue Dst, LValue Src); 4062 void callCStructCopyAssignmentOperator(LValue Dst, LValue Src); 4063 void callCStructMoveAssignmentOperator(LValue Dst, LValue Src); 4064 4065 RValue 4066 EmitCXXMemberOrOperatorCall(const CXXMethodDecl *Method, 4067 const CGCallee &Callee, 4068 ReturnValueSlot ReturnValue, llvm::Value *This, 4069 llvm::Value *ImplicitParam, 4070 QualType ImplicitParamTy, const CallExpr *E, 4071 CallArgList *RtlArgs); 4072 RValue EmitCXXDestructorCall(GlobalDecl Dtor, const CGCallee &Callee, 4073 llvm::Value *This, QualType ThisTy, 4074 llvm::Value *ImplicitParam, 4075 QualType ImplicitParamTy, const CallExpr *E); 4076 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E, 4077 ReturnValueSlot ReturnValue); 4078 RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE, 4079 const CXXMethodDecl *MD, 4080 ReturnValueSlot ReturnValue, 4081 bool HasQualifier, 4082 NestedNameSpecifier *Qualifier, 4083 bool IsArrow, const Expr *Base); 4084 // Compute the object pointer. 4085 Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base, 4086 llvm::Value *memberPtr, 4087 const MemberPointerType *memberPtrType, 4088 LValueBaseInfo *BaseInfo = nullptr, 4089 TBAAAccessInfo *TBAAInfo = nullptr); 4090 RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, 4091 ReturnValueSlot ReturnValue); 4092 4093 RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, 4094 const CXXMethodDecl *MD, 4095 ReturnValueSlot ReturnValue); 4096 RValue EmitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E); 4097 4098 RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, 4099 ReturnValueSlot ReturnValue); 4100 4101 RValue EmitNVPTXDevicePrintfCallExpr(const CallExpr *E); 4102 RValue EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E); 4103 RValue EmitOpenMPDevicePrintfCallExpr(const CallExpr *E); 4104 4105 RValue EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, 4106 const CallExpr *E, ReturnValueSlot ReturnValue); 4107 4108 RValue emitRotate(const CallExpr *E, bool IsRotateRight); 4109 4110 /// Emit IR for __builtin_os_log_format. 4111 RValue emitBuiltinOSLogFormat(const CallExpr &E); 4112 4113 /// Emit IR for __builtin_is_aligned. 4114 RValue EmitBuiltinIsAligned(const CallExpr *E); 4115 /// Emit IR for __builtin_align_up/__builtin_align_down. 4116 RValue EmitBuiltinAlignTo(const CallExpr *E, bool AlignUp); 4117 4118 llvm::Function *generateBuiltinOSLogHelperFunction( 4119 const analyze_os_log::OSLogBufferLayout &Layout, 4120 CharUnits BufferAlignment); 4121 4122 RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue); 4123 4124 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call 4125 /// is unhandled by the current target. 4126 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E, 4127 ReturnValueSlot ReturnValue); 4128 4129 llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty, 4130 const llvm::CmpInst::Predicate Fp, 4131 const llvm::CmpInst::Predicate Ip, 4132 const llvm::Twine &Name = ""); 4133 llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E, 4134 ReturnValueSlot ReturnValue, 4135 llvm::Triple::ArchType Arch); 4136 llvm::Value *EmitARMMVEBuiltinExpr(unsigned BuiltinID, const CallExpr *E, 4137 ReturnValueSlot ReturnValue, 4138 llvm::Triple::ArchType Arch); 4139 llvm::Value *EmitARMCDEBuiltinExpr(unsigned BuiltinID, const CallExpr *E, 4140 ReturnValueSlot ReturnValue, 4141 llvm::Triple::ArchType Arch); 4142 llvm::Value *EmitCMSEClearRecord(llvm::Value *V, llvm::IntegerType *ITy, 4143 QualType RTy); 4144 llvm::Value *EmitCMSEClearRecord(llvm::Value *V, llvm::ArrayType *ATy, 4145 QualType RTy); 4146 4147 llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID, 4148 unsigned LLVMIntrinsic, 4149 unsigned AltLLVMIntrinsic, 4150 const char *NameHint, 4151 unsigned Modifier, 4152 const CallExpr *E, 4153 SmallVectorImpl<llvm::Value *> &Ops, 4154 Address PtrOp0, Address PtrOp1, 4155 llvm::Triple::ArchType Arch); 4156 4157 llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID, 4158 unsigned Modifier, llvm::Type *ArgTy, 4159 const CallExpr *E); 4160 llvm::Value *EmitNeonCall(llvm::Function *F, 4161 SmallVectorImpl<llvm::Value*> &O, 4162 const char *name, 4163 unsigned shift = 0, bool rightshift = false); 4164 llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx, 4165 const llvm::ElementCount &Count); 4166 llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx); 4167 llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty, 4168 bool negateForRightShift); 4169 llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt, 4170 llvm::Type *Ty, bool usgn, const char *name); 4171 llvm::Value *vectorWrapScalar16(llvm::Value *Op); 4172 /// SVEBuiltinMemEltTy - Returns the memory element type for this memory 4173 /// access builtin. Only required if it can't be inferred from the base 4174 /// pointer operand. 4175 llvm::Type *SVEBuiltinMemEltTy(const SVETypeFlags &TypeFlags); 4176 4177 SmallVector<llvm::Type *, 2> 4178 getSVEOverloadTypes(const SVETypeFlags &TypeFlags, llvm::Type *ReturnType, 4179 ArrayRef<llvm::Value *> Ops); 4180 llvm::Type *getEltType(const SVETypeFlags &TypeFlags); 4181 llvm::ScalableVectorType *getSVEType(const SVETypeFlags &TypeFlags); 4182 llvm::ScalableVectorType *getSVEPredType(const SVETypeFlags &TypeFlags); 4183 llvm::Value *EmitSVEAllTruePred(const SVETypeFlags &TypeFlags); 4184 llvm::Value *EmitSVEDupX(llvm::Value *Scalar); 4185 llvm::Value *EmitSVEDupX(llvm::Value *Scalar, llvm::Type *Ty); 4186 llvm::Value *EmitSVEReinterpret(llvm::Value *Val, llvm::Type *Ty); 4187 llvm::Value *EmitSVEPMull(const SVETypeFlags &TypeFlags, 4188 llvm::SmallVectorImpl<llvm::Value *> &Ops, 4189 unsigned BuiltinID); 4190 llvm::Value *EmitSVEMovl(const SVETypeFlags &TypeFlags, 4191 llvm::ArrayRef<llvm::Value *> Ops, 4192 unsigned BuiltinID); 4193 llvm::Value *EmitSVEPredicateCast(llvm::Value *Pred, 4194 llvm::ScalableVectorType *VTy); 4195 llvm::Value *EmitSVEGatherLoad(const SVETypeFlags &TypeFlags, 4196 llvm::SmallVectorImpl<llvm::Value *> &Ops, 4197 unsigned IntID); 4198 llvm::Value *EmitSVEScatterStore(const SVETypeFlags &TypeFlags, 4199 llvm::SmallVectorImpl<llvm::Value *> &Ops, 4200 unsigned IntID); 4201 llvm::Value *EmitSVEMaskedLoad(const CallExpr *, llvm::Type *ReturnTy, 4202 SmallVectorImpl<llvm::Value *> &Ops, 4203 unsigned BuiltinID, bool IsZExtReturn); 4204 llvm::Value *EmitSVEMaskedStore(const CallExpr *, 4205 SmallVectorImpl<llvm::Value *> &Ops, 4206 unsigned BuiltinID); 4207 llvm::Value *EmitSVEPrefetchLoad(const SVETypeFlags &TypeFlags, 4208 SmallVectorImpl<llvm::Value *> &Ops, 4209 unsigned BuiltinID); 4210 llvm::Value *EmitSVEGatherPrefetch(const SVETypeFlags &TypeFlags, 4211 SmallVectorImpl<llvm::Value *> &Ops, 4212 unsigned IntID); 4213 llvm::Value *EmitSVEStructLoad(const SVETypeFlags &TypeFlags, 4214 SmallVectorImpl<llvm::Value *> &Ops, 4215 unsigned IntID); 4216 llvm::Value *EmitSVEStructStore(const SVETypeFlags &TypeFlags, 4217 SmallVectorImpl<llvm::Value *> &Ops, 4218 unsigned IntID); 4219 llvm::Value *EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 4220 4221 llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E, 4222 llvm::Triple::ArchType Arch); 4223 llvm::Value *EmitBPFBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 4224 4225 llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops); 4226 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); 4227 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 4228 llvm::Value *EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 4229 llvm::Value *EmitSystemZBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 4230 llvm::Value *EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 4231 llvm::Value *EmitWebAssemblyBuiltinExpr(unsigned BuiltinID, 4232 const CallExpr *E); 4233 llvm::Value *EmitHexagonBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 4234 llvm::Value *EmitRISCVBuiltinExpr(unsigned BuiltinID, const CallExpr *E, 4235 ReturnValueSlot ReturnValue); 4236 bool ProcessOrderScopeAMDGCN(llvm::Value *Order, llvm::Value *Scope, 4237 llvm::AtomicOrdering &AO, 4238 llvm::SyncScope::ID &SSID); 4239 4240 enum class MSVCIntrin; 4241 llvm::Value *EmitMSVCBuiltinExpr(MSVCIntrin BuiltinID, const CallExpr *E); 4242 4243 llvm::Value *EmitBuiltinAvailable(const VersionTuple &Version); 4244 4245 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E); 4246 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); 4247 llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E); 4248 llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E); 4249 llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E); 4250 llvm::Value *EmitObjCCollectionLiteral(const Expr *E, 4251 const ObjCMethodDecl *MethodWithObjects); 4252 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E); 4253 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E, 4254 ReturnValueSlot Return = ReturnValueSlot()); 4255 4256 /// Retrieves the default cleanup kind for an ARC cleanup. 4257 /// Except under -fobjc-arc-eh, ARC cleanups are normal-only. 4258 CleanupKind getARCCleanupKind() { 4259 return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions 4260 ? NormalAndEHCleanup : NormalCleanup; 4261 } 4262 4263 // ARC primitives. 4264 void EmitARCInitWeak(Address addr, llvm::Value *value); 4265 void EmitARCDestroyWeak(Address addr); 4266 llvm::Value *EmitARCLoadWeak(Address addr); 4267 llvm::Value *EmitARCLoadWeakRetained(Address addr); 4268 llvm::Value *EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored); 4269 void emitARCCopyAssignWeak(QualType Ty, Address DstAddr, Address SrcAddr); 4270 void emitARCMoveAssignWeak(QualType Ty, Address DstAddr, Address SrcAddr); 4271 void EmitARCCopyWeak(Address dst, Address src); 4272 void EmitARCMoveWeak(Address dst, Address src); 4273 llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value); 4274 llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value); 4275 llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value, 4276 bool resultIgnored); 4277 llvm::Value *EmitARCStoreStrongCall(Address addr, llvm::Value *value, 4278 bool resultIgnored); 4279 llvm::Value *EmitARCRetain(QualType type, llvm::Value *value); 4280 llvm::Value *EmitARCRetainNonBlock(llvm::Value *value); 4281 llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory); 4282 void EmitARCDestroyStrong(Address addr, ARCPreciseLifetime_t precise); 4283 void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise); 4284 llvm::Value *EmitARCAutorelease(llvm::Value *value); 4285 llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value); 4286 llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value); 4287 llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value); 4288 llvm::Value *EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value); 4289 4290 llvm::Value *EmitObjCAutorelease(llvm::Value *value, llvm::Type *returnType); 4291 llvm::Value *EmitObjCRetainNonBlock(llvm::Value *value, 4292 llvm::Type *returnType); 4293 void EmitObjCRelease(llvm::Value *value, ARCPreciseLifetime_t precise); 4294 4295 std::pair<LValue,llvm::Value*> 4296 EmitARCStoreAutoreleasing(const BinaryOperator *e); 4297 std::pair<LValue,llvm::Value*> 4298 EmitARCStoreStrong(const BinaryOperator *e, bool ignored); 4299 std::pair<LValue,llvm::Value*> 4300 EmitARCStoreUnsafeUnretained(const BinaryOperator *e, bool ignored); 4301 4302 llvm::Value *EmitObjCAlloc(llvm::Value *value, 4303 llvm::Type *returnType); 4304 llvm::Value *EmitObjCAllocWithZone(llvm::Value *value, 4305 llvm::Type *returnType); 4306 llvm::Value *EmitObjCAllocInit(llvm::Value *value, llvm::Type *resultType); 4307 4308 llvm::Value *EmitObjCThrowOperand(const Expr *expr); 4309 llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr); 4310 llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr); 4311 4312 llvm::Value *EmitARCExtendBlockObject(const Expr *expr); 4313 llvm::Value *EmitARCReclaimReturnedObject(const Expr *e, 4314 bool allowUnsafeClaim); 4315 llvm::Value *EmitARCRetainScalarExpr(const Expr *expr); 4316 llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr); 4317 llvm::Value *EmitARCUnsafeUnretainedScalarExpr(const Expr *expr); 4318 4319 void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values); 4320 4321 void EmitARCNoopIntrinsicUse(ArrayRef<llvm::Value *> values); 4322 4323 static Destroyer destroyARCStrongImprecise; 4324 static Destroyer destroyARCStrongPrecise; 4325 static Destroyer destroyARCWeak; 4326 static Destroyer emitARCIntrinsicUse; 4327 static Destroyer destroyNonTrivialCStruct; 4328 4329 void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr); 4330 llvm::Value *EmitObjCAutoreleasePoolPush(); 4331 llvm::Value *EmitObjCMRRAutoreleasePoolPush(); 4332 void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr); 4333 void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr); 4334 4335 /// Emits a reference binding to the passed in expression. 4336 RValue EmitReferenceBindingToExpr(const Expr *E); 4337 4338 //===--------------------------------------------------------------------===// 4339 // Expression Emission 4340 //===--------------------------------------------------------------------===// 4341 4342 // Expressions are broken into three classes: scalar, complex, aggregate. 4343 4344 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM 4345 /// scalar type, returning the result. 4346 llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false); 4347 4348 /// Emit a conversion from the specified type to the specified destination 4349 /// type, both of which are LLVM scalar types. 4350 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy, 4351 QualType DstTy, SourceLocation Loc); 4352 4353 /// Emit a conversion from the specified complex type to the specified 4354 /// destination type, where the destination type is an LLVM scalar type. 4355 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, 4356 QualType DstTy, 4357 SourceLocation Loc); 4358 4359 /// EmitAggExpr - Emit the computation of the specified expression 4360 /// of aggregate type. The result is computed into the given slot, 4361 /// which may be null to indicate that the value is not needed. 4362 void EmitAggExpr(const Expr *E, AggValueSlot AS); 4363 4364 /// EmitAggExprToLValue - Emit the computation of the specified expression of 4365 /// aggregate type into a temporary LValue. 4366 LValue EmitAggExprToLValue(const Expr *E); 4367 4368 /// Build all the stores needed to initialize an aggregate at Dest with the 4369 /// value Val. 4370 void EmitAggregateStore(llvm::Value *Val, Address Dest, bool DestIsVolatile); 4371 4372 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object, 4373 /// make sure it survives garbage collection until this point. 4374 void EmitExtendGCLifetime(llvm::Value *object); 4375 4376 /// EmitComplexExpr - Emit the computation of the specified expression of 4377 /// complex type, returning the result. 4378 ComplexPairTy EmitComplexExpr(const Expr *E, 4379 bool IgnoreReal = false, 4380 bool IgnoreImag = false); 4381 4382 /// EmitComplexExprIntoLValue - Emit the given expression of complex 4383 /// type and place its result into the specified l-value. 4384 void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit); 4385 4386 /// EmitStoreOfComplex - Store a complex number into the specified l-value. 4387 void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit); 4388 4389 /// EmitLoadOfComplex - Load a complex number from the specified l-value. 4390 ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc); 4391 4392 Address emitAddrOfRealComponent(Address complex, QualType complexType); 4393 Address emitAddrOfImagComponent(Address complex, QualType complexType); 4394 4395 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the 4396 /// global variable that has already been created for it. If the initializer 4397 /// has a different type than GV does, this may free GV and return a different 4398 /// one. Otherwise it just returns GV. 4399 llvm::GlobalVariable * 4400 AddInitializerToStaticVarDecl(const VarDecl &D, 4401 llvm::GlobalVariable *GV); 4402 4403 // Emit an @llvm.invariant.start call for the given memory region. 4404 void EmitInvariantStart(llvm::Constant *Addr, CharUnits Size); 4405 4406 /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++ 4407 /// variable with global storage. 4408 void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr, 4409 bool PerformInit); 4410 4411 llvm::Function *createAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor, 4412 llvm::Constant *Addr); 4413 4414 llvm::Function *createTLSAtExitStub(const VarDecl &VD, 4415 llvm::FunctionCallee Dtor, 4416 llvm::Constant *Addr, 4417 llvm::FunctionCallee &AtExit); 4418 4419 /// Call atexit() with a function that passes the given argument to 4420 /// the given function. 4421 void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::FunctionCallee fn, 4422 llvm::Constant *addr); 4423 4424 /// Call atexit() with function dtorStub. 4425 void registerGlobalDtorWithAtExit(llvm::Constant *dtorStub); 4426 4427 /// Call unatexit() with function dtorStub. 4428 llvm::Value *unregisterGlobalDtorWithUnAtExit(llvm::Constant *dtorStub); 4429 4430 /// Emit code in this function to perform a guarded variable 4431 /// initialization. Guarded initializations are used when it's not 4432 /// possible to prove that an initialization will be done exactly 4433 /// once, e.g. with a static local variable or a static data member 4434 /// of a class template. 4435 void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr, 4436 bool PerformInit); 4437 4438 enum class GuardKind { VariableGuard, TlsGuard }; 4439 4440 /// Emit a branch to select whether or not to perform guarded initialization. 4441 void EmitCXXGuardedInitBranch(llvm::Value *NeedsInit, 4442 llvm::BasicBlock *InitBlock, 4443 llvm::BasicBlock *NoInitBlock, 4444 GuardKind Kind, const VarDecl *D); 4445 4446 /// GenerateCXXGlobalInitFunc - Generates code for initializing global 4447 /// variables. 4448 void 4449 GenerateCXXGlobalInitFunc(llvm::Function *Fn, 4450 ArrayRef<llvm::Function *> CXXThreadLocals, 4451 ConstantAddress Guard = ConstantAddress::invalid()); 4452 4453 /// GenerateCXXGlobalCleanUpFunc - Generates code for cleaning up global 4454 /// variables. 4455 void GenerateCXXGlobalCleanUpFunc( 4456 llvm::Function *Fn, 4457 ArrayRef<std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH, 4458 llvm::Constant *>> 4459 DtorsOrStermFinalizers); 4460 4461 void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, 4462 const VarDecl *D, 4463 llvm::GlobalVariable *Addr, 4464 bool PerformInit); 4465 4466 void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest); 4467 4468 void EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, const Expr *Exp); 4469 4470 void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true); 4471 4472 RValue EmitAtomicExpr(AtomicExpr *E); 4473 4474 //===--------------------------------------------------------------------===// 4475 // Annotations Emission 4476 //===--------------------------------------------------------------------===// 4477 4478 /// Emit an annotation call (intrinsic). 4479 llvm::Value *EmitAnnotationCall(llvm::Function *AnnotationFn, 4480 llvm::Value *AnnotatedVal, 4481 StringRef AnnotationStr, 4482 SourceLocation Location, 4483 const AnnotateAttr *Attr); 4484 4485 /// Emit local annotations for the local variable V, declared by D. 4486 void EmitVarAnnotations(const VarDecl *D, llvm::Value *V); 4487 4488 /// Emit field annotations for the given field & value. Returns the 4489 /// annotation result. 4490 Address EmitFieldAnnotations(const FieldDecl *D, Address V); 4491 4492 //===--------------------------------------------------------------------===// 4493 // Internal Helpers 4494 //===--------------------------------------------------------------------===// 4495 4496 /// ContainsLabel - Return true if the statement contains a label in it. If 4497 /// this statement is not executed normally, it not containing a label means 4498 /// that we can just remove the code. 4499 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false); 4500 4501 /// containsBreak - Return true if the statement contains a break out of it. 4502 /// If the statement (recursively) contains a switch or loop with a break 4503 /// inside of it, this is fine. 4504 static bool containsBreak(const Stmt *S); 4505 4506 /// Determine if the given statement might introduce a declaration into the 4507 /// current scope, by being a (possibly-labelled) DeclStmt. 4508 static bool mightAddDeclToScope(const Stmt *S); 4509 4510 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold 4511 /// to a constant, or if it does but contains a label, return false. If it 4512 /// constant folds return true and set the boolean result in Result. 4513 bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result, 4514 bool AllowLabels = false); 4515 4516 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold 4517 /// to a constant, or if it does but contains a label, return false. If it 4518 /// constant folds return true and set the folded value. 4519 bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result, 4520 bool AllowLabels = false); 4521 4522 /// isInstrumentedCondition - Determine whether the given condition is an 4523 /// instrumentable condition (i.e. no "&&" or "||"). 4524 static bool isInstrumentedCondition(const Expr *C); 4525 4526 /// EmitBranchToCounterBlock - Emit a conditional branch to a new block that 4527 /// increments a profile counter based on the semantics of the given logical 4528 /// operator opcode. This is used to instrument branch condition coverage 4529 /// for logical operators. 4530 void EmitBranchToCounterBlock(const Expr *Cond, BinaryOperator::Opcode LOp, 4531 llvm::BasicBlock *TrueBlock, 4532 llvm::BasicBlock *FalseBlock, 4533 uint64_t TrueCount = 0, 4534 Stmt::Likelihood LH = Stmt::LH_None, 4535 const Expr *CntrIdx = nullptr); 4536 4537 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an 4538 /// if statement) to the specified blocks. Based on the condition, this might 4539 /// try to simplify the codegen of the conditional based on the branch. 4540 /// TrueCount should be the number of times we expect the condition to 4541 /// evaluate to true based on PGO data. 4542 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, 4543 llvm::BasicBlock *FalseBlock, uint64_t TrueCount, 4544 Stmt::Likelihood LH = Stmt::LH_None); 4545 4546 /// Given an assignment `*LHS = RHS`, emit a test that checks if \p RHS is 4547 /// nonnull, if \p LHS is marked _Nonnull. 4548 void EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, SourceLocation Loc); 4549 4550 /// An enumeration which makes it easier to specify whether or not an 4551 /// operation is a subtraction. 4552 enum { NotSubtraction = false, IsSubtraction = true }; 4553 4554 /// Same as IRBuilder::CreateInBoundsGEP, but additionally emits a check to 4555 /// detect undefined behavior when the pointer overflow sanitizer is enabled. 4556 /// \p SignedIndices indicates whether any of the GEP indices are signed. 4557 /// \p IsSubtraction indicates whether the expression used to form the GEP 4558 /// is a subtraction. 4559 llvm::Value *EmitCheckedInBoundsGEP(llvm::Value *Ptr, 4560 ArrayRef<llvm::Value *> IdxList, 4561 bool SignedIndices, 4562 bool IsSubtraction, 4563 SourceLocation Loc, 4564 const Twine &Name = ""); 4565 4566 /// Specifies which type of sanitizer check to apply when handling a 4567 /// particular builtin. 4568 enum BuiltinCheckKind { 4569 BCK_CTZPassedZero, 4570 BCK_CLZPassedZero, 4571 }; 4572 4573 /// Emits an argument for a call to a builtin. If the builtin sanitizer is 4574 /// enabled, a runtime check specified by \p Kind is also emitted. 4575 llvm::Value *EmitCheckedArgForBuiltin(const Expr *E, BuiltinCheckKind Kind); 4576 4577 /// Emit a description of a type in a format suitable for passing to 4578 /// a runtime sanitizer handler. 4579 llvm::Constant *EmitCheckTypeDescriptor(QualType T); 4580 4581 /// Convert a value into a format suitable for passing to a runtime 4582 /// sanitizer handler. 4583 llvm::Value *EmitCheckValue(llvm::Value *V); 4584 4585 /// Emit a description of a source location in a format suitable for 4586 /// passing to a runtime sanitizer handler. 4587 llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc); 4588 4589 /// Create a basic block that will either trap or call a handler function in 4590 /// the UBSan runtime with the provided arguments, and create a conditional 4591 /// branch to it. 4592 void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked, 4593 SanitizerHandler Check, ArrayRef<llvm::Constant *> StaticArgs, 4594 ArrayRef<llvm::Value *> DynamicArgs); 4595 4596 /// Emit a slow path cross-DSO CFI check which calls __cfi_slowpath 4597 /// if Cond if false. 4598 void EmitCfiSlowPathCheck(SanitizerMask Kind, llvm::Value *Cond, 4599 llvm::ConstantInt *TypeId, llvm::Value *Ptr, 4600 ArrayRef<llvm::Constant *> StaticArgs); 4601 4602 /// Emit a reached-unreachable diagnostic if \p Loc is valid and runtime 4603 /// checking is enabled. Otherwise, just emit an unreachable instruction. 4604 void EmitUnreachable(SourceLocation Loc); 4605 4606 /// Create a basic block that will call the trap intrinsic, and emit a 4607 /// conditional branch to it, for the -ftrapv checks. 4608 void EmitTrapCheck(llvm::Value *Checked, SanitizerHandler CheckHandlerID); 4609 4610 /// Emit a call to trap or debugtrap and attach function attribute 4611 /// "trap-func-name" if specified. 4612 llvm::CallInst *EmitTrapCall(llvm::Intrinsic::ID IntrID); 4613 4614 /// Emit a stub for the cross-DSO CFI check function. 4615 void EmitCfiCheckStub(); 4616 4617 /// Emit a cross-DSO CFI failure handling function. 4618 void EmitCfiCheckFail(); 4619 4620 /// Create a check for a function parameter that may potentially be 4621 /// declared as non-null. 4622 void EmitNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc, 4623 AbstractCallee AC, unsigned ParmNum); 4624 4625 /// EmitCallArg - Emit a single call argument. 4626 void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType); 4627 4628 /// EmitDelegateCallArg - We are performing a delegate call; that 4629 /// is, the current function is delegating to another one. Produce 4630 /// a r-value suitable for passing the given parameter. 4631 void EmitDelegateCallArg(CallArgList &args, const VarDecl *param, 4632 SourceLocation loc); 4633 4634 /// SetFPAccuracy - Set the minimum required accuracy of the given floating 4635 /// point operation, expressed as the maximum relative error in ulp. 4636 void SetFPAccuracy(llvm::Value *Val, float Accuracy); 4637 4638 /// Set the codegen fast-math flags. 4639 void SetFastMathFlags(FPOptions FPFeatures); 4640 4641 private: 4642 llvm::MDNode *getRangeForLoadFromType(QualType Ty); 4643 void EmitReturnOfRValue(RValue RV, QualType Ty); 4644 4645 void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New); 4646 4647 llvm::SmallVector<std::pair<llvm::WeakTrackingVH, llvm::Value *>, 4> 4648 DeferredReplacements; 4649 4650 /// Set the address of a local variable. 4651 void setAddrOfLocalVar(const VarDecl *VD, Address Addr) { 4652 assert(!LocalDeclMap.count(VD) && "Decl already exists in LocalDeclMap!"); 4653 LocalDeclMap.insert({VD, Addr}); 4654 } 4655 4656 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty 4657 /// from function arguments into \arg Dst. See ABIArgInfo::Expand. 4658 /// 4659 /// \param AI - The first function argument of the expansion. 4660 void ExpandTypeFromArgs(QualType Ty, LValue Dst, 4661 llvm::Function::arg_iterator &AI); 4662 4663 /// ExpandTypeToArgs - Expand an CallArg \arg Arg, with the LLVM type for \arg 4664 /// Ty, into individual arguments on the provided vector \arg IRCallArgs, 4665 /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand. 4666 void ExpandTypeToArgs(QualType Ty, CallArg Arg, llvm::FunctionType *IRFuncTy, 4667 SmallVectorImpl<llvm::Value *> &IRCallArgs, 4668 unsigned &IRCallArgPos); 4669 4670 llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info, 4671 const Expr *InputExpr, std::string &ConstraintStr); 4672 4673 llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info, 4674 LValue InputValue, QualType InputType, 4675 std::string &ConstraintStr, 4676 SourceLocation Loc); 4677 4678 /// Attempts to statically evaluate the object size of E. If that 4679 /// fails, emits code to figure the size of E out for us. This is 4680 /// pass_object_size aware. 4681 /// 4682 /// If EmittedExpr is non-null, this will use that instead of re-emitting E. 4683 llvm::Value *evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type, 4684 llvm::IntegerType *ResType, 4685 llvm::Value *EmittedE, 4686 bool IsDynamic); 4687 4688 /// Emits the size of E, as required by __builtin_object_size. This 4689 /// function is aware of pass_object_size parameters, and will act accordingly 4690 /// if E is a parameter with the pass_object_size attribute. 4691 llvm::Value *emitBuiltinObjectSize(const Expr *E, unsigned Type, 4692 llvm::IntegerType *ResType, 4693 llvm::Value *EmittedE, 4694 bool IsDynamic); 4695 4696 void emitZeroOrPatternForAutoVarInit(QualType type, const VarDecl &D, 4697 Address Loc); 4698 4699 public: 4700 enum class EvaluationOrder { 4701 ///! No language constraints on evaluation order. 4702 Default, 4703 ///! Language semantics require left-to-right evaluation. 4704 ForceLeftToRight, 4705 ///! Language semantics require right-to-left evaluation. 4706 ForceRightToLeft 4707 }; 4708 4709 // Wrapper for function prototype sources. Wraps either a FunctionProtoType or 4710 // an ObjCMethodDecl. 4711 struct PrototypeWrapper { 4712 llvm::PointerUnion<const FunctionProtoType *, const ObjCMethodDecl *> P; 4713 4714 PrototypeWrapper(const FunctionProtoType *FT) : P(FT) {} 4715 PrototypeWrapper(const ObjCMethodDecl *MD) : P(MD) {} 4716 }; 4717 4718 void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, 4719 llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange, 4720 AbstractCallee AC = AbstractCallee(), 4721 unsigned ParamsToSkip = 0, 4722 EvaluationOrder Order = EvaluationOrder::Default); 4723 4724 /// EmitPointerWithAlignment - Given an expression with a pointer type, 4725 /// emit the value and compute our best estimate of the alignment of the 4726 /// pointee. 4727 /// 4728 /// \param BaseInfo - If non-null, this will be initialized with 4729 /// information about the source of the alignment and the may-alias 4730 /// attribute. Note that this function will conservatively fall back on 4731 /// the type when it doesn't recognize the expression and may-alias will 4732 /// be set to false. 4733 /// 4734 /// One reasonable way to use this information is when there's a language 4735 /// guarantee that the pointer must be aligned to some stricter value, and 4736 /// we're simply trying to ensure that sufficiently obvious uses of under- 4737 /// aligned objects don't get miscompiled; for example, a placement new 4738 /// into the address of a local variable. In such a case, it's quite 4739 /// reasonable to just ignore the returned alignment when it isn't from an 4740 /// explicit source. 4741 Address EmitPointerWithAlignment(const Expr *Addr, 4742 LValueBaseInfo *BaseInfo = nullptr, 4743 TBAAAccessInfo *TBAAInfo = nullptr); 4744 4745 /// If \p E references a parameter with pass_object_size info or a constant 4746 /// array size modifier, emit the object size divided by the size of \p EltTy. 4747 /// Otherwise return null. 4748 llvm::Value *LoadPassedObjectSize(const Expr *E, QualType EltTy); 4749 4750 void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK); 4751 4752 struct MultiVersionResolverOption { 4753 llvm::Function *Function; 4754 struct Conds { 4755 StringRef Architecture; 4756 llvm::SmallVector<StringRef, 8> Features; 4757 4758 Conds(StringRef Arch, ArrayRef<StringRef> Feats) 4759 : Architecture(Arch), Features(Feats.begin(), Feats.end()) {} 4760 } Conditions; 4761 4762 MultiVersionResolverOption(llvm::Function *F, StringRef Arch, 4763 ArrayRef<StringRef> Feats) 4764 : Function(F), Conditions(Arch, Feats) {} 4765 }; 4766 4767 // Emits the body of a multiversion function's resolver. Assumes that the 4768 // options are already sorted in the proper order, with the 'default' option 4769 // last (if it exists). 4770 void EmitMultiVersionResolver(llvm::Function *Resolver, 4771 ArrayRef<MultiVersionResolverOption> Options); 4772 4773 private: 4774 QualType getVarArgType(const Expr *Arg); 4775 4776 void EmitDeclMetadata(); 4777 4778 BlockByrefHelpers *buildByrefHelpers(llvm::StructType &byrefType, 4779 const AutoVarEmission &emission); 4780 4781 void AddObjCARCExceptionMetadata(llvm::Instruction *Inst); 4782 4783 llvm::Value *GetValueForARMHint(unsigned BuiltinID); 4784 llvm::Value *EmitX86CpuIs(const CallExpr *E); 4785 llvm::Value *EmitX86CpuIs(StringRef CPUStr); 4786 llvm::Value *EmitX86CpuSupports(const CallExpr *E); 4787 llvm::Value *EmitX86CpuSupports(ArrayRef<StringRef> FeatureStrs); 4788 llvm::Value *EmitX86CpuSupports(uint64_t Mask); 4789 llvm::Value *EmitX86CpuInit(); 4790 llvm::Value *FormResolverCondition(const MultiVersionResolverOption &RO); 4791 }; 4792 4793 /// TargetFeatures - This class is used to check whether the builtin function 4794 /// has the required tagert specific features. It is able to support the 4795 /// combination of ','(and), '|'(or), and '()'. By default, the priority of 4796 /// ',' is higher than that of '|' . 4797 /// E.g: 4798 /// A,B|C means the builtin function requires both A and B, or C. 4799 /// If we want the builtin function requires both A and B, or both A and C, 4800 /// there are two ways: A,B|A,C or A,(B|C). 4801 /// The FeaturesList should not contain spaces, and brackets must appear in 4802 /// pairs. 4803 class TargetFeatures { 4804 struct FeatureListStatus { 4805 bool HasFeatures; 4806 StringRef CurFeaturesList; 4807 }; 4808 4809 const llvm::StringMap<bool> &CallerFeatureMap; 4810 4811 FeatureListStatus getAndFeatures(StringRef FeatureList) { 4812 int InParentheses = 0; 4813 bool HasFeatures = true; 4814 size_t SubexpressionStart = 0; 4815 for (size_t i = 0, e = FeatureList.size(); i < e; ++i) { 4816 char CurrentToken = FeatureList[i]; 4817 switch (CurrentToken) { 4818 default: 4819 break; 4820 case '(': 4821 if (InParentheses == 0) 4822 SubexpressionStart = i + 1; 4823 ++InParentheses; 4824 break; 4825 case ')': 4826 --InParentheses; 4827 assert(InParentheses >= 0 && "Parentheses are not in pair"); 4828 LLVM_FALLTHROUGH; 4829 case '|': 4830 case ',': 4831 if (InParentheses == 0) { 4832 if (HasFeatures && i != SubexpressionStart) { 4833 StringRef F = FeatureList.slice(SubexpressionStart, i); 4834 HasFeatures = CurrentToken == ')' ? hasRequiredFeatures(F) 4835 : CallerFeatureMap.lookup(F); 4836 } 4837 SubexpressionStart = i + 1; 4838 if (CurrentToken == '|') { 4839 return {HasFeatures, FeatureList.substr(SubexpressionStart)}; 4840 } 4841 } 4842 break; 4843 } 4844 } 4845 assert(InParentheses == 0 && "Parentheses are not in pair"); 4846 if (HasFeatures && SubexpressionStart != FeatureList.size()) 4847 HasFeatures = 4848 CallerFeatureMap.lookup(FeatureList.substr(SubexpressionStart)); 4849 return {HasFeatures, StringRef()}; 4850 } 4851 4852 public: 4853 bool hasRequiredFeatures(StringRef FeatureList) { 4854 FeatureListStatus FS = {false, FeatureList}; 4855 while (!FS.HasFeatures && !FS.CurFeaturesList.empty()) 4856 FS = getAndFeatures(FS.CurFeaturesList); 4857 return FS.HasFeatures; 4858 } 4859 4860 TargetFeatures(const llvm::StringMap<bool> &CallerFeatureMap) 4861 : CallerFeatureMap(CallerFeatureMap) {} 4862 }; 4863 4864 inline DominatingLLVMValue::saved_type 4865 DominatingLLVMValue::save(CodeGenFunction &CGF, llvm::Value *value) { 4866 if (!needsSaving(value)) return saved_type(value, false); 4867 4868 // Otherwise, we need an alloca. 4869 auto align = CharUnits::fromQuantity( 4870 CGF.CGM.getDataLayout().getPrefTypeAlignment(value->getType())); 4871 Address alloca = 4872 CGF.CreateTempAlloca(value->getType(), align, "cond-cleanup.save"); 4873 CGF.Builder.CreateStore(value, alloca); 4874 4875 return saved_type(alloca.getPointer(), true); 4876 } 4877 4878 inline llvm::Value *DominatingLLVMValue::restore(CodeGenFunction &CGF, 4879 saved_type value) { 4880 // If the value says it wasn't saved, trust that it's still dominating. 4881 if (!value.getInt()) return value.getPointer(); 4882 4883 // Otherwise, it should be an alloca instruction, as set up in save(). 4884 auto alloca = cast<llvm::AllocaInst>(value.getPointer()); 4885 return CGF.Builder.CreateAlignedLoad(alloca->getAllocatedType(), alloca, 4886 alloca->getAlign()); 4887 } 4888 4889 } // end namespace CodeGen 4890 4891 // Map the LangOption for floating point exception behavior into 4892 // the corresponding enum in the IR. 4893 llvm::fp::ExceptionBehavior 4894 ToConstrainedExceptMD(LangOptions::FPExceptionModeKind Kind); 4895 } // end namespace clang 4896 4897 #endif 4898