1 //===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the internal per-function state used for llvm translation.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
17 #include "CGBuilder.h"
18 #include "CGDebugInfo.h"
19 #include "CGLoopInfo.h"
21 #include "CodeGenModule.h"
22 #include "CodeGenPGO.h"
23 #include "EHScopeStack.h"
24 #include "clang/AST/CharUnits.h"
25 #include "clang/AST/ExprCXX.h"
26 #include "clang/AST/ExprObjC.h"
27 #include "clang/AST/ExprOpenMP.h"
28 #include "clang/AST/Type.h"
29 #include "clang/Basic/ABI.h"
30 #include "clang/Basic/CapturedStmt.h"
31 #include "clang/Basic/OpenMPKinds.h"
32 #include "clang/Basic/TargetInfo.h"
33 #include "clang/Frontend/CodeGenOptions.h"
34 #include "llvm/ADT/ArrayRef.h"
35 #include "llvm/ADT/DenseMap.h"
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/IR/ValueHandle.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Transforms/Utils/SanitizerStats.h"
55 class CXXDestructorDecl;
56 class CXXForRangeStmt;
60 class EnumConstantDecl;
62 class FunctionProtoType;
64 class ObjCContainerDecl;
65 class ObjCInterfaceDecl;
68 class ObjCImplementationDecl;
69 class ObjCPropertyImplDecl;
72 class ObjCForCollectionStmt;
74 class ObjCAtThrowStmt;
75 class ObjCAtSynchronizedStmt;
76 class ObjCAutoreleasePoolStmt;
84 class BlockByrefHelpers;
87 class BlockFieldFlags;
88 class RegionCodeGenTy;
89 class TargetCodeGenInfo;
92 /// The kind of evaluation to perform on values of a particular
93 /// type. Basically, is the code in CGExprScalar, CGExprComplex, or
96 /// TODO: should vectors maybe be split out into their own thing?
97 enum TypeEvaluationKind {
103 /// CodeGenFunction - This class organizes the per-function state that is used
104 /// while generating LLVM code.
105 class CodeGenFunction : public CodeGenTypeCache {
106 CodeGenFunction(const CodeGenFunction &) = delete;
107 void operator=(const CodeGenFunction &) = delete;
109 friend class CGCXXABI;
111 /// A jump destination is an abstract label, branching to which may
112 /// require a jump out through normal cleanups.
114 JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
115 JumpDest(llvm::BasicBlock *Block,
116 EHScopeStack::stable_iterator Depth,
118 : Block(Block), ScopeDepth(Depth), Index(Index) {}
120 bool isValid() const { return Block != nullptr; }
121 llvm::BasicBlock *getBlock() const { return Block; }
122 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
123 unsigned getDestIndex() const { return Index; }
125 // This should be used cautiously.
126 void setScopeDepth(EHScopeStack::stable_iterator depth) {
131 llvm::BasicBlock *Block;
132 EHScopeStack::stable_iterator ScopeDepth;
136 CodeGenModule &CGM; // Per-module state.
137 const TargetInfo &Target;
139 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
140 LoopInfoStack LoopStack;
143 /// \brief CGBuilder insert helper. This function is called after an
144 /// instruction is created using Builder.
145 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
146 llvm::BasicBlock *BB,
147 llvm::BasicBlock::iterator InsertPt) const;
149 /// CurFuncDecl - Holds the Decl for the current outermost
150 /// non-closure context.
151 const Decl *CurFuncDecl;
152 /// CurCodeDecl - This is the inner-most code context, which includes blocks.
153 const Decl *CurCodeDecl;
154 const CGFunctionInfo *CurFnInfo;
156 llvm::Function *CurFn;
158 /// CurGD - The GlobalDecl for the current function being compiled.
161 /// PrologueCleanupDepth - The cleanup depth enclosing all the
162 /// cleanups associated with the parameters.
163 EHScopeStack::stable_iterator PrologueCleanupDepth;
165 /// ReturnBlock - Unified return block.
166 JumpDest ReturnBlock;
168 /// ReturnValue - The temporary alloca to hold the return
169 /// value. This is invalid iff the function has no return value.
172 /// AllocaInsertPoint - This is an instruction in the entry block before which
173 /// we prefer to insert allocas.
174 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
176 /// \brief API for captured statement code generation.
177 class CGCapturedStmtInfo {
179 explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
180 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
181 explicit CGCapturedStmtInfo(const CapturedStmt &S,
182 CapturedRegionKind K = CR_Default)
183 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
185 RecordDecl::field_iterator Field =
186 S.getCapturedRecordDecl()->field_begin();
187 for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
189 I != E; ++I, ++Field) {
190 if (I->capturesThis())
191 CXXThisFieldDecl = *Field;
192 else if (I->capturesVariable())
193 CaptureFields[I->getCapturedVar()] = *Field;
194 else if (I->capturesVariableByCopy())
195 CaptureFields[I->getCapturedVar()] = *Field;
199 virtual ~CGCapturedStmtInfo();
201 CapturedRegionKind getKind() const { return Kind; }
203 virtual void setContextValue(llvm::Value *V) { ThisValue = V; }
204 // \brief Retrieve the value of the context parameter.
205 virtual llvm::Value *getContextValue() const { return ThisValue; }
207 /// \brief Lookup the captured field decl for a variable.
208 virtual const FieldDecl *lookup(const VarDecl *VD) const {
209 return CaptureFields.lookup(VD);
212 bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; }
213 virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
215 static bool classof(const CGCapturedStmtInfo *) {
219 /// \brief Emit the captured statement body.
220 virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) {
221 CGF.incrementProfileCounter(S);
225 /// \brief Get the name of the capture helper.
226 virtual StringRef getHelperName() const { return "__captured_stmt"; }
229 /// \brief The kind of captured statement being generated.
230 CapturedRegionKind Kind;
232 /// \brief Keep the map between VarDecl and FieldDecl.
233 llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
235 /// \brief The base address of the captured record, passed in as the first
236 /// argument of the parallel region function.
237 llvm::Value *ThisValue;
239 /// \brief Captured 'this' type.
240 FieldDecl *CXXThisFieldDecl;
242 CGCapturedStmtInfo *CapturedStmtInfo;
244 /// \brief RAII for correct setting/restoring of CapturedStmtInfo.
245 class CGCapturedStmtRAII {
247 CodeGenFunction &CGF;
248 CGCapturedStmtInfo *PrevCapturedStmtInfo;
250 CGCapturedStmtRAII(CodeGenFunction &CGF,
251 CGCapturedStmtInfo *NewCapturedStmtInfo)
252 : CGF(CGF), PrevCapturedStmtInfo(CGF.CapturedStmtInfo) {
253 CGF.CapturedStmtInfo = NewCapturedStmtInfo;
255 ~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; }
258 /// \brief Sanitizers enabled for this function.
259 SanitizerSet SanOpts;
261 /// \brief True if CodeGen currently emits code implementing sanitizer checks.
262 bool IsSanitizerScope;
264 /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope.
265 class SanitizerScope {
266 CodeGenFunction *CGF;
268 SanitizerScope(CodeGenFunction *CGF);
272 /// In C++, whether we are code generating a thunk. This controls whether we
273 /// should emit cleanups.
276 /// In ARC, whether we should autorelease the return value.
277 bool AutoreleaseResult;
279 /// Whether we processed a Microsoft-style asm block during CodeGen. These can
280 /// potentially set the return value.
283 const FunctionDecl *CurSEHParent = nullptr;
285 /// True if the current function is an outlined SEH helper. This can be a
286 /// finally block or filter expression.
287 bool IsOutlinedSEHHelper;
289 const CodeGen::CGBlockInfo *BlockInfo;
290 llvm::Value *BlockPointer;
292 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
293 FieldDecl *LambdaThisCaptureField;
295 /// \brief A mapping from NRVO variables to the flags used to indicate
296 /// when the NRVO has been applied to this variable.
297 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
299 EHScopeStack EHStack;
300 llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
301 llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack;
303 llvm::Instruction *CurrentFuncletPad = nullptr;
305 class CallLifetimeEnd final : public EHScopeStack::Cleanup {
310 CallLifetimeEnd(Address addr, llvm::Value *size)
311 : Addr(addr.getPointer()), Size(size) {}
313 void Emit(CodeGenFunction &CGF, Flags flags) override {
314 CGF.EmitLifetimeEnd(Size, Addr);
318 /// Header for data within LifetimeExtendedCleanupStack.
319 struct LifetimeExtendedCleanupHeader {
320 /// The size of the following cleanup object.
322 /// The kind of cleanup to push: a value from the CleanupKind enumeration.
325 size_t getSize() const { return Size; }
326 CleanupKind getKind() const { return Kind; }
329 /// i32s containing the indexes of the cleanup destinations.
330 llvm::AllocaInst *NormalCleanupDest;
332 unsigned NextCleanupDestIndex;
334 /// FirstBlockInfo - The head of a singly-linked-list of block layouts.
335 CGBlockInfo *FirstBlockInfo;
337 /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
338 llvm::BasicBlock *EHResumeBlock;
340 /// The exception slot. All landing pads write the current exception pointer
341 /// into this alloca.
342 llvm::Value *ExceptionSlot;
344 /// The selector slot. Under the MandatoryCleanup model, all landing pads
345 /// write the current selector value into this alloca.
346 llvm::AllocaInst *EHSelectorSlot;
348 /// A stack of exception code slots. Entering an __except block pushes a slot
349 /// on the stack and leaving pops one. The __exception_code() intrinsic loads
350 /// a value from the top of the stack.
351 SmallVector<Address, 1> SEHCodeSlotStack;
353 /// Value returned by __exception_info intrinsic.
354 llvm::Value *SEHInfo = nullptr;
356 /// Emits a landing pad for the current EH stack.
357 llvm::BasicBlock *EmitLandingPad();
359 llvm::BasicBlock *getInvokeDestImpl();
362 typename DominatingValue<T>::saved_type saveValueInCond(T value) {
363 return DominatingValue<T>::save(*this, value);
367 /// ObjCEHValueStack - Stack of Objective-C exception values, used for
369 SmallVector<llvm::Value*, 8> ObjCEHValueStack;
371 /// A class controlling the emission of a finally block.
373 /// Where the catchall's edge through the cleanup should go.
374 JumpDest RethrowDest;
376 /// A function to call to enter the catch.
377 llvm::Constant *BeginCatchFn;
379 /// An i1 variable indicating whether or not the @finally is
380 /// running for an exception.
381 llvm::AllocaInst *ForEHVar;
383 /// An i8* variable into which the exception pointer to rethrow
385 llvm::AllocaInst *SavedExnVar;
388 void enter(CodeGenFunction &CGF, const Stmt *Finally,
389 llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
390 llvm::Constant *rethrowFn);
391 void exit(CodeGenFunction &CGF);
394 /// Returns true inside SEH __try blocks.
395 bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); }
397 /// Returns true while emitting a cleanuppad.
398 bool isCleanupPadScope() const {
399 return CurrentFuncletPad && isa<llvm::CleanupPadInst>(CurrentFuncletPad);
402 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
403 /// current full-expression. Safe against the possibility that
404 /// we're currently inside a conditionally-evaluated expression.
405 template <class T, class... As>
406 void pushFullExprCleanup(CleanupKind kind, As... A) {
407 // If we're not in a conditional branch, or if none of the
408 // arguments requires saving, then use the unconditional cleanup.
409 if (!isInConditionalBranch())
410 return EHStack.pushCleanup<T>(kind, A...);
412 // Stash values in a tuple so we can guarantee the order of saves.
413 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
414 SavedTuple Saved{saveValueInCond(A)...};
416 typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
417 EHStack.pushCleanupTuple<CleanupType>(kind, Saved);
418 initFullExprCleanup();
421 /// \brief Queue a cleanup to be pushed after finishing the current
423 template <class T, class... As>
424 void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) {
425 assert(!isInConditionalBranch() && "can't defer conditional cleanup");
427 LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind };
429 size_t OldSize = LifetimeExtendedCleanupStack.size();
430 LifetimeExtendedCleanupStack.resize(
431 LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size);
433 static_assert(sizeof(Header) % llvm::AlignOf<T>::Alignment == 0,
434 "Cleanup will be allocated on misaligned address");
435 char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
436 new (Buffer) LifetimeExtendedCleanupHeader(Header);
437 new (Buffer + sizeof(Header)) T(A...);
440 /// Set up the last cleaup that was pushed as a conditional
441 /// full-expression cleanup.
442 void initFullExprCleanup();
444 /// PushDestructorCleanup - Push a cleanup to call the
445 /// complete-object destructor of an object of the given type at the
446 /// given address. Does nothing if T is not a C++ class type with a
447 /// non-trivial destructor.
448 void PushDestructorCleanup(QualType T, Address Addr);
450 /// PushDestructorCleanup - Push a cleanup to call the
451 /// complete-object variant of the given destructor on the object at
452 /// the given address.
453 void PushDestructorCleanup(const CXXDestructorDecl *Dtor, Address Addr);
455 /// PopCleanupBlock - Will pop the cleanup entry on the stack and
456 /// process all branch fixups.
457 void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
459 /// DeactivateCleanupBlock - Deactivates the given cleanup block.
460 /// The block cannot be reactivated. Pops it if it's the top of the
463 /// \param DominatingIP - An instruction which is known to
464 /// dominate the current IP (if set) and which lies along
465 /// all paths of execution between the current IP and the
466 /// the point at which the cleanup comes into scope.
467 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
468 llvm::Instruction *DominatingIP);
470 /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
471 /// Cannot be used to resurrect a deactivated cleanup.
473 /// \param DominatingIP - An instruction which is known to
474 /// dominate the current IP (if set) and which lies along
475 /// all paths of execution between the current IP and the
476 /// the point at which the cleanup comes into scope.
477 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
478 llvm::Instruction *DominatingIP);
480 /// \brief Enters a new scope for capturing cleanups, all of which
481 /// will be executed once the scope is exited.
482 class RunCleanupsScope {
483 EHScopeStack::stable_iterator CleanupStackDepth;
484 size_t LifetimeExtendedCleanupStackSize;
485 bool OldDidCallStackSave;
490 RunCleanupsScope(const RunCleanupsScope &) = delete;
491 void operator=(const RunCleanupsScope &) = delete;
494 CodeGenFunction& CGF;
497 /// \brief Enter a new cleanup scope.
498 explicit RunCleanupsScope(CodeGenFunction &CGF)
499 : PerformCleanup(true), CGF(CGF)
501 CleanupStackDepth = CGF.EHStack.stable_begin();
502 LifetimeExtendedCleanupStackSize =
503 CGF.LifetimeExtendedCleanupStack.size();
504 OldDidCallStackSave = CGF.DidCallStackSave;
505 CGF.DidCallStackSave = false;
508 /// \brief Exit this cleanup scope, emitting any accumulated
510 ~RunCleanupsScope() {
511 if (PerformCleanup) {
512 CGF.DidCallStackSave = OldDidCallStackSave;
513 CGF.PopCleanupBlocks(CleanupStackDepth,
514 LifetimeExtendedCleanupStackSize);
518 /// \brief Determine whether this scope requires any cleanups.
519 bool requiresCleanups() const {
520 return CGF.EHStack.stable_begin() != CleanupStackDepth;
523 /// \brief Force the emission of cleanups now, instead of waiting
524 /// until this object is destroyed.
525 void ForceCleanup() {
526 assert(PerformCleanup && "Already forced cleanup");
527 CGF.DidCallStackSave = OldDidCallStackSave;
528 CGF.PopCleanupBlocks(CleanupStackDepth,
529 LifetimeExtendedCleanupStackSize);
530 PerformCleanup = false;
534 class LexicalScope : public RunCleanupsScope {
536 SmallVector<const LabelDecl*, 4> Labels;
537 LexicalScope *ParentScope;
539 LexicalScope(const LexicalScope &) = delete;
540 void operator=(const LexicalScope &) = delete;
543 /// \brief Enter a new cleanup scope.
544 explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
545 : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
546 CGF.CurLexicalScope = this;
547 if (CGDebugInfo *DI = CGF.getDebugInfo())
548 DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
551 void addLabel(const LabelDecl *label) {
552 assert(PerformCleanup && "adding label to dead scope?");
553 Labels.push_back(label);
556 /// \brief Exit this cleanup scope, emitting any accumulated
559 if (CGDebugInfo *DI = CGF.getDebugInfo())
560 DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
562 // If we should perform a cleanup, force them now. Note that
563 // this ends the cleanup scope before rescoping any labels.
564 if (PerformCleanup) {
565 ApplyDebugLocation DL(CGF, Range.getEnd());
570 /// \brief Force the emission of cleanups now, instead of waiting
571 /// until this object is destroyed.
572 void ForceCleanup() {
573 CGF.CurLexicalScope = ParentScope;
574 RunCleanupsScope::ForceCleanup();
580 void rescopeLabels();
583 typedef llvm::DenseMap<const Decl *, Address> DeclMapTy;
585 /// \brief The scope used to remap some variables as private in the OpenMP
586 /// loop body (or other captured region emitted without outlining), and to
587 /// restore old vars back on exit.
588 class OMPPrivateScope : public RunCleanupsScope {
589 DeclMapTy SavedLocals;
590 DeclMapTy SavedPrivates;
593 OMPPrivateScope(const OMPPrivateScope &) = delete;
594 void operator=(const OMPPrivateScope &) = delete;
597 /// \brief Enter a new OpenMP private scope.
598 explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
600 /// \brief Registers \a LocalVD variable as a private and apply \a
601 /// PrivateGen function for it to generate corresponding private variable.
602 /// \a PrivateGen returns an address of the generated private variable.
603 /// \return true if the variable is registered as private, false if it has
604 /// been privatized already.
606 addPrivate(const VarDecl *LocalVD,
607 llvm::function_ref<Address()> PrivateGen) {
608 assert(PerformCleanup && "adding private to dead scope");
610 // Only save it once.
611 if (SavedLocals.count(LocalVD)) return false;
613 // Copy the existing local entry to SavedLocals.
614 auto it = CGF.LocalDeclMap.find(LocalVD);
615 if (it != CGF.LocalDeclMap.end()) {
616 SavedLocals.insert({LocalVD, it->second});
618 SavedLocals.insert({LocalVD, Address::invalid()});
621 // Generate the private entry.
622 Address Addr = PrivateGen();
623 QualType VarTy = LocalVD->getType();
624 if (VarTy->isReferenceType()) {
625 Address Temp = CGF.CreateMemTemp(VarTy);
626 CGF.Builder.CreateStore(Addr.getPointer(), Temp);
629 SavedPrivates.insert({LocalVD, Addr});
634 /// \brief Privatizes local variables previously registered as private.
635 /// Registration is separate from the actual privatization to allow
636 /// initializers use values of the original variables, not the private one.
637 /// This is important, for example, if the private variable is a class
638 /// variable initialized by a constructor that references other private
639 /// variables. But at initialization original variables must be used, not
641 /// \return true if at least one variable was privatized, false otherwise.
643 copyInto(SavedPrivates, CGF.LocalDeclMap);
644 SavedPrivates.clear();
645 return !SavedLocals.empty();
648 void ForceCleanup() {
649 RunCleanupsScope::ForceCleanup();
650 copyInto(SavedLocals, CGF.LocalDeclMap);
654 /// \brief Exit scope - all the mapped variables are restored.
660 /// Checks if the global variable is captured in current function.
661 bool isGlobalVarCaptured(const VarDecl *VD) const {
662 return !VD->isLocalVarDeclOrParm() && CGF.LocalDeclMap.count(VD) > 0;
666 /// Copy all the entries in the source map over the corresponding
667 /// entries in the destination, which must exist.
668 static void copyInto(const DeclMapTy &src, DeclMapTy &dest) {
669 for (auto &pair : src) {
670 if (!pair.second.isValid()) {
671 dest.erase(pair.first);
675 auto it = dest.find(pair.first);
676 if (it != dest.end()) {
677 it->second = pair.second;
685 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
686 /// that have been added.
687 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
689 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
690 /// that have been added, then adds all lifetime-extended cleanups from
691 /// the given position to the stack.
692 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
693 size_t OldLifetimeExtendedStackSize);
695 void ResolveBranchFixups(llvm::BasicBlock *Target);
697 /// The given basic block lies in the current EH scope, but may be a
698 /// target of a potentially scope-crossing jump; get a stable handle
699 /// to which we can perform this jump later.
700 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
701 return JumpDest(Target,
702 EHStack.getInnermostNormalCleanup(),
703 NextCleanupDestIndex++);
706 /// The given basic block lies in the current EH scope, but may be a
707 /// target of a potentially scope-crossing jump; get a stable handle
708 /// to which we can perform this jump later.
709 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
710 return getJumpDestInCurrentScope(createBasicBlock(Name));
713 /// EmitBranchThroughCleanup - Emit a branch from the current insert
714 /// block through the normal cleanup handling code (if any) and then
716 void EmitBranchThroughCleanup(JumpDest Dest);
718 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
719 /// specified destination obviously has no cleanups to run. 'false' is always
720 /// a conservatively correct answer for this method.
721 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
723 /// popCatchScope - Pops the catch scope at the top of the EHScope
724 /// stack, emitting any required code (other than the catch handlers
726 void popCatchScope();
728 llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
729 llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
730 llvm::BasicBlock *getMSVCDispatchBlock(EHScopeStack::stable_iterator scope);
732 /// An object to manage conditionally-evaluated expressions.
733 class ConditionalEvaluation {
734 llvm::BasicBlock *StartBB;
737 ConditionalEvaluation(CodeGenFunction &CGF)
738 : StartBB(CGF.Builder.GetInsertBlock()) {}
740 void begin(CodeGenFunction &CGF) {
741 assert(CGF.OutermostConditional != this);
742 if (!CGF.OutermostConditional)
743 CGF.OutermostConditional = this;
746 void end(CodeGenFunction &CGF) {
747 assert(CGF.OutermostConditional != nullptr);
748 if (CGF.OutermostConditional == this)
749 CGF.OutermostConditional = nullptr;
752 /// Returns a block which will be executed prior to each
753 /// evaluation of the conditional code.
754 llvm::BasicBlock *getStartingBlock() const {
759 /// isInConditionalBranch - Return true if we're currently emitting
760 /// one branch or the other of a conditional expression.
761 bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
763 void setBeforeOutermostConditional(llvm::Value *value, Address addr) {
764 assert(isInConditionalBranch());
765 llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
766 auto store = new llvm::StoreInst(value, addr.getPointer(), &block->back());
767 store->setAlignment(addr.getAlignment().getQuantity());
770 /// An RAII object to record that we're evaluating a statement
772 class StmtExprEvaluation {
773 CodeGenFunction &CGF;
775 /// We have to save the outermost conditional: cleanups in a
776 /// statement expression aren't conditional just because the
778 ConditionalEvaluation *SavedOutermostConditional;
781 StmtExprEvaluation(CodeGenFunction &CGF)
782 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
783 CGF.OutermostConditional = nullptr;
786 ~StmtExprEvaluation() {
787 CGF.OutermostConditional = SavedOutermostConditional;
788 CGF.EnsureInsertPoint();
792 /// An object which temporarily prevents a value from being
793 /// destroyed by aggressive peephole optimizations that assume that
794 /// all uses of a value have been realized in the IR.
795 class PeepholeProtection {
796 llvm::Instruction *Inst;
797 friend class CodeGenFunction;
800 PeepholeProtection() : Inst(nullptr) {}
803 /// A non-RAII class containing all the information about a bound
804 /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for
805 /// this which makes individual mappings very simple; using this
806 /// class directly is useful when you have a variable number of
807 /// opaque values or don't want the RAII functionality for some
809 class OpaqueValueMappingData {
810 const OpaqueValueExpr *OpaqueValue;
812 CodeGenFunction::PeepholeProtection Protection;
814 OpaqueValueMappingData(const OpaqueValueExpr *ov,
816 : OpaqueValue(ov), BoundLValue(boundLValue) {}
818 OpaqueValueMappingData() : OpaqueValue(nullptr) {}
820 static bool shouldBindAsLValue(const Expr *expr) {
821 // gl-values should be bound as l-values for obvious reasons.
822 // Records should be bound as l-values because IR generation
823 // always keeps them in memory. Expressions of function type
824 // act exactly like l-values but are formally required to be
826 return expr->isGLValue() ||
827 expr->getType()->isFunctionType() ||
828 hasAggregateEvaluationKind(expr->getType());
831 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
832 const OpaqueValueExpr *ov,
834 if (shouldBindAsLValue(ov))
835 return bind(CGF, ov, CGF.EmitLValue(e));
836 return bind(CGF, ov, CGF.EmitAnyExpr(e));
839 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
840 const OpaqueValueExpr *ov,
842 assert(shouldBindAsLValue(ov));
843 CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
844 return OpaqueValueMappingData(ov, true);
847 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
848 const OpaqueValueExpr *ov,
850 assert(!shouldBindAsLValue(ov));
851 CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
853 OpaqueValueMappingData data(ov, false);
855 // Work around an extremely aggressive peephole optimization in
856 // EmitScalarConversion which assumes that all other uses of a
858 data.Protection = CGF.protectFromPeepholes(rv);
863 bool isValid() const { return OpaqueValue != nullptr; }
864 void clear() { OpaqueValue = nullptr; }
866 void unbind(CodeGenFunction &CGF) {
867 assert(OpaqueValue && "no data to unbind!");
870 CGF.OpaqueLValues.erase(OpaqueValue);
872 CGF.OpaqueRValues.erase(OpaqueValue);
873 CGF.unprotectFromPeepholes(Protection);
878 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
879 class OpaqueValueMapping {
880 CodeGenFunction &CGF;
881 OpaqueValueMappingData Data;
884 static bool shouldBindAsLValue(const Expr *expr) {
885 return OpaqueValueMappingData::shouldBindAsLValue(expr);
888 /// Build the opaque value mapping for the given conditional
889 /// operator if it's the GNU ?: extension. This is a common
890 /// enough pattern that the convenience operator is really
893 OpaqueValueMapping(CodeGenFunction &CGF,
894 const AbstractConditionalOperator *op) : CGF(CGF) {
895 if (isa<ConditionalOperator>(op))
899 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
900 Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
904 OpaqueValueMapping(CodeGenFunction &CGF,
905 const OpaqueValueExpr *opaqueValue,
907 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
910 OpaqueValueMapping(CodeGenFunction &CGF,
911 const OpaqueValueExpr *opaqueValue,
913 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
921 ~OpaqueValueMapping() {
922 if (Data.isValid()) Data.unbind(CGF);
927 CGDebugInfo *DebugInfo;
928 bool DisableDebugInfo;
930 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
931 /// calling llvm.stacksave for multiple VLAs in the same scope.
932 bool DidCallStackSave;
934 /// IndirectBranch - The first time an indirect goto is seen we create a block
935 /// with an indirect branch. Every time we see the address of a label taken,
936 /// we add the label to the indirect goto. Every subsequent indirect goto is
937 /// codegen'd as a jump to the IndirectBranch's basic block.
938 llvm::IndirectBrInst *IndirectBranch;
940 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
942 DeclMapTy LocalDeclMap;
944 /// SizeArguments - If a ParmVarDecl had the pass_object_size attribute, this
945 /// will contain a mapping from said ParmVarDecl to its implicit "object_size"
947 llvm::SmallDenseMap<const ParmVarDecl *, const ImplicitParamDecl *, 2>
950 /// Track escaped local variables with auto storage. Used during SEH
951 /// outlining to produce a call to llvm.localescape.
952 llvm::DenseMap<llvm::AllocaInst *, int> EscapedLocals;
954 /// LabelMap - This keeps track of the LLVM basic block for each C label.
955 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
957 // BreakContinueStack - This keeps track of where break and continue
958 // statements should jump to.
959 struct BreakContinue {
960 BreakContinue(JumpDest Break, JumpDest Continue)
961 : BreakBlock(Break), ContinueBlock(Continue) {}
964 JumpDest ContinueBlock;
966 SmallVector<BreakContinue, 8> BreakContinueStack;
970 /// Calculate branch weights appropriate for PGO data
971 llvm::MDNode *createProfileWeights(uint64_t TrueCount, uint64_t FalseCount);
972 llvm::MDNode *createProfileWeights(ArrayRef<uint64_t> Weights);
973 llvm::MDNode *createProfileWeightsForLoop(const Stmt *Cond,
977 /// Increment the profiler's counter for the given statement.
978 void incrementProfileCounter(const Stmt *S) {
979 if (CGM.getCodeGenOpts().hasProfileClangInstr())
980 PGO.emitCounterIncrement(Builder, S);
981 PGO.setCurrentStmt(S);
984 /// Get the profiler's count for the given statement.
985 uint64_t getProfileCount(const Stmt *S) {
986 Optional<uint64_t> Count = PGO.getStmtCount(S);
987 if (!Count.hasValue())
992 /// Set the profiler's current count.
993 void setCurrentProfileCount(uint64_t Count) {
994 PGO.setCurrentRegionCount(Count);
997 /// Get the profiler's current count. This is generally the count for the most
998 /// recently incremented counter.
999 uint64_t getCurrentProfileCount() {
1000 return PGO.getCurrentRegionCount();
1005 /// SwitchInsn - This is nearest current switch instruction. It is null if
1006 /// current context is not in a switch.
1007 llvm::SwitchInst *SwitchInsn;
1008 /// The branch weights of SwitchInsn when doing instrumentation based PGO.
1009 SmallVector<uint64_t, 16> *SwitchWeights;
1011 /// CaseRangeBlock - This block holds if condition check for last case
1012 /// statement range in current switch instruction.
1013 llvm::BasicBlock *CaseRangeBlock;
1015 /// OpaqueLValues - Keeps track of the current set of opaque value
1017 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
1018 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
1020 // VLASizeMap - This keeps track of the associated size for each VLA type.
1021 // We track this by the size expression rather than the type itself because
1022 // in certain situations, like a const qualifier applied to an VLA typedef,
1023 // multiple VLA types can share the same size expression.
1024 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
1025 // enter/leave scopes.
1026 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
1028 /// A block containing a single 'unreachable' instruction. Created
1029 /// lazily by getUnreachableBlock().
1030 llvm::BasicBlock *UnreachableBlock;
1032 /// Counts of the number return expressions in the function.
1033 unsigned NumReturnExprs;
1035 /// Count the number of simple (constant) return expressions in the function.
1036 unsigned NumSimpleReturnExprs;
1038 /// The last regular (non-return) debug location (breakpoint) in the function.
1039 SourceLocation LastStopPoint;
1042 /// A scope within which we are constructing the fields of an object which
1043 /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
1044 /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
1045 class FieldConstructionScope {
1047 FieldConstructionScope(CodeGenFunction &CGF, Address This)
1048 : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
1049 CGF.CXXDefaultInitExprThis = This;
1051 ~FieldConstructionScope() {
1052 CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
1056 CodeGenFunction &CGF;
1057 Address OldCXXDefaultInitExprThis;
1060 /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
1061 /// is overridden to be the object under construction.
1062 class CXXDefaultInitExprScope {
1064 CXXDefaultInitExprScope(CodeGenFunction &CGF)
1065 : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue),
1066 OldCXXThisAlignment(CGF.CXXThisAlignment) {
1067 CGF.CXXThisValue = CGF.CXXDefaultInitExprThis.getPointer();
1068 CGF.CXXThisAlignment = CGF.CXXDefaultInitExprThis.getAlignment();
1070 ~CXXDefaultInitExprScope() {
1071 CGF.CXXThisValue = OldCXXThisValue;
1072 CGF.CXXThisAlignment = OldCXXThisAlignment;
1076 CodeGenFunction &CGF;
1077 llvm::Value *OldCXXThisValue;
1078 CharUnits OldCXXThisAlignment;
1081 class InlinedInheritingConstructorScope {
1083 InlinedInheritingConstructorScope(CodeGenFunction &CGF, GlobalDecl GD)
1084 : CGF(CGF), OldCurGD(CGF.CurGD), OldCurFuncDecl(CGF.CurFuncDecl),
1085 OldCurCodeDecl(CGF.CurCodeDecl),
1086 OldCXXABIThisDecl(CGF.CXXABIThisDecl),
1087 OldCXXABIThisValue(CGF.CXXABIThisValue),
1088 OldCXXThisValue(CGF.CXXThisValue),
1089 OldCXXABIThisAlignment(CGF.CXXABIThisAlignment),
1090 OldCXXThisAlignment(CGF.CXXThisAlignment),
1091 OldReturnValue(CGF.ReturnValue), OldFnRetTy(CGF.FnRetTy),
1092 OldCXXInheritedCtorInitExprArgs(
1093 std::move(CGF.CXXInheritedCtorInitExprArgs)) {
1095 CGF.CurFuncDecl = CGF.CurCodeDecl =
1096 cast<CXXConstructorDecl>(GD.getDecl());
1097 CGF.CXXABIThisDecl = nullptr;
1098 CGF.CXXABIThisValue = nullptr;
1099 CGF.CXXThisValue = nullptr;
1100 CGF.CXXABIThisAlignment = CharUnits();
1101 CGF.CXXThisAlignment = CharUnits();
1102 CGF.ReturnValue = Address::invalid();
1103 CGF.FnRetTy = QualType();
1104 CGF.CXXInheritedCtorInitExprArgs.clear();
1106 ~InlinedInheritingConstructorScope() {
1107 CGF.CurGD = OldCurGD;
1108 CGF.CurFuncDecl = OldCurFuncDecl;
1109 CGF.CurCodeDecl = OldCurCodeDecl;
1110 CGF.CXXABIThisDecl = OldCXXABIThisDecl;
1111 CGF.CXXABIThisValue = OldCXXABIThisValue;
1112 CGF.CXXThisValue = OldCXXThisValue;
1113 CGF.CXXABIThisAlignment = OldCXXABIThisAlignment;
1114 CGF.CXXThisAlignment = OldCXXThisAlignment;
1115 CGF.ReturnValue = OldReturnValue;
1116 CGF.FnRetTy = OldFnRetTy;
1117 CGF.CXXInheritedCtorInitExprArgs =
1118 std::move(OldCXXInheritedCtorInitExprArgs);
1122 CodeGenFunction &CGF;
1123 GlobalDecl OldCurGD;
1124 const Decl *OldCurFuncDecl;
1125 const Decl *OldCurCodeDecl;
1126 ImplicitParamDecl *OldCXXABIThisDecl;
1127 llvm::Value *OldCXXABIThisValue;
1128 llvm::Value *OldCXXThisValue;
1129 CharUnits OldCXXABIThisAlignment;
1130 CharUnits OldCXXThisAlignment;
1131 Address OldReturnValue;
1132 QualType OldFnRetTy;
1133 CallArgList OldCXXInheritedCtorInitExprArgs;
1137 /// CXXThisDecl - When generating code for a C++ member function,
1138 /// this will hold the implicit 'this' declaration.
1139 ImplicitParamDecl *CXXABIThisDecl;
1140 llvm::Value *CXXABIThisValue;
1141 llvm::Value *CXXThisValue;
1142 CharUnits CXXABIThisAlignment;
1143 CharUnits CXXThisAlignment;
1145 /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1146 /// this expression.
1147 Address CXXDefaultInitExprThis = Address::invalid();
1149 /// The values of function arguments to use when evaluating
1150 /// CXXInheritedCtorInitExprs within this context.
1151 CallArgList CXXInheritedCtorInitExprArgs;
1153 /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1154 /// destructor, this will hold the implicit argument (e.g. VTT).
1155 ImplicitParamDecl *CXXStructorImplicitParamDecl;
1156 llvm::Value *CXXStructorImplicitParamValue;
1158 /// OutermostConditional - Points to the outermost active
1159 /// conditional control. This is used so that we know if a
1160 /// temporary should be destroyed conditionally.
1161 ConditionalEvaluation *OutermostConditional;
1163 /// The current lexical scope.
1164 LexicalScope *CurLexicalScope;
1166 /// The current source location that should be used for exception
1168 SourceLocation CurEHLocation;
1170 /// BlockByrefInfos - For each __block variable, contains
1171 /// information about the layout of the variable.
1172 llvm::DenseMap<const ValueDecl *, BlockByrefInfo> BlockByrefInfos;
1174 llvm::BasicBlock *TerminateLandingPad;
1175 llvm::BasicBlock *TerminateHandler;
1176 llvm::BasicBlock *TrapBB;
1178 /// Add a kernel metadata node to the named metadata node 'opencl.kernels'.
1179 /// In the kernel metadata node, reference the kernel function and metadata
1180 /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2):
1181 /// - A node for the vec_type_hint(<type>) qualifier contains string
1182 /// "vec_type_hint", an undefined value of the <type> data type,
1183 /// and a Boolean that is true if the <type> is integer and signed.
1184 /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string
1185 /// "work_group_size_hint", and three 32-bit integers X, Y and Z.
1186 /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string
1187 /// "reqd_work_group_size", and three 32-bit integers X, Y and Z.
1188 void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1189 llvm::Function *Fn);
1192 CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
1195 CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1196 ASTContext &getContext() const { return CGM.getContext(); }
1197 CGDebugInfo *getDebugInfo() {
1198 if (DisableDebugInfo)
1202 void disableDebugInfo() { DisableDebugInfo = true; }
1203 void enableDebugInfo() { DisableDebugInfo = false; }
1205 bool shouldUseFusedARCCalls() {
1206 return CGM.getCodeGenOpts().OptimizationLevel == 0;
1209 const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
1211 /// Returns a pointer to the function's exception object and selector slot,
1212 /// which is assigned in every landing pad.
1213 Address getExceptionSlot();
1214 Address getEHSelectorSlot();
1216 /// Returns the contents of the function's exception object and selector
1218 llvm::Value *getExceptionFromSlot();
1219 llvm::Value *getSelectorFromSlot();
1221 Address getNormalCleanupDestSlot();
1223 llvm::BasicBlock *getUnreachableBlock() {
1224 if (!UnreachableBlock) {
1225 UnreachableBlock = createBasicBlock("unreachable");
1226 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1228 return UnreachableBlock;
1231 llvm::BasicBlock *getInvokeDest() {
1232 if (!EHStack.requiresLandingPad()) return nullptr;
1233 return getInvokeDestImpl();
1236 bool currentFunctionUsesSEHTry() const { return CurSEHParent != nullptr; }
1238 const TargetInfo &getTarget() const { return Target; }
1239 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
1241 //===--------------------------------------------------------------------===//
1243 //===--------------------------------------------------------------------===//
1245 typedef void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty);
1247 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1248 Address arrayEndPointer,
1249 QualType elementType,
1250 CharUnits elementAlignment,
1251 Destroyer *destroyer);
1252 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1253 llvm::Value *arrayEnd,
1254 QualType elementType,
1255 CharUnits elementAlignment,
1256 Destroyer *destroyer);
1258 void pushDestroy(QualType::DestructionKind dtorKind,
1259 Address addr, QualType type);
1260 void pushEHDestroy(QualType::DestructionKind dtorKind,
1261 Address addr, QualType type);
1262 void pushDestroy(CleanupKind kind, Address addr, QualType type,
1263 Destroyer *destroyer, bool useEHCleanupForArray);
1264 void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr,
1265 QualType type, Destroyer *destroyer,
1266 bool useEHCleanupForArray);
1267 void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1268 llvm::Value *CompletePtr,
1269 QualType ElementType);
1270 void pushStackRestore(CleanupKind kind, Address SPMem);
1271 void emitDestroy(Address addr, QualType type, Destroyer *destroyer,
1272 bool useEHCleanupForArray);
1273 llvm::Function *generateDestroyHelper(Address addr, QualType type,
1274 Destroyer *destroyer,
1275 bool useEHCleanupForArray,
1277 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
1278 QualType elementType, CharUnits elementAlign,
1279 Destroyer *destroyer,
1280 bool checkZeroLength, bool useEHCleanup);
1282 Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
1284 /// Determines whether an EH cleanup is required to destroy a type
1285 /// with the given destruction kind.
1286 bool needsEHCleanup(QualType::DestructionKind kind) {
1288 case QualType::DK_none:
1290 case QualType::DK_cxx_destructor:
1291 case QualType::DK_objc_weak_lifetime:
1292 return getLangOpts().Exceptions;
1293 case QualType::DK_objc_strong_lifetime:
1294 return getLangOpts().Exceptions &&
1295 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1297 llvm_unreachable("bad destruction kind");
1300 CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1301 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1304 //===--------------------------------------------------------------------===//
1306 //===--------------------------------------------------------------------===//
1308 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
1310 void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
1312 /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
1313 void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1314 const ObjCPropertyImplDecl *PID);
1315 void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
1316 const ObjCPropertyImplDecl *propImpl,
1317 const ObjCMethodDecl *GetterMothodDecl,
1318 llvm::Constant *AtomicHelperFn);
1320 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1321 ObjCMethodDecl *MD, bool ctor);
1323 /// GenerateObjCSetter - Synthesize an Objective-C property setter function
1324 /// for the given property.
1325 void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1326 const ObjCPropertyImplDecl *PID);
1327 void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1328 const ObjCPropertyImplDecl *propImpl,
1329 llvm::Constant *AtomicHelperFn);
1331 //===--------------------------------------------------------------------===//
1333 //===--------------------------------------------------------------------===//
1335 llvm::Value *EmitBlockLiteral(const BlockExpr *);
1336 llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
1337 static void destroyBlockInfos(CGBlockInfo *info);
1339 llvm::Function *GenerateBlockFunction(GlobalDecl GD,
1340 const CGBlockInfo &Info,
1341 const DeclMapTy &ldm,
1342 bool IsLambdaConversionToBlock);
1344 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1345 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
1346 llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
1347 const ObjCPropertyImplDecl *PID);
1348 llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
1349 const ObjCPropertyImplDecl *PID);
1350 llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
1352 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1354 class AutoVarEmission;
1356 void emitByrefStructureInit(const AutoVarEmission &emission);
1357 void enterByrefCleanup(const AutoVarEmission &emission);
1359 void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum,
1362 Address LoadBlockStruct();
1363 Address GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
1365 /// BuildBlockByrefAddress - Computes the location of the
1366 /// data in a variable which is declared as __block.
1367 Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V,
1368 bool followForward = true);
1369 Address emitBlockByrefAddress(Address baseAddr,
1370 const BlockByrefInfo &info,
1372 const llvm::Twine &name);
1374 const BlockByrefInfo &getBlockByrefInfo(const VarDecl *var);
1376 QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args);
1378 void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1379 const CGFunctionInfo &FnInfo);
1380 /// \brief Emit code for the start of a function.
1381 /// \param Loc The location to be associated with the function.
1382 /// \param StartLoc The location of the function body.
1383 void StartFunction(GlobalDecl GD,
1386 const CGFunctionInfo &FnInfo,
1387 const FunctionArgList &Args,
1388 SourceLocation Loc = SourceLocation(),
1389 SourceLocation StartLoc = SourceLocation());
1391 void EmitConstructorBody(FunctionArgList &Args);
1392 void EmitDestructorBody(FunctionArgList &Args);
1393 void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
1394 void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body);
1395 void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S);
1397 void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
1398 CallArgList &CallArgs);
1399 void EmitLambdaToBlockPointerBody(FunctionArgList &Args);
1400 void EmitLambdaBlockInvokeBody();
1401 void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
1402 void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD);
1403 void EmitAsanPrologueOrEpilogue(bool Prologue);
1405 /// \brief Emit the unified return block, trying to avoid its emission when
1407 /// \return The debug location of the user written return statement if the
1408 /// return block is is avoided.
1409 llvm::DebugLoc EmitReturnBlock();
1411 /// FinishFunction - Complete IR generation of the current function. It is
1412 /// legal to call this function even if there is no current insertion point.
1413 void FinishFunction(SourceLocation EndLoc=SourceLocation());
1415 void StartThunk(llvm::Function *Fn, GlobalDecl GD,
1416 const CGFunctionInfo &FnInfo);
1418 void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk);
1422 /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
1423 void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr,
1424 llvm::Value *Callee);
1426 /// Generate a thunk for the given method.
1427 void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1428 GlobalDecl GD, const ThunkInfo &Thunk);
1430 llvm::Function *GenerateVarArgsThunk(llvm::Function *Fn,
1431 const CGFunctionInfo &FnInfo,
1432 GlobalDecl GD, const ThunkInfo &Thunk);
1434 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1435 FunctionArgList &Args);
1437 void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init,
1438 ArrayRef<VarDecl *> ArrayIndexes);
1440 /// Struct with all informations about dynamic [sub]class needed to set vptr.
1443 const CXXRecordDecl *NearestVBase;
1444 CharUnits OffsetFromNearestVBase;
1445 const CXXRecordDecl *VTableClass;
1448 /// Initialize the vtable pointer of the given subobject.
1449 void InitializeVTablePointer(const VPtr &vptr);
1451 typedef llvm::SmallVector<VPtr, 4> VPtrsVector;
1453 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1454 VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass);
1456 void getVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase,
1457 CharUnits OffsetFromNearestVBase,
1458 bool BaseIsNonVirtualPrimaryBase,
1459 const CXXRecordDecl *VTableClass,
1460 VisitedVirtualBasesSetTy &VBases, VPtrsVector &vptrs);
1462 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
1464 /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1466 llvm::Value *GetVTablePtr(Address This, llvm::Type *VTableTy,
1467 const CXXRecordDecl *VTableClass);
1469 enum CFITypeCheckKind {
1473 CFITCK_UnrelatedCast,
1477 /// \brief Derived is the presumed address of an object of type T after a
1478 /// cast. If T is a polymorphic class type, emit a check that the virtual
1479 /// table for Derived belongs to a class derived from T.
1480 void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived,
1481 bool MayBeNull, CFITypeCheckKind TCK,
1482 SourceLocation Loc);
1484 /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
1485 /// If vptr CFI is enabled, emit a check that VTable is valid.
1486 void EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable,
1487 CFITypeCheckKind TCK, SourceLocation Loc);
1489 /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for
1490 /// RD using llvm.type.test.
1491 void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable,
1492 CFITypeCheckKind TCK, SourceLocation Loc);
1494 /// If whole-program virtual table optimization is enabled, emit an assumption
1495 /// that VTable is a member of RD's type identifier. Or, if vptr CFI is
1496 /// enabled, emit a check that VTable is a member of RD's type identifier.
1497 void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD,
1498 llvm::Value *VTable, SourceLocation Loc);
1500 /// Returns whether we should perform a type checked load when loading a
1501 /// virtual function for virtual calls to members of RD. This is generally
1502 /// true when both vcall CFI and whole-program-vtables are enabled.
1503 bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD);
1505 /// Emit a type checked load from the given vtable.
1506 llvm::Value *EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable,
1507 uint64_t VTableByteOffset);
1509 /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
1510 /// expr can be devirtualized.
1511 bool CanDevirtualizeMemberFunctionCall(const Expr *Base,
1512 const CXXMethodDecl *MD);
1514 /// EnterDtorCleanups - Enter the cleanups necessary to complete the
1515 /// given phase of destruction for a destructor. The end result
1516 /// should call destructors on members and base classes in reverse
1517 /// order of their construction.
1518 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
1520 /// ShouldInstrumentFunction - Return true if the current function should be
1521 /// instrumented with __cyg_profile_func_* calls
1522 bool ShouldInstrumentFunction();
1524 /// ShouldXRayInstrument - Return true if the current function should be
1525 /// instrumented with XRay nop sleds.
1526 bool ShouldXRayInstrumentFunction() const;
1528 /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1529 /// instrumentation function with the current function and the call site, if
1530 /// function instrumentation is enabled.
1531 void EmitFunctionInstrumentation(const char *Fn);
1533 /// EmitMCountInstrumentation - Emit call to .mcount.
1534 void EmitMCountInstrumentation();
1536 /// EmitFunctionProlog - Emit the target specific LLVM code to load the
1537 /// arguments for the given function. This is also responsible for naming the
1538 /// LLVM function arguments.
1539 void EmitFunctionProlog(const CGFunctionInfo &FI,
1541 const FunctionArgList &Args);
1543 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
1544 /// given temporary.
1545 void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
1546 SourceLocation EndLoc);
1548 /// EmitStartEHSpec - Emit the start of the exception spec.
1549 void EmitStartEHSpec(const Decl *D);
1551 /// EmitEndEHSpec - Emit the end of the exception spec.
1552 void EmitEndEHSpec(const Decl *D);
1554 /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1555 llvm::BasicBlock *getTerminateLandingPad();
1557 /// getTerminateHandler - Return a handler (not a landing pad, just
1558 /// a catch handler) that just calls terminate. This is used when
1559 /// a terminate scope encloses a try.
1560 llvm::BasicBlock *getTerminateHandler();
1562 llvm::Type *ConvertTypeForMem(QualType T);
1563 llvm::Type *ConvertType(QualType T);
1564 llvm::Type *ConvertType(const TypeDecl *T) {
1565 return ConvertType(getContext().getTypeDeclType(T));
1568 /// LoadObjCSelf - Load the value of self. This function is only valid while
1569 /// generating code for an Objective-C method.
1570 llvm::Value *LoadObjCSelf();
1572 /// TypeOfSelfObject - Return type of object that this self represents.
1573 QualType TypeOfSelfObject();
1575 /// hasAggregateLLVMType - Return true if the specified AST type will map into
1576 /// an aggregate LLVM type or is void.
1577 static TypeEvaluationKind getEvaluationKind(QualType T);
1579 static bool hasScalarEvaluationKind(QualType T) {
1580 return getEvaluationKind(T) == TEK_Scalar;
1583 static bool hasAggregateEvaluationKind(QualType T) {
1584 return getEvaluationKind(T) == TEK_Aggregate;
1587 /// createBasicBlock - Create an LLVM basic block.
1588 llvm::BasicBlock *createBasicBlock(const Twine &name = "",
1589 llvm::Function *parent = nullptr,
1590 llvm::BasicBlock *before = nullptr) {
1592 return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
1594 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
1598 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
1600 JumpDest getJumpDestForLabel(const LabelDecl *S);
1602 /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1603 /// another basic block, simplify it. This assumes that no other code could
1604 /// potentially reference the basic block.
1605 void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1607 /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
1608 /// adding a fall-through branch from the current insert block if
1609 /// necessary. It is legal to call this function even if there is no current
1610 /// insertion point.
1612 /// IsFinished - If true, indicates that the caller has finished emitting
1613 /// branches to the given block and does not expect to emit code into it. This
1614 /// means the block can be ignored if it is unreachable.
1615 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
1617 /// EmitBlockAfterUses - Emit the given block somewhere hopefully
1618 /// near its uses, and leave the insertion point in it.
1619 void EmitBlockAfterUses(llvm::BasicBlock *BB);
1621 /// EmitBranch - Emit a branch to the specified basic block from the current
1622 /// insert block, taking care to avoid creation of branches from dummy
1623 /// blocks. It is legal to call this function even if there is no current
1624 /// insertion point.
1626 /// This function clears the current insertion point. The caller should follow
1627 /// calls to this function with calls to Emit*Block prior to generation new
1629 void EmitBranch(llvm::BasicBlock *Block);
1631 /// HaveInsertPoint - True if an insertion point is defined. If not, this
1632 /// indicates that the current code being emitted is unreachable.
1633 bool HaveInsertPoint() const {
1634 return Builder.GetInsertBlock() != nullptr;
1637 /// EnsureInsertPoint - Ensure that an insertion point is defined so that
1638 /// emitted IR has a place to go. Note that by definition, if this function
1639 /// creates a block then that block is unreachable; callers may do better to
1640 /// detect when no insertion point is defined and simply skip IR generation.
1641 void EnsureInsertPoint() {
1642 if (!HaveInsertPoint())
1643 EmitBlock(createBasicBlock());
1646 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1647 /// specified stmt yet.
1648 void ErrorUnsupported(const Stmt *S, const char *Type);
1650 //===--------------------------------------------------------------------===//
1652 //===--------------------------------------------------------------------===//
1654 LValue MakeAddrLValue(Address Addr, QualType T,
1655 AlignmentSource AlignSource = AlignmentSource::Type) {
1656 return LValue::MakeAddr(Addr, T, getContext(), AlignSource,
1657 CGM.getTBAAInfo(T));
1660 LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment,
1661 AlignmentSource AlignSource = AlignmentSource::Type) {
1662 return LValue::MakeAddr(Address(V, Alignment), T, getContext(),
1663 AlignSource, CGM.getTBAAInfo(T));
1666 LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T);
1667 LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
1668 CharUnits getNaturalTypeAlignment(QualType T,
1669 AlignmentSource *Source = nullptr,
1670 bool forPointeeType = false);
1671 CharUnits getNaturalPointeeTypeAlignment(QualType T,
1672 AlignmentSource *Source = nullptr);
1674 Address EmitLoadOfReference(Address Ref, const ReferenceType *RefTy,
1675 AlignmentSource *Source = nullptr);
1676 LValue EmitLoadOfReferenceLValue(Address Ref, const ReferenceType *RefTy);
1678 Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy,
1679 AlignmentSource *Source = nullptr);
1680 LValue EmitLoadOfPointerLValue(Address Ptr, const PointerType *PtrTy);
1682 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
1683 /// block. The caller is responsible for setting an appropriate alignment on
1685 llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
1686 const Twine &Name = "tmp");
1687 Address CreateTempAlloca(llvm::Type *Ty, CharUnits align,
1688 const Twine &Name = "tmp");
1690 /// CreateDefaultAlignedTempAlloca - This creates an alloca with the
1691 /// default ABI alignment of the given LLVM type.
1693 /// IMPORTANT NOTE: This is *not* generally the right alignment for
1694 /// any given AST type that happens to have been lowered to the
1695 /// given IR type. This should only ever be used for function-local,
1696 /// IR-driven manipulations like saving and restoring a value. Do
1697 /// not hand this address off to arbitrary IRGen routines, and especially
1698 /// do not pass it as an argument to a function that might expect a
1699 /// properly ABI-aligned value.
1700 Address CreateDefaultAlignTempAlloca(llvm::Type *Ty,
1701 const Twine &Name = "tmp");
1703 /// InitTempAlloca - Provide an initial value for the given alloca which
1704 /// will be observable at all locations in the function.
1706 /// The address should be something that was returned from one of
1707 /// the CreateTempAlloca or CreateMemTemp routines, and the
1708 /// initializer must be valid in the entry block (i.e. it must
1709 /// either be a constant or an argument value).
1710 void InitTempAlloca(Address Alloca, llvm::Value *Value);
1712 /// CreateIRTemp - Create a temporary IR object of the given type, with
1713 /// appropriate alignment. This routine should only be used when an temporary
1714 /// value needs to be stored into an alloca (for example, to avoid explicit
1715 /// PHI construction), but the type is the IR type, not the type appropriate
1716 /// for storing in memory.
1718 /// That is, this is exactly equivalent to CreateMemTemp, but calling
1719 /// ConvertType instead of ConvertTypeForMem.
1720 Address CreateIRTemp(QualType T, const Twine &Name = "tmp");
1722 /// CreateMemTemp - Create a temporary memory object of the given type, with
1723 /// appropriate alignment.
1724 Address CreateMemTemp(QualType T, const Twine &Name = "tmp");
1725 Address CreateMemTemp(QualType T, CharUnits Align, const Twine &Name = "tmp");
1727 /// CreateAggTemp - Create a temporary memory object for the given
1729 AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
1730 return AggValueSlot::forAddr(CreateMemTemp(T, Name),
1732 AggValueSlot::IsNotDestructed,
1733 AggValueSlot::DoesNotNeedGCBarriers,
1734 AggValueSlot::IsNotAliased);
1737 /// Emit a cast to void* in the appropriate address space.
1738 llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1740 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
1741 /// expression and compare the result against zero, returning an Int1Ty value.
1742 llvm::Value *EvaluateExprAsBool(const Expr *E);
1744 /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
1745 void EmitIgnoredExpr(const Expr *E);
1747 /// EmitAnyExpr - Emit code to compute the specified expression which can have
1748 /// any type. The result is returned as an RValue struct. If this is an
1749 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
1750 /// the result should be returned.
1752 /// \param ignoreResult True if the resulting value isn't used.
1753 RValue EmitAnyExpr(const Expr *E,
1754 AggValueSlot aggSlot = AggValueSlot::ignored(),
1755 bool ignoreResult = false);
1757 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
1758 // or the value of the expression, depending on how va_list is defined.
1759 Address EmitVAListRef(const Expr *E);
1761 /// Emit a "reference" to a __builtin_ms_va_list; this is
1762 /// always the value of the expression, because a __builtin_ms_va_list is a
1763 /// pointer to a char.
1764 Address EmitMSVAListRef(const Expr *E);
1766 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
1767 /// always be accessible even if no aggregate location is provided.
1768 RValue EmitAnyExprToTemp(const Expr *E);
1770 /// EmitAnyExprToMem - Emits the code necessary to evaluate an
1771 /// arbitrary expression into the given memory location.
1772 void EmitAnyExprToMem(const Expr *E, Address Location,
1773 Qualifiers Quals, bool IsInitializer);
1775 void EmitAnyExprToExn(const Expr *E, Address Addr);
1777 /// EmitExprAsInit - Emits the code necessary to initialize a
1778 /// location in memory with the given initializer.
1779 void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1780 bool capturedByInit);
1782 /// hasVolatileMember - returns true if aggregate type has a volatile
1784 bool hasVolatileMember(QualType T) {
1785 if (const RecordType *RT = T->getAs<RecordType>()) {
1786 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
1787 return RD->hasVolatileMember();
1791 /// EmitAggregateCopy - Emit an aggregate assignment.
1793 /// The difference to EmitAggregateCopy is that tail padding is not copied.
1794 /// This is required for correctness when assigning non-POD structures in C++.
1795 void EmitAggregateAssign(Address DestPtr, Address SrcPtr,
1797 bool IsVolatile = hasVolatileMember(EltTy);
1798 EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, true);
1801 void EmitAggregateCopyCtor(Address DestPtr, Address SrcPtr,
1802 QualType DestTy, QualType SrcTy) {
1803 EmitAggregateCopy(DestPtr, SrcPtr, SrcTy, /*IsVolatile=*/false,
1804 /*IsAssignment=*/false);
1807 /// EmitAggregateCopy - Emit an aggregate copy.
1809 /// \param isVolatile - True iff either the source or the destination is
1811 /// \param isAssignment - If false, allow padding to be copied. This often
1812 /// yields more efficient.
1813 void EmitAggregateCopy(Address DestPtr, Address SrcPtr,
1814 QualType EltTy, bool isVolatile=false,
1815 bool isAssignment = false);
1817 /// GetAddrOfLocalVar - Return the address of a local variable.
1818 Address GetAddrOfLocalVar(const VarDecl *VD) {
1819 auto it = LocalDeclMap.find(VD);
1820 assert(it != LocalDeclMap.end() &&
1821 "Invalid argument to GetAddrOfLocalVar(), no decl!");
1825 /// getOpaqueLValueMapping - Given an opaque value expression (which
1826 /// must be mapped to an l-value), return its mapping.
1827 const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
1828 assert(OpaqueValueMapping::shouldBindAsLValue(e));
1830 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
1831 it = OpaqueLValues.find(e);
1832 assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
1836 /// getOpaqueRValueMapping - Given an opaque value expression (which
1837 /// must be mapped to an r-value), return its mapping.
1838 const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
1839 assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1841 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
1842 it = OpaqueRValues.find(e);
1843 assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
1847 /// getAccessedFieldNo - Given an encoded value and a result number, return
1848 /// the input field number being accessed.
1849 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
1851 llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
1852 llvm::BasicBlock *GetIndirectGotoBlock();
1854 /// EmitNullInitialization - Generate code to set a value of the given type to
1855 /// null, If the type contains data member pointers, they will be initialized
1856 /// to -1 in accordance with the Itanium C++ ABI.
1857 void EmitNullInitialization(Address DestPtr, QualType Ty);
1859 /// Emits a call to an LLVM variable-argument intrinsic, either
1860 /// \c llvm.va_start or \c llvm.va_end.
1861 /// \param ArgValue A reference to the \c va_list as emitted by either
1862 /// \c EmitVAListRef or \c EmitMSVAListRef.
1863 /// \param IsStart If \c true, emits a call to \c llvm.va_start; otherwise,
1864 /// calls \c llvm.va_end.
1865 llvm::Value *EmitVAStartEnd(llvm::Value *ArgValue, bool IsStart);
1867 /// Generate code to get an argument from the passed in pointer
1868 /// and update it accordingly.
1869 /// \param VE The \c VAArgExpr for which to generate code.
1870 /// \param VAListAddr Receives a reference to the \c va_list as emitted by
1871 /// either \c EmitVAListRef or \c EmitMSVAListRef.
1872 /// \returns A pointer to the argument.
1873 // FIXME: We should be able to get rid of this method and use the va_arg
1874 // instruction in LLVM instead once it works well enough.
1875 Address EmitVAArg(VAArgExpr *VE, Address &VAListAddr);
1877 /// emitArrayLength - Compute the length of an array, even if it's a
1878 /// VLA, and drill down to the base element type.
1879 llvm::Value *emitArrayLength(const ArrayType *arrayType,
1883 /// EmitVLASize - Capture all the sizes for the VLA expressions in
1884 /// the given variably-modified type and store them in the VLASizeMap.
1886 /// This function can be called with a null (unreachable) insert point.
1887 void EmitVariablyModifiedType(QualType Ty);
1889 /// getVLASize - Returns an LLVM value that corresponds to the size,
1890 /// in non-variably-sized elements, of a variable length array type,
1891 /// plus that largest non-variably-sized element type. Assumes that
1892 /// the type has already been emitted with EmitVariablyModifiedType.
1893 std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1894 std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
1896 /// LoadCXXThis - Load the value of 'this'. This function is only valid while
1897 /// generating code for an C++ member function.
1898 llvm::Value *LoadCXXThis() {
1899 assert(CXXThisValue && "no 'this' value for this function");
1900 return CXXThisValue;
1902 Address LoadCXXThisAddress();
1904 /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1906 // FIXME: Every place that calls LoadCXXVTT is something
1907 // that needs to be abstracted properly.
1908 llvm::Value *LoadCXXVTT() {
1909 assert(CXXStructorImplicitParamValue && "no VTT value for this function");
1910 return CXXStructorImplicitParamValue;
1913 /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
1914 /// complete class to the given direct base.
1916 GetAddressOfDirectBaseInCompleteClass(Address Value,
1917 const CXXRecordDecl *Derived,
1918 const CXXRecordDecl *Base,
1919 bool BaseIsVirtual);
1921 static bool ShouldNullCheckClassCastValue(const CastExpr *Cast);
1923 /// GetAddressOfBaseClass - This function will add the necessary delta to the
1924 /// load of 'this' and returns address of the base class.
1925 Address GetAddressOfBaseClass(Address Value,
1926 const CXXRecordDecl *Derived,
1927 CastExpr::path_const_iterator PathBegin,
1928 CastExpr::path_const_iterator PathEnd,
1929 bool NullCheckValue, SourceLocation Loc);
1931 Address GetAddressOfDerivedClass(Address Value,
1932 const CXXRecordDecl *Derived,
1933 CastExpr::path_const_iterator PathBegin,
1934 CastExpr::path_const_iterator PathEnd,
1935 bool NullCheckValue);
1937 /// GetVTTParameter - Return the VTT parameter that should be passed to a
1938 /// base constructor/destructor with virtual bases.
1939 /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
1940 /// to ItaniumCXXABI.cpp together with all the references to VTT.
1941 llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
1944 void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1945 CXXCtorType CtorType,
1946 const FunctionArgList &Args,
1947 SourceLocation Loc);
1948 // It's important not to confuse this and the previous function. Delegating
1949 // constructors are the C++0x feature. The constructor delegate optimization
1950 // is used to reduce duplication in the base and complete consturctors where
1951 // they are substantially the same.
1952 void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1953 const FunctionArgList &Args);
1955 /// Emit a call to an inheriting constructor (that is, one that invokes a
1956 /// constructor inherited from a base class) by inlining its definition. This
1957 /// is necessary if the ABI does not support forwarding the arguments to the
1958 /// base class constructor (because they're variadic or similar).
1959 void EmitInlinedInheritingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1960 CXXCtorType CtorType,
1961 bool ForVirtualBase,
1965 /// Emit a call to a constructor inherited from a base class, passing the
1966 /// current constructor's arguments along unmodified (without even making
1968 void EmitInheritedCXXConstructorCall(const CXXConstructorDecl *D,
1969 bool ForVirtualBase, Address This,
1970 bool InheritedFromVBase,
1971 const CXXInheritedCtorInitExpr *E);
1973 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1974 bool ForVirtualBase, bool Delegating,
1975 Address This, const CXXConstructExpr *E);
1977 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1978 bool ForVirtualBase, bool Delegating,
1979 Address This, CallArgList &Args);
1981 /// Emit assumption load for all bases. Requires to be be called only on
1982 /// most-derived class and not under construction of the object.
1983 void EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, Address This);
1985 /// Emit assumption that vptr load == global vtable.
1986 void EmitVTableAssumptionLoad(const VPtr &vptr, Address This);
1988 void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1989 Address This, Address Src,
1990 const CXXConstructExpr *E);
1992 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1993 const ArrayType *ArrayTy,
1995 const CXXConstructExpr *E,
1996 bool ZeroInitialization = false);
1998 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1999 llvm::Value *NumElements,
2001 const CXXConstructExpr *E,
2002 bool ZeroInitialization = false);
2004 static Destroyer destroyCXXObject;
2006 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
2007 bool ForVirtualBase, bool Delegating,
2010 void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
2011 llvm::Type *ElementTy, Address NewPtr,
2012 llvm::Value *NumElements,
2013 llvm::Value *AllocSizeWithoutCookie);
2015 void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
2018 llvm::Value *EmitLifetimeStart(uint64_t Size, llvm::Value *Addr);
2019 void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr);
2021 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
2022 void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
2024 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
2027 RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
2028 const Expr *Arg, bool IsDelete);
2030 llvm::Value *EmitCXXTypeidExpr(const CXXTypeidExpr *E);
2031 llvm::Value *EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE);
2032 Address EmitCXXUuidofExpr(const CXXUuidofExpr *E);
2034 /// \brief Situations in which we might emit a check for the suitability of a
2035 /// pointer or glvalue.
2036 enum TypeCheckKind {
2037 /// Checking the operand of a load. Must be suitably sized and aligned.
2039 /// Checking the destination of a store. Must be suitably sized and aligned.
2041 /// Checking the bound value in a reference binding. Must be suitably sized
2042 /// and aligned, but is not required to refer to an object (until the
2043 /// reference is used), per core issue 453.
2044 TCK_ReferenceBinding,
2045 /// Checking the object expression in a non-static data member access. Must
2046 /// be an object within its lifetime.
2048 /// Checking the 'this' pointer for a call to a non-static member function.
2049 /// Must be an object within its lifetime.
2051 /// Checking the 'this' pointer for a constructor call.
2052 TCK_ConstructorCall,
2053 /// Checking the operand of a static_cast to a derived pointer type. Must be
2054 /// null or an object within its lifetime.
2055 TCK_DowncastPointer,
2056 /// Checking the operand of a static_cast to a derived reference type. Must
2057 /// be an object within its lifetime.
2058 TCK_DowncastReference,
2059 /// Checking the operand of a cast to a base object. Must be suitably sized
2062 /// Checking the operand of a cast to a virtual base object. Must be an
2063 /// object within its lifetime.
2064 TCK_UpcastToVirtualBase
2067 /// \brief Whether any type-checking sanitizers are enabled. If \c false,
2068 /// calls to EmitTypeCheck can be skipped.
2069 bool sanitizePerformTypeCheck() const;
2071 /// \brief Emit a check that \p V is the address of storage of the
2072 /// appropriate size and alignment for an object of type \p Type.
2073 void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
2074 QualType Type, CharUnits Alignment = CharUnits::Zero(),
2075 bool SkipNullCheck = false);
2077 /// \brief Emit a check that \p Base points into an array object, which
2078 /// we can access at index \p Index. \p Accessed should be \c false if we
2079 /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
2080 void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
2081 QualType IndexType, bool Accessed);
2083 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
2084 bool isInc, bool isPre);
2085 ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
2086 bool isInc, bool isPre);
2088 void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment,
2089 llvm::Value *OffsetValue = nullptr) {
2090 Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
2094 //===--------------------------------------------------------------------===//
2095 // Declaration Emission
2096 //===--------------------------------------------------------------------===//
2098 /// EmitDecl - Emit a declaration.
2100 /// This function can be called with a null (unreachable) insert point.
2101 void EmitDecl(const Decl &D);
2103 /// EmitVarDecl - Emit a local variable declaration.
2105 /// This function can be called with a null (unreachable) insert point.
2106 void EmitVarDecl(const VarDecl &D);
2108 void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
2109 bool capturedByInit);
2110 void EmitScalarInit(llvm::Value *init, LValue lvalue);
2112 typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
2113 llvm::Value *Address);
2115 /// \brief Determine whether the given initializer is trivial in the sense
2116 /// that it requires no code to be generated.
2117 bool isTrivialInitializer(const Expr *Init);
2119 /// EmitAutoVarDecl - Emit an auto variable declaration.
2121 /// This function can be called with a null (unreachable) insert point.
2122 void EmitAutoVarDecl(const VarDecl &D);
2124 class AutoVarEmission {
2125 friend class CodeGenFunction;
2127 const VarDecl *Variable;
2129 /// The address of the alloca. Invalid if the variable was emitted
2130 /// as a global constant.
2133 llvm::Value *NRVOFlag;
2135 /// True if the variable is a __block variable.
2138 /// True if the variable is of aggregate type and has a constant
2140 bool IsConstantAggregate;
2142 /// Non-null if we should use lifetime annotations.
2143 llvm::Value *SizeForLifetimeMarkers;
2146 AutoVarEmission(Invalid) : Variable(nullptr), Addr(Address::invalid()) {}
2148 AutoVarEmission(const VarDecl &variable)
2149 : Variable(&variable), Addr(Address::invalid()), NRVOFlag(nullptr),
2150 IsByRef(false), IsConstantAggregate(false),
2151 SizeForLifetimeMarkers(nullptr) {}
2153 bool wasEmittedAsGlobal() const { return !Addr.isValid(); }
2156 static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
2158 bool useLifetimeMarkers() const {
2159 return SizeForLifetimeMarkers != nullptr;
2161 llvm::Value *getSizeForLifetimeMarkers() const {
2162 assert(useLifetimeMarkers());
2163 return SizeForLifetimeMarkers;
2166 /// Returns the raw, allocated address, which is not necessarily
2167 /// the address of the object itself.
2168 Address getAllocatedAddress() const {
2172 /// Returns the address of the object within this declaration.
2173 /// Note that this does not chase the forwarding pointer for
2175 Address getObjectAddress(CodeGenFunction &CGF) const {
2176 if (!IsByRef) return Addr;
2178 return CGF.emitBlockByrefAddress(Addr, Variable, /*forward*/ false);
2181 AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
2182 void EmitAutoVarInit(const AutoVarEmission &emission);
2183 void EmitAutoVarCleanups(const AutoVarEmission &emission);
2184 void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
2185 QualType::DestructionKind dtorKind);
2187 void EmitStaticVarDecl(const VarDecl &D,
2188 llvm::GlobalValue::LinkageTypes Linkage);
2193 ParamValue(llvm::Value *V, unsigned A) : Value(V), Alignment(A) {}
2195 static ParamValue forDirect(llvm::Value *value) {
2196 return ParamValue(value, 0);
2198 static ParamValue forIndirect(Address addr) {
2199 assert(!addr.getAlignment().isZero());
2200 return ParamValue(addr.getPointer(), addr.getAlignment().getQuantity());
2203 bool isIndirect() const { return Alignment != 0; }
2204 llvm::Value *getAnyValue() const { return Value; }
2206 llvm::Value *getDirectValue() const {
2207 assert(!isIndirect());
2211 Address getIndirectAddress() const {
2212 assert(isIndirect());
2213 return Address(Value, CharUnits::fromQuantity(Alignment));
2217 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
2218 void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo);
2220 /// protectFromPeepholes - Protect a value that we're intending to
2221 /// store to the side, but which will probably be used later, from
2222 /// aggressive peepholing optimizations that might delete it.
2224 /// Pass the result to unprotectFromPeepholes to declare that
2225 /// protection is no longer required.
2227 /// There's no particular reason why this shouldn't apply to
2228 /// l-values, it's just that no existing peepholes work on pointers.
2229 PeepholeProtection protectFromPeepholes(RValue rvalue);
2230 void unprotectFromPeepholes(PeepholeProtection protection);
2232 //===--------------------------------------------------------------------===//
2233 // Statement Emission
2234 //===--------------------------------------------------------------------===//
2236 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
2237 void EmitStopPoint(const Stmt *S);
2239 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
2240 /// this function even if there is no current insertion point.
2242 /// This function may clear the current insertion point; callers should use
2243 /// EnsureInsertPoint if they wish to subsequently generate code without first
2244 /// calling EmitBlock, EmitBranch, or EmitStmt.
2245 void EmitStmt(const Stmt *S);
2247 /// EmitSimpleStmt - Try to emit a "simple" statement which does not
2248 /// necessarily require an insertion point or debug information; typically
2249 /// because the statement amounts to a jump or a container of other
2252 /// \return True if the statement was handled.
2253 bool EmitSimpleStmt(const Stmt *S);
2255 Address EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
2256 AggValueSlot AVS = AggValueSlot::ignored());
2257 Address EmitCompoundStmtWithoutScope(const CompoundStmt &S,
2258 bool GetLast = false,
2260 AggValueSlot::ignored());
2262 /// EmitLabel - Emit the block for the given label. It is legal to call this
2263 /// function even if there is no current insertion point.
2264 void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
2266 void EmitLabelStmt(const LabelStmt &S);
2267 void EmitAttributedStmt(const AttributedStmt &S);
2268 void EmitGotoStmt(const GotoStmt &S);
2269 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
2270 void EmitIfStmt(const IfStmt &S);
2272 void EmitWhileStmt(const WhileStmt &S,
2273 ArrayRef<const Attr *> Attrs = None);
2274 void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
2275 void EmitForStmt(const ForStmt &S,
2276 ArrayRef<const Attr *> Attrs = None);
2277 void EmitReturnStmt(const ReturnStmt &S);
2278 void EmitDeclStmt(const DeclStmt &S);
2279 void EmitBreakStmt(const BreakStmt &S);
2280 void EmitContinueStmt(const ContinueStmt &S);
2281 void EmitSwitchStmt(const SwitchStmt &S);
2282 void EmitDefaultStmt(const DefaultStmt &S);
2283 void EmitCaseStmt(const CaseStmt &S);
2284 void EmitCaseStmtRange(const CaseStmt &S);
2285 void EmitAsmStmt(const AsmStmt &S);
2287 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
2288 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
2289 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
2290 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
2291 void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
2293 void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
2294 void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
2296 void EmitCXXTryStmt(const CXXTryStmt &S);
2297 void EmitSEHTryStmt(const SEHTryStmt &S);
2298 void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
2299 void EnterSEHTryStmt(const SEHTryStmt &S);
2300 void ExitSEHTryStmt(const SEHTryStmt &S);
2302 void startOutlinedSEHHelper(CodeGenFunction &ParentCGF, bool IsFilter,
2303 const Stmt *OutlinedStmt);
2305 llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
2306 const SEHExceptStmt &Except);
2308 llvm::Function *GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF,
2309 const SEHFinallyStmt &Finally);
2311 void EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF,
2312 llvm::Value *ParentFP,
2313 llvm::Value *EntryEBP);
2314 llvm::Value *EmitSEHExceptionCode();
2315 llvm::Value *EmitSEHExceptionInfo();
2316 llvm::Value *EmitSEHAbnormalTermination();
2318 /// Scan the outlined statement for captures from the parent function. For
2319 /// each capture, mark the capture as escaped and emit a call to
2320 /// llvm.localrecover. Insert the localrecover result into the LocalDeclMap.
2321 void EmitCapturedLocals(CodeGenFunction &ParentCGF, const Stmt *OutlinedStmt,
2324 /// Recovers the address of a local in a parent function. ParentVar is the
2325 /// address of the variable used in the immediate parent function. It can
2326 /// either be an alloca or a call to llvm.localrecover if there are nested
2327 /// outlined functions. ParentFP is the frame pointer of the outermost parent
2329 Address recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF,
2331 llvm::Value *ParentFP);
2333 void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
2334 ArrayRef<const Attr *> Attrs = None);
2336 /// Returns calculated size of the specified type.
2337 llvm::Value *getTypeSize(QualType Ty);
2338 LValue InitCapturedStruct(const CapturedStmt &S);
2339 llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
2340 llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
2341 Address GenerateCapturedStmtArgument(const CapturedStmt &S);
2342 llvm::Function *GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S);
2343 void GenerateOpenMPCapturedVars(const CapturedStmt &S,
2344 SmallVectorImpl<llvm::Value *> &CapturedVars);
2345 void emitOMPSimpleStore(LValue LVal, RValue RVal, QualType RValTy,
2346 SourceLocation Loc);
2347 /// \brief Perform element by element copying of arrays with type \a
2348 /// OriginalType from \a SrcAddr to \a DestAddr using copying procedure
2349 /// generated by \a CopyGen.
2351 /// \param DestAddr Address of the destination array.
2352 /// \param SrcAddr Address of the source array.
2353 /// \param OriginalType Type of destination and source arrays.
2354 /// \param CopyGen Copying procedure that copies value of single array element
2355 /// to another single array element.
2356 void EmitOMPAggregateAssign(
2357 Address DestAddr, Address SrcAddr, QualType OriginalType,
2358 const llvm::function_ref<void(Address, Address)> &CopyGen);
2359 /// \brief Emit proper copying of data from one variable to another.
2361 /// \param OriginalType Original type of the copied variables.
2362 /// \param DestAddr Destination address.
2363 /// \param SrcAddr Source address.
2364 /// \param DestVD Destination variable used in \a CopyExpr (for arrays, has
2365 /// type of the base array element).
2366 /// \param SrcVD Source variable used in \a CopyExpr (for arrays, has type of
2367 /// the base array element).
2368 /// \param Copy Actual copygin expression for copying data from \a SrcVD to \a
2370 void EmitOMPCopy(QualType OriginalType,
2371 Address DestAddr, Address SrcAddr,
2372 const VarDecl *DestVD, const VarDecl *SrcVD,
2374 /// \brief Emit atomic update code for constructs: \a X = \a X \a BO \a E or
2375 /// \a X = \a E \a BO \a E.
2377 /// \param X Value to be updated.
2378 /// \param E Update value.
2379 /// \param BO Binary operation for update operation.
2380 /// \param IsXLHSInRHSPart true if \a X is LHS in RHS part of the update
2381 /// expression, false otherwise.
2382 /// \param AO Atomic ordering of the generated atomic instructions.
2383 /// \param CommonGen Code generator for complex expressions that cannot be
2384 /// expressed through atomicrmw instruction.
2385 /// \returns <true, OldAtomicValue> if simple 'atomicrmw' instruction was
2386 /// generated, <false, RValue::get(nullptr)> otherwise.
2387 std::pair<bool, RValue> EmitOMPAtomicSimpleUpdateExpr(
2388 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
2389 llvm::AtomicOrdering AO, SourceLocation Loc,
2390 const llvm::function_ref<RValue(RValue)> &CommonGen);
2391 bool EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
2392 OMPPrivateScope &PrivateScope);
2393 void EmitOMPPrivateClause(const OMPExecutableDirective &D,
2394 OMPPrivateScope &PrivateScope);
2395 /// \brief Emit code for copyin clause in \a D directive. The next code is
2396 /// generated at the start of outlined functions for directives:
2398 /// threadprivate_var1 = master_threadprivate_var1;
2399 /// operator=(threadprivate_var2, master_threadprivate_var2);
2401 /// __kmpc_barrier(&loc, global_tid);
2404 /// \param D OpenMP directive possibly with 'copyin' clause(s).
2405 /// \returns true if at least one copyin variable is found, false otherwise.
2406 bool EmitOMPCopyinClause(const OMPExecutableDirective &D);
2407 /// \brief Emit initial code for lastprivate variables. If some variable is
2408 /// not also firstprivate, then the default initialization is used. Otherwise
2409 /// initialization of this variable is performed by EmitOMPFirstprivateClause
2412 /// \param D Directive that may have 'lastprivate' directives.
2413 /// \param PrivateScope Private scope for capturing lastprivate variables for
2414 /// proper codegen in internal captured statement.
2416 /// \returns true if there is at least one lastprivate variable, false
2418 bool EmitOMPLastprivateClauseInit(const OMPExecutableDirective &D,
2419 OMPPrivateScope &PrivateScope);
2420 /// \brief Emit final copying of lastprivate values to original variables at
2421 /// the end of the worksharing or simd directive.
2423 /// \param D Directive that has at least one 'lastprivate' directives.
2424 /// \param IsLastIterCond Boolean condition that must be set to 'i1 true' if
2425 /// it is the last iteration of the loop code in associated directive, or to
2426 /// 'i1 false' otherwise. If this item is nullptr, no final check is required.
2427 void EmitOMPLastprivateClauseFinal(const OMPExecutableDirective &D,
2429 llvm::Value *IsLastIterCond = nullptr);
2430 /// Emit initial code for linear clauses.
2431 void EmitOMPLinearClause(const OMPLoopDirective &D,
2432 CodeGenFunction::OMPPrivateScope &PrivateScope);
2433 /// Emit final code for linear clauses.
2434 /// \param CondGen Optional conditional code for final part of codegen for
2436 void EmitOMPLinearClauseFinal(
2437 const OMPLoopDirective &D,
2438 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen);
2439 /// \brief Emit initial code for reduction variables. Creates reduction copies
2440 /// and initializes them with the values according to OpenMP standard.
2442 /// \param D Directive (possibly) with the 'reduction' clause.
2443 /// \param PrivateScope Private scope for capturing reduction variables for
2444 /// proper codegen in internal captured statement.
2446 void EmitOMPReductionClauseInit(const OMPExecutableDirective &D,
2447 OMPPrivateScope &PrivateScope);
2448 /// \brief Emit final update of reduction values to original variables at
2449 /// the end of the directive.
2451 /// \param D Directive that has at least one 'reduction' directives.
2452 void EmitOMPReductionClauseFinal(const OMPExecutableDirective &D);
2453 /// \brief Emit initial code for linear variables. Creates private copies
2454 /// and initializes them with the values according to OpenMP standard.
2456 /// \param D Directive (possibly) with the 'linear' clause.
2457 void EmitOMPLinearClauseInit(const OMPLoopDirective &D);
2459 typedef const llvm::function_ref<void(CodeGenFunction & /*CGF*/,
2460 llvm::Value * /*OutlinedFn*/,
2461 const OMPTaskDataTy & /*Data*/)>
2463 void EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
2464 const RegionCodeGenTy &BodyGen,
2465 const TaskGenTy &TaskGen, OMPTaskDataTy &Data);
2467 void EmitOMPParallelDirective(const OMPParallelDirective &S);
2468 void EmitOMPSimdDirective(const OMPSimdDirective &S);
2469 void EmitOMPForDirective(const OMPForDirective &S);
2470 void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
2471 void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
2472 void EmitOMPSectionDirective(const OMPSectionDirective &S);
2473 void EmitOMPSingleDirective(const OMPSingleDirective &S);
2474 void EmitOMPMasterDirective(const OMPMasterDirective &S);
2475 void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
2476 void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
2477 void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
2478 void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
2479 void EmitOMPTaskDirective(const OMPTaskDirective &S);
2480 void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
2481 void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
2482 void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
2483 void EmitOMPTaskgroupDirective(const OMPTaskgroupDirective &S);
2484 void EmitOMPFlushDirective(const OMPFlushDirective &S);
2485 void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
2486 void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
2487 void EmitOMPTargetDirective(const OMPTargetDirective &S);
2488 void EmitOMPTargetDataDirective(const OMPTargetDataDirective &S);
2489 void EmitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective &S);
2490 void EmitOMPTargetExitDataDirective(const OMPTargetExitDataDirective &S);
2491 void EmitOMPTargetUpdateDirective(const OMPTargetUpdateDirective &S);
2492 void EmitOMPTargetParallelDirective(const OMPTargetParallelDirective &S);
2494 EmitOMPTargetParallelForDirective(const OMPTargetParallelForDirective &S);
2495 void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
2497 EmitOMPCancellationPointDirective(const OMPCancellationPointDirective &S);
2498 void EmitOMPCancelDirective(const OMPCancelDirective &S);
2499 void EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S);
2500 void EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S);
2501 void EmitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective &S);
2502 void EmitOMPDistributeDirective(const OMPDistributeDirective &S);
2503 void EmitOMPDistributeLoop(const OMPDistributeDirective &S);
2504 void EmitOMPDistributeParallelForDirective(
2505 const OMPDistributeParallelForDirective &S);
2506 void EmitOMPDistributeParallelForSimdDirective(
2507 const OMPDistributeParallelForSimdDirective &S);
2508 void EmitOMPDistributeSimdDirective(const OMPDistributeSimdDirective &S);
2509 void EmitOMPTargetParallelForSimdDirective(
2510 const OMPTargetParallelForSimdDirective &S);
2512 /// Emit outlined function for the target directive.
2513 static std::pair<llvm::Function * /*OutlinedFn*/,
2514 llvm::Constant * /*OutlinedFnID*/>
2515 EmitOMPTargetDirectiveOutlinedFunction(CodeGenModule &CGM,
2516 const OMPTargetDirective &S,
2517 StringRef ParentName,
2518 bool IsOffloadEntry);
2519 /// \brief Emit inner loop of the worksharing/simd construct.
2521 /// \param S Directive, for which the inner loop must be emitted.
2522 /// \param RequiresCleanup true, if directive has some associated private
2524 /// \param LoopCond Bollean condition for loop continuation.
2525 /// \param IncExpr Increment expression for loop control variable.
2526 /// \param BodyGen Generator for the inner body of the inner loop.
2527 /// \param PostIncGen Genrator for post-increment code (required for ordered
2528 /// loop directvies).
2529 void EmitOMPInnerLoop(
2530 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
2531 const Expr *IncExpr,
2532 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
2533 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen);
2535 JumpDest getOMPCancelDestination(OpenMPDirectiveKind Kind);
2536 /// Emit initial code for loop counters of loop-based directives.
2537 void EmitOMPPrivateLoopCounters(const OMPLoopDirective &S,
2538 OMPPrivateScope &LoopScope);
2541 /// Helpers for the OpenMP loop directives.
2542 void EmitOMPLoopBody(const OMPLoopDirective &D, JumpDest LoopExit);
2543 void EmitOMPSimdInit(const OMPLoopDirective &D, bool IsMonotonic = false);
2544 void EmitOMPSimdFinal(
2545 const OMPLoopDirective &D,
2546 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen);
2547 /// \brief Emit code for the worksharing loop-based directive.
2548 /// \return true, if this construct has any lastprivate clause, false -
2550 bool EmitOMPWorksharingLoop(const OMPLoopDirective &S);
2551 void EmitOMPOuterLoop(bool IsMonotonic, bool DynamicOrOrdered,
2552 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
2553 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk);
2554 void EmitOMPForOuterLoop(const OpenMPScheduleTy &ScheduleKind,
2555 bool IsMonotonic, const OMPLoopDirective &S,
2556 OMPPrivateScope &LoopScope, bool Ordered, Address LB,
2557 Address UB, Address ST, Address IL,
2558 llvm::Value *Chunk);
2559 void EmitOMPDistributeOuterLoop(
2560 OpenMPDistScheduleClauseKind ScheduleKind,
2561 const OMPDistributeDirective &S, OMPPrivateScope &LoopScope,
2562 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk);
2563 /// \brief Emit code for sections directive.
2564 void EmitSections(const OMPExecutableDirective &S);
2568 //===--------------------------------------------------------------------===//
2569 // LValue Expression Emission
2570 //===--------------------------------------------------------------------===//
2572 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
2573 RValue GetUndefRValue(QualType Ty);
2575 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
2576 /// and issue an ErrorUnsupported style diagnostic (using the
2578 RValue EmitUnsupportedRValue(const Expr *E,
2581 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
2582 /// an ErrorUnsupported style diagnostic (using the provided Name).
2583 LValue EmitUnsupportedLValue(const Expr *E,
2586 /// EmitLValue - Emit code to compute a designator that specifies the location
2587 /// of the expression.
2589 /// This can return one of two things: a simple address or a bitfield
2590 /// reference. In either case, the LLVM Value* in the LValue structure is
2591 /// guaranteed to be an LLVM pointer type.
2593 /// If this returns a bitfield reference, nothing about the pointee type of
2594 /// the LLVM value is known: For example, it may not be a pointer to an
2597 /// If this returns a normal address, and if the lvalue's C type is fixed
2598 /// size, this method guarantees that the returned pointer type will point to
2599 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
2600 /// variable length type, this is not possible.
2602 LValue EmitLValue(const Expr *E);
2604 /// \brief Same as EmitLValue but additionally we generate checking code to
2605 /// guard against undefined behavior. This is only suitable when we know
2606 /// that the address will be used to access the object.
2607 LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
2609 RValue convertTempToRValue(Address addr, QualType type,
2610 SourceLocation Loc);
2612 void EmitAtomicInit(Expr *E, LValue lvalue);
2614 bool LValueIsSuitableForInlineAtomic(LValue Src);
2616 RValue EmitAtomicLoad(LValue LV, SourceLocation SL,
2617 AggValueSlot Slot = AggValueSlot::ignored());
2619 RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
2620 llvm::AtomicOrdering AO, bool IsVolatile = false,
2621 AggValueSlot slot = AggValueSlot::ignored());
2623 void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
2625 void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO,
2626 bool IsVolatile, bool isInit);
2628 std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange(
2629 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
2630 llvm::AtomicOrdering Success =
2631 llvm::AtomicOrdering::SequentiallyConsistent,
2632 llvm::AtomicOrdering Failure =
2633 llvm::AtomicOrdering::SequentiallyConsistent,
2634 bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored());
2636 void EmitAtomicUpdate(LValue LVal, llvm::AtomicOrdering AO,
2637 const llvm::function_ref<RValue(RValue)> &UpdateOp,
2640 /// EmitToMemory - Change a scalar value from its value
2641 /// representation to its in-memory representation.
2642 llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
2644 /// EmitFromMemory - Change a scalar value from its memory
2645 /// representation to its value representation.
2646 llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
2648 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2649 /// care to appropriately convert from the memory representation to
2650 /// the LLVM value representation.
2651 llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty,
2653 AlignmentSource AlignSource =
2654 AlignmentSource::Type,
2655 llvm::MDNode *TBAAInfo = nullptr,
2656 QualType TBAABaseTy = QualType(),
2657 uint64_t TBAAOffset = 0,
2658 bool isNontemporal = false);
2660 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2661 /// care to appropriately convert from the memory representation to
2662 /// the LLVM value representation. The l-value must be a simple
2664 llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
2666 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2667 /// care to appropriately convert from the memory representation to
2668 /// the LLVM value representation.
2669 void EmitStoreOfScalar(llvm::Value *Value, Address Addr,
2670 bool Volatile, QualType Ty,
2671 AlignmentSource AlignSource = AlignmentSource::Type,
2672 llvm::MDNode *TBAAInfo = nullptr, bool isInit = false,
2673 QualType TBAABaseTy = QualType(),
2674 uint64_t TBAAOffset = 0, bool isNontemporal = false);
2676 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2677 /// care to appropriately convert from the memory representation to
2678 /// the LLVM value representation. The l-value must be a simple
2679 /// l-value. The isInit flag indicates whether this is an initialization.
2680 /// If so, atomic qualifiers are ignored and the store is always non-atomic.
2681 void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
2683 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
2684 /// this method emits the address of the lvalue, then loads the result as an
2685 /// rvalue, returning the rvalue.
2686 RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
2687 RValue EmitLoadOfExtVectorElementLValue(LValue V);
2688 RValue EmitLoadOfBitfieldLValue(LValue LV);
2689 RValue EmitLoadOfGlobalRegLValue(LValue LV);
2691 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
2692 /// lvalue, where both are guaranteed to the have the same type, and that type
2694 void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false);
2695 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
2696 void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
2698 /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
2699 /// as EmitStoreThroughLValue.
2701 /// \param Result [out] - If non-null, this will be set to a Value* for the
2702 /// bit-field contents after the store, appropriate for use as the result of
2703 /// an assignment to the bit-field.
2704 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
2705 llvm::Value **Result=nullptr);
2707 /// Emit an l-value for an assignment (simple or compound) of complex type.
2708 LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
2709 LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
2710 LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
2711 llvm::Value *&Result);
2713 // Note: only available for agg return types
2714 LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
2715 LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
2716 // Note: only available for agg return types
2717 LValue EmitCallExprLValue(const CallExpr *E);
2718 // Note: only available for agg return types
2719 LValue EmitVAArgExprLValue(const VAArgExpr *E);
2720 LValue EmitDeclRefLValue(const DeclRefExpr *E);
2721 LValue EmitStringLiteralLValue(const StringLiteral *E);
2722 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
2723 LValue EmitPredefinedLValue(const PredefinedExpr *E);
2724 LValue EmitUnaryOpLValue(const UnaryOperator *E);
2725 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2726 bool Accessed = false);
2727 LValue EmitOMPArraySectionExpr(const OMPArraySectionExpr *E,
2728 bool IsLowerBound = true);
2729 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
2730 LValue EmitMemberExpr(const MemberExpr *E);
2731 LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
2732 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
2733 LValue EmitInitListLValue(const InitListExpr *E);
2734 LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
2735 LValue EmitCastLValue(const CastExpr *E);
2736 LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
2737 LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
2739 Address EmitExtVectorElementLValue(LValue V);
2741 RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
2743 Address EmitArrayToPointerDecay(const Expr *Array,
2744 AlignmentSource *AlignSource = nullptr);
2746 class ConstantEmission {
2747 llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
2748 ConstantEmission(llvm::Constant *C, bool isReference)
2749 : ValueAndIsReference(C, isReference) {}
2751 ConstantEmission() {}
2752 static ConstantEmission forReference(llvm::Constant *C) {
2753 return ConstantEmission(C, true);
2755 static ConstantEmission forValue(llvm::Constant *C) {
2756 return ConstantEmission(C, false);
2759 explicit operator bool() const {
2760 return ValueAndIsReference.getOpaqueValue() != nullptr;
2763 bool isReference() const { return ValueAndIsReference.getInt(); }
2764 LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
2765 assert(isReference());
2766 return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
2767 refExpr->getType());
2770 llvm::Constant *getValue() const {
2771 assert(!isReference());
2772 return ValueAndIsReference.getPointer();
2776 ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
2778 RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
2779 AggValueSlot slot = AggValueSlot::ignored());
2780 LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
2782 llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
2783 const ObjCIvarDecl *Ivar);
2784 LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
2785 LValue EmitLValueForLambdaField(const FieldDecl *Field);
2787 /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
2788 /// if the Field is a reference, this will return the address of the reference
2789 /// and not the address of the value stored in the reference.
2790 LValue EmitLValueForFieldInitialization(LValue Base,
2791 const FieldDecl* Field);
2793 LValue EmitLValueForIvar(QualType ObjectTy,
2794 llvm::Value* Base, const ObjCIvarDecl *Ivar,
2795 unsigned CVRQualifiers);
2797 LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
2798 LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
2799 LValue EmitLambdaLValue(const LambdaExpr *E);
2800 LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
2801 LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
2803 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
2804 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
2805 LValue EmitStmtExprLValue(const StmtExpr *E);
2806 LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
2807 LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
2808 void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
2810 //===--------------------------------------------------------------------===//
2811 // Scalar Expression Emission
2812 //===--------------------------------------------------------------------===//
2814 /// EmitCall - Generate a call of the given function, expecting the given
2815 /// result type, and using the given argument list which specifies both the
2816 /// LLVM arguments and the types they were derived from.
2817 RValue EmitCall(const CGFunctionInfo &FnInfo, llvm::Value *Callee,
2818 ReturnValueSlot ReturnValue, const CallArgList &Args,
2819 CGCalleeInfo CalleeInfo = CGCalleeInfo(),
2820 llvm::Instruction **callOrInvoke = nullptr);
2822 RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E,
2823 ReturnValueSlot ReturnValue,
2824 CGCalleeInfo CalleeInfo = CGCalleeInfo(),
2825 llvm::Value *Chain = nullptr);
2826 RValue EmitCallExpr(const CallExpr *E,
2827 ReturnValueSlot ReturnValue = ReturnValueSlot());
2829 void checkTargetFeatures(const CallExpr *E, const FunctionDecl *TargetDecl);
2831 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2832 const Twine &name = "");
2833 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2834 ArrayRef<llvm::Value*> args,
2835 const Twine &name = "");
2836 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2837 const Twine &name = "");
2838 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2839 ArrayRef<llvm::Value*> args,
2840 const Twine &name = "");
2842 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2843 ArrayRef<llvm::Value *> Args,
2844 const Twine &Name = "");
2845 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2846 ArrayRef<llvm::Value*> args,
2847 const Twine &name = "");
2848 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2849 const Twine &name = "");
2850 void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2851 ArrayRef<llvm::Value*> args);
2853 llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
2854 NestedNameSpecifier *Qual,
2857 llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2859 const CXXRecordDecl *RD);
2862 EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2863 ReturnValueSlot ReturnValue, llvm::Value *This,
2864 llvm::Value *ImplicitParam,
2865 QualType ImplicitParamTy, const CallExpr *E);
2866 RValue EmitCXXDestructorCall(const CXXDestructorDecl *DD, llvm::Value *Callee,
2867 llvm::Value *This, llvm::Value *ImplicitParam,
2868 QualType ImplicitParamTy, const CallExpr *E,
2870 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2871 ReturnValueSlot ReturnValue);
2872 RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE,
2873 const CXXMethodDecl *MD,
2874 ReturnValueSlot ReturnValue,
2876 NestedNameSpecifier *Qualifier,
2877 bool IsArrow, const Expr *Base);
2878 // Compute the object pointer.
2879 Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base,
2880 llvm::Value *memberPtr,
2881 const MemberPointerType *memberPtrType,
2882 AlignmentSource *AlignSource = nullptr);
2883 RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2884 ReturnValueSlot ReturnValue);
2886 RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
2887 const CXXMethodDecl *MD,
2888 ReturnValueSlot ReturnValue);
2890 RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
2891 ReturnValueSlot ReturnValue);
2893 RValue EmitCUDADevicePrintfCallExpr(const CallExpr *E,
2894 ReturnValueSlot ReturnValue);
2896 RValue EmitBuiltinExpr(const FunctionDecl *FD,
2897 unsigned BuiltinID, const CallExpr *E,
2898 ReturnValueSlot ReturnValue);
2900 RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
2902 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
2903 /// is unhandled by the current target.
2904 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2906 llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
2907 const llvm::CmpInst::Predicate Fp,
2908 const llvm::CmpInst::Predicate Ip,
2909 const llvm::Twine &Name = "");
2910 llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2912 llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
2913 unsigned LLVMIntrinsic,
2914 unsigned AltLLVMIntrinsic,
2915 const char *NameHint,
2918 SmallVectorImpl<llvm::Value *> &Ops,
2919 Address PtrOp0, Address PtrOp1);
2920 llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2921 unsigned Modifier, llvm::Type *ArgTy,
2923 llvm::Value *EmitNeonCall(llvm::Function *F,
2924 SmallVectorImpl<llvm::Value*> &O,
2926 unsigned shift = 0, bool rightshift = false);
2927 llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
2928 llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
2929 bool negateForRightShift);
2930 llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
2931 llvm::Type *Ty, bool usgn, const char *name);
2932 llvm::Value *vectorWrapScalar16(llvm::Value *Op);
2933 llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2935 llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
2936 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2937 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2938 llvm::Value *EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2939 llvm::Value *EmitSystemZBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2940 llvm::Value *EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2941 llvm::Value *EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
2944 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
2945 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
2946 llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
2947 llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
2948 llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2949 llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
2950 const ObjCMethodDecl *MethodWithObjects);
2951 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
2952 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2953 ReturnValueSlot Return = ReturnValueSlot());
2955 /// Retrieves the default cleanup kind for an ARC cleanup.
2956 /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2957 CleanupKind getARCCleanupKind() {
2958 return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2959 ? NormalAndEHCleanup : NormalCleanup;
2963 void EmitARCInitWeak(Address addr, llvm::Value *value);
2964 void EmitARCDestroyWeak(Address addr);
2965 llvm::Value *EmitARCLoadWeak(Address addr);
2966 llvm::Value *EmitARCLoadWeakRetained(Address addr);
2967 llvm::Value *EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored);
2968 void EmitARCCopyWeak(Address dst, Address src);
2969 void EmitARCMoveWeak(Address dst, Address src);
2970 llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2971 llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
2972 llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
2973 bool resultIgnored);
2974 llvm::Value *EmitARCStoreStrongCall(Address addr, llvm::Value *value,
2975 bool resultIgnored);
2976 llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2977 llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
2978 llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
2979 void EmitARCDestroyStrong(Address addr, ARCPreciseLifetime_t precise);
2980 void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
2981 llvm::Value *EmitARCAutorelease(llvm::Value *value);
2982 llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2983 llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2984 llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2985 llvm::Value *EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value);
2987 std::pair<LValue,llvm::Value*>
2988 EmitARCStoreAutoreleasing(const BinaryOperator *e);
2989 std::pair<LValue,llvm::Value*>
2990 EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2991 std::pair<LValue,llvm::Value*>
2992 EmitARCStoreUnsafeUnretained(const BinaryOperator *e, bool ignored);
2994 llvm::Value *EmitObjCThrowOperand(const Expr *expr);
2995 llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2996 llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2998 llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
2999 llvm::Value *EmitARCReclaimReturnedObject(const Expr *e,
3000 bool allowUnsafeClaim);
3001 llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
3002 llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
3003 llvm::Value *EmitARCUnsafeUnretainedScalarExpr(const Expr *expr);
3005 void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
3007 static Destroyer destroyARCStrongImprecise;
3008 static Destroyer destroyARCStrongPrecise;
3009 static Destroyer destroyARCWeak;
3011 void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
3012 llvm::Value *EmitObjCAutoreleasePoolPush();
3013 llvm::Value *EmitObjCMRRAutoreleasePoolPush();
3014 void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
3015 void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
3017 /// \brief Emits a reference binding to the passed in expression.
3018 RValue EmitReferenceBindingToExpr(const Expr *E);
3020 //===--------------------------------------------------------------------===//
3021 // Expression Emission
3022 //===--------------------------------------------------------------------===//
3024 // Expressions are broken into three classes: scalar, complex, aggregate.
3026 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
3027 /// scalar type, returning the result.
3028 llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
3030 /// Emit a conversion from the specified type to the specified destination
3031 /// type, both of which are LLVM scalar types.
3032 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
3033 QualType DstTy, SourceLocation Loc);
3035 /// Emit a conversion from the specified complex type to the specified
3036 /// destination type, where the destination type is an LLVM scalar type.
3037 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
3039 SourceLocation Loc);
3041 /// EmitAggExpr - Emit the computation of the specified expression
3042 /// of aggregate type. The result is computed into the given slot,
3043 /// which may be null to indicate that the value is not needed.
3044 void EmitAggExpr(const Expr *E, AggValueSlot AS);
3046 /// EmitAggExprToLValue - Emit the computation of the specified expression of
3047 /// aggregate type into a temporary LValue.
3048 LValue EmitAggExprToLValue(const Expr *E);
3050 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
3051 /// make sure it survives garbage collection until this point.
3052 void EmitExtendGCLifetime(llvm::Value *object);
3054 /// EmitComplexExpr - Emit the computation of the specified expression of
3055 /// complex type, returning the result.
3056 ComplexPairTy EmitComplexExpr(const Expr *E,
3057 bool IgnoreReal = false,
3058 bool IgnoreImag = false);
3060 /// EmitComplexExprIntoLValue - Emit the given expression of complex
3061 /// type and place its result into the specified l-value.
3062 void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
3064 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
3065 void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
3067 /// EmitLoadOfComplex - Load a complex number from the specified l-value.
3068 ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
3070 Address emitAddrOfRealComponent(Address complex, QualType complexType);
3071 Address emitAddrOfImagComponent(Address complex, QualType complexType);
3073 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
3074 /// global variable that has already been created for it. If the initializer
3075 /// has a different type than GV does, this may free GV and return a different
3076 /// one. Otherwise it just returns GV.
3077 llvm::GlobalVariable *
3078 AddInitializerToStaticVarDecl(const VarDecl &D,
3079 llvm::GlobalVariable *GV);
3082 /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
3083 /// variable with global storage.
3084 void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr,
3087 llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor,
3088 llvm::Constant *Addr);
3090 /// Call atexit() with a function that passes the given argument to
3091 /// the given function.
3092 void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn,
3093 llvm::Constant *addr);
3095 /// Emit code in this function to perform a guarded variable
3096 /// initialization. Guarded initializations are used when it's not
3097 /// possible to prove that an initialization will be done exactly
3098 /// once, e.g. with a static local variable or a static data member
3099 /// of a class template.
3100 void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
3103 /// GenerateCXXGlobalInitFunc - Generates code for initializing global
3105 void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
3106 ArrayRef<llvm::Function *> CXXThreadLocals,
3107 Address Guard = Address::invalid());
3109 /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global
3111 void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
3112 const std::vector<std::pair<llvm::WeakVH,
3113 llvm::Constant*> > &DtorsAndObjects);
3115 void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
3117 llvm::GlobalVariable *Addr,
3120 void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
3122 void EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, const Expr *Exp);
3124 void enterFullExpression(const ExprWithCleanups *E) {
3125 if (E->getNumObjects() == 0) return;
3126 enterNonTrivialFullExpression(E);
3128 void enterNonTrivialFullExpression(const ExprWithCleanups *E);
3130 void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
3132 void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
3134 RValue EmitAtomicExpr(AtomicExpr *E);
3136 //===--------------------------------------------------------------------===//
3137 // Annotations Emission
3138 //===--------------------------------------------------------------------===//
3140 /// Emit an annotation call (intrinsic or builtin).
3141 llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn,
3142 llvm::Value *AnnotatedVal,
3143 StringRef AnnotationStr,
3144 SourceLocation Location);
3146 /// Emit local annotations for the local variable V, declared by D.
3147 void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
3149 /// Emit field annotations for the given field & value. Returns the
3150 /// annotation result.
3151 Address EmitFieldAnnotations(const FieldDecl *D, Address V);
3153 //===--------------------------------------------------------------------===//
3155 //===--------------------------------------------------------------------===//
3157 /// ContainsLabel - Return true if the statement contains a label in it. If
3158 /// this statement is not executed normally, it not containing a label means
3159 /// that we can just remove the code.
3160 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
3162 /// containsBreak - Return true if the statement contains a break out of it.
3163 /// If the statement (recursively) contains a switch or loop with a break
3164 /// inside of it, this is fine.
3165 static bool containsBreak(const Stmt *S);
3167 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
3168 /// to a constant, or if it does but contains a label, return false. If it
3169 /// constant folds return true and set the boolean result in Result.
3170 bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result,
3171 bool AllowLabels = false);
3173 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
3174 /// to a constant, or if it does but contains a label, return false. If it
3175 /// constant folds return true and set the folded value.
3176 bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result,
3177 bool AllowLabels = false);
3179 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
3180 /// if statement) to the specified blocks. Based on the condition, this might
3181 /// try to simplify the codegen of the conditional based on the branch.
3182 /// TrueCount should be the number of times we expect the condition to
3183 /// evaluate to true based on PGO data.
3184 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
3185 llvm::BasicBlock *FalseBlock, uint64_t TrueCount);
3187 /// \brief Emit a description of a type in a format suitable for passing to
3188 /// a runtime sanitizer handler.
3189 llvm::Constant *EmitCheckTypeDescriptor(QualType T);
3191 /// \brief Convert a value into a format suitable for passing to a runtime
3192 /// sanitizer handler.
3193 llvm::Value *EmitCheckValue(llvm::Value *V);
3195 /// \brief Emit a description of a source location in a format suitable for
3196 /// passing to a runtime sanitizer handler.
3197 llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
3199 /// \brief Create a basic block that will call a handler function in a
3200 /// sanitizer runtime with the provided arguments, and create a conditional
3202 void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked,
3203 StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
3204 ArrayRef<llvm::Value *> DynamicArgs);
3206 /// \brief Emit a slow path cross-DSO CFI check which calls __cfi_slowpath
3207 /// if Cond if false.
3208 void EmitCfiSlowPathCheck(SanitizerMask Kind, llvm::Value *Cond,
3209 llvm::ConstantInt *TypeId, llvm::Value *Ptr,
3210 ArrayRef<llvm::Constant *> StaticArgs);
3212 /// \brief Create a basic block that will call the trap intrinsic, and emit a
3213 /// conditional branch to it, for the -ftrapv checks.
3214 void EmitTrapCheck(llvm::Value *Checked);
3216 /// \brief Emit a call to trap or debugtrap and attach function attribute
3217 /// "trap-func-name" if specified.
3218 llvm::CallInst *EmitTrapCall(llvm::Intrinsic::ID IntrID);
3220 /// \brief Emit a cross-DSO CFI failure handling function.
3221 void EmitCfiCheckFail();
3223 /// \brief Create a check for a function parameter that may potentially be
3224 /// declared as non-null.
3225 void EmitNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc,
3226 const FunctionDecl *FD, unsigned ParmNum);
3228 /// EmitCallArg - Emit a single call argument.
3229 void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
3231 /// EmitDelegateCallArg - We are performing a delegate call; that
3232 /// is, the current function is delegating to another one. Produce
3233 /// a r-value suitable for passing the given parameter.
3234 void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
3235 SourceLocation loc);
3237 /// SetFPAccuracy - Set the minimum required accuracy of the given floating
3238 /// point operation, expressed as the maximum relative error in ulp.
3239 void SetFPAccuracy(llvm::Value *Val, float Accuracy);
3242 llvm::MDNode *getRangeForLoadFromType(QualType Ty);
3243 void EmitReturnOfRValue(RValue RV, QualType Ty);
3245 void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
3247 llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4>
3248 DeferredReplacements;
3250 /// Set the address of a local variable.
3251 void setAddrOfLocalVar(const VarDecl *VD, Address Addr) {
3252 assert(!LocalDeclMap.count(VD) && "Decl already exists in LocalDeclMap!");
3253 LocalDeclMap.insert({VD, Addr});
3256 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
3257 /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
3259 /// \param AI - The first function argument of the expansion.
3260 void ExpandTypeFromArgs(QualType Ty, LValue Dst,
3261 SmallVectorImpl<llvm::Value *>::iterator &AI);
3263 /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg
3264 /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
3265 /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
3266 void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
3267 SmallVectorImpl<llvm::Value *> &IRCallArgs,
3268 unsigned &IRCallArgPos);
3270 llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
3271 const Expr *InputExpr, std::string &ConstraintStr);
3273 llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
3274 LValue InputValue, QualType InputType,
3275 std::string &ConstraintStr,
3276 SourceLocation Loc);
3278 /// \brief Attempts to statically evaluate the object size of E. If that
3279 /// fails, emits code to figure the size of E out for us. This is
3280 /// pass_object_size aware.
3281 llvm::Value *evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type,
3282 llvm::IntegerType *ResType);
3284 /// \brief Emits the size of E, as required by __builtin_object_size. This
3285 /// function is aware of pass_object_size parameters, and will act accordingly
3286 /// if E is a parameter with the pass_object_size attribute.
3287 llvm::Value *emitBuiltinObjectSize(const Expr *E, unsigned Type,
3288 llvm::IntegerType *ResType);
3292 // Determine whether the given argument is an Objective-C method
3293 // that may have type parameters in its signature.
3294 static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method) {
3295 const DeclContext *dc = method->getDeclContext();
3296 if (const ObjCInterfaceDecl *classDecl= dyn_cast<ObjCInterfaceDecl>(dc)) {
3297 return classDecl->getTypeParamListAsWritten();
3300 if (const ObjCCategoryDecl *catDecl = dyn_cast<ObjCCategoryDecl>(dc)) {
3301 return catDecl->getTypeParamList();
3307 template<typename T>
3308 static bool isObjCMethodWithTypeParams(const T *) { return false; }
3311 /// EmitCallArgs - Emit call arguments for a function.
3312 template <typename T>
3313 void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo,
3314 llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
3315 const FunctionDecl *CalleeDecl = nullptr,
3316 unsigned ParamsToSkip = 0) {
3317 SmallVector<QualType, 16> ArgTypes;
3318 CallExpr::const_arg_iterator Arg = ArgRange.begin();
3320 assert((ParamsToSkip == 0 || CallArgTypeInfo) &&
3321 "Can't skip parameters if type info is not provided");
3322 if (CallArgTypeInfo) {
3324 bool isGenericMethod = isObjCMethodWithTypeParams(CallArgTypeInfo);
3327 // First, use the argument types that the type info knows about
3328 for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip,
3329 E = CallArgTypeInfo->param_type_end();
3330 I != E; ++I, ++Arg) {
3331 assert(Arg != ArgRange.end() && "Running over edge of argument list!");
3332 assert((isGenericMethod ||
3333 ((*I)->isVariablyModifiedType() ||
3334 (*I).getNonReferenceType()->isObjCRetainableType() ||
3336 .getCanonicalType((*I).getNonReferenceType())
3339 .getCanonicalType((*Arg)->getType())
3341 "type mismatch in call argument!");
3342 ArgTypes.push_back(*I);
3346 // Either we've emitted all the call args, or we have a call to variadic
3348 assert((Arg == ArgRange.end() || !CallArgTypeInfo ||
3349 CallArgTypeInfo->isVariadic()) &&
3350 "Extra arguments in non-variadic function!");
3352 // If we still have any arguments, emit them using the type of the argument.
3353 for (auto *A : llvm::make_range(Arg, ArgRange.end()))
3354 ArgTypes.push_back(getVarArgType(A));
3356 EmitCallArgs(Args, ArgTypes, ArgRange, CalleeDecl, ParamsToSkip);
3359 void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes,
3360 llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
3361 const FunctionDecl *CalleeDecl = nullptr,
3362 unsigned ParamsToSkip = 0);
3364 /// EmitPointerWithAlignment - Given an expression with a pointer
3365 /// type, emit the value and compute our best estimate of the
3366 /// alignment of the pointee.
3368 /// Note that this function will conservatively fall back on the type
3371 /// \param Source - If non-null, this will be initialized with
3372 /// information about the source of the alignment. Note that this
3373 /// function will conservatively fall back on the type when it
3374 /// doesn't recognize the expression, which means that sometimes
3376 /// a worst-case One
3377 /// reasonable way to use this information is when there's a
3378 /// language guarantee that the pointer must be aligned to some
3379 /// stricter value, and we're simply trying to ensure that
3380 /// sufficiently obvious uses of under-aligned objects don't get
3381 /// miscompiled; for example, a placement new into the address of
3382 /// a local variable. In such a case, it's quite reasonable to
3383 /// just ignore the returned alignment when it isn't from an
3384 /// explicit source.
3385 Address EmitPointerWithAlignment(const Expr *Addr,
3386 AlignmentSource *Source = nullptr);
3388 void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK);
3391 QualType getVarArgType(const Expr *Arg);
3393 const TargetCodeGenInfo &getTargetHooks() const {
3394 return CGM.getTargetCodeGenInfo();
3397 void EmitDeclMetadata();
3399 BlockByrefHelpers *buildByrefHelpers(llvm::StructType &byrefType,
3400 const AutoVarEmission &emission);
3402 void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
3404 llvm::Value *GetValueForARMHint(unsigned BuiltinID);
3407 /// Helper class with most of the code for saving a value for a
3408 /// conditional expression cleanup.
3409 struct DominatingLLVMValue {
3410 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
3412 /// Answer whether the given value needs extra work to be saved.
3413 static bool needsSaving(llvm::Value *value) {
3414 // If it's not an instruction, we don't need to save.
3415 if (!isa<llvm::Instruction>(value)) return false;
3417 // If it's an instruction in the entry block, we don't need to save.
3418 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
3419 return (block != &block->getParent()->getEntryBlock());
3422 /// Try to save the given value.
3423 static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
3424 if (!needsSaving(value)) return saved_type(value, false);
3426 // Otherwise, we need an alloca.
3427 auto align = CharUnits::fromQuantity(
3428 CGF.CGM.getDataLayout().getPrefTypeAlignment(value->getType()));
3430 CGF.CreateTempAlloca(value->getType(), align, "cond-cleanup.save");
3431 CGF.Builder.CreateStore(value, alloca);
3433 return saved_type(alloca.getPointer(), true);
3436 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
3437 // If the value says it wasn't saved, trust that it's still dominating.
3438 if (!value.getInt()) return value.getPointer();
3440 // Otherwise, it should be an alloca instruction, as set up in save().
3441 auto alloca = cast<llvm::AllocaInst>(value.getPointer());
3442 return CGF.Builder.CreateAlignedLoad(alloca, alloca->getAlignment());
3446 /// A partial specialization of DominatingValue for llvm::Values that
3447 /// might be llvm::Instructions.
3448 template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
3450 static type restore(CodeGenFunction &CGF, saved_type value) {
3451 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
3455 /// A specialization of DominatingValue for Address.
3456 template <> struct DominatingValue<Address> {
3457 typedef Address type;
3460 DominatingLLVMValue::saved_type SavedValue;
3461 CharUnits Alignment;
3464 static bool needsSaving(type value) {
3465 return DominatingLLVMValue::needsSaving(value.getPointer());
3467 static saved_type save(CodeGenFunction &CGF, type value) {
3468 return { DominatingLLVMValue::save(CGF, value.getPointer()),
3469 value.getAlignment() };
3471 static type restore(CodeGenFunction &CGF, saved_type value) {
3472 return Address(DominatingLLVMValue::restore(CGF, value.SavedValue),
3477 /// A specialization of DominatingValue for RValue.
3478 template <> struct DominatingValue<RValue> {
3479 typedef RValue type;
3481 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
3482 AggregateAddress, ComplexAddress };
3486 unsigned Align : 29;
3487 saved_type(llvm::Value *v, Kind k, unsigned a = 0)
3488 : Value(v), K(k), Align(a) {}
3491 static bool needsSaving(RValue value);
3492 static saved_type save(CodeGenFunction &CGF, RValue value);
3493 RValue restore(CodeGenFunction &CGF);
3495 // implementations in CGCleanup.cpp
3498 static bool needsSaving(type value) {
3499 return saved_type::needsSaving(value);
3501 static saved_type save(CodeGenFunction &CGF, type value) {
3502 return saved_type::save(CGF, value);
3504 static type restore(CodeGenFunction &CGF, saved_type value) {
3505 return value.restore(CGF);
3509 } // end namespace CodeGen
3510 } // end namespace clang