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