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/Type.h"
28 #include "clang/Basic/ABI.h"
29 #include "clang/Basic/CapturedStmt.h"
30 #include "clang/Basic/OpenMPKinds.h"
31 #include "clang/Basic/TargetInfo.h"
32 #include "clang/Frontend/CodeGenOptions.h"
33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/IR/ValueHandle.h"
37 #include "llvm/Support/Debug.h"
53 class CXXDestructorDecl;
54 class CXXForRangeStmt;
58 class EnumConstantDecl;
60 class FunctionProtoType;
62 class ObjCContainerDecl;
63 class ObjCInterfaceDecl;
66 class ObjCImplementationDecl;
67 class ObjCPropertyImplDecl;
69 class TargetCodeGenInfo;
71 class ObjCForCollectionStmt;
73 class ObjCAtThrowStmt;
74 class ObjCAtSynchronizedStmt;
75 class ObjCAutoreleasePoolStmt;
84 class BlockFieldFlags;
86 /// The kind of evaluation to perform on values of a particular
87 /// type. Basically, is the code in CGExprScalar, CGExprComplex, or
90 /// TODO: should vectors maybe be split out into their own thing?
91 enum TypeEvaluationKind {
97 /// CodeGenFunction - This class organizes the per-function state that is used
98 /// while generating LLVM code.
99 class CodeGenFunction : public CodeGenTypeCache {
100 CodeGenFunction(const CodeGenFunction &) = delete;
101 void operator=(const CodeGenFunction &) = delete;
103 friend class CGCXXABI;
105 /// A jump destination is an abstract label, branching to which may
106 /// require a jump out through normal cleanups.
108 JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
109 JumpDest(llvm::BasicBlock *Block,
110 EHScopeStack::stable_iterator Depth,
112 : Block(Block), ScopeDepth(Depth), Index(Index) {}
114 bool isValid() const { return Block != nullptr; }
115 llvm::BasicBlock *getBlock() const { return Block; }
116 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
117 unsigned getDestIndex() const { return Index; }
119 // This should be used cautiously.
120 void setScopeDepth(EHScopeStack::stable_iterator depth) {
125 llvm::BasicBlock *Block;
126 EHScopeStack::stable_iterator ScopeDepth;
130 CodeGenModule &CGM; // Per-module state.
131 const TargetInfo &Target;
133 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
134 LoopInfoStack LoopStack;
137 /// \brief CGBuilder insert helper. This function is called after an
138 /// instruction is created using Builder.
139 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
140 llvm::BasicBlock *BB,
141 llvm::BasicBlock::iterator InsertPt) const;
143 /// CurFuncDecl - Holds the Decl for the current outermost
144 /// non-closure context.
145 const Decl *CurFuncDecl;
146 /// CurCodeDecl - This is the inner-most code context, which includes blocks.
147 const Decl *CurCodeDecl;
148 const CGFunctionInfo *CurFnInfo;
150 llvm::Function *CurFn;
152 /// CurGD - The GlobalDecl for the current function being compiled.
155 /// PrologueCleanupDepth - The cleanup depth enclosing all the
156 /// cleanups associated with the parameters.
157 EHScopeStack::stable_iterator PrologueCleanupDepth;
159 /// ReturnBlock - Unified return block.
160 JumpDest ReturnBlock;
162 /// ReturnValue - The temporary alloca to hold the return value. This is null
163 /// iff the function has no return value.
164 llvm::Value *ReturnValue;
166 /// AllocaInsertPoint - This is an instruction in the entry block before which
167 /// we prefer to insert allocas.
168 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
170 /// \brief API for captured statement code generation.
171 class CGCapturedStmtInfo {
173 explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
174 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
175 explicit CGCapturedStmtInfo(const CapturedStmt &S,
176 CapturedRegionKind K = CR_Default)
177 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
179 RecordDecl::field_iterator Field =
180 S.getCapturedRecordDecl()->field_begin();
181 for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
183 I != E; ++I, ++Field) {
184 if (I->capturesThis())
185 CXXThisFieldDecl = *Field;
186 else if (I->capturesVariable())
187 CaptureFields[I->getCapturedVar()] = *Field;
191 virtual ~CGCapturedStmtInfo();
193 CapturedRegionKind getKind() const { return Kind; }
195 virtual void setContextValue(llvm::Value *V) { ThisValue = V; }
196 // \brief Retrieve the value of the context parameter.
197 virtual llvm::Value *getContextValue() const { return ThisValue; }
199 /// \brief Lookup the captured field decl for a variable.
200 virtual const FieldDecl *lookup(const VarDecl *VD) const {
201 return CaptureFields.lookup(VD);
204 bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; }
205 virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
207 static bool classof(const CGCapturedStmtInfo *) {
211 /// \brief Emit the captured statement body.
212 virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) {
213 CGF.incrementProfileCounter(S);
217 /// \brief Get the name of the capture helper.
218 virtual StringRef getHelperName() const { return "__captured_stmt"; }
221 /// \brief The kind of captured statement being generated.
222 CapturedRegionKind Kind;
224 /// \brief Keep the map between VarDecl and FieldDecl.
225 llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
227 /// \brief The base address of the captured record, passed in as the first
228 /// argument of the parallel region function.
229 llvm::Value *ThisValue;
231 /// \brief Captured 'this' type.
232 FieldDecl *CXXThisFieldDecl;
234 CGCapturedStmtInfo *CapturedStmtInfo;
236 /// \brief RAII for correct setting/restoring of CapturedStmtInfo.
237 class CGCapturedStmtRAII {
239 CodeGenFunction &CGF;
240 CGCapturedStmtInfo *PrevCapturedStmtInfo;
242 CGCapturedStmtRAII(CodeGenFunction &CGF,
243 CGCapturedStmtInfo *NewCapturedStmtInfo)
244 : CGF(CGF), PrevCapturedStmtInfo(CGF.CapturedStmtInfo) {
245 CGF.CapturedStmtInfo = NewCapturedStmtInfo;
247 ~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; }
250 /// BoundsChecking - Emit run-time bounds checks. Higher values mean
251 /// potentially higher performance penalties.
252 unsigned char BoundsChecking;
254 /// \brief Sanitizers enabled for this function.
255 SanitizerSet SanOpts;
257 /// \brief True if CodeGen currently emits code implementing sanitizer checks.
258 bool IsSanitizerScope;
260 /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope.
261 class SanitizerScope {
262 CodeGenFunction *CGF;
264 SanitizerScope(CodeGenFunction *CGF);
268 /// In C++, whether we are code generating a thunk. This controls whether we
269 /// should emit cleanups.
272 /// In ARC, whether we should autorelease the return value.
273 bool AutoreleaseResult;
275 /// Whether we processed a Microsoft-style asm block during CodeGen. These can
276 /// potentially set the return value.
279 /// True if the current function is an outlined SEH helper. This can be a
280 /// finally block or filter expression.
281 bool IsOutlinedSEHHelper;
283 const CodeGen::CGBlockInfo *BlockInfo;
284 llvm::Value *BlockPointer;
286 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
287 FieldDecl *LambdaThisCaptureField;
289 /// \brief A mapping from NRVO variables to the flags used to indicate
290 /// when the NRVO has been applied to this variable.
291 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
293 EHScopeStack EHStack;
294 llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
295 llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack;
297 /// Header for data within LifetimeExtendedCleanupStack.
298 struct LifetimeExtendedCleanupHeader {
299 /// The size of the following cleanup object.
301 /// The kind of cleanup to push: a value from the CleanupKind enumeration.
304 size_t getSize() const { return Size; }
305 CleanupKind getKind() const { return Kind; }
308 /// i32s containing the indexes of the cleanup destinations.
309 llvm::AllocaInst *NormalCleanupDest;
311 unsigned NextCleanupDestIndex;
313 /// FirstBlockInfo - The head of a singly-linked-list of block layouts.
314 CGBlockInfo *FirstBlockInfo;
316 /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
317 llvm::BasicBlock *EHResumeBlock;
319 /// The exception slot. All landing pads write the current exception pointer
320 /// into this alloca.
321 llvm::Value *ExceptionSlot;
323 /// The selector slot. Under the MandatoryCleanup model, all landing pads
324 /// write the current selector value into this alloca.
325 llvm::AllocaInst *EHSelectorSlot;
327 /// A stack of exception code slots. Entering an __except block pushes a slot
328 /// on the stack and leaving pops one. The __exception_code() intrinsic loads
329 /// a value from the top of the stack.
330 SmallVector<llvm::Value *, 1> SEHCodeSlotStack;
332 /// Value returned by __exception_info intrinsic.
333 llvm::Value *SEHInfo = nullptr;
335 /// Emits a landing pad for the current EH stack.
336 llvm::BasicBlock *EmitLandingPad();
338 llvm::BasicBlock *getInvokeDestImpl();
341 typename DominatingValue<T>::saved_type saveValueInCond(T value) {
342 return DominatingValue<T>::save(*this, value);
346 /// ObjCEHValueStack - Stack of Objective-C exception values, used for
348 SmallVector<llvm::Value*, 8> ObjCEHValueStack;
350 /// A class controlling the emission of a finally block.
352 /// Where the catchall's edge through the cleanup should go.
353 JumpDest RethrowDest;
355 /// A function to call to enter the catch.
356 llvm::Constant *BeginCatchFn;
358 /// An i1 variable indicating whether or not the @finally is
359 /// running for an exception.
360 llvm::AllocaInst *ForEHVar;
362 /// An i8* variable into which the exception pointer to rethrow
364 llvm::AllocaInst *SavedExnVar;
367 void enter(CodeGenFunction &CGF, const Stmt *Finally,
368 llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
369 llvm::Constant *rethrowFn);
370 void exit(CodeGenFunction &CGF);
373 /// Returns true inside SEH __try blocks.
374 bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); }
376 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
377 /// current full-expression. Safe against the possibility that
378 /// we're currently inside a conditionally-evaluated expression.
379 template <class T, class... As>
380 void pushFullExprCleanup(CleanupKind kind, As... A) {
381 // If we're not in a conditional branch, or if none of the
382 // arguments requires saving, then use the unconditional cleanup.
383 if (!isInConditionalBranch())
384 return EHStack.pushCleanup<T>(kind, A...);
386 // Stash values in a tuple so we can guarantee the order of saves.
387 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
388 SavedTuple Saved{saveValueInCond(A)...};
390 typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
391 EHStack.pushCleanupTuple<CleanupType>(kind, Saved);
392 initFullExprCleanup();
395 /// \brief Queue a cleanup to be pushed after finishing the current
397 template <class T, class... As>
398 void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) {
399 assert(!isInConditionalBranch() && "can't defer conditional cleanup");
401 LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind };
403 size_t OldSize = LifetimeExtendedCleanupStack.size();
404 LifetimeExtendedCleanupStack.resize(
405 LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size);
407 static_assert(sizeof(Header) % llvm::AlignOf<T>::Alignment == 0,
408 "Cleanup will be allocated on misaligned address");
409 char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
410 new (Buffer) LifetimeExtendedCleanupHeader(Header);
411 new (Buffer + sizeof(Header)) T(A...);
414 /// Set up the last cleaup that was pushed as a conditional
415 /// full-expression cleanup.
416 void initFullExprCleanup();
418 /// PushDestructorCleanup - Push a cleanup to call the
419 /// complete-object destructor of an object of the given type at the
420 /// given address. Does nothing if T is not a C++ class type with a
421 /// non-trivial destructor.
422 void PushDestructorCleanup(QualType T, llvm::Value *Addr);
424 /// PushDestructorCleanup - Push a cleanup to call the
425 /// complete-object variant of the given destructor on the object at
426 /// the given address.
427 void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
430 /// PopCleanupBlock - Will pop the cleanup entry on the stack and
431 /// process all branch fixups.
432 void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
434 /// DeactivateCleanupBlock - Deactivates the given cleanup block.
435 /// The block cannot be reactivated. Pops it if it's the top of the
438 /// \param DominatingIP - An instruction which is known to
439 /// dominate the current IP (if set) and which lies along
440 /// all paths of execution between the current IP and the
441 /// the point at which the cleanup comes into scope.
442 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
443 llvm::Instruction *DominatingIP);
445 /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
446 /// Cannot be used to resurrect a deactivated cleanup.
448 /// \param DominatingIP - An instruction which is known to
449 /// dominate the current IP (if set) and which lies along
450 /// all paths of execution between the current IP and the
451 /// the point at which the cleanup comes into scope.
452 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
453 llvm::Instruction *DominatingIP);
455 /// \brief Enters a new scope for capturing cleanups, all of which
456 /// will be executed once the scope is exited.
457 class RunCleanupsScope {
458 EHScopeStack::stable_iterator CleanupStackDepth;
459 size_t LifetimeExtendedCleanupStackSize;
460 bool OldDidCallStackSave;
465 RunCleanupsScope(const RunCleanupsScope &) = delete;
466 void operator=(const RunCleanupsScope &) = delete;
469 CodeGenFunction& CGF;
472 /// \brief Enter a new cleanup scope.
473 explicit RunCleanupsScope(CodeGenFunction &CGF)
474 : PerformCleanup(true), CGF(CGF)
476 CleanupStackDepth = CGF.EHStack.stable_begin();
477 LifetimeExtendedCleanupStackSize =
478 CGF.LifetimeExtendedCleanupStack.size();
479 OldDidCallStackSave = CGF.DidCallStackSave;
480 CGF.DidCallStackSave = false;
483 /// \brief Exit this cleanup scope, emitting any accumulated
485 ~RunCleanupsScope() {
486 if (PerformCleanup) {
487 CGF.DidCallStackSave = OldDidCallStackSave;
488 CGF.PopCleanupBlocks(CleanupStackDepth,
489 LifetimeExtendedCleanupStackSize);
493 /// \brief Determine whether this scope requires any cleanups.
494 bool requiresCleanups() const {
495 return CGF.EHStack.stable_begin() != CleanupStackDepth;
498 /// \brief Force the emission of cleanups now, instead of waiting
499 /// until this object is destroyed.
500 void ForceCleanup() {
501 assert(PerformCleanup && "Already forced cleanup");
502 CGF.DidCallStackSave = OldDidCallStackSave;
503 CGF.PopCleanupBlocks(CleanupStackDepth,
504 LifetimeExtendedCleanupStackSize);
505 PerformCleanup = false;
509 class LexicalScope : public RunCleanupsScope {
511 SmallVector<const LabelDecl*, 4> Labels;
512 LexicalScope *ParentScope;
514 LexicalScope(const LexicalScope &) = delete;
515 void operator=(const LexicalScope &) = delete;
518 /// \brief Enter a new cleanup scope.
519 explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
520 : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
521 CGF.CurLexicalScope = this;
522 if (CGDebugInfo *DI = CGF.getDebugInfo())
523 DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
526 void addLabel(const LabelDecl *label) {
527 assert(PerformCleanup && "adding label to dead scope?");
528 Labels.push_back(label);
531 /// \brief Exit this cleanup scope, emitting any accumulated
534 if (CGDebugInfo *DI = CGF.getDebugInfo())
535 DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
537 // If we should perform a cleanup, force them now. Note that
538 // this ends the cleanup scope before rescoping any labels.
539 if (PerformCleanup) {
540 ApplyDebugLocation DL(CGF, Range.getEnd());
545 /// \brief Force the emission of cleanups now, instead of waiting
546 /// until this object is destroyed.
547 void ForceCleanup() {
548 CGF.CurLexicalScope = ParentScope;
549 RunCleanupsScope::ForceCleanup();
555 void rescopeLabels();
558 /// \brief The scope used to remap some variables as private in the OpenMP
559 /// loop body (or other captured region emitted without outlining), and to
560 /// restore old vars back on exit.
561 class OMPPrivateScope : public RunCleanupsScope {
562 typedef llvm::DenseMap<const VarDecl *, llvm::Value *> VarDeclMapTy;
563 VarDeclMapTy SavedLocals;
564 VarDeclMapTy SavedPrivates;
567 OMPPrivateScope(const OMPPrivateScope &) = delete;
568 void operator=(const OMPPrivateScope &) = delete;
571 /// \brief Enter a new OpenMP private scope.
572 explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
574 /// \brief Registers \a LocalVD variable as a private and apply \a
575 /// PrivateGen function for it to generate corresponding private variable.
576 /// \a PrivateGen returns an address of the generated private variable.
577 /// \return true if the variable is registered as private, false if it has
578 /// been privatized already.
580 addPrivate(const VarDecl *LocalVD,
581 const std::function<llvm::Value *()> &PrivateGen) {
582 assert(PerformCleanup && "adding private to dead scope");
583 if (SavedLocals.count(LocalVD) > 0) return false;
584 SavedLocals[LocalVD] = CGF.LocalDeclMap.lookup(LocalVD);
585 CGF.LocalDeclMap.erase(LocalVD);
586 SavedPrivates[LocalVD] = PrivateGen();
587 CGF.LocalDeclMap[LocalVD] = SavedLocals[LocalVD];
591 /// \brief Privatizes local variables previously registered as private.
592 /// Registration is separate from the actual privatization to allow
593 /// initializers use values of the original variables, not the private one.
594 /// This is important, for example, if the private variable is a class
595 /// variable initialized by a constructor that references other private
596 /// variables. But at initialization original variables must be used, not
598 /// \return true if at least one variable was privatized, false otherwise.
600 for (auto VDPair : SavedPrivates) {
601 CGF.LocalDeclMap[VDPair.first] = VDPair.second;
603 SavedPrivates.clear();
604 return !SavedLocals.empty();
607 void ForceCleanup() {
608 RunCleanupsScope::ForceCleanup();
609 // Remap vars back to the original values.
610 for (auto I : SavedLocals) {
611 CGF.LocalDeclMap[I.first] = I.second;
616 /// \brief Exit scope - all the mapped variables are restored.
623 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
624 /// that have been added.
625 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
627 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
628 /// that have been added, then adds all lifetime-extended cleanups from
629 /// the given position to the stack.
630 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
631 size_t OldLifetimeExtendedStackSize);
633 void ResolveBranchFixups(llvm::BasicBlock *Target);
635 /// The given basic block lies in the current EH scope, but may be a
636 /// target of a potentially scope-crossing jump; get a stable handle
637 /// to which we can perform this jump later.
638 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
639 return JumpDest(Target,
640 EHStack.getInnermostNormalCleanup(),
641 NextCleanupDestIndex++);
644 /// The given basic block lies in the current EH scope, but may be a
645 /// target of a potentially scope-crossing jump; get a stable handle
646 /// to which we can perform this jump later.
647 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
648 return getJumpDestInCurrentScope(createBasicBlock(Name));
651 /// EmitBranchThroughCleanup - Emit a branch from the current insert
652 /// block through the normal cleanup handling code (if any) and then
654 void EmitBranchThroughCleanup(JumpDest Dest);
656 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
657 /// specified destination obviously has no cleanups to run. 'false' is always
658 /// a conservatively correct answer for this method.
659 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
661 /// popCatchScope - Pops the catch scope at the top of the EHScope
662 /// stack, emitting any required code (other than the catch handlers
664 void popCatchScope();
666 llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
667 llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
669 /// An object to manage conditionally-evaluated expressions.
670 class ConditionalEvaluation {
671 llvm::BasicBlock *StartBB;
674 ConditionalEvaluation(CodeGenFunction &CGF)
675 : StartBB(CGF.Builder.GetInsertBlock()) {}
677 void begin(CodeGenFunction &CGF) {
678 assert(CGF.OutermostConditional != this);
679 if (!CGF.OutermostConditional)
680 CGF.OutermostConditional = this;
683 void end(CodeGenFunction &CGF) {
684 assert(CGF.OutermostConditional != nullptr);
685 if (CGF.OutermostConditional == this)
686 CGF.OutermostConditional = nullptr;
689 /// Returns a block which will be executed prior to each
690 /// evaluation of the conditional code.
691 llvm::BasicBlock *getStartingBlock() const {
696 /// isInConditionalBranch - Return true if we're currently emitting
697 /// one branch or the other of a conditional expression.
698 bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
700 void setBeforeOutermostConditional(llvm::Value *value, llvm::Value *addr) {
701 assert(isInConditionalBranch());
702 llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
703 new llvm::StoreInst(value, addr, &block->back());
706 /// An RAII object to record that we're evaluating a statement
708 class StmtExprEvaluation {
709 CodeGenFunction &CGF;
711 /// We have to save the outermost conditional: cleanups in a
712 /// statement expression aren't conditional just because the
714 ConditionalEvaluation *SavedOutermostConditional;
717 StmtExprEvaluation(CodeGenFunction &CGF)
718 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
719 CGF.OutermostConditional = nullptr;
722 ~StmtExprEvaluation() {
723 CGF.OutermostConditional = SavedOutermostConditional;
724 CGF.EnsureInsertPoint();
728 /// An object which temporarily prevents a value from being
729 /// destroyed by aggressive peephole optimizations that assume that
730 /// all uses of a value have been realized in the IR.
731 class PeepholeProtection {
732 llvm::Instruction *Inst;
733 friend class CodeGenFunction;
736 PeepholeProtection() : Inst(nullptr) {}
739 /// A non-RAII class containing all the information about a bound
740 /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for
741 /// this which makes individual mappings very simple; using this
742 /// class directly is useful when you have a variable number of
743 /// opaque values or don't want the RAII functionality for some
745 class OpaqueValueMappingData {
746 const OpaqueValueExpr *OpaqueValue;
748 CodeGenFunction::PeepholeProtection Protection;
750 OpaqueValueMappingData(const OpaqueValueExpr *ov,
752 : OpaqueValue(ov), BoundLValue(boundLValue) {}
754 OpaqueValueMappingData() : OpaqueValue(nullptr) {}
756 static bool shouldBindAsLValue(const Expr *expr) {
757 // gl-values should be bound as l-values for obvious reasons.
758 // Records should be bound as l-values because IR generation
759 // always keeps them in memory. Expressions of function type
760 // act exactly like l-values but are formally required to be
762 return expr->isGLValue() ||
763 expr->getType()->isFunctionType() ||
764 hasAggregateEvaluationKind(expr->getType());
767 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
768 const OpaqueValueExpr *ov,
770 if (shouldBindAsLValue(ov))
771 return bind(CGF, ov, CGF.EmitLValue(e));
772 return bind(CGF, ov, CGF.EmitAnyExpr(e));
775 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
776 const OpaqueValueExpr *ov,
778 assert(shouldBindAsLValue(ov));
779 CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
780 return OpaqueValueMappingData(ov, true);
783 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
784 const OpaqueValueExpr *ov,
786 assert(!shouldBindAsLValue(ov));
787 CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
789 OpaqueValueMappingData data(ov, false);
791 // Work around an extremely aggressive peephole optimization in
792 // EmitScalarConversion which assumes that all other uses of a
794 data.Protection = CGF.protectFromPeepholes(rv);
799 bool isValid() const { return OpaqueValue != nullptr; }
800 void clear() { OpaqueValue = nullptr; }
802 void unbind(CodeGenFunction &CGF) {
803 assert(OpaqueValue && "no data to unbind!");
806 CGF.OpaqueLValues.erase(OpaqueValue);
808 CGF.OpaqueRValues.erase(OpaqueValue);
809 CGF.unprotectFromPeepholes(Protection);
814 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
815 class OpaqueValueMapping {
816 CodeGenFunction &CGF;
817 OpaqueValueMappingData Data;
820 static bool shouldBindAsLValue(const Expr *expr) {
821 return OpaqueValueMappingData::shouldBindAsLValue(expr);
824 /// Build the opaque value mapping for the given conditional
825 /// operator if it's the GNU ?: extension. This is a common
826 /// enough pattern that the convenience operator is really
829 OpaqueValueMapping(CodeGenFunction &CGF,
830 const AbstractConditionalOperator *op) : CGF(CGF) {
831 if (isa<ConditionalOperator>(op))
835 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
836 Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
840 OpaqueValueMapping(CodeGenFunction &CGF,
841 const OpaqueValueExpr *opaqueValue,
843 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
846 OpaqueValueMapping(CodeGenFunction &CGF,
847 const OpaqueValueExpr *opaqueValue,
849 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
857 ~OpaqueValueMapping() {
858 if (Data.isValid()) Data.unbind(CGF);
862 /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
863 /// number that holds the value.
864 std::pair<llvm::Type *, unsigned>
865 getByRefValueLLVMField(const ValueDecl *VD) const;
867 /// BuildBlockByrefAddress - Computes address location of the
868 /// variable which is declared as __block.
869 llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
872 CGDebugInfo *DebugInfo;
873 bool DisableDebugInfo;
875 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
876 /// calling llvm.stacksave for multiple VLAs in the same scope.
877 bool DidCallStackSave;
879 /// IndirectBranch - The first time an indirect goto is seen we create a block
880 /// with an indirect branch. Every time we see the address of a label taken,
881 /// we add the label to the indirect goto. Every subsequent indirect goto is
882 /// codegen'd as a jump to the IndirectBranch's basic block.
883 llvm::IndirectBrInst *IndirectBranch;
885 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
887 typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
888 DeclMapTy LocalDeclMap;
890 /// Track escaped local variables with auto storage. Used during SEH
891 /// outlining to produce a call to llvm.localescape.
892 llvm::DenseMap<llvm::AllocaInst *, int> EscapedLocals;
894 /// LabelMap - This keeps track of the LLVM basic block for each C label.
895 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
897 // BreakContinueStack - This keeps track of where break and continue
898 // statements should jump to.
899 struct BreakContinue {
900 BreakContinue(JumpDest Break, JumpDest Continue)
901 : BreakBlock(Break), ContinueBlock(Continue) {}
904 JumpDest ContinueBlock;
906 SmallVector<BreakContinue, 8> BreakContinueStack;
910 /// Calculate branch weights appropriate for PGO data
911 llvm::MDNode *createProfileWeights(uint64_t TrueCount, uint64_t FalseCount);
912 llvm::MDNode *createProfileWeights(ArrayRef<uint64_t> Weights);
913 llvm::MDNode *createProfileWeightsForLoop(const Stmt *Cond,
917 /// Increment the profiler's counter for the given statement.
918 void incrementProfileCounter(const Stmt *S) {
919 if (CGM.getCodeGenOpts().ProfileInstrGenerate)
920 PGO.emitCounterIncrement(Builder, S);
921 PGO.setCurrentStmt(S);
924 /// Get the profiler's count for the given statement.
925 uint64_t getProfileCount(const Stmt *S) {
926 Optional<uint64_t> Count = PGO.getStmtCount(S);
927 if (!Count.hasValue())
932 /// Set the profiler's current count.
933 void setCurrentProfileCount(uint64_t Count) {
934 PGO.setCurrentRegionCount(Count);
937 /// Get the profiler's current count. This is generally the count for the most
938 /// recently incremented counter.
939 uint64_t getCurrentProfileCount() {
940 return PGO.getCurrentRegionCount();
945 /// SwitchInsn - This is nearest current switch instruction. It is null if
946 /// current context is not in a switch.
947 llvm::SwitchInst *SwitchInsn;
948 /// The branch weights of SwitchInsn when doing instrumentation based PGO.
949 SmallVector<uint64_t, 16> *SwitchWeights;
951 /// CaseRangeBlock - This block holds if condition check for last case
952 /// statement range in current switch instruction.
953 llvm::BasicBlock *CaseRangeBlock;
955 /// OpaqueLValues - Keeps track of the current set of opaque value
957 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
958 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
960 // VLASizeMap - This keeps track of the associated size for each VLA type.
961 // We track this by the size expression rather than the type itself because
962 // in certain situations, like a const qualifier applied to an VLA typedef,
963 // multiple VLA types can share the same size expression.
964 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
965 // enter/leave scopes.
966 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
968 /// A block containing a single 'unreachable' instruction. Created
969 /// lazily by getUnreachableBlock().
970 llvm::BasicBlock *UnreachableBlock;
972 /// Counts of the number return expressions in the function.
973 unsigned NumReturnExprs;
975 /// Count the number of simple (constant) return expressions in the function.
976 unsigned NumSimpleReturnExprs;
978 /// The last regular (non-return) debug location (breakpoint) in the function.
979 SourceLocation LastStopPoint;
982 /// A scope within which we are constructing the fields of an object which
983 /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
984 /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
985 class FieldConstructionScope {
987 FieldConstructionScope(CodeGenFunction &CGF, llvm::Value *This)
988 : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
989 CGF.CXXDefaultInitExprThis = This;
991 ~FieldConstructionScope() {
992 CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
996 CodeGenFunction &CGF;
997 llvm::Value *OldCXXDefaultInitExprThis;
1000 /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
1001 /// is overridden to be the object under construction.
1002 class CXXDefaultInitExprScope {
1004 CXXDefaultInitExprScope(CodeGenFunction &CGF)
1005 : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue) {
1006 CGF.CXXThisValue = CGF.CXXDefaultInitExprThis;
1008 ~CXXDefaultInitExprScope() {
1009 CGF.CXXThisValue = OldCXXThisValue;
1013 CodeGenFunction &CGF;
1014 llvm::Value *OldCXXThisValue;
1018 /// CXXThisDecl - When generating code for a C++ member function,
1019 /// this will hold the implicit 'this' declaration.
1020 ImplicitParamDecl *CXXABIThisDecl;
1021 llvm::Value *CXXABIThisValue;
1022 llvm::Value *CXXThisValue;
1024 /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1025 /// this expression.
1026 llvm::Value *CXXDefaultInitExprThis;
1028 /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1029 /// destructor, this will hold the implicit argument (e.g. VTT).
1030 ImplicitParamDecl *CXXStructorImplicitParamDecl;
1031 llvm::Value *CXXStructorImplicitParamValue;
1033 /// OutermostConditional - Points to the outermost active
1034 /// conditional control. This is used so that we know if a
1035 /// temporary should be destroyed conditionally.
1036 ConditionalEvaluation *OutermostConditional;
1038 /// The current lexical scope.
1039 LexicalScope *CurLexicalScope;
1041 /// The current source location that should be used for exception
1043 SourceLocation CurEHLocation;
1045 /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
1046 /// type as well as the field number that contains the actual data.
1047 llvm::DenseMap<const ValueDecl *, std::pair<llvm::Type *,
1048 unsigned> > ByRefValueInfo;
1050 llvm::BasicBlock *TerminateLandingPad;
1051 llvm::BasicBlock *TerminateHandler;
1052 llvm::BasicBlock *TrapBB;
1054 /// Add a kernel metadata node to the named metadata node 'opencl.kernels'.
1055 /// In the kernel metadata node, reference the kernel function and metadata
1056 /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2):
1057 /// - A node for the vec_type_hint(<type>) qualifier contains string
1058 /// "vec_type_hint", an undefined value of the <type> data type,
1059 /// and a Boolean that is true if the <type> is integer and signed.
1060 /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string
1061 /// "work_group_size_hint", and three 32-bit integers X, Y and Z.
1062 /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string
1063 /// "reqd_work_group_size", and three 32-bit integers X, Y and Z.
1064 void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1065 llvm::Function *Fn);
1068 CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
1071 CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1072 ASTContext &getContext() const { return CGM.getContext(); }
1073 CGDebugInfo *getDebugInfo() {
1074 if (DisableDebugInfo)
1078 void disableDebugInfo() { DisableDebugInfo = true; }
1079 void enableDebugInfo() { DisableDebugInfo = false; }
1081 bool shouldUseFusedARCCalls() {
1082 return CGM.getCodeGenOpts().OptimizationLevel == 0;
1085 const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
1087 /// Returns a pointer to the function's exception object and selector slot,
1088 /// which is assigned in every landing pad.
1089 llvm::Value *getExceptionSlot();
1090 llvm::Value *getEHSelectorSlot();
1092 /// Returns the contents of the function's exception object and selector
1094 llvm::Value *getExceptionFromSlot();
1095 llvm::Value *getSelectorFromSlot();
1097 llvm::Value *getNormalCleanupDestSlot();
1099 llvm::BasicBlock *getUnreachableBlock() {
1100 if (!UnreachableBlock) {
1101 UnreachableBlock = createBasicBlock("unreachable");
1102 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1104 return UnreachableBlock;
1107 llvm::BasicBlock *getInvokeDest() {
1108 if (!EHStack.requiresLandingPad()) return nullptr;
1109 return getInvokeDestImpl();
1112 bool currentFunctionUsesSEHTry() const {
1113 const auto *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
1114 return FD && FD->usesSEHTry();
1117 const TargetInfo &getTarget() const { return Target; }
1118 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
1120 //===--------------------------------------------------------------------===//
1122 //===--------------------------------------------------------------------===//
1124 typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty);
1126 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1127 llvm::Value *arrayEndPointer,
1128 QualType elementType,
1129 Destroyer *destroyer);
1130 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1131 llvm::Value *arrayEnd,
1132 QualType elementType,
1133 Destroyer *destroyer);
1135 void pushDestroy(QualType::DestructionKind dtorKind,
1136 llvm::Value *addr, QualType type);
1137 void pushEHDestroy(QualType::DestructionKind dtorKind,
1138 llvm::Value *addr, QualType type);
1139 void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type,
1140 Destroyer *destroyer, bool useEHCleanupForArray);
1141 void pushLifetimeExtendedDestroy(CleanupKind kind, llvm::Value *addr,
1142 QualType type, Destroyer *destroyer,
1143 bool useEHCleanupForArray);
1144 void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1145 llvm::Value *CompletePtr,
1146 QualType ElementType);
1147 void pushStackRestore(CleanupKind kind, llvm::Value *SPMem);
1148 void emitDestroy(llvm::Value *addr, QualType type, Destroyer *destroyer,
1149 bool useEHCleanupForArray);
1150 llvm::Function *generateDestroyHelper(llvm::Constant *addr, QualType type,
1151 Destroyer *destroyer,
1152 bool useEHCleanupForArray,
1154 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
1155 QualType type, Destroyer *destroyer,
1156 bool checkZeroLength, bool useEHCleanup);
1158 Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
1160 /// Determines whether an EH cleanup is required to destroy a type
1161 /// with the given destruction kind.
1162 bool needsEHCleanup(QualType::DestructionKind kind) {
1164 case QualType::DK_none:
1166 case QualType::DK_cxx_destructor:
1167 case QualType::DK_objc_weak_lifetime:
1168 return getLangOpts().Exceptions;
1169 case QualType::DK_objc_strong_lifetime:
1170 return getLangOpts().Exceptions &&
1171 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1173 llvm_unreachable("bad destruction kind");
1176 CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1177 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1180 //===--------------------------------------------------------------------===//
1182 //===--------------------------------------------------------------------===//
1184 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
1186 void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
1188 /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
1189 void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1190 const ObjCPropertyImplDecl *PID);
1191 void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
1192 const ObjCPropertyImplDecl *propImpl,
1193 const ObjCMethodDecl *GetterMothodDecl,
1194 llvm::Constant *AtomicHelperFn);
1196 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1197 ObjCMethodDecl *MD, bool ctor);
1199 /// GenerateObjCSetter - Synthesize an Objective-C property setter function
1200 /// for the given property.
1201 void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1202 const ObjCPropertyImplDecl *PID);
1203 void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1204 const ObjCPropertyImplDecl *propImpl,
1205 llvm::Constant *AtomicHelperFn);
1206 bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
1207 bool IvarTypeWithAggrGCObjects(QualType Ty);
1209 //===--------------------------------------------------------------------===//
1211 //===--------------------------------------------------------------------===//
1213 llvm::Value *EmitBlockLiteral(const BlockExpr *);
1214 llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
1215 static void destroyBlockInfos(CGBlockInfo *info);
1216 llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
1217 const CGBlockInfo &Info,
1219 llvm::Constant *BlockVarLayout);
1221 llvm::Function *GenerateBlockFunction(GlobalDecl GD,
1222 const CGBlockInfo &Info,
1223 const DeclMapTy &ldm,
1224 bool IsLambdaConversionToBlock);
1226 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1227 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
1228 llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
1229 const ObjCPropertyImplDecl *PID);
1230 llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
1231 const ObjCPropertyImplDecl *PID);
1232 llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
1234 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1236 class AutoVarEmission;
1238 void emitByrefStructureInit(const AutoVarEmission &emission);
1239 void enterByrefCleanup(const AutoVarEmission &emission);
1241 llvm::Value *LoadBlockStruct() {
1242 assert(BlockPointer && "no block pointer set!");
1243 return BlockPointer;
1246 void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
1247 void AllocateBlockDecl(const DeclRefExpr *E);
1248 llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
1249 llvm::Type *BuildByRefType(const VarDecl *var);
1251 void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1252 const CGFunctionInfo &FnInfo);
1253 /// \brief Emit code for the start of a function.
1254 /// \param Loc The location to be associated with the function.
1255 /// \param StartLoc The location of the function body.
1256 void StartFunction(GlobalDecl GD,
1259 const CGFunctionInfo &FnInfo,
1260 const FunctionArgList &Args,
1261 SourceLocation Loc = SourceLocation(),
1262 SourceLocation StartLoc = SourceLocation());
1264 void EmitConstructorBody(FunctionArgList &Args);
1265 void EmitDestructorBody(FunctionArgList &Args);
1266 void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
1267 void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body);
1268 void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S);
1270 void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
1271 CallArgList &CallArgs);
1272 void EmitLambdaToBlockPointerBody(FunctionArgList &Args);
1273 void EmitLambdaBlockInvokeBody();
1274 void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
1275 void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD);
1276 void EmitAsanPrologueOrEpilogue(bool Prologue);
1278 /// \brief Emit the unified return block, trying to avoid its emission when
1280 /// \return The debug location of the user written return statement if the
1281 /// return block is is avoided.
1282 llvm::DebugLoc EmitReturnBlock();
1284 /// FinishFunction - Complete IR generation of the current function. It is
1285 /// legal to call this function even if there is no current insertion point.
1286 void FinishFunction(SourceLocation EndLoc=SourceLocation());
1288 void StartThunk(llvm::Function *Fn, GlobalDecl GD,
1289 const CGFunctionInfo &FnInfo);
1291 void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk);
1293 /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
1294 void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr,
1295 llvm::Value *Callee);
1297 /// Generate a thunk for the given method.
1298 void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1299 GlobalDecl GD, const ThunkInfo &Thunk);
1301 llvm::Function *GenerateVarArgsThunk(llvm::Function *Fn,
1302 const CGFunctionInfo &FnInfo,
1303 GlobalDecl GD, const ThunkInfo &Thunk);
1305 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1306 FunctionArgList &Args);
1308 void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init,
1309 ArrayRef<VarDecl *> ArrayIndexes);
1311 /// InitializeVTablePointer - Initialize the vtable pointer of the given
1314 void InitializeVTablePointer(BaseSubobject Base,
1315 const CXXRecordDecl *NearestVBase,
1316 CharUnits OffsetFromNearestVBase,
1317 const CXXRecordDecl *VTableClass);
1319 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1320 void InitializeVTablePointers(BaseSubobject Base,
1321 const CXXRecordDecl *NearestVBase,
1322 CharUnits OffsetFromNearestVBase,
1323 bool BaseIsNonVirtualPrimaryBase,
1324 const CXXRecordDecl *VTableClass,
1325 VisitedVirtualBasesSetTy& VBases);
1327 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
1329 /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1331 llvm::Value *GetVTablePtr(llvm::Value *This, llvm::Type *Ty);
1333 enum CFITypeCheckKind {
1337 CFITCK_UnrelatedCast,
1340 /// \brief Derived is the presumed address of an object of type T after a
1341 /// cast. If T is a polymorphic class type, emit a check that the virtual
1342 /// table for Derived belongs to a class derived from T.
1343 void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived,
1344 bool MayBeNull, CFITypeCheckKind TCK,
1345 SourceLocation Loc);
1347 /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
1348 /// If vptr CFI is enabled, emit a check that VTable is valid.
1349 void EmitVTablePtrCheckForCall(const CXXMethodDecl *MD, llvm::Value *VTable,
1350 CFITypeCheckKind TCK, SourceLocation Loc);
1352 /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for
1353 /// RD using llvm.bitset.test.
1354 void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable,
1355 CFITypeCheckKind TCK, SourceLocation Loc);
1357 /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
1358 /// expr can be devirtualized.
1359 bool CanDevirtualizeMemberFunctionCall(const Expr *Base,
1360 const CXXMethodDecl *MD);
1362 /// EnterDtorCleanups - Enter the cleanups necessary to complete the
1363 /// given phase of destruction for a destructor. The end result
1364 /// should call destructors on members and base classes in reverse
1365 /// order of their construction.
1366 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
1368 /// ShouldInstrumentFunction - Return true if the current function should be
1369 /// instrumented with __cyg_profile_func_* calls
1370 bool ShouldInstrumentFunction();
1372 /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1373 /// instrumentation function with the current function and the call site, if
1374 /// function instrumentation is enabled.
1375 void EmitFunctionInstrumentation(const char *Fn);
1377 /// EmitMCountInstrumentation - Emit call to .mcount.
1378 void EmitMCountInstrumentation();
1380 /// EmitFunctionProlog - Emit the target specific LLVM code to load the
1381 /// arguments for the given function. This is also responsible for naming the
1382 /// LLVM function arguments.
1383 void EmitFunctionProlog(const CGFunctionInfo &FI,
1385 const FunctionArgList &Args);
1387 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
1388 /// given temporary.
1389 void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
1390 SourceLocation EndLoc);
1392 /// EmitStartEHSpec - Emit the start of the exception spec.
1393 void EmitStartEHSpec(const Decl *D);
1395 /// EmitEndEHSpec - Emit the end of the exception spec.
1396 void EmitEndEHSpec(const Decl *D);
1398 /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1399 llvm::BasicBlock *getTerminateLandingPad();
1401 /// getTerminateHandler - Return a handler (not a landing pad, just
1402 /// a catch handler) that just calls terminate. This is used when
1403 /// a terminate scope encloses a try.
1404 llvm::BasicBlock *getTerminateHandler();
1406 llvm::Type *ConvertTypeForMem(QualType T);
1407 llvm::Type *ConvertType(QualType T);
1408 llvm::Type *ConvertType(const TypeDecl *T) {
1409 return ConvertType(getContext().getTypeDeclType(T));
1412 /// LoadObjCSelf - Load the value of self. This function is only valid while
1413 /// generating code for an Objective-C method.
1414 llvm::Value *LoadObjCSelf();
1416 /// TypeOfSelfObject - Return type of object that this self represents.
1417 QualType TypeOfSelfObject();
1419 /// hasAggregateLLVMType - Return true if the specified AST type will map into
1420 /// an aggregate LLVM type or is void.
1421 static TypeEvaluationKind getEvaluationKind(QualType T);
1423 static bool hasScalarEvaluationKind(QualType T) {
1424 return getEvaluationKind(T) == TEK_Scalar;
1427 static bool hasAggregateEvaluationKind(QualType T) {
1428 return getEvaluationKind(T) == TEK_Aggregate;
1431 /// createBasicBlock - Create an LLVM basic block.
1432 llvm::BasicBlock *createBasicBlock(const Twine &name = "",
1433 llvm::Function *parent = nullptr,
1434 llvm::BasicBlock *before = nullptr) {
1436 return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
1438 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
1442 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
1444 JumpDest getJumpDestForLabel(const LabelDecl *S);
1446 /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1447 /// another basic block, simplify it. This assumes that no other code could
1448 /// potentially reference the basic block.
1449 void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1451 /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
1452 /// adding a fall-through branch from the current insert block if
1453 /// necessary. It is legal to call this function even if there is no current
1454 /// insertion point.
1456 /// IsFinished - If true, indicates that the caller has finished emitting
1457 /// branches to the given block and does not expect to emit code into it. This
1458 /// means the block can be ignored if it is unreachable.
1459 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
1461 /// EmitBlockAfterUses - Emit the given block somewhere hopefully
1462 /// near its uses, and leave the insertion point in it.
1463 void EmitBlockAfterUses(llvm::BasicBlock *BB);
1465 /// EmitBranch - Emit a branch to the specified basic block from the current
1466 /// insert block, taking care to avoid creation of branches from dummy
1467 /// blocks. It is legal to call this function even if there is no current
1468 /// insertion point.
1470 /// This function clears the current insertion point. The caller should follow
1471 /// calls to this function with calls to Emit*Block prior to generation new
1473 void EmitBranch(llvm::BasicBlock *Block);
1475 /// HaveInsertPoint - True if an insertion point is defined. If not, this
1476 /// indicates that the current code being emitted is unreachable.
1477 bool HaveInsertPoint() const {
1478 return Builder.GetInsertBlock() != nullptr;
1481 /// EnsureInsertPoint - Ensure that an insertion point is defined so that
1482 /// emitted IR has a place to go. Note that by definition, if this function
1483 /// creates a block then that block is unreachable; callers may do better to
1484 /// detect when no insertion point is defined and simply skip IR generation.
1485 void EnsureInsertPoint() {
1486 if (!HaveInsertPoint())
1487 EmitBlock(createBasicBlock());
1490 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1491 /// specified stmt yet.
1492 void ErrorUnsupported(const Stmt *S, const char *Type);
1494 //===--------------------------------------------------------------------===//
1496 //===--------------------------------------------------------------------===//
1498 LValue MakeAddrLValue(llvm::Value *V, QualType T,
1499 CharUnits Alignment = CharUnits()) {
1500 return LValue::MakeAddr(V, T, Alignment, getContext(),
1501 CGM.getTBAAInfo(T));
1504 LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
1506 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
1507 /// block. The caller is responsible for setting an appropriate alignment on
1509 llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
1510 const Twine &Name = "tmp");
1512 /// InitTempAlloca - Provide an initial value for the given alloca.
1513 void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1515 /// CreateIRTemp - Create a temporary IR object of the given type, with
1516 /// appropriate alignment. This routine should only be used when an temporary
1517 /// value needs to be stored into an alloca (for example, to avoid explicit
1518 /// PHI construction), but the type is the IR type, not the type appropriate
1519 /// for storing in memory.
1520 llvm::AllocaInst *CreateIRTemp(QualType T, const Twine &Name = "tmp");
1522 /// CreateMemTemp - Create a temporary memory object of the given type, with
1523 /// appropriate alignment.
1524 llvm::AllocaInst *CreateMemTemp(QualType T, const Twine &Name = "tmp");
1526 /// CreateAggTemp - Create a temporary memory object for the given
1528 AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
1529 CharUnits Alignment = getContext().getTypeAlignInChars(T);
1530 return AggValueSlot::forAddr(CreateMemTemp(T, Name), Alignment,
1532 AggValueSlot::IsNotDestructed,
1533 AggValueSlot::DoesNotNeedGCBarriers,
1534 AggValueSlot::IsNotAliased);
1537 /// CreateInAllocaTmp - Create a temporary memory object for the given
1539 AggValueSlot CreateInAllocaTmp(QualType T, const Twine &Name = "inalloca");
1541 /// Emit a cast to void* in the appropriate address space.
1542 llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1544 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
1545 /// expression and compare the result against zero, returning an Int1Ty value.
1546 llvm::Value *EvaluateExprAsBool(const Expr *E);
1548 /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
1549 void EmitIgnoredExpr(const Expr *E);
1551 /// EmitAnyExpr - Emit code to compute the specified expression which can have
1552 /// any type. The result is returned as an RValue struct. If this is an
1553 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
1554 /// the result should be returned.
1556 /// \param ignoreResult True if the resulting value isn't used.
1557 RValue EmitAnyExpr(const Expr *E,
1558 AggValueSlot aggSlot = AggValueSlot::ignored(),
1559 bool ignoreResult = false);
1561 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
1562 // or the value of the expression, depending on how va_list is defined.
1563 llvm::Value *EmitVAListRef(const Expr *E);
1565 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
1566 /// always be accessible even if no aggregate location is provided.
1567 RValue EmitAnyExprToTemp(const Expr *E);
1569 /// EmitAnyExprToMem - Emits the code necessary to evaluate an
1570 /// arbitrary expression into the given memory location.
1571 void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
1572 Qualifiers Quals, bool IsInitializer);
1574 void EmitAnyExprToExn(const Expr *E, llvm::Value *Addr);
1576 /// EmitExprAsInit - Emits the code necessary to initialize a
1577 /// location in memory with the given initializer.
1578 void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1579 bool capturedByInit);
1581 /// hasVolatileMember - returns true if aggregate type has a volatile
1583 bool hasVolatileMember(QualType T) {
1584 if (const RecordType *RT = T->getAs<RecordType>()) {
1585 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
1586 return RD->hasVolatileMember();
1590 /// EmitAggregateCopy - Emit an aggregate assignment.
1592 /// The difference to EmitAggregateCopy is that tail padding is not copied.
1593 /// This is required for correctness when assigning non-POD structures in C++.
1594 void EmitAggregateAssign(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1596 bool IsVolatile = hasVolatileMember(EltTy);
1597 EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, CharUnits::Zero(),
1601 void EmitAggregateCopyCtor(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1602 QualType DestTy, QualType SrcTy) {
1603 CharUnits DestTypeAlign = getContext().getTypeAlignInChars(DestTy);
1604 CharUnits SrcTypeAlign = getContext().getTypeAlignInChars(SrcTy);
1605 EmitAggregateCopy(DestPtr, SrcPtr, SrcTy, /*IsVolatile=*/false,
1606 std::min(DestTypeAlign, SrcTypeAlign),
1607 /*IsAssignment=*/false);
1610 /// EmitAggregateCopy - Emit an aggregate copy.
1612 /// \param isVolatile - True iff either the source or the destination is
1614 /// \param isAssignment - If false, allow padding to be copied. This often
1615 /// yields more efficient.
1616 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1617 QualType EltTy, bool isVolatile=false,
1618 CharUnits Alignment = CharUnits::Zero(),
1619 bool isAssignment = false);
1621 /// StartBlock - Start new block named N. If insert block is a dummy block
1623 void StartBlock(const char *N);
1625 /// GetAddrOfLocalVar - Return the address of a local variable.
1626 llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
1627 llvm::Value *Res = LocalDeclMap[VD];
1628 assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
1632 /// getOpaqueLValueMapping - Given an opaque value expression (which
1633 /// must be mapped to an l-value), return its mapping.
1634 const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
1635 assert(OpaqueValueMapping::shouldBindAsLValue(e));
1637 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
1638 it = OpaqueLValues.find(e);
1639 assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
1643 /// getOpaqueRValueMapping - Given an opaque value expression (which
1644 /// must be mapped to an r-value), return its mapping.
1645 const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
1646 assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1648 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
1649 it = OpaqueRValues.find(e);
1650 assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
1654 /// getAccessedFieldNo - Given an encoded value and a result number, return
1655 /// the input field number being accessed.
1656 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
1658 llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
1659 llvm::BasicBlock *GetIndirectGotoBlock();
1661 /// EmitNullInitialization - Generate code to set a value of the given type to
1662 /// null, If the type contains data member pointers, they will be initialized
1663 /// to -1 in accordance with the Itanium C++ ABI.
1664 void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
1666 // EmitVAArg - Generate code to get an argument from the passed in pointer
1667 // and update it accordingly. The return value is a pointer to the argument.
1668 // FIXME: We should be able to get rid of this method and use the va_arg
1669 // instruction in LLVM instead once it works well enough.
1670 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
1672 /// emitArrayLength - Compute the length of an array, even if it's a
1673 /// VLA, and drill down to the base element type.
1674 llvm::Value *emitArrayLength(const ArrayType *arrayType,
1676 llvm::Value *&addr);
1678 /// EmitVLASize - Capture all the sizes for the VLA expressions in
1679 /// the given variably-modified type and store them in the VLASizeMap.
1681 /// This function can be called with a null (unreachable) insert point.
1682 void EmitVariablyModifiedType(QualType Ty);
1684 /// getVLASize - Returns an LLVM value that corresponds to the size,
1685 /// in non-variably-sized elements, of a variable length array type,
1686 /// plus that largest non-variably-sized element type. Assumes that
1687 /// the type has already been emitted with EmitVariablyModifiedType.
1688 std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1689 std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
1691 /// LoadCXXThis - Load the value of 'this'. This function is only valid while
1692 /// generating code for an C++ member function.
1693 llvm::Value *LoadCXXThis() {
1694 assert(CXXThisValue && "no 'this' value for this function");
1695 return CXXThisValue;
1698 /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1700 // FIXME: Every place that calls LoadCXXVTT is something
1701 // that needs to be abstracted properly.
1702 llvm::Value *LoadCXXVTT() {
1703 assert(CXXStructorImplicitParamValue && "no VTT value for this function");
1704 return CXXStructorImplicitParamValue;
1707 /// LoadCXXStructorImplicitParam - Load the implicit parameter
1708 /// for a constructor/destructor.
1709 llvm::Value *LoadCXXStructorImplicitParam() {
1710 assert(CXXStructorImplicitParamValue &&
1711 "no implicit argument value for this function");
1712 return CXXStructorImplicitParamValue;
1715 /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
1716 /// complete class to the given direct base.
1718 GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
1719 const CXXRecordDecl *Derived,
1720 const CXXRecordDecl *Base,
1721 bool BaseIsVirtual);
1723 /// GetAddressOfBaseClass - This function will add the necessary delta to the
1724 /// load of 'this' and returns address of the base class.
1725 llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
1726 const CXXRecordDecl *Derived,
1727 CastExpr::path_const_iterator PathBegin,
1728 CastExpr::path_const_iterator PathEnd,
1729 bool NullCheckValue, SourceLocation Loc);
1731 llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
1732 const CXXRecordDecl *Derived,
1733 CastExpr::path_const_iterator PathBegin,
1734 CastExpr::path_const_iterator PathEnd,
1735 bool NullCheckValue);
1737 /// GetVTTParameter - Return the VTT parameter that should be passed to a
1738 /// base constructor/destructor with virtual bases.
1739 /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
1740 /// to ItaniumCXXABI.cpp together with all the references to VTT.
1741 llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
1744 void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1745 CXXCtorType CtorType,
1746 const FunctionArgList &Args,
1747 SourceLocation Loc);
1748 // It's important not to confuse this and the previous function. Delegating
1749 // constructors are the C++0x feature. The constructor delegate optimization
1750 // is used to reduce duplication in the base and complete consturctors where
1751 // they are substantially the same.
1752 void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1753 const FunctionArgList &Args);
1754 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1755 bool ForVirtualBase, bool Delegating,
1756 llvm::Value *This, const CXXConstructExpr *E);
1758 void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1759 llvm::Value *This, llvm::Value *Src,
1760 const CXXConstructExpr *E);
1762 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1763 const ConstantArrayType *ArrayTy,
1764 llvm::Value *ArrayPtr,
1765 const CXXConstructExpr *E,
1766 bool ZeroInitialization = false);
1768 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1769 llvm::Value *NumElements,
1770 llvm::Value *ArrayPtr,
1771 const CXXConstructExpr *E,
1772 bool ZeroInitialization = false);
1774 static Destroyer destroyCXXObject;
1776 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
1777 bool ForVirtualBase, bool Delegating,
1780 void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
1781 llvm::Type *ElementTy, llvm::Value *NewPtr,
1782 llvm::Value *NumElements,
1783 llvm::Value *AllocSizeWithoutCookie);
1785 void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
1788 llvm::Value *EmitLifetimeStart(uint64_t Size, llvm::Value *Addr);
1789 void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr);
1791 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
1792 void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
1794 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
1797 RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
1798 const Expr *Arg, bool IsDelete);
1800 llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
1801 llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
1802 llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E);
1804 /// \brief Situations in which we might emit a check for the suitability of a
1805 /// pointer or glvalue.
1806 enum TypeCheckKind {
1807 /// Checking the operand of a load. Must be suitably sized and aligned.
1809 /// Checking the destination of a store. Must be suitably sized and aligned.
1811 /// Checking the bound value in a reference binding. Must be suitably sized
1812 /// and aligned, but is not required to refer to an object (until the
1813 /// reference is used), per core issue 453.
1814 TCK_ReferenceBinding,
1815 /// Checking the object expression in a non-static data member access. Must
1816 /// be an object within its lifetime.
1818 /// Checking the 'this' pointer for a call to a non-static member function.
1819 /// Must be an object within its lifetime.
1821 /// Checking the 'this' pointer for a constructor call.
1822 TCK_ConstructorCall,
1823 /// Checking the operand of a static_cast to a derived pointer type. Must be
1824 /// null or an object within its lifetime.
1825 TCK_DowncastPointer,
1826 /// Checking the operand of a static_cast to a derived reference type. Must
1827 /// be an object within its lifetime.
1828 TCK_DowncastReference,
1829 /// Checking the operand of a cast to a base object. Must be suitably sized
1832 /// Checking the operand of a cast to a virtual base object. Must be an
1833 /// object within its lifetime.
1834 TCK_UpcastToVirtualBase
1837 /// \brief Whether any type-checking sanitizers are enabled. If \c false,
1838 /// calls to EmitTypeCheck can be skipped.
1839 bool sanitizePerformTypeCheck() const;
1841 /// \brief Emit a check that \p V is the address of storage of the
1842 /// appropriate size and alignment for an object of type \p Type.
1843 void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
1844 QualType Type, CharUnits Alignment = CharUnits::Zero(),
1845 bool SkipNullCheck = false);
1847 /// \brief Emit a check that \p Base points into an array object, which
1848 /// we can access at index \p Index. \p Accessed should be \c false if we
1849 /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
1850 void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
1851 QualType IndexType, bool Accessed);
1853 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1854 bool isInc, bool isPre);
1855 ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1856 bool isInc, bool isPre);
1858 void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment,
1859 llvm::Value *OffsetValue = nullptr) {
1860 Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
1864 //===--------------------------------------------------------------------===//
1865 // Declaration Emission
1866 //===--------------------------------------------------------------------===//
1868 /// EmitDecl - Emit a declaration.
1870 /// This function can be called with a null (unreachable) insert point.
1871 void EmitDecl(const Decl &D);
1873 /// EmitVarDecl - Emit a local variable declaration.
1875 /// This function can be called with a null (unreachable) insert point.
1876 void EmitVarDecl(const VarDecl &D);
1878 void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1879 bool capturedByInit);
1880 void EmitScalarInit(llvm::Value *init, LValue lvalue);
1882 typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1883 llvm::Value *Address);
1885 /// \brief Determine whether the given initializer is trivial in the sense
1886 /// that it requires no code to be generated.
1887 bool isTrivialInitializer(const Expr *Init);
1889 /// EmitAutoVarDecl - Emit an auto variable declaration.
1891 /// This function can be called with a null (unreachable) insert point.
1892 void EmitAutoVarDecl(const VarDecl &D);
1894 class AutoVarEmission {
1895 friend class CodeGenFunction;
1897 const VarDecl *Variable;
1899 /// The alignment of the variable.
1900 CharUnits Alignment;
1902 /// The address of the alloca. Null if the variable was emitted
1903 /// as a global constant.
1904 llvm::Value *Address;
1906 llvm::Value *NRVOFlag;
1908 /// True if the variable is a __block variable.
1911 /// True if the variable is of aggregate type and has a constant
1913 bool IsConstantAggregate;
1915 /// Non-null if we should use lifetime annotations.
1916 llvm::Value *SizeForLifetimeMarkers;
1919 AutoVarEmission(Invalid) : Variable(nullptr) {}
1921 AutoVarEmission(const VarDecl &variable)
1922 : Variable(&variable), Address(nullptr), NRVOFlag(nullptr),
1923 IsByRef(false), IsConstantAggregate(false),
1924 SizeForLifetimeMarkers(nullptr) {}
1926 bool wasEmittedAsGlobal() const { return Address == nullptr; }
1929 static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
1931 bool useLifetimeMarkers() const {
1932 return SizeForLifetimeMarkers != nullptr;
1934 llvm::Value *getSizeForLifetimeMarkers() const {
1935 assert(useLifetimeMarkers());
1936 return SizeForLifetimeMarkers;
1939 /// Returns the raw, allocated address, which is not necessarily
1940 /// the address of the object itself.
1941 llvm::Value *getAllocatedAddress() const {
1945 /// Returns the address of the object within this declaration.
1946 /// Note that this does not chase the forwarding pointer for
1948 llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
1949 if (!IsByRef) return Address;
1951 auto F = CGF.getByRefValueLLVMField(Variable);
1952 return CGF.Builder.CreateStructGEP(F.first, Address, F.second,
1953 Variable->getNameAsString());
1956 AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
1957 void EmitAutoVarInit(const AutoVarEmission &emission);
1958 void EmitAutoVarCleanups(const AutoVarEmission &emission);
1959 void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
1960 QualType::DestructionKind dtorKind);
1962 void EmitStaticVarDecl(const VarDecl &D,
1963 llvm::GlobalValue::LinkageTypes Linkage);
1965 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
1966 void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, bool ArgIsPointer,
1969 /// protectFromPeepholes - Protect a value that we're intending to
1970 /// store to the side, but which will probably be used later, from
1971 /// aggressive peepholing optimizations that might delete it.
1973 /// Pass the result to unprotectFromPeepholes to declare that
1974 /// protection is no longer required.
1976 /// There's no particular reason why this shouldn't apply to
1977 /// l-values, it's just that no existing peepholes work on pointers.
1978 PeepholeProtection protectFromPeepholes(RValue rvalue);
1979 void unprotectFromPeepholes(PeepholeProtection protection);
1981 //===--------------------------------------------------------------------===//
1982 // Statement Emission
1983 //===--------------------------------------------------------------------===//
1985 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
1986 void EmitStopPoint(const Stmt *S);
1988 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
1989 /// this function even if there is no current insertion point.
1991 /// This function may clear the current insertion point; callers should use
1992 /// EnsureInsertPoint if they wish to subsequently generate code without first
1993 /// calling EmitBlock, EmitBranch, or EmitStmt.
1994 void EmitStmt(const Stmt *S);
1996 /// EmitSimpleStmt - Try to emit a "simple" statement which does not
1997 /// necessarily require an insertion point or debug information; typically
1998 /// because the statement amounts to a jump or a container of other
2001 /// \return True if the statement was handled.
2002 bool EmitSimpleStmt(const Stmt *S);
2004 llvm::Value *EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
2005 AggValueSlot AVS = AggValueSlot::ignored());
2006 llvm::Value *EmitCompoundStmtWithoutScope(const CompoundStmt &S,
2007 bool GetLast = false,
2009 AggValueSlot::ignored());
2011 /// EmitLabel - Emit the block for the given label. It is legal to call this
2012 /// function even if there is no current insertion point.
2013 void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
2015 void EmitLabelStmt(const LabelStmt &S);
2016 void EmitAttributedStmt(const AttributedStmt &S);
2017 void EmitGotoStmt(const GotoStmt &S);
2018 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
2019 void EmitIfStmt(const IfStmt &S);
2021 void EmitCondBrHints(llvm::LLVMContext &Context, llvm::BranchInst *CondBr,
2022 ArrayRef<const Attr *> Attrs);
2023 void EmitWhileStmt(const WhileStmt &S,
2024 ArrayRef<const Attr *> Attrs = None);
2025 void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
2026 void EmitForStmt(const ForStmt &S,
2027 ArrayRef<const Attr *> Attrs = None);
2028 void EmitReturnStmt(const ReturnStmt &S);
2029 void EmitDeclStmt(const DeclStmt &S);
2030 void EmitBreakStmt(const BreakStmt &S);
2031 void EmitContinueStmt(const ContinueStmt &S);
2032 void EmitSwitchStmt(const SwitchStmt &S);
2033 void EmitDefaultStmt(const DefaultStmt &S);
2034 void EmitCaseStmt(const CaseStmt &S);
2035 void EmitCaseStmtRange(const CaseStmt &S);
2036 void EmitAsmStmt(const AsmStmt &S);
2038 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
2039 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
2040 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
2041 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
2042 void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
2044 void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
2045 void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
2047 void EmitCXXTryStmt(const CXXTryStmt &S);
2048 void EmitSEHTryStmt(const SEHTryStmt &S);
2049 void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
2050 void EnterSEHTryStmt(const SEHTryStmt &S);
2051 void ExitSEHTryStmt(const SEHTryStmt &S);
2053 void startOutlinedSEHHelper(CodeGenFunction &ParentCGF, bool IsFilter,
2054 const Stmt *OutlinedStmt);
2056 llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
2057 const SEHExceptStmt &Except);
2059 llvm::Function *GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF,
2060 const SEHFinallyStmt &Finally);
2062 void EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF,
2063 llvm::Value *ParentFP,
2064 llvm::Value *EntryEBP);
2065 llvm::Value *EmitSEHExceptionCode();
2066 llvm::Value *EmitSEHExceptionInfo();
2067 llvm::Value *EmitSEHAbnormalTermination();
2069 /// Scan the outlined statement for captures from the parent function. For
2070 /// each capture, mark the capture as escaped and emit a call to
2071 /// llvm.localrecover. Insert the localrecover result into the LocalDeclMap.
2072 void EmitCapturedLocals(CodeGenFunction &ParentCGF, const Stmt *OutlinedStmt,
2075 /// Recovers the address of a local in a parent function. ParentVar is the
2076 /// address of the variable used in the immediate parent function. It can
2077 /// either be an alloca or a call to llvm.localrecover if there are nested
2078 /// outlined functions. ParentFP is the frame pointer of the outermost parent
2080 llvm::Value *recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF,
2081 llvm::Value *ParentVar,
2082 llvm::Value *ParentFP);
2084 void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
2085 ArrayRef<const Attr *> Attrs = None);
2087 LValue InitCapturedStruct(const CapturedStmt &S);
2088 llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
2089 void GenerateCapturedStmtFunctionProlog(const CapturedStmt &S);
2090 llvm::Function *GenerateCapturedStmtFunctionEpilog(const CapturedStmt &S);
2091 llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
2092 llvm::Value *GenerateCapturedStmtArgument(const CapturedStmt &S);
2093 /// \brief Perform element by element copying of arrays with type \a
2094 /// OriginalType from \a SrcAddr to \a DestAddr using copying procedure
2095 /// generated by \a CopyGen.
2097 /// \param DestAddr Address of the destination array.
2098 /// \param SrcAddr Address of the source array.
2099 /// \param OriginalType Type of destination and source arrays.
2100 /// \param CopyGen Copying procedure that copies value of single array element
2101 /// to another single array element.
2102 void EmitOMPAggregateAssign(
2103 llvm::Value *DestAddr, llvm::Value *SrcAddr, QualType OriginalType,
2104 const llvm::function_ref<void(llvm::Value *, llvm::Value *)> &CopyGen);
2105 /// \brief Emit proper copying of data from one variable to another.
2107 /// \param OriginalType Original type of the copied variables.
2108 /// \param DestAddr Destination address.
2109 /// \param SrcAddr Source address.
2110 /// \param DestVD Destination variable used in \a CopyExpr (for arrays, has
2111 /// type of the base array element).
2112 /// \param SrcVD Source variable used in \a CopyExpr (for arrays, has type of
2113 /// the base array element).
2114 /// \param Copy Actual copygin expression for copying data from \a SrcVD to \a
2116 void EmitOMPCopy(CodeGenFunction &CGF, QualType OriginalType,
2117 llvm::Value *DestAddr, llvm::Value *SrcAddr,
2118 const VarDecl *DestVD, const VarDecl *SrcVD,
2120 /// \brief Emit atomic update code for constructs: \a X = \a X \a BO \a E or
2121 /// \a X = \a E \a BO \a E.
2123 /// \param X Value to be updated.
2124 /// \param E Update value.
2125 /// \param BO Binary operation for update operation.
2126 /// \param IsXLHSInRHSPart true if \a X is LHS in RHS part of the update
2127 /// expression, false otherwise.
2128 /// \param AO Atomic ordering of the generated atomic instructions.
2129 /// \param CommonGen Code generator for complex expressions that cannot be
2130 /// expressed through atomicrmw instruction.
2131 /// \returns <true, OldAtomicValue> if simple 'atomicrmw' instruction was
2132 /// generated, <false, RValue::get(nullptr)> otherwise.
2133 std::pair<bool, RValue> EmitOMPAtomicSimpleUpdateExpr(
2134 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
2135 llvm::AtomicOrdering AO, SourceLocation Loc,
2136 const llvm::function_ref<RValue(RValue)> &CommonGen);
2137 bool EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
2138 OMPPrivateScope &PrivateScope);
2139 void EmitOMPPrivateClause(const OMPExecutableDirective &D,
2140 OMPPrivateScope &PrivateScope);
2141 /// \brief Emit code for copyin clause in \a D directive. The next code is
2142 /// generated at the start of outlined functions for directives:
2144 /// threadprivate_var1 = master_threadprivate_var1;
2145 /// operator=(threadprivate_var2, master_threadprivate_var2);
2147 /// __kmpc_barrier(&loc, global_tid);
2150 /// \param D OpenMP directive possibly with 'copyin' clause(s).
2151 /// \returns true if at least one copyin variable is found, false otherwise.
2152 bool EmitOMPCopyinClause(const OMPExecutableDirective &D);
2153 /// \brief Emit initial code for lastprivate variables. If some variable is
2154 /// not also firstprivate, then the default initialization is used. Otherwise
2155 /// initialization of this variable is performed by EmitOMPFirstprivateClause
2158 /// \param D Directive that may have 'lastprivate' directives.
2159 /// \param PrivateScope Private scope for capturing lastprivate variables for
2160 /// proper codegen in internal captured statement.
2162 /// \returns true if there is at least one lastprivate variable, false
2164 bool EmitOMPLastprivateClauseInit(const OMPExecutableDirective &D,
2165 OMPPrivateScope &PrivateScope);
2166 /// \brief Emit final copying of lastprivate values to original variables at
2167 /// the end of the worksharing or simd directive.
2169 /// \param D Directive that has at least one 'lastprivate' directives.
2170 /// \param IsLastIterCond Boolean condition that must be set to 'i1 true' if
2171 /// it is the last iteration of the loop code in associated directive, or to
2172 /// 'i1 false' otherwise. If this item is nullptr, no final check is required.
2173 void EmitOMPLastprivateClauseFinal(const OMPExecutableDirective &D,
2174 llvm::Value *IsLastIterCond = nullptr);
2175 /// \brief Emit initial code for reduction variables. Creates reduction copies
2176 /// and initializes them with the values according to OpenMP standard.
2178 /// \param D Directive (possibly) with the 'reduction' clause.
2179 /// \param PrivateScope Private scope for capturing reduction variables for
2180 /// proper codegen in internal captured statement.
2182 void EmitOMPReductionClauseInit(const OMPExecutableDirective &D,
2183 OMPPrivateScope &PrivateScope);
2184 /// \brief Emit final update of reduction values to original variables at
2185 /// the end of the directive.
2187 /// \param D Directive that has at least one 'reduction' directives.
2188 void EmitOMPReductionClauseFinal(const OMPExecutableDirective &D);
2189 /// \brief Emit initial code for linear variables. Creates private copies
2190 /// and initializes them with the values according to OpenMP standard.
2192 /// \param D Directive (possibly) with the 'linear' clause.
2193 void EmitOMPLinearClauseInit(const OMPLoopDirective &D);
2195 void EmitOMPParallelDirective(const OMPParallelDirective &S);
2196 void EmitOMPSimdDirective(const OMPSimdDirective &S);
2197 void EmitOMPForDirective(const OMPForDirective &S);
2198 void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
2199 void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
2200 void EmitOMPSectionDirective(const OMPSectionDirective &S);
2201 void EmitOMPSingleDirective(const OMPSingleDirective &S);
2202 void EmitOMPMasterDirective(const OMPMasterDirective &S);
2203 void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
2204 void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
2205 void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
2206 void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
2207 void EmitOMPTaskDirective(const OMPTaskDirective &S);
2208 void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
2209 void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
2210 void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
2211 void EmitOMPTaskgroupDirective(const OMPTaskgroupDirective &S);
2212 void EmitOMPFlushDirective(const OMPFlushDirective &S);
2213 void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
2214 void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
2215 void EmitOMPTargetDirective(const OMPTargetDirective &S);
2216 void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
2218 EmitOMPCancellationPointDirective(const OMPCancellationPointDirective &S);
2219 void EmitOMPCancelDirective(const OMPCancelDirective &S);
2221 /// \brief Emit inner loop of the worksharing/simd construct.
2223 /// \param S Directive, for which the inner loop must be emitted.
2224 /// \param RequiresCleanup true, if directive has some associated private
2226 /// \param LoopCond Bollean condition for loop continuation.
2227 /// \param IncExpr Increment expression for loop control variable.
2228 /// \param BodyGen Generator for the inner body of the inner loop.
2229 /// \param PostIncGen Genrator for post-increment code (required for ordered
2230 /// loop directvies).
2231 void EmitOMPInnerLoop(
2232 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
2233 const Expr *IncExpr,
2234 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
2235 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen);
2237 JumpDest getOMPCancelDestination(OpenMPDirectiveKind Kind);
2241 /// Helpers for the OpenMP loop directives.
2242 void EmitOMPLoopBody(const OMPLoopDirective &D, JumpDest LoopExit);
2243 void EmitOMPSimdInit(const OMPLoopDirective &D);
2244 void EmitOMPSimdFinal(const OMPLoopDirective &D);
2245 /// \brief Emit code for the worksharing loop-based directive.
2246 /// \return true, if this construct has any lastprivate clause, false -
2248 bool EmitOMPWorksharingLoop(const OMPLoopDirective &S);
2249 void EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind,
2250 const OMPLoopDirective &S,
2251 OMPPrivateScope &LoopScope, bool Ordered,
2252 llvm::Value *LB, llvm::Value *UB, llvm::Value *ST,
2253 llvm::Value *IL, llvm::Value *Chunk);
2254 /// \brief Emit code for sections directive.
2255 OpenMPDirectiveKind EmitSections(const OMPExecutableDirective &S);
2259 //===--------------------------------------------------------------------===//
2260 // LValue Expression Emission
2261 //===--------------------------------------------------------------------===//
2263 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
2264 RValue GetUndefRValue(QualType Ty);
2266 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
2267 /// and issue an ErrorUnsupported style diagnostic (using the
2269 RValue EmitUnsupportedRValue(const Expr *E,
2272 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
2273 /// an ErrorUnsupported style diagnostic (using the provided Name).
2274 LValue EmitUnsupportedLValue(const Expr *E,
2277 /// EmitLValue - Emit code to compute a designator that specifies the location
2278 /// of the expression.
2280 /// This can return one of two things: a simple address or a bitfield
2281 /// reference. In either case, the LLVM Value* in the LValue structure is
2282 /// guaranteed to be an LLVM pointer type.
2284 /// If this returns a bitfield reference, nothing about the pointee type of
2285 /// the LLVM value is known: For example, it may not be a pointer to an
2288 /// If this returns a normal address, and if the lvalue's C type is fixed
2289 /// size, this method guarantees that the returned pointer type will point to
2290 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
2291 /// variable length type, this is not possible.
2293 LValue EmitLValue(const Expr *E);
2295 /// \brief Same as EmitLValue but additionally we generate checking code to
2296 /// guard against undefined behavior. This is only suitable when we know
2297 /// that the address will be used to access the object.
2298 LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
2300 RValue convertTempToRValue(llvm::Value *addr, QualType type,
2301 SourceLocation Loc);
2303 void EmitAtomicInit(Expr *E, LValue lvalue);
2305 bool LValueIsSuitableForInlineAtomic(LValue Src);
2306 bool typeIsSuitableForInlineAtomic(QualType Ty, bool IsVolatile) const;
2308 RValue EmitAtomicLoad(LValue LV, SourceLocation SL,
2309 AggValueSlot Slot = AggValueSlot::ignored());
2311 RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
2312 llvm::AtomicOrdering AO, bool IsVolatile = false,
2313 AggValueSlot slot = AggValueSlot::ignored());
2315 void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
2317 void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO,
2318 bool IsVolatile, bool isInit);
2320 std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange(
2321 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
2322 llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
2323 llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
2324 bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored());
2326 void EmitAtomicUpdate(LValue LVal, llvm::AtomicOrdering AO,
2327 const llvm::function_ref<RValue(RValue)> &UpdateOp,
2330 /// EmitToMemory - Change a scalar value from its value
2331 /// representation to its in-memory representation.
2332 llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
2334 /// EmitFromMemory - Change a scalar value from its memory
2335 /// representation to its value representation.
2336 llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
2338 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2339 /// care to appropriately convert from the memory representation to
2340 /// the LLVM value representation.
2341 llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
2342 unsigned Alignment, QualType Ty,
2344 llvm::MDNode *TBAAInfo = nullptr,
2345 QualType TBAABaseTy = QualType(),
2346 uint64_t TBAAOffset = 0);
2348 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2349 /// care to appropriately convert from the memory representation to
2350 /// the LLVM value representation. The l-value must be a simple
2352 llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
2354 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2355 /// care to appropriately convert from the memory representation to
2356 /// the LLVM value representation.
2357 void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
2358 bool Volatile, unsigned Alignment, QualType Ty,
2359 llvm::MDNode *TBAAInfo = nullptr, bool isInit = false,
2360 QualType TBAABaseTy = QualType(),
2361 uint64_t TBAAOffset = 0);
2363 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2364 /// care to appropriately convert from the memory representation to
2365 /// the LLVM value representation. The l-value must be a simple
2366 /// l-value. The isInit flag indicates whether this is an initialization.
2367 /// If so, atomic qualifiers are ignored and the store is always non-atomic.
2368 void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
2370 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
2371 /// this method emits the address of the lvalue, then loads the result as an
2372 /// rvalue, returning the rvalue.
2373 RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
2374 RValue EmitLoadOfExtVectorElementLValue(LValue V);
2375 RValue EmitLoadOfBitfieldLValue(LValue LV);
2376 RValue EmitLoadOfGlobalRegLValue(LValue LV);
2378 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
2379 /// lvalue, where both are guaranteed to the have the same type, and that type
2381 void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false);
2382 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
2383 void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
2385 /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
2386 /// as EmitStoreThroughLValue.
2388 /// \param Result [out] - If non-null, this will be set to a Value* for the
2389 /// bit-field contents after the store, appropriate for use as the result of
2390 /// an assignment to the bit-field.
2391 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
2392 llvm::Value **Result=nullptr);
2394 /// Emit an l-value for an assignment (simple or compound) of complex type.
2395 LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
2396 LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
2397 LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
2398 llvm::Value *&Result);
2400 // Note: only available for agg return types
2401 LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
2402 LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
2403 // Note: only available for agg return types
2404 LValue EmitCallExprLValue(const CallExpr *E);
2405 // Note: only available for agg return types
2406 LValue EmitVAArgExprLValue(const VAArgExpr *E);
2407 LValue EmitDeclRefLValue(const DeclRefExpr *E);
2408 LValue EmitReadRegister(const VarDecl *VD);
2409 LValue EmitStringLiteralLValue(const StringLiteral *E);
2410 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
2411 LValue EmitPredefinedLValue(const PredefinedExpr *E);
2412 LValue EmitUnaryOpLValue(const UnaryOperator *E);
2413 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2414 bool Accessed = false);
2415 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
2416 LValue EmitMemberExpr(const MemberExpr *E);
2417 LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
2418 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
2419 LValue EmitInitListLValue(const InitListExpr *E);
2420 LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
2421 LValue EmitCastLValue(const CastExpr *E);
2422 LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
2423 LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
2425 llvm::Value *EmitExtVectorElementLValue(LValue V);
2427 RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
2429 class ConstantEmission {
2430 llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
2431 ConstantEmission(llvm::Constant *C, bool isReference)
2432 : ValueAndIsReference(C, isReference) {}
2434 ConstantEmission() {}
2435 static ConstantEmission forReference(llvm::Constant *C) {
2436 return ConstantEmission(C, true);
2438 static ConstantEmission forValue(llvm::Constant *C) {
2439 return ConstantEmission(C, false);
2442 explicit operator bool() const {
2443 return ValueAndIsReference.getOpaqueValue() != nullptr;
2446 bool isReference() const { return ValueAndIsReference.getInt(); }
2447 LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
2448 assert(isReference());
2449 return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
2450 refExpr->getType());
2453 llvm::Constant *getValue() const {
2454 assert(!isReference());
2455 return ValueAndIsReference.getPointer();
2459 ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
2461 RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
2462 AggValueSlot slot = AggValueSlot::ignored());
2463 LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
2465 llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
2466 const ObjCIvarDecl *Ivar);
2467 LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
2468 LValue EmitLValueForLambdaField(const FieldDecl *Field);
2470 /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
2471 /// if the Field is a reference, this will return the address of the reference
2472 /// and not the address of the value stored in the reference.
2473 LValue EmitLValueForFieldInitialization(LValue Base,
2474 const FieldDecl* Field);
2476 LValue EmitLValueForIvar(QualType ObjectTy,
2477 llvm::Value* Base, const ObjCIvarDecl *Ivar,
2478 unsigned CVRQualifiers);
2480 LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
2481 LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
2482 LValue EmitLambdaLValue(const LambdaExpr *E);
2483 LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
2484 LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
2486 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
2487 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
2488 LValue EmitStmtExprLValue(const StmtExpr *E);
2489 LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
2490 LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
2491 void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
2493 //===--------------------------------------------------------------------===//
2494 // Scalar Expression Emission
2495 //===--------------------------------------------------------------------===//
2497 /// EmitCall - Generate a call of the given function, expecting the given
2498 /// result type, and using the given argument list which specifies both the
2499 /// LLVM arguments and the types they were derived from.
2501 /// \param TargetDecl - If given, the decl of the function in a direct call;
2502 /// used to set attributes on the call (noreturn, etc.).
2503 RValue EmitCall(const CGFunctionInfo &FnInfo,
2504 llvm::Value *Callee,
2505 ReturnValueSlot ReturnValue,
2506 const CallArgList &Args,
2507 const Decl *TargetDecl = nullptr,
2508 llvm::Instruction **callOrInvoke = nullptr);
2510 RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E,
2511 ReturnValueSlot ReturnValue,
2512 const Decl *TargetDecl = nullptr,
2513 llvm::Value *Chain = nullptr);
2514 RValue EmitCallExpr(const CallExpr *E,
2515 ReturnValueSlot ReturnValue = ReturnValueSlot());
2517 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2518 const Twine &name = "");
2519 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2520 ArrayRef<llvm::Value*> args,
2521 const Twine &name = "");
2522 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2523 const Twine &name = "");
2524 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2525 ArrayRef<llvm::Value*> args,
2526 const Twine &name = "");
2528 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2529 ArrayRef<llvm::Value *> Args,
2530 const Twine &Name = "");
2531 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2532 const Twine &Name = "");
2533 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2534 ArrayRef<llvm::Value*> args,
2535 const Twine &name = "");
2536 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2537 const Twine &name = "");
2538 void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2539 ArrayRef<llvm::Value*> args);
2541 llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
2542 NestedNameSpecifier *Qual,
2545 llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2547 const CXXRecordDecl *RD);
2550 EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2551 ReturnValueSlot ReturnValue, llvm::Value *This,
2552 llvm::Value *ImplicitParam,
2553 QualType ImplicitParamTy, const CallExpr *E);
2554 RValue EmitCXXStructorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2555 ReturnValueSlot ReturnValue, llvm::Value *This,
2556 llvm::Value *ImplicitParam,
2557 QualType ImplicitParamTy, const CallExpr *E,
2559 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2560 ReturnValueSlot ReturnValue);
2561 RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE,
2562 const CXXMethodDecl *MD,
2563 ReturnValueSlot ReturnValue,
2565 NestedNameSpecifier *Qualifier,
2566 bool IsArrow, const Expr *Base);
2567 // Compute the object pointer.
2568 RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2569 ReturnValueSlot ReturnValue);
2571 RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
2572 const CXXMethodDecl *MD,
2573 ReturnValueSlot ReturnValue);
2575 RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
2576 ReturnValueSlot ReturnValue);
2579 RValue EmitBuiltinExpr(const FunctionDecl *FD,
2580 unsigned BuiltinID, const CallExpr *E,
2581 ReturnValueSlot ReturnValue);
2583 RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
2585 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
2586 /// is unhandled by the current target.
2587 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2589 llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
2590 const llvm::CmpInst::Predicate Fp,
2591 const llvm::CmpInst::Predicate Ip,
2592 const llvm::Twine &Name = "");
2593 llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2595 llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
2596 unsigned LLVMIntrinsic,
2597 unsigned AltLLVMIntrinsic,
2598 const char *NameHint,
2601 SmallVectorImpl<llvm::Value *> &Ops,
2602 llvm::Value *Align = nullptr);
2603 llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2604 unsigned Modifier, llvm::Type *ArgTy,
2606 llvm::Value *EmitNeonCall(llvm::Function *F,
2607 SmallVectorImpl<llvm::Value*> &O,
2609 unsigned shift = 0, bool rightshift = false);
2610 llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
2611 llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
2612 bool negateForRightShift);
2613 llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
2614 llvm::Type *Ty, bool usgn, const char *name);
2615 // Helper functions for EmitAArch64BuiltinExpr.
2616 llvm::Value *vectorWrapScalar8(llvm::Value *Op);
2617 llvm::Value *vectorWrapScalar16(llvm::Value *Op);
2618 llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2620 llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
2621 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2622 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2623 llvm::Value *EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2624 llvm::Value *EmitSystemZBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2625 llvm::Value *EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2627 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
2628 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
2629 llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
2630 llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
2631 llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2632 llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
2633 const ObjCMethodDecl *MethodWithObjects);
2634 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
2635 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2636 ReturnValueSlot Return = ReturnValueSlot());
2638 /// Retrieves the default cleanup kind for an ARC cleanup.
2639 /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2640 CleanupKind getARCCleanupKind() {
2641 return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2642 ? NormalAndEHCleanup : NormalCleanup;
2646 void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
2647 void EmitARCDestroyWeak(llvm::Value *addr);
2648 llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
2649 llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
2650 llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
2652 void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
2653 void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
2654 llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2655 llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
2656 llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
2657 bool resultIgnored);
2658 llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
2659 bool resultIgnored);
2660 llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2661 llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
2662 llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
2663 void EmitARCDestroyStrong(llvm::Value *addr, ARCPreciseLifetime_t precise);
2664 void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
2665 llvm::Value *EmitARCAutorelease(llvm::Value *value);
2666 llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2667 llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2668 llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2670 std::pair<LValue,llvm::Value*>
2671 EmitARCStoreAutoreleasing(const BinaryOperator *e);
2672 std::pair<LValue,llvm::Value*>
2673 EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2675 llvm::Value *EmitObjCThrowOperand(const Expr *expr);
2677 llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2678 llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2679 llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2681 llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
2682 llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2683 llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2685 void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
2687 static Destroyer destroyARCStrongImprecise;
2688 static Destroyer destroyARCStrongPrecise;
2689 static Destroyer destroyARCWeak;
2691 void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2692 llvm::Value *EmitObjCAutoreleasePoolPush();
2693 llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2694 void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2695 void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2697 /// \brief Emits a reference binding to the passed in expression.
2698 RValue EmitReferenceBindingToExpr(const Expr *E);
2700 //===--------------------------------------------------------------------===//
2701 // Expression Emission
2702 //===--------------------------------------------------------------------===//
2704 // Expressions are broken into three classes: scalar, complex, aggregate.
2706 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
2707 /// scalar type, returning the result.
2708 llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
2710 /// EmitScalarConversion - Emit a conversion from the specified type to the
2711 /// specified destination type, both of which are LLVM scalar types.
2712 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
2715 /// EmitComplexToScalarConversion - Emit a conversion from the specified
2716 /// complex type to the specified destination type, where the destination type
2717 /// is an LLVM scalar type.
2718 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
2722 /// EmitAggExpr - Emit the computation of the specified expression
2723 /// of aggregate type. The result is computed into the given slot,
2724 /// which may be null to indicate that the value is not needed.
2725 void EmitAggExpr(const Expr *E, AggValueSlot AS);
2727 /// EmitAggExprToLValue - Emit the computation of the specified expression of
2728 /// aggregate type into a temporary LValue.
2729 LValue EmitAggExprToLValue(const Expr *E);
2731 /// EmitGCMemmoveCollectable - Emit special API for structs with object
2733 void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
2736 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
2737 /// make sure it survives garbage collection until this point.
2738 void EmitExtendGCLifetime(llvm::Value *object);
2740 /// EmitComplexExpr - Emit the computation of the specified expression of
2741 /// complex type, returning the result.
2742 ComplexPairTy EmitComplexExpr(const Expr *E,
2743 bool IgnoreReal = false,
2744 bool IgnoreImag = false);
2746 /// EmitComplexExprIntoLValue - Emit the given expression of complex
2747 /// type and place its result into the specified l-value.
2748 void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
2750 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
2751 void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
2753 /// EmitLoadOfComplex - Load a complex number from the specified l-value.
2754 ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
2756 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
2757 /// global variable that has already been created for it. If the initializer
2758 /// has a different type than GV does, this may free GV and return a different
2759 /// one. Otherwise it just returns GV.
2760 llvm::GlobalVariable *
2761 AddInitializerToStaticVarDecl(const VarDecl &D,
2762 llvm::GlobalVariable *GV);
2765 /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
2766 /// variable with global storage.
2767 void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr,
2770 llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor,
2771 llvm::Constant *Addr);
2773 /// Call atexit() with a function that passes the given argument to
2774 /// the given function.
2775 void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn,
2776 llvm::Constant *addr);
2778 /// Emit code in this function to perform a guarded variable
2779 /// initialization. Guarded initializations are used when it's not
2780 /// possible to prove that an initialization will be done exactly
2781 /// once, e.g. with a static local variable or a static data member
2782 /// of a class template.
2783 void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
2786 /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2788 void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
2789 ArrayRef<llvm::Function *> CXXThreadLocals,
2790 llvm::GlobalVariable *Guard = nullptr);
2792 /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global
2794 void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
2795 const std::vector<std::pair<llvm::WeakVH,
2796 llvm::Constant*> > &DtorsAndObjects);
2798 void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2800 llvm::GlobalVariable *Addr,
2803 void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
2805 void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
2808 void enterFullExpression(const ExprWithCleanups *E) {
2809 if (E->getNumObjects() == 0) return;
2810 enterNonTrivialFullExpression(E);
2812 void enterNonTrivialFullExpression(const ExprWithCleanups *E);
2814 void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
2816 void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
2818 RValue EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest = nullptr);
2820 //===--------------------------------------------------------------------===//
2821 // Annotations Emission
2822 //===--------------------------------------------------------------------===//
2824 /// Emit an annotation call (intrinsic or builtin).
2825 llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn,
2826 llvm::Value *AnnotatedVal,
2827 StringRef AnnotationStr,
2828 SourceLocation Location);
2830 /// Emit local annotations for the local variable V, declared by D.
2831 void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
2833 /// Emit field annotations for the given field & value. Returns the
2834 /// annotation result.
2835 llvm::Value *EmitFieldAnnotations(const FieldDecl *D, llvm::Value *V);
2837 //===--------------------------------------------------------------------===//
2839 //===--------------------------------------------------------------------===//
2841 /// ContainsLabel - Return true if the statement contains a label in it. If
2842 /// this statement is not executed normally, it not containing a label means
2843 /// that we can just remove the code.
2844 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
2846 /// containsBreak - Return true if the statement contains a break out of it.
2847 /// If the statement (recursively) contains a switch or loop with a break
2848 /// inside of it, this is fine.
2849 static bool containsBreak(const Stmt *S);
2851 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2852 /// to a constant, or if it does but contains a label, return false. If it
2853 /// constant folds return true and set the boolean result in Result.
2854 bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
2856 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2857 /// to a constant, or if it does but contains a label, return false. If it
2858 /// constant folds return true and set the folded value.
2859 bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result);
2861 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
2862 /// if statement) to the specified blocks. Based on the condition, this might
2863 /// try to simplify the codegen of the conditional based on the branch.
2864 /// TrueCount should be the number of times we expect the condition to
2865 /// evaluate to true based on PGO data.
2866 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
2867 llvm::BasicBlock *FalseBlock, uint64_t TrueCount);
2869 /// \brief Emit a description of a type in a format suitable for passing to
2870 /// a runtime sanitizer handler.
2871 llvm::Constant *EmitCheckTypeDescriptor(QualType T);
2873 /// \brief Convert a value into a format suitable for passing to a runtime
2874 /// sanitizer handler.
2875 llvm::Value *EmitCheckValue(llvm::Value *V);
2877 /// \brief Emit a description of a source location in a format suitable for
2878 /// passing to a runtime sanitizer handler.
2879 llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
2881 /// \brief Create a basic block that will call a handler function in a
2882 /// sanitizer runtime with the provided arguments, and create a conditional
2884 void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked,
2885 StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
2886 ArrayRef<llvm::Value *> DynamicArgs);
2888 /// \brief Create a basic block that will call the trap intrinsic, and emit a
2889 /// conditional branch to it, for the -ftrapv checks.
2890 void EmitTrapCheck(llvm::Value *Checked);
2892 /// \brief Emit a call to trap or debugtrap and attach function attribute
2893 /// "trap-func-name" if specified.
2894 llvm::CallInst *EmitTrapCall(llvm::Intrinsic::ID IntrID);
2896 /// \brief Create a check for a function parameter that may potentially be
2897 /// declared as non-null.
2898 void EmitNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc,
2899 const FunctionDecl *FD, unsigned ParmNum);
2901 /// EmitCallArg - Emit a single call argument.
2902 void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
2904 /// EmitDelegateCallArg - We are performing a delegate call; that
2905 /// is, the current function is delegating to another one. Produce
2906 /// a r-value suitable for passing the given parameter.
2907 void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
2908 SourceLocation loc);
2910 /// SetFPAccuracy - Set the minimum required accuracy of the given floating
2911 /// point operation, expressed as the maximum relative error in ulp.
2912 void SetFPAccuracy(llvm::Value *Val, float Accuracy);
2915 llvm::MDNode *getRangeForLoadFromType(QualType Ty);
2916 void EmitReturnOfRValue(RValue RV, QualType Ty);
2918 void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
2920 llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4>
2921 DeferredReplacements;
2923 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
2924 /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
2926 /// \param AI - The first function argument of the expansion.
2927 void ExpandTypeFromArgs(QualType Ty, LValue Dst,
2928 SmallVectorImpl<llvm::Argument *>::iterator &AI);
2930 /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg
2931 /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
2932 /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
2933 void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
2934 SmallVectorImpl<llvm::Value *> &IRCallArgs,
2935 unsigned &IRCallArgPos);
2937 llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
2938 const Expr *InputExpr, std::string &ConstraintStr);
2940 llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
2941 LValue InputValue, QualType InputType,
2942 std::string &ConstraintStr,
2943 SourceLocation Loc);
2947 // Determine whether the given argument is an Objective-C method
2948 // that may have type parameters in its signature.
2949 static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method) {
2950 const DeclContext *dc = method->getDeclContext();
2951 if (const ObjCInterfaceDecl *classDecl= dyn_cast<ObjCInterfaceDecl>(dc)) {
2952 return classDecl->getTypeParamListAsWritten();
2955 if (const ObjCCategoryDecl *catDecl = dyn_cast<ObjCCategoryDecl>(dc)) {
2956 return catDecl->getTypeParamList();
2962 template<typename T>
2963 static bool isObjCMethodWithTypeParams(const T *) { return false; }
2966 /// EmitCallArgs - Emit call arguments for a function.
2967 template <typename T>
2968 void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo,
2969 CallExpr::const_arg_iterator ArgBeg,
2970 CallExpr::const_arg_iterator ArgEnd,
2971 const FunctionDecl *CalleeDecl = nullptr,
2972 unsigned ParamsToSkip = 0) {
2973 SmallVector<QualType, 16> ArgTypes;
2974 CallExpr::const_arg_iterator Arg = ArgBeg;
2976 assert((ParamsToSkip == 0 || CallArgTypeInfo) &&
2977 "Can't skip parameters if type info is not provided");
2978 if (CallArgTypeInfo) {
2980 bool isGenericMethod = isObjCMethodWithTypeParams(CallArgTypeInfo);
2983 // First, use the argument types that the type info knows about
2984 for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip,
2985 E = CallArgTypeInfo->param_type_end();
2986 I != E; ++I, ++Arg) {
2987 assert(Arg != ArgEnd && "Running over edge of argument list!");
2988 assert((isGenericMethod ||
2989 ((*I)->isVariablyModifiedType() ||
2990 (*I).getNonReferenceType()->isObjCRetainableType() ||
2992 .getCanonicalType((*I).getNonReferenceType())
2995 .getCanonicalType(Arg->getType())
2997 "type mismatch in call argument!");
2998 ArgTypes.push_back(*I);
3002 // Either we've emitted all the call args, or we have a call to variadic
3005 (Arg == ArgEnd || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) &&
3006 "Extra arguments in non-variadic function!");
3008 // If we still have any arguments, emit them using the type of the argument.
3009 for (; Arg != ArgEnd; ++Arg)
3010 ArgTypes.push_back(getVarArgType(*Arg));
3012 EmitCallArgs(Args, ArgTypes, ArgBeg, ArgEnd, CalleeDecl, ParamsToSkip);
3015 void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes,
3016 CallExpr::const_arg_iterator ArgBeg,
3017 CallExpr::const_arg_iterator ArgEnd,
3018 const FunctionDecl *CalleeDecl = nullptr,
3019 unsigned ParamsToSkip = 0);
3022 QualType getVarArgType(const Expr *Arg);
3024 const TargetCodeGenInfo &getTargetHooks() const {
3025 return CGM.getTargetCodeGenInfo();
3028 void EmitDeclMetadata();
3030 CodeGenModule::ByrefHelpers *
3031 buildByrefHelpers(llvm::StructType &byrefType,
3032 const AutoVarEmission &emission);
3034 void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
3036 /// GetPointeeAlignment - Given an expression with a pointer type, emit the
3037 /// value and compute our best estimate of the alignment of the pointee.
3038 std::pair<llvm::Value*, unsigned> EmitPointerWithAlignment(const Expr *Addr);
3040 llvm::Value *GetValueForARMHint(unsigned BuiltinID);
3043 /// Helper class with most of the code for saving a value for a
3044 /// conditional expression cleanup.
3045 struct DominatingLLVMValue {
3046 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
3048 /// Answer whether the given value needs extra work to be saved.
3049 static bool needsSaving(llvm::Value *value) {
3050 // If it's not an instruction, we don't need to save.
3051 if (!isa<llvm::Instruction>(value)) return false;
3053 // If it's an instruction in the entry block, we don't need to save.
3054 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
3055 return (block != &block->getParent()->getEntryBlock());
3058 /// Try to save the given value.
3059 static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
3060 if (!needsSaving(value)) return saved_type(value, false);
3062 // Otherwise we need an alloca.
3063 llvm::Value *alloca =
3064 CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
3065 CGF.Builder.CreateStore(value, alloca);
3067 return saved_type(alloca, true);
3070 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
3071 if (!value.getInt()) return value.getPointer();
3072 return CGF.Builder.CreateLoad(value.getPointer());
3076 /// A partial specialization of DominatingValue for llvm::Values that
3077 /// might be llvm::Instructions.
3078 template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
3080 static type restore(CodeGenFunction &CGF, saved_type value) {
3081 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
3085 /// A specialization of DominatingValue for RValue.
3086 template <> struct DominatingValue<RValue> {
3087 typedef RValue type;
3089 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
3090 AggregateAddress, ComplexAddress };
3094 saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
3097 static bool needsSaving(RValue value);
3098 static saved_type save(CodeGenFunction &CGF, RValue value);
3099 RValue restore(CodeGenFunction &CGF);
3101 // implementations in CGExprCXX.cpp
3104 static bool needsSaving(type value) {
3105 return saved_type::needsSaving(value);
3107 static saved_type save(CodeGenFunction &CGF, type value) {
3108 return saved_type::save(CGF, value);
3110 static type restore(CodeGenFunction &CGF, saved_type value) {
3111 return value.restore(CGF);
3115 } // end namespace CodeGen
3116 } // end namespace clang