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/TargetInfo.h"
31 #include "clang/Frontend/CodeGenOptions.h"
32 #include "llvm/ADT/ArrayRef.h"
33 #include "llvm/ADT/DenseMap.h"
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/IR/ValueHandle.h"
36 #include "llvm/Support/Debug.h"
52 class CXXDestructorDecl;
53 class CXXForRangeStmt;
57 class EnumConstantDecl;
59 class FunctionProtoType;
61 class ObjCContainerDecl;
62 class ObjCInterfaceDecl;
65 class ObjCImplementationDecl;
66 class ObjCPropertyImplDecl;
68 class TargetCodeGenInfo;
70 class ObjCForCollectionStmt;
72 class ObjCAtThrowStmt;
73 class ObjCAtSynchronizedStmt;
74 class ObjCAutoreleasePoolStmt;
83 class BlockFieldFlags;
85 /// The kind of evaluation to perform on values of a particular
86 /// type. Basically, is the code in CGExprScalar, CGExprComplex, or
89 /// TODO: should vectors maybe be split out into their own thing?
90 enum TypeEvaluationKind {
96 /// CodeGenFunction - This class organizes the per-function state that is used
97 /// while generating LLVM code.
98 class CodeGenFunction : public CodeGenTypeCache {
99 CodeGenFunction(const CodeGenFunction &) LLVM_DELETED_FUNCTION;
100 void operator=(const CodeGenFunction &) LLVM_DELETED_FUNCTION;
102 friend class CGCXXABI;
104 /// A jump destination is an abstract label, branching to which may
105 /// require a jump out through normal cleanups.
107 JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
108 JumpDest(llvm::BasicBlock *Block,
109 EHScopeStack::stable_iterator Depth,
111 : Block(Block), ScopeDepth(Depth), Index(Index) {}
113 bool isValid() const { return Block != nullptr; }
114 llvm::BasicBlock *getBlock() const { return Block; }
115 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
116 unsigned getDestIndex() const { return Index; }
118 // This should be used cautiously.
119 void setScopeDepth(EHScopeStack::stable_iterator depth) {
124 llvm::BasicBlock *Block;
125 EHScopeStack::stable_iterator ScopeDepth;
129 CodeGenModule &CGM; // Per-module state.
130 const TargetInfo &Target;
132 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
133 LoopInfoStack LoopStack;
136 /// \brief CGBuilder insert helper. This function is called after an
137 /// instruction is created using Builder.
138 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
139 llvm::BasicBlock *BB,
140 llvm::BasicBlock::iterator InsertPt) const;
142 /// CurFuncDecl - Holds the Decl for the current outermost
143 /// non-closure context.
144 const Decl *CurFuncDecl;
145 /// CurCodeDecl - This is the inner-most code context, which includes blocks.
146 const Decl *CurCodeDecl;
147 const CGFunctionInfo *CurFnInfo;
149 llvm::Function *CurFn;
151 /// CurGD - The GlobalDecl for the current function being compiled.
154 /// PrologueCleanupDepth - The cleanup depth enclosing all the
155 /// cleanups associated with the parameters.
156 EHScopeStack::stable_iterator PrologueCleanupDepth;
158 /// ReturnBlock - Unified return block.
159 JumpDest ReturnBlock;
161 /// ReturnValue - The temporary alloca to hold the return value. This is null
162 /// iff the function has no return value.
163 llvm::Value *ReturnValue;
165 /// AllocaInsertPoint - This is an instruction in the entry block before which
166 /// we prefer to insert allocas.
167 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
169 /// \brief API for captured statement code generation.
170 class CGCapturedStmtInfo {
172 explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
173 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
174 explicit CGCapturedStmtInfo(const CapturedStmt &S,
175 CapturedRegionKind K = CR_Default)
176 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
178 RecordDecl::field_iterator Field =
179 S.getCapturedRecordDecl()->field_begin();
180 for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
182 I != E; ++I, ++Field) {
183 if (I->capturesThis())
184 CXXThisFieldDecl = *Field;
185 else if (I->capturesVariable())
186 CaptureFields[I->getCapturedVar()] = *Field;
190 virtual ~CGCapturedStmtInfo();
192 CapturedRegionKind getKind() const { return Kind; }
194 void setContextValue(llvm::Value *V) { ThisValue = V; }
195 // \brief Retrieve the value of the context parameter.
196 llvm::Value *getContextValue() const { return ThisValue; }
198 /// \brief Lookup the captured field decl for a variable.
199 const FieldDecl *lookup(const VarDecl *VD) const {
200 return CaptureFields.lookup(VD);
203 bool isCXXThisExprCaptured() const { return CXXThisFieldDecl != nullptr; }
204 FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
206 static bool classof(const CGCapturedStmtInfo *) {
210 /// \brief Emit the captured statement body.
211 virtual void EmitBody(CodeGenFunction &CGF, Stmt *S) {
212 RegionCounter Cnt = CGF.getPGORegionCounter(S);
213 Cnt.beginRegion(CGF.Builder);
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 /// BoundsChecking - Emit run-time bounds checks. Higher values mean
237 /// potentially higher performance penalties.
238 unsigned char BoundsChecking;
240 /// \brief Sanitizers enabled for this function.
241 SanitizerSet SanOpts;
243 /// \brief True if CodeGen currently emits code implementing sanitizer checks.
244 bool IsSanitizerScope;
246 /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope.
247 class SanitizerScope {
248 CodeGenFunction *CGF;
250 SanitizerScope(CodeGenFunction *CGF);
254 /// In C++, whether we are code generating a thunk. This controls whether we
255 /// should emit cleanups.
258 /// In ARC, whether we should autorelease the return value.
259 bool AutoreleaseResult;
261 /// Whether we processed a Microsoft-style asm block during CodeGen. These can
262 /// potentially set the return value.
265 const CodeGen::CGBlockInfo *BlockInfo;
266 llvm::Value *BlockPointer;
268 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
269 FieldDecl *LambdaThisCaptureField;
271 /// \brief A mapping from NRVO variables to the flags used to indicate
272 /// when the NRVO has been applied to this variable.
273 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
275 EHScopeStack EHStack;
276 llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
278 /// Header for data within LifetimeExtendedCleanupStack.
279 struct LifetimeExtendedCleanupHeader {
280 /// The size of the following cleanup object.
282 /// The kind of cleanup to push: a value from the CleanupKind enumeration.
285 size_t getSize() const { return size_t(Size); }
286 CleanupKind getKind() const { return static_cast<CleanupKind>(Kind); }
289 /// i32s containing the indexes of the cleanup destinations.
290 llvm::AllocaInst *NormalCleanupDest;
292 unsigned NextCleanupDestIndex;
294 /// FirstBlockInfo - The head of a singly-linked-list of block layouts.
295 CGBlockInfo *FirstBlockInfo;
297 /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
298 llvm::BasicBlock *EHResumeBlock;
300 /// The exception slot. All landing pads write the current exception pointer
301 /// into this alloca.
302 llvm::Value *ExceptionSlot;
304 /// The selector slot. Under the MandatoryCleanup model, all landing pads
305 /// write the current selector value into this alloca.
306 llvm::AllocaInst *EHSelectorSlot;
308 /// Emits a landing pad for the current EH stack.
309 llvm::BasicBlock *EmitLandingPad();
311 llvm::BasicBlock *getInvokeDestImpl();
314 typename DominatingValue<T>::saved_type saveValueInCond(T value) {
315 return DominatingValue<T>::save(*this, value);
319 /// ObjCEHValueStack - Stack of Objective-C exception values, used for
321 SmallVector<llvm::Value*, 8> ObjCEHValueStack;
323 /// A class controlling the emission of a finally block.
325 /// Where the catchall's edge through the cleanup should go.
326 JumpDest RethrowDest;
328 /// A function to call to enter the catch.
329 llvm::Constant *BeginCatchFn;
331 /// An i1 variable indicating whether or not the @finally is
332 /// running for an exception.
333 llvm::AllocaInst *ForEHVar;
335 /// An i8* variable into which the exception pointer to rethrow
337 llvm::AllocaInst *SavedExnVar;
340 void enter(CodeGenFunction &CGF, const Stmt *Finally,
341 llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
342 llvm::Constant *rethrowFn);
343 void exit(CodeGenFunction &CGF);
346 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
347 /// current full-expression. Safe against the possibility that
348 /// we're currently inside a conditionally-evaluated expression.
349 template <class T, class A0>
350 void pushFullExprCleanup(CleanupKind kind, A0 a0) {
351 // If we're not in a conditional branch, or if none of the
352 // arguments requires saving, then use the unconditional cleanup.
353 if (!isInConditionalBranch())
354 return EHStack.pushCleanup<T>(kind, a0);
356 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
358 typedef EHScopeStack::ConditionalCleanup1<T, A0> CleanupType;
359 EHStack.pushCleanup<CleanupType>(kind, a0_saved);
360 initFullExprCleanup();
363 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
364 /// current full-expression. Safe against the possibility that
365 /// we're currently inside a conditionally-evaluated expression.
366 template <class T, class A0, class A1>
367 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) {
368 // If we're not in a conditional branch, or if none of the
369 // arguments requires saving, then use the unconditional cleanup.
370 if (!isInConditionalBranch())
371 return EHStack.pushCleanup<T>(kind, a0, a1);
373 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
374 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
376 typedef EHScopeStack::ConditionalCleanup2<T, A0, A1> CleanupType;
377 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved);
378 initFullExprCleanup();
381 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
382 /// current full-expression. Safe against the possibility that
383 /// we're currently inside a conditionally-evaluated expression.
384 template <class T, class A0, class A1, class A2>
385 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2) {
386 // If we're not in a conditional branch, or if none of the
387 // arguments requires saving, then use the unconditional cleanup.
388 if (!isInConditionalBranch()) {
389 return EHStack.pushCleanup<T>(kind, a0, a1, a2);
392 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
393 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
394 typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
396 typedef EHScopeStack::ConditionalCleanup3<T, A0, A1, A2> CleanupType;
397 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, a2_saved);
398 initFullExprCleanup();
401 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
402 /// current full-expression. Safe against the possibility that
403 /// we're currently inside a conditionally-evaluated expression.
404 template <class T, class A0, class A1, class A2, class A3>
405 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2, A3 a3) {
406 // If we're not in a conditional branch, or if none of the
407 // arguments requires saving, then use the unconditional cleanup.
408 if (!isInConditionalBranch()) {
409 return EHStack.pushCleanup<T>(kind, a0, a1, a2, a3);
412 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
413 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
414 typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
415 typename DominatingValue<A3>::saved_type a3_saved = saveValueInCond(a3);
417 typedef EHScopeStack::ConditionalCleanup4<T, A0, A1, A2, A3> CleanupType;
418 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved,
420 initFullExprCleanup();
423 /// \brief Queue a cleanup to be pushed after finishing the current
425 template <class T, class A0, class A1, class A2, class A3>
426 void pushCleanupAfterFullExpr(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3) {
427 assert(!isInConditionalBranch() && "can't defer conditional cleanup");
429 LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind };
431 size_t OldSize = LifetimeExtendedCleanupStack.size();
432 LifetimeExtendedCleanupStack.resize(
433 LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size);
435 char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
436 new (Buffer) LifetimeExtendedCleanupHeader(Header);
437 new (Buffer + sizeof(Header)) T(a0, a1, a2, a3);
440 /// Set up the last cleaup that was pushed as a conditional
441 /// full-expression cleanup.
442 void initFullExprCleanup();
444 /// PushDestructorCleanup - Push a cleanup to call the
445 /// complete-object destructor of an object of the given type at the
446 /// given address. Does nothing if T is not a C++ class type with a
447 /// non-trivial destructor.
448 void PushDestructorCleanup(QualType T, llvm::Value *Addr);
450 /// PushDestructorCleanup - Push a cleanup to call the
451 /// complete-object variant of the given destructor on the object at
452 /// the given address.
453 void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
456 /// PopCleanupBlock - Will pop the cleanup entry on the stack and
457 /// process all branch fixups.
458 void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
460 /// DeactivateCleanupBlock - Deactivates the given cleanup block.
461 /// The block cannot be reactivated. Pops it if it's the top of the
464 /// \param DominatingIP - An instruction which is known to
465 /// dominate the current IP (if set) and which lies along
466 /// all paths of execution between the current IP and the
467 /// the point at which the cleanup comes into scope.
468 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
469 llvm::Instruction *DominatingIP);
471 /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
472 /// Cannot be used to resurrect a deactivated cleanup.
474 /// \param DominatingIP - An instruction which is known to
475 /// dominate the current IP (if set) and which lies along
476 /// all paths of execution between the current IP and the
477 /// the point at which the cleanup comes into scope.
478 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
479 llvm::Instruction *DominatingIP);
481 /// \brief Enters a new scope for capturing cleanups, all of which
482 /// will be executed once the scope is exited.
483 class RunCleanupsScope {
484 EHScopeStack::stable_iterator CleanupStackDepth;
485 size_t LifetimeExtendedCleanupStackSize;
486 bool OldDidCallStackSave;
491 RunCleanupsScope(const RunCleanupsScope &) LLVM_DELETED_FUNCTION;
492 void operator=(const RunCleanupsScope &) LLVM_DELETED_FUNCTION;
495 CodeGenFunction& CGF;
498 /// \brief Enter a new cleanup scope.
499 explicit RunCleanupsScope(CodeGenFunction &CGF)
500 : PerformCleanup(true), CGF(CGF)
502 CleanupStackDepth = CGF.EHStack.stable_begin();
503 LifetimeExtendedCleanupStackSize =
504 CGF.LifetimeExtendedCleanupStack.size();
505 OldDidCallStackSave = CGF.DidCallStackSave;
506 CGF.DidCallStackSave = false;
509 /// \brief Exit this cleanup scope, emitting any accumulated
511 ~RunCleanupsScope() {
512 if (PerformCleanup) {
513 CGF.DidCallStackSave = OldDidCallStackSave;
514 CGF.PopCleanupBlocks(CleanupStackDepth,
515 LifetimeExtendedCleanupStackSize);
519 /// \brief Determine whether this scope requires any cleanups.
520 bool requiresCleanups() const {
521 return CGF.EHStack.stable_begin() != CleanupStackDepth;
524 /// \brief Force the emission of cleanups now, instead of waiting
525 /// until this object is destroyed.
526 void ForceCleanup() {
527 assert(PerformCleanup && "Already forced cleanup");
528 CGF.DidCallStackSave = OldDidCallStackSave;
529 CGF.PopCleanupBlocks(CleanupStackDepth,
530 LifetimeExtendedCleanupStackSize);
531 PerformCleanup = false;
535 class LexicalScope : public RunCleanupsScope {
537 SmallVector<const LabelDecl*, 4> Labels;
538 LexicalScope *ParentScope;
540 LexicalScope(const LexicalScope &) LLVM_DELETED_FUNCTION;
541 void operator=(const LexicalScope &) LLVM_DELETED_FUNCTION;
544 /// \brief Enter a new cleanup scope.
545 explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
546 : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
547 CGF.CurLexicalScope = this;
548 if (CGDebugInfo *DI = CGF.getDebugInfo())
549 DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
552 void addLabel(const LabelDecl *label) {
553 assert(PerformCleanup && "adding label to dead scope?");
554 Labels.push_back(label);
557 /// \brief Exit this cleanup scope, emitting any accumulated
560 if (CGDebugInfo *DI = CGF.getDebugInfo())
561 DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
563 // If we should perform a cleanup, force them now. Note that
564 // this ends the cleanup scope before rescoping any labels.
565 if (PerformCleanup) ForceCleanup();
568 /// \brief Force the emission of cleanups now, instead of waiting
569 /// until this object is destroyed.
570 void ForceCleanup() {
571 CGF.CurLexicalScope = ParentScope;
572 RunCleanupsScope::ForceCleanup();
578 void rescopeLabels();
581 /// \brief The scope used to remap some variables as private in the OpenMP
582 /// loop body (or other captured region emitted without outlining), and to
583 /// restore old vars back on exit.
584 class OMPPrivateScope : public RunCleanupsScope {
585 typedef llvm::DenseMap<const VarDecl *, llvm::Value *> VarDeclMapTy;
586 VarDeclMapTy SavedLocals;
587 VarDeclMapTy SavedPrivates;
590 OMPPrivateScope(const OMPPrivateScope &) LLVM_DELETED_FUNCTION;
591 void operator=(const OMPPrivateScope &) LLVM_DELETED_FUNCTION;
594 /// \brief Enter a new OpenMP private scope.
595 explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
597 /// \brief Registers \a LocalVD variable as a private and apply \a
598 /// PrivateGen function for it to generate corresponding private variable.
599 /// \a PrivateGen returns an address of the generated private variable.
600 /// \return true if the variable is registered as private, false if it has
601 /// been privatized already.
603 addPrivate(const VarDecl *LocalVD,
604 const std::function<llvm::Value *()> &PrivateGen) {
605 assert(PerformCleanup && "adding private to dead scope");
606 if (SavedLocals.count(LocalVD) > 0) return false;
607 SavedLocals[LocalVD] = CGF.LocalDeclMap.lookup(LocalVD);
608 CGF.LocalDeclMap.erase(LocalVD);
609 SavedPrivates[LocalVD] = PrivateGen();
610 CGF.LocalDeclMap[LocalVD] = SavedLocals[LocalVD];
614 /// \brief Privatizes local variables previously registered as private.
615 /// Registration is separate from the actual privatization to allow
616 /// initializers use values of the original variables, not the private one.
617 /// This is important, for example, if the private variable is a class
618 /// variable initialized by a constructor that references other private
619 /// variables. But at initialization original variables must be used, not
621 /// \return true if at least one variable was privatized, false otherwise.
623 for (auto VDPair : SavedPrivates) {
624 CGF.LocalDeclMap[VDPair.first] = VDPair.second;
626 SavedPrivates.clear();
627 return !SavedLocals.empty();
630 void ForceCleanup() {
631 RunCleanupsScope::ForceCleanup();
632 // Remap vars back to the original values.
633 for (auto I : SavedLocals) {
634 CGF.LocalDeclMap[I.first] = I.second;
639 /// \brief Exit scope - all the mapped variables are restored.
640 ~OMPPrivateScope() { ForceCleanup(); }
643 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
644 /// that have been added.
645 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
647 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
648 /// that have been added, then adds all lifetime-extended cleanups from
649 /// the given position to the stack.
650 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
651 size_t OldLifetimeExtendedStackSize);
653 void ResolveBranchFixups(llvm::BasicBlock *Target);
655 /// The given basic block lies in the current EH scope, but may be a
656 /// target of a potentially scope-crossing jump; get a stable handle
657 /// to which we can perform this jump later.
658 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
659 return JumpDest(Target,
660 EHStack.getInnermostNormalCleanup(),
661 NextCleanupDestIndex++);
664 /// The given basic block lies in the current EH scope, but may be a
665 /// target of a potentially scope-crossing jump; get a stable handle
666 /// to which we can perform this jump later.
667 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
668 return getJumpDestInCurrentScope(createBasicBlock(Name));
671 /// EmitBranchThroughCleanup - Emit a branch from the current insert
672 /// block through the normal cleanup handling code (if any) and then
674 void EmitBranchThroughCleanup(JumpDest Dest);
676 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
677 /// specified destination obviously has no cleanups to run. 'false' is always
678 /// a conservatively correct answer for this method.
679 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
681 /// popCatchScope - Pops the catch scope at the top of the EHScope
682 /// stack, emitting any required code (other than the catch handlers
684 void popCatchScope();
686 llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
687 llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
689 /// An object to manage conditionally-evaluated expressions.
690 class ConditionalEvaluation {
691 llvm::BasicBlock *StartBB;
694 ConditionalEvaluation(CodeGenFunction &CGF)
695 : StartBB(CGF.Builder.GetInsertBlock()) {}
697 void begin(CodeGenFunction &CGF) {
698 assert(CGF.OutermostConditional != this);
699 if (!CGF.OutermostConditional)
700 CGF.OutermostConditional = this;
703 void end(CodeGenFunction &CGF) {
704 assert(CGF.OutermostConditional != nullptr);
705 if (CGF.OutermostConditional == this)
706 CGF.OutermostConditional = nullptr;
709 /// Returns a block which will be executed prior to each
710 /// evaluation of the conditional code.
711 llvm::BasicBlock *getStartingBlock() const {
716 /// isInConditionalBranch - Return true if we're currently emitting
717 /// one branch or the other of a conditional expression.
718 bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
720 void setBeforeOutermostConditional(llvm::Value *value, llvm::Value *addr) {
721 assert(isInConditionalBranch());
722 llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
723 new llvm::StoreInst(value, addr, &block->back());
726 /// An RAII object to record that we're evaluating a statement
728 class StmtExprEvaluation {
729 CodeGenFunction &CGF;
731 /// We have to save the outermost conditional: cleanups in a
732 /// statement expression aren't conditional just because the
734 ConditionalEvaluation *SavedOutermostConditional;
737 StmtExprEvaluation(CodeGenFunction &CGF)
738 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
739 CGF.OutermostConditional = nullptr;
742 ~StmtExprEvaluation() {
743 CGF.OutermostConditional = SavedOutermostConditional;
744 CGF.EnsureInsertPoint();
748 /// An object which temporarily prevents a value from being
749 /// destroyed by aggressive peephole optimizations that assume that
750 /// all uses of a value have been realized in the IR.
751 class PeepholeProtection {
752 llvm::Instruction *Inst;
753 friend class CodeGenFunction;
756 PeepholeProtection() : Inst(nullptr) {}
759 /// A non-RAII class containing all the information about a bound
760 /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for
761 /// this which makes individual mappings very simple; using this
762 /// class directly is useful when you have a variable number of
763 /// opaque values or don't want the RAII functionality for some
765 class OpaqueValueMappingData {
766 const OpaqueValueExpr *OpaqueValue;
768 CodeGenFunction::PeepholeProtection Protection;
770 OpaqueValueMappingData(const OpaqueValueExpr *ov,
772 : OpaqueValue(ov), BoundLValue(boundLValue) {}
774 OpaqueValueMappingData() : OpaqueValue(nullptr) {}
776 static bool shouldBindAsLValue(const Expr *expr) {
777 // gl-values should be bound as l-values for obvious reasons.
778 // Records should be bound as l-values because IR generation
779 // always keeps them in memory. Expressions of function type
780 // act exactly like l-values but are formally required to be
782 return expr->isGLValue() ||
783 expr->getType()->isFunctionType() ||
784 hasAggregateEvaluationKind(expr->getType());
787 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
788 const OpaqueValueExpr *ov,
790 if (shouldBindAsLValue(ov))
791 return bind(CGF, ov, CGF.EmitLValue(e));
792 return bind(CGF, ov, CGF.EmitAnyExpr(e));
795 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
796 const OpaqueValueExpr *ov,
798 assert(shouldBindAsLValue(ov));
799 CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
800 return OpaqueValueMappingData(ov, true);
803 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
804 const OpaqueValueExpr *ov,
806 assert(!shouldBindAsLValue(ov));
807 CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
809 OpaqueValueMappingData data(ov, false);
811 // Work around an extremely aggressive peephole optimization in
812 // EmitScalarConversion which assumes that all other uses of a
814 data.Protection = CGF.protectFromPeepholes(rv);
819 bool isValid() const { return OpaqueValue != nullptr; }
820 void clear() { OpaqueValue = nullptr; }
822 void unbind(CodeGenFunction &CGF) {
823 assert(OpaqueValue && "no data to unbind!");
826 CGF.OpaqueLValues.erase(OpaqueValue);
828 CGF.OpaqueRValues.erase(OpaqueValue);
829 CGF.unprotectFromPeepholes(Protection);
834 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
835 class OpaqueValueMapping {
836 CodeGenFunction &CGF;
837 OpaqueValueMappingData Data;
840 static bool shouldBindAsLValue(const Expr *expr) {
841 return OpaqueValueMappingData::shouldBindAsLValue(expr);
844 /// Build the opaque value mapping for the given conditional
845 /// operator if it's the GNU ?: extension. This is a common
846 /// enough pattern that the convenience operator is really
849 OpaqueValueMapping(CodeGenFunction &CGF,
850 const AbstractConditionalOperator *op) : CGF(CGF) {
851 if (isa<ConditionalOperator>(op))
855 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
856 Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
860 OpaqueValueMapping(CodeGenFunction &CGF,
861 const OpaqueValueExpr *opaqueValue,
863 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
866 OpaqueValueMapping(CodeGenFunction &CGF,
867 const OpaqueValueExpr *opaqueValue,
869 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
877 ~OpaqueValueMapping() {
878 if (Data.isValid()) Data.unbind(CGF);
882 /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
883 /// number that holds the value.
884 unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
886 /// BuildBlockByrefAddress - Computes address location of the
887 /// variable which is declared as __block.
888 llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
891 CGDebugInfo *DebugInfo;
892 bool DisableDebugInfo;
894 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
895 /// calling llvm.stacksave for multiple VLAs in the same scope.
896 bool DidCallStackSave;
898 /// IndirectBranch - The first time an indirect goto is seen we create a block
899 /// with an indirect branch. Every time we see the address of a label taken,
900 /// we add the label to the indirect goto. Every subsequent indirect goto is
901 /// codegen'd as a jump to the IndirectBranch's basic block.
902 llvm::IndirectBrInst *IndirectBranch;
904 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
906 typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
907 DeclMapTy LocalDeclMap;
909 /// LabelMap - This keeps track of the LLVM basic block for each C label.
910 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
912 // BreakContinueStack - This keeps track of where break and continue
913 // statements should jump to.
914 struct BreakContinue {
915 BreakContinue(JumpDest Break, JumpDest Continue)
916 : BreakBlock(Break), ContinueBlock(Continue) {}
919 JumpDest ContinueBlock;
921 SmallVector<BreakContinue, 8> BreakContinueStack;
926 /// Get a counter for instrumentation of the region associated with the given
928 RegionCounter getPGORegionCounter(const Stmt *S) {
929 return RegionCounter(PGO, S);
933 /// SwitchInsn - This is nearest current switch instruction. It is null if
934 /// current context is not in a switch.
935 llvm::SwitchInst *SwitchInsn;
936 /// The branch weights of SwitchInsn when doing instrumentation based PGO.
937 SmallVector<uint64_t, 16> *SwitchWeights;
939 /// CaseRangeBlock - This block holds if condition check for last case
940 /// statement range in current switch instruction.
941 llvm::BasicBlock *CaseRangeBlock;
943 /// OpaqueLValues - Keeps track of the current set of opaque value
945 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
946 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
948 // VLASizeMap - This keeps track of the associated size for each VLA type.
949 // We track this by the size expression rather than the type itself because
950 // in certain situations, like a const qualifier applied to an VLA typedef,
951 // multiple VLA types can share the same size expression.
952 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
953 // enter/leave scopes.
954 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
956 /// A block containing a single 'unreachable' instruction. Created
957 /// lazily by getUnreachableBlock().
958 llvm::BasicBlock *UnreachableBlock;
960 /// Counts of the number return expressions in the function.
961 unsigned NumReturnExprs;
963 /// Count the number of simple (constant) return expressions in the function.
964 unsigned NumSimpleReturnExprs;
966 /// The last regular (non-return) debug location (breakpoint) in the function.
967 SourceLocation LastStopPoint;
970 /// A scope within which we are constructing the fields of an object which
971 /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
972 /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
973 class FieldConstructionScope {
975 FieldConstructionScope(CodeGenFunction &CGF, llvm::Value *This)
976 : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
977 CGF.CXXDefaultInitExprThis = This;
979 ~FieldConstructionScope() {
980 CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
984 CodeGenFunction &CGF;
985 llvm::Value *OldCXXDefaultInitExprThis;
988 /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
989 /// is overridden to be the object under construction.
990 class CXXDefaultInitExprScope {
992 CXXDefaultInitExprScope(CodeGenFunction &CGF)
993 : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue) {
994 CGF.CXXThisValue = CGF.CXXDefaultInitExprThis;
996 ~CXXDefaultInitExprScope() {
997 CGF.CXXThisValue = OldCXXThisValue;
1001 CodeGenFunction &CGF;
1002 llvm::Value *OldCXXThisValue;
1006 /// CXXThisDecl - When generating code for a C++ member function,
1007 /// this will hold the implicit 'this' declaration.
1008 ImplicitParamDecl *CXXABIThisDecl;
1009 llvm::Value *CXXABIThisValue;
1010 llvm::Value *CXXThisValue;
1012 /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1013 /// this expression.
1014 llvm::Value *CXXDefaultInitExprThis;
1016 /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1017 /// destructor, this will hold the implicit argument (e.g. VTT).
1018 ImplicitParamDecl *CXXStructorImplicitParamDecl;
1019 llvm::Value *CXXStructorImplicitParamValue;
1021 /// OutermostConditional - Points to the outermost active
1022 /// conditional control. This is used so that we know if a
1023 /// temporary should be destroyed conditionally.
1024 ConditionalEvaluation *OutermostConditional;
1026 /// The current lexical scope.
1027 LexicalScope *CurLexicalScope;
1029 /// The current source location that should be used for exception
1031 SourceLocation CurEHLocation;
1033 /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
1034 /// type as well as the field number that contains the actual data.
1035 llvm::DenseMap<const ValueDecl *, std::pair<llvm::Type *,
1036 unsigned> > ByRefValueInfo;
1038 llvm::BasicBlock *TerminateLandingPad;
1039 llvm::BasicBlock *TerminateHandler;
1040 llvm::BasicBlock *TrapBB;
1042 /// Add a kernel metadata node to the named metadata node 'opencl.kernels'.
1043 /// In the kernel metadata node, reference the kernel function and metadata
1044 /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2):
1045 /// - A node for the vec_type_hint(<type>) qualifier contains string
1046 /// "vec_type_hint", an undefined value of the <type> data type,
1047 /// and a Boolean that is true if the <type> is integer and signed.
1048 /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string
1049 /// "work_group_size_hint", and three 32-bit integers X, Y and Z.
1050 /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string
1051 /// "reqd_work_group_size", and three 32-bit integers X, Y and Z.
1052 void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1053 llvm::Function *Fn);
1056 CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
1059 CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1060 ASTContext &getContext() const { return CGM.getContext(); }
1061 CGDebugInfo *getDebugInfo() {
1062 if (DisableDebugInfo)
1066 void disableDebugInfo() { DisableDebugInfo = true; }
1067 void enableDebugInfo() { DisableDebugInfo = false; }
1069 bool shouldUseFusedARCCalls() {
1070 return CGM.getCodeGenOpts().OptimizationLevel == 0;
1073 const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
1075 /// Returns a pointer to the function's exception object and selector slot,
1076 /// which is assigned in every landing pad.
1077 llvm::Value *getExceptionSlot();
1078 llvm::Value *getEHSelectorSlot();
1080 /// Returns the contents of the function's exception object and selector
1082 llvm::Value *getExceptionFromSlot();
1083 llvm::Value *getSelectorFromSlot();
1085 llvm::Value *getNormalCleanupDestSlot();
1087 llvm::BasicBlock *getUnreachableBlock() {
1088 if (!UnreachableBlock) {
1089 UnreachableBlock = createBasicBlock("unreachable");
1090 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1092 return UnreachableBlock;
1095 llvm::BasicBlock *getInvokeDest() {
1096 if (!EHStack.requiresLandingPad()) return nullptr;
1097 return getInvokeDestImpl();
1100 const TargetInfo &getTarget() const { return Target; }
1101 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
1103 //===--------------------------------------------------------------------===//
1105 //===--------------------------------------------------------------------===//
1107 typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty);
1109 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1110 llvm::Value *arrayEndPointer,
1111 QualType elementType,
1112 Destroyer *destroyer);
1113 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1114 llvm::Value *arrayEnd,
1115 QualType elementType,
1116 Destroyer *destroyer);
1118 void pushDestroy(QualType::DestructionKind dtorKind,
1119 llvm::Value *addr, QualType type);
1120 void pushEHDestroy(QualType::DestructionKind dtorKind,
1121 llvm::Value *addr, QualType type);
1122 void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type,
1123 Destroyer *destroyer, bool useEHCleanupForArray);
1124 void pushLifetimeExtendedDestroy(CleanupKind kind, llvm::Value *addr,
1125 QualType type, Destroyer *destroyer,
1126 bool useEHCleanupForArray);
1127 void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1128 llvm::Value *CompletePtr,
1129 QualType ElementType);
1130 void pushStackRestore(CleanupKind kind, llvm::Value *SPMem);
1131 void emitDestroy(llvm::Value *addr, QualType type, Destroyer *destroyer,
1132 bool useEHCleanupForArray);
1133 llvm::Function *generateDestroyHelper(llvm::Constant *addr, QualType type,
1134 Destroyer *destroyer,
1135 bool useEHCleanupForArray,
1137 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
1138 QualType type, Destroyer *destroyer,
1139 bool checkZeroLength, bool useEHCleanup);
1141 Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
1143 /// Determines whether an EH cleanup is required to destroy a type
1144 /// with the given destruction kind.
1145 bool needsEHCleanup(QualType::DestructionKind kind) {
1147 case QualType::DK_none:
1149 case QualType::DK_cxx_destructor:
1150 case QualType::DK_objc_weak_lifetime:
1151 return getLangOpts().Exceptions;
1152 case QualType::DK_objc_strong_lifetime:
1153 return getLangOpts().Exceptions &&
1154 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1156 llvm_unreachable("bad destruction kind");
1159 CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1160 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1163 //===--------------------------------------------------------------------===//
1165 //===--------------------------------------------------------------------===//
1167 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
1169 void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
1171 /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
1172 void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1173 const ObjCPropertyImplDecl *PID);
1174 void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
1175 const ObjCPropertyImplDecl *propImpl,
1176 const ObjCMethodDecl *GetterMothodDecl,
1177 llvm::Constant *AtomicHelperFn);
1179 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1180 ObjCMethodDecl *MD, bool ctor);
1182 /// GenerateObjCSetter - Synthesize an Objective-C property setter function
1183 /// for the given property.
1184 void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1185 const ObjCPropertyImplDecl *PID);
1186 void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1187 const ObjCPropertyImplDecl *propImpl,
1188 llvm::Constant *AtomicHelperFn);
1189 bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
1190 bool IvarTypeWithAggrGCObjects(QualType Ty);
1192 //===--------------------------------------------------------------------===//
1194 //===--------------------------------------------------------------------===//
1196 llvm::Value *EmitBlockLiteral(const BlockExpr *);
1197 llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
1198 static void destroyBlockInfos(CGBlockInfo *info);
1199 llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
1200 const CGBlockInfo &Info,
1202 llvm::Constant *BlockVarLayout);
1204 llvm::Function *GenerateBlockFunction(GlobalDecl GD,
1205 const CGBlockInfo &Info,
1206 const DeclMapTy &ldm,
1207 bool IsLambdaConversionToBlock);
1209 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1210 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
1211 llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
1212 const ObjCPropertyImplDecl *PID);
1213 llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
1214 const ObjCPropertyImplDecl *PID);
1215 llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
1217 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1219 class AutoVarEmission;
1221 void emitByrefStructureInit(const AutoVarEmission &emission);
1222 void enterByrefCleanup(const AutoVarEmission &emission);
1224 llvm::Value *LoadBlockStruct() {
1225 assert(BlockPointer && "no block pointer set!");
1226 return BlockPointer;
1229 void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
1230 void AllocateBlockDecl(const DeclRefExpr *E);
1231 llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
1232 llvm::Type *BuildByRefType(const VarDecl *var);
1234 void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1235 const CGFunctionInfo &FnInfo);
1236 /// \brief Emit code for the start of a function.
1237 /// \param Loc The location to be associated with the function.
1238 /// \param StartLoc The location of the function body.
1239 void StartFunction(GlobalDecl GD,
1242 const CGFunctionInfo &FnInfo,
1243 const FunctionArgList &Args,
1244 SourceLocation Loc = SourceLocation(),
1245 SourceLocation StartLoc = SourceLocation());
1247 void EmitConstructorBody(FunctionArgList &Args);
1248 void EmitDestructorBody(FunctionArgList &Args);
1249 void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
1250 void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body);
1251 void EmitBlockWithFallThrough(llvm::BasicBlock *BB, RegionCounter &Cnt);
1253 void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
1254 CallArgList &CallArgs);
1255 void EmitLambdaToBlockPointerBody(FunctionArgList &Args);
1256 void EmitLambdaBlockInvokeBody();
1257 void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
1258 void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD);
1259 void EmitAsanPrologueOrEpilogue(bool Prologue);
1261 /// EmitReturnBlock - Emit the unified return block, trying to avoid its
1262 /// emission when possible.
1263 llvm::DebugLoc EmitReturnBlock();
1265 /// FinishFunction - Complete IR generation of the current function. It is
1266 /// legal to call this function even if there is no current insertion point.
1267 void FinishFunction(SourceLocation EndLoc=SourceLocation());
1269 void StartThunk(llvm::Function *Fn, GlobalDecl GD, const CGFunctionInfo &FnInfo);
1271 void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk);
1273 /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
1274 void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr,
1275 llvm::Value *Callee);
1277 /// GenerateThunk - Generate a thunk for the given method.
1278 void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1279 GlobalDecl GD, const ThunkInfo &Thunk);
1281 void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1282 GlobalDecl GD, const ThunkInfo &Thunk);
1284 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1285 FunctionArgList &Args);
1287 void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init,
1288 ArrayRef<VarDecl *> ArrayIndexes);
1290 /// InitializeVTablePointer - Initialize the vtable pointer of the given
1293 void InitializeVTablePointer(BaseSubobject Base,
1294 const CXXRecordDecl *NearestVBase,
1295 CharUnits OffsetFromNearestVBase,
1296 const CXXRecordDecl *VTableClass);
1298 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1299 void InitializeVTablePointers(BaseSubobject Base,
1300 const CXXRecordDecl *NearestVBase,
1301 CharUnits OffsetFromNearestVBase,
1302 bool BaseIsNonVirtualPrimaryBase,
1303 const CXXRecordDecl *VTableClass,
1304 VisitedVirtualBasesSetTy& VBases);
1306 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
1308 /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1310 llvm::Value *GetVTablePtr(llvm::Value *This, llvm::Type *Ty);
1313 /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
1314 /// expr can be devirtualized.
1315 bool CanDevirtualizeMemberFunctionCall(const Expr *Base,
1316 const CXXMethodDecl *MD);
1318 /// EnterDtorCleanups - Enter the cleanups necessary to complete the
1319 /// given phase of destruction for a destructor. The end result
1320 /// should call destructors on members and base classes in reverse
1321 /// order of their construction.
1322 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
1324 /// ShouldInstrumentFunction - Return true if the current function should be
1325 /// instrumented with __cyg_profile_func_* calls
1326 bool ShouldInstrumentFunction();
1328 /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1329 /// instrumentation function with the current function and the call site, if
1330 /// function instrumentation is enabled.
1331 void EmitFunctionInstrumentation(const char *Fn);
1333 /// EmitMCountInstrumentation - Emit call to .mcount.
1334 void EmitMCountInstrumentation();
1336 /// EmitFunctionProlog - Emit the target specific LLVM code to load the
1337 /// arguments for the given function. This is also responsible for naming the
1338 /// LLVM function arguments.
1339 void EmitFunctionProlog(const CGFunctionInfo &FI,
1341 const FunctionArgList &Args);
1343 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
1344 /// given temporary.
1345 void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
1346 SourceLocation EndLoc);
1348 /// EmitStartEHSpec - Emit the start of the exception spec.
1349 void EmitStartEHSpec(const Decl *D);
1351 /// EmitEndEHSpec - Emit the end of the exception spec.
1352 void EmitEndEHSpec(const Decl *D);
1354 /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1355 llvm::BasicBlock *getTerminateLandingPad();
1357 /// getTerminateHandler - Return a handler (not a landing pad, just
1358 /// a catch handler) that just calls terminate. This is used when
1359 /// a terminate scope encloses a try.
1360 llvm::BasicBlock *getTerminateHandler();
1362 llvm::Type *ConvertTypeForMem(QualType T);
1363 llvm::Type *ConvertType(QualType T);
1364 llvm::Type *ConvertType(const TypeDecl *T) {
1365 return ConvertType(getContext().getTypeDeclType(T));
1368 /// LoadObjCSelf - Load the value of self. This function is only valid while
1369 /// generating code for an Objective-C method.
1370 llvm::Value *LoadObjCSelf();
1372 /// TypeOfSelfObject - Return type of object that this self represents.
1373 QualType TypeOfSelfObject();
1375 /// hasAggregateLLVMType - Return true if the specified AST type will map into
1376 /// an aggregate LLVM type or is void.
1377 static TypeEvaluationKind getEvaluationKind(QualType T);
1379 static bool hasScalarEvaluationKind(QualType T) {
1380 return getEvaluationKind(T) == TEK_Scalar;
1383 static bool hasAggregateEvaluationKind(QualType T) {
1384 return getEvaluationKind(T) == TEK_Aggregate;
1387 /// createBasicBlock - Create an LLVM basic block.
1388 llvm::BasicBlock *createBasicBlock(const Twine &name = "",
1389 llvm::Function *parent = nullptr,
1390 llvm::BasicBlock *before = nullptr) {
1392 return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
1394 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
1398 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
1400 JumpDest getJumpDestForLabel(const LabelDecl *S);
1402 /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1403 /// another basic block, simplify it. This assumes that no other code could
1404 /// potentially reference the basic block.
1405 void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1407 /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
1408 /// adding a fall-through branch from the current insert block if
1409 /// necessary. It is legal to call this function even if there is no current
1410 /// insertion point.
1412 /// IsFinished - If true, indicates that the caller has finished emitting
1413 /// branches to the given block and does not expect to emit code into it. This
1414 /// means the block can be ignored if it is unreachable.
1415 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
1417 /// EmitBlockAfterUses - Emit the given block somewhere hopefully
1418 /// near its uses, and leave the insertion point in it.
1419 void EmitBlockAfterUses(llvm::BasicBlock *BB);
1421 /// EmitBranch - Emit a branch to the specified basic block from the current
1422 /// insert block, taking care to avoid creation of branches from dummy
1423 /// blocks. It is legal to call this function even if there is no current
1424 /// insertion point.
1426 /// This function clears the current insertion point. The caller should follow
1427 /// calls to this function with calls to Emit*Block prior to generation new
1429 void EmitBranch(llvm::BasicBlock *Block);
1431 /// HaveInsertPoint - True if an insertion point is defined. If not, this
1432 /// indicates that the current code being emitted is unreachable.
1433 bool HaveInsertPoint() const {
1434 return Builder.GetInsertBlock() != nullptr;
1437 /// EnsureInsertPoint - Ensure that an insertion point is defined so that
1438 /// emitted IR has a place to go. Note that by definition, if this function
1439 /// creates a block then that block is unreachable; callers may do better to
1440 /// detect when no insertion point is defined and simply skip IR generation.
1441 void EnsureInsertPoint() {
1442 if (!HaveInsertPoint())
1443 EmitBlock(createBasicBlock());
1446 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1447 /// specified stmt yet.
1448 void ErrorUnsupported(const Stmt *S, const char *Type);
1450 //===--------------------------------------------------------------------===//
1452 //===--------------------------------------------------------------------===//
1454 LValue MakeAddrLValue(llvm::Value *V, QualType T,
1455 CharUnits Alignment = CharUnits()) {
1456 return LValue::MakeAddr(V, T, Alignment, getContext(),
1457 CGM.getTBAAInfo(T));
1460 LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
1462 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
1463 /// block. The caller is responsible for setting an appropriate alignment on
1465 llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
1466 const Twine &Name = "tmp");
1468 /// InitTempAlloca - Provide an initial value for the given alloca.
1469 void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1471 /// CreateIRTemp - Create a temporary IR object of the given type, with
1472 /// appropriate alignment. This routine should only be used when an temporary
1473 /// value needs to be stored into an alloca (for example, to avoid explicit
1474 /// PHI construction), but the type is the IR type, not the type appropriate
1475 /// for storing in memory.
1476 llvm::AllocaInst *CreateIRTemp(QualType T, const Twine &Name = "tmp");
1478 /// CreateMemTemp - Create a temporary memory object of the given type, with
1479 /// appropriate alignment.
1480 llvm::AllocaInst *CreateMemTemp(QualType T, const Twine &Name = "tmp");
1482 /// CreateAggTemp - Create a temporary memory object for the given
1484 AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
1485 CharUnits Alignment = getContext().getTypeAlignInChars(T);
1486 return AggValueSlot::forAddr(CreateMemTemp(T, Name), Alignment,
1488 AggValueSlot::IsNotDestructed,
1489 AggValueSlot::DoesNotNeedGCBarriers,
1490 AggValueSlot::IsNotAliased);
1493 /// CreateInAllocaTmp - Create a temporary memory object for the given
1495 AggValueSlot CreateInAllocaTmp(QualType T, const Twine &Name = "inalloca");
1497 /// Emit a cast to void* in the appropriate address space.
1498 llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1500 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
1501 /// expression and compare the result against zero, returning an Int1Ty value.
1502 llvm::Value *EvaluateExprAsBool(const Expr *E);
1504 /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
1505 void EmitIgnoredExpr(const Expr *E);
1507 /// EmitAnyExpr - Emit code to compute the specified expression which can have
1508 /// any type. The result is returned as an RValue struct. If this is an
1509 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
1510 /// the result should be returned.
1512 /// \param ignoreResult True if the resulting value isn't used.
1513 RValue EmitAnyExpr(const Expr *E,
1514 AggValueSlot aggSlot = AggValueSlot::ignored(),
1515 bool ignoreResult = false);
1517 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
1518 // or the value of the expression, depending on how va_list is defined.
1519 llvm::Value *EmitVAListRef(const Expr *E);
1521 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
1522 /// always be accessible even if no aggregate location is provided.
1523 RValue EmitAnyExprToTemp(const Expr *E);
1525 /// EmitAnyExprToMem - Emits the code necessary to evaluate an
1526 /// arbitrary expression into the given memory location.
1527 void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
1528 Qualifiers Quals, bool IsInitializer);
1530 /// EmitExprAsInit - Emits the code necessary to initialize a
1531 /// location in memory with the given initializer.
1532 void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1533 bool capturedByInit);
1535 /// hasVolatileMember - returns true if aggregate type has a volatile
1537 bool hasVolatileMember(QualType T) {
1538 if (const RecordType *RT = T->getAs<RecordType>()) {
1539 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
1540 return RD->hasVolatileMember();
1544 /// EmitAggregateCopy - Emit an aggregate assignment.
1546 /// The difference to EmitAggregateCopy is that tail padding is not copied.
1547 /// This is required for correctness when assigning non-POD structures in C++.
1548 void EmitAggregateAssign(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1550 bool IsVolatile = hasVolatileMember(EltTy);
1551 EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, CharUnits::Zero(),
1555 /// EmitAggregateCopy - Emit an aggregate copy.
1557 /// \param isVolatile - True iff either the source or the destination is
1559 /// \param isAssignment - If false, allow padding to be copied. This often
1560 /// yields more efficient.
1561 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1562 QualType EltTy, bool isVolatile=false,
1563 CharUnits Alignment = CharUnits::Zero(),
1564 bool isAssignment = false);
1566 /// StartBlock - Start new block named N. If insert block is a dummy block
1568 void StartBlock(const char *N);
1570 /// GetAddrOfLocalVar - Return the address of a local variable.
1571 llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
1572 llvm::Value *Res = LocalDeclMap[VD];
1573 assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
1577 /// getOpaqueLValueMapping - Given an opaque value expression (which
1578 /// must be mapped to an l-value), return its mapping.
1579 const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
1580 assert(OpaqueValueMapping::shouldBindAsLValue(e));
1582 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
1583 it = OpaqueLValues.find(e);
1584 assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
1588 /// getOpaqueRValueMapping - Given an opaque value expression (which
1589 /// must be mapped to an r-value), return its mapping.
1590 const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
1591 assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1593 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
1594 it = OpaqueRValues.find(e);
1595 assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
1599 /// getAccessedFieldNo - Given an encoded value and a result number, return
1600 /// the input field number being accessed.
1601 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
1603 llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
1604 llvm::BasicBlock *GetIndirectGotoBlock();
1606 /// EmitNullInitialization - Generate code to set a value of the given type to
1607 /// null, If the type contains data member pointers, they will be initialized
1608 /// to -1 in accordance with the Itanium C++ ABI.
1609 void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
1611 // EmitVAArg - Generate code to get an argument from the passed in pointer
1612 // and update it accordingly. The return value is a pointer to the argument.
1613 // FIXME: We should be able to get rid of this method and use the va_arg
1614 // instruction in LLVM instead once it works well enough.
1615 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
1617 /// emitArrayLength - Compute the length of an array, even if it's a
1618 /// VLA, and drill down to the base element type.
1619 llvm::Value *emitArrayLength(const ArrayType *arrayType,
1621 llvm::Value *&addr);
1623 /// EmitVLASize - Capture all the sizes for the VLA expressions in
1624 /// the given variably-modified type and store them in the VLASizeMap.
1626 /// This function can be called with a null (unreachable) insert point.
1627 void EmitVariablyModifiedType(QualType Ty);
1629 /// getVLASize - Returns an LLVM value that corresponds to the size,
1630 /// in non-variably-sized elements, of a variable length array type,
1631 /// plus that largest non-variably-sized element type. Assumes that
1632 /// the type has already been emitted with EmitVariablyModifiedType.
1633 std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1634 std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
1636 /// LoadCXXThis - Load the value of 'this'. This function is only valid while
1637 /// generating code for an C++ member function.
1638 llvm::Value *LoadCXXThis() {
1639 assert(CXXThisValue && "no 'this' value for this function");
1640 return CXXThisValue;
1643 /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1645 // FIXME: Every place that calls LoadCXXVTT is something
1646 // that needs to be abstracted properly.
1647 llvm::Value *LoadCXXVTT() {
1648 assert(CXXStructorImplicitParamValue && "no VTT value for this function");
1649 return CXXStructorImplicitParamValue;
1652 /// LoadCXXStructorImplicitParam - Load the implicit parameter
1653 /// for a constructor/destructor.
1654 llvm::Value *LoadCXXStructorImplicitParam() {
1655 assert(CXXStructorImplicitParamValue &&
1656 "no implicit argument value for this function");
1657 return CXXStructorImplicitParamValue;
1660 /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
1661 /// complete class to the given direct base.
1663 GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
1664 const CXXRecordDecl *Derived,
1665 const CXXRecordDecl *Base,
1666 bool BaseIsVirtual);
1668 /// GetAddressOfBaseClass - This function will add the necessary delta to the
1669 /// load of 'this' and returns address of the base class.
1670 llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
1671 const CXXRecordDecl *Derived,
1672 CastExpr::path_const_iterator PathBegin,
1673 CastExpr::path_const_iterator PathEnd,
1674 bool NullCheckValue, SourceLocation Loc);
1676 llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
1677 const CXXRecordDecl *Derived,
1678 CastExpr::path_const_iterator PathBegin,
1679 CastExpr::path_const_iterator PathEnd,
1680 bool NullCheckValue);
1682 /// GetVTTParameter - Return the VTT parameter that should be passed to a
1683 /// base constructor/destructor with virtual bases.
1684 /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
1685 /// to ItaniumCXXABI.cpp together with all the references to VTT.
1686 llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
1689 void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1690 CXXCtorType CtorType,
1691 const FunctionArgList &Args,
1692 SourceLocation Loc);
1693 // It's important not to confuse this and the previous function. Delegating
1694 // constructors are the C++0x feature. The constructor delegate optimization
1695 // is used to reduce duplication in the base and complete consturctors where
1696 // they are substantially the same.
1697 void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1698 const FunctionArgList &Args);
1699 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1700 bool ForVirtualBase, bool Delegating,
1701 llvm::Value *This, const CXXConstructExpr *E);
1703 void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1704 llvm::Value *This, llvm::Value *Src,
1705 const CXXConstructExpr *E);
1707 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1708 const ConstantArrayType *ArrayTy,
1709 llvm::Value *ArrayPtr,
1710 const CXXConstructExpr *E,
1711 bool ZeroInitialization = false);
1713 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1714 llvm::Value *NumElements,
1715 llvm::Value *ArrayPtr,
1716 const CXXConstructExpr *E,
1717 bool ZeroInitialization = false);
1719 static Destroyer destroyCXXObject;
1721 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
1722 bool ForVirtualBase, bool Delegating,
1725 void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
1726 llvm::Value *NewPtr, llvm::Value *NumElements,
1727 llvm::Value *AllocSizeWithoutCookie);
1729 void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
1732 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
1733 void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
1735 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
1738 RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
1739 const Expr *Arg, bool IsDelete);
1741 llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
1742 llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
1743 llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E);
1745 /// \brief Situations in which we might emit a check for the suitability of a
1746 /// pointer or glvalue.
1747 enum TypeCheckKind {
1748 /// Checking the operand of a load. Must be suitably sized and aligned.
1750 /// Checking the destination of a store. Must be suitably sized and aligned.
1752 /// Checking the bound value in a reference binding. Must be suitably sized
1753 /// and aligned, but is not required to refer to an object (until the
1754 /// reference is used), per core issue 453.
1755 TCK_ReferenceBinding,
1756 /// Checking the object expression in a non-static data member access. Must
1757 /// be an object within its lifetime.
1759 /// Checking the 'this' pointer for a call to a non-static member function.
1760 /// Must be an object within its lifetime.
1762 /// Checking the 'this' pointer for a constructor call.
1763 TCK_ConstructorCall,
1764 /// Checking the operand of a static_cast to a derived pointer type. Must be
1765 /// null or an object within its lifetime.
1766 TCK_DowncastPointer,
1767 /// Checking the operand of a static_cast to a derived reference type. Must
1768 /// be an object within its lifetime.
1769 TCK_DowncastReference,
1770 /// Checking the operand of a cast to a base object. Must be suitably sized
1773 /// Checking the operand of a cast to a virtual base object. Must be an
1774 /// object within its lifetime.
1775 TCK_UpcastToVirtualBase
1778 /// \brief Whether any type-checking sanitizers are enabled. If \c false,
1779 /// calls to EmitTypeCheck can be skipped.
1780 bool sanitizePerformTypeCheck() const;
1782 /// \brief Emit a check that \p V is the address of storage of the
1783 /// appropriate size and alignment for an object of type \p Type.
1784 void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
1785 QualType Type, CharUnits Alignment = CharUnits::Zero(),
1786 bool SkipNullCheck = false);
1788 /// \brief Emit a check that \p Base points into an array object, which
1789 /// we can access at index \p Index. \p Accessed should be \c false if we
1790 /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
1791 void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
1792 QualType IndexType, bool Accessed);
1794 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1795 bool isInc, bool isPre);
1796 ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1797 bool isInc, bool isPre);
1799 void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment,
1800 llvm::Value *OffsetValue = nullptr) {
1801 Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
1805 //===--------------------------------------------------------------------===//
1806 // Declaration Emission
1807 //===--------------------------------------------------------------------===//
1809 /// EmitDecl - Emit a declaration.
1811 /// This function can be called with a null (unreachable) insert point.
1812 void EmitDecl(const Decl &D);
1814 /// EmitVarDecl - Emit a local variable declaration.
1816 /// This function can be called with a null (unreachable) insert point.
1817 void EmitVarDecl(const VarDecl &D);
1819 void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1820 bool capturedByInit);
1821 void EmitScalarInit(llvm::Value *init, LValue lvalue);
1823 typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1824 llvm::Value *Address);
1826 /// \brief Determine whether the given initializer is trivial in the sense
1827 /// that it requires no code to be generated.
1828 bool isTrivialInitializer(const Expr *Init);
1830 /// EmitAutoVarDecl - Emit an auto variable declaration.
1832 /// This function can be called with a null (unreachable) insert point.
1833 void EmitAutoVarDecl(const VarDecl &D);
1835 class AutoVarEmission {
1836 friend class CodeGenFunction;
1838 const VarDecl *Variable;
1840 /// The alignment of the variable.
1841 CharUnits Alignment;
1843 /// The address of the alloca. Null if the variable was emitted
1844 /// as a global constant.
1845 llvm::Value *Address;
1847 llvm::Value *NRVOFlag;
1849 /// True if the variable is a __block variable.
1852 /// True if the variable is of aggregate type and has a constant
1854 bool IsConstantAggregate;
1856 /// Non-null if we should use lifetime annotations.
1857 llvm::Value *SizeForLifetimeMarkers;
1860 AutoVarEmission(Invalid) : Variable(nullptr) {}
1862 AutoVarEmission(const VarDecl &variable)
1863 : Variable(&variable), Address(nullptr), NRVOFlag(nullptr),
1864 IsByRef(false), IsConstantAggregate(false),
1865 SizeForLifetimeMarkers(nullptr) {}
1867 bool wasEmittedAsGlobal() const { return Address == nullptr; }
1870 static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
1872 bool useLifetimeMarkers() const {
1873 return SizeForLifetimeMarkers != nullptr;
1875 llvm::Value *getSizeForLifetimeMarkers() const {
1876 assert(useLifetimeMarkers());
1877 return SizeForLifetimeMarkers;
1880 /// Returns the raw, allocated address, which is not necessarily
1881 /// the address of the object itself.
1882 llvm::Value *getAllocatedAddress() const {
1886 /// Returns the address of the object within this declaration.
1887 /// Note that this does not chase the forwarding pointer for
1889 llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
1890 if (!IsByRef) return Address;
1892 return CGF.Builder.CreateStructGEP(Address,
1893 CGF.getByRefValueLLVMField(Variable),
1894 Variable->getNameAsString());
1897 AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
1898 void EmitAutoVarInit(const AutoVarEmission &emission);
1899 void EmitAutoVarCleanups(const AutoVarEmission &emission);
1900 void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
1901 QualType::DestructionKind dtorKind);
1903 void EmitStaticVarDecl(const VarDecl &D,
1904 llvm::GlobalValue::LinkageTypes Linkage);
1906 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
1907 void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, bool ArgIsPointer,
1910 /// protectFromPeepholes - Protect a value that we're intending to
1911 /// store to the side, but which will probably be used later, from
1912 /// aggressive peepholing optimizations that might delete it.
1914 /// Pass the result to unprotectFromPeepholes to declare that
1915 /// protection is no longer required.
1917 /// There's no particular reason why this shouldn't apply to
1918 /// l-values, it's just that no existing peepholes work on pointers.
1919 PeepholeProtection protectFromPeepholes(RValue rvalue);
1920 void unprotectFromPeepholes(PeepholeProtection protection);
1922 //===--------------------------------------------------------------------===//
1923 // Statement Emission
1924 //===--------------------------------------------------------------------===//
1926 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
1927 void EmitStopPoint(const Stmt *S);
1929 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
1930 /// this function even if there is no current insertion point.
1932 /// This function may clear the current insertion point; callers should use
1933 /// EnsureInsertPoint if they wish to subsequently generate code without first
1934 /// calling EmitBlock, EmitBranch, or EmitStmt.
1935 void EmitStmt(const Stmt *S);
1937 /// EmitSimpleStmt - Try to emit a "simple" statement which does not
1938 /// necessarily require an insertion point or debug information; typically
1939 /// because the statement amounts to a jump or a container of other
1942 /// \return True if the statement was handled.
1943 bool EmitSimpleStmt(const Stmt *S);
1945 llvm::Value *EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
1946 AggValueSlot AVS = AggValueSlot::ignored());
1947 llvm::Value *EmitCompoundStmtWithoutScope(const CompoundStmt &S,
1948 bool GetLast = false,
1950 AggValueSlot::ignored());
1952 /// EmitLabel - Emit the block for the given label. It is legal to call this
1953 /// function even if there is no current insertion point.
1954 void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
1956 void EmitLabelStmt(const LabelStmt &S);
1957 void EmitAttributedStmt(const AttributedStmt &S);
1958 void EmitGotoStmt(const GotoStmt &S);
1959 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
1960 void EmitIfStmt(const IfStmt &S);
1962 void EmitCondBrHints(llvm::LLVMContext &Context, llvm::BranchInst *CondBr,
1963 ArrayRef<const Attr *> Attrs);
1964 void EmitWhileStmt(const WhileStmt &S,
1965 ArrayRef<const Attr *> Attrs = None);
1966 void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
1967 void EmitForStmt(const ForStmt &S,
1968 ArrayRef<const Attr *> Attrs = None);
1969 void EmitReturnStmt(const ReturnStmt &S);
1970 void EmitDeclStmt(const DeclStmt &S);
1971 void EmitBreakStmt(const BreakStmt &S);
1972 void EmitContinueStmt(const ContinueStmt &S);
1973 void EmitSwitchStmt(const SwitchStmt &S);
1974 void EmitDefaultStmt(const DefaultStmt &S);
1975 void EmitCaseStmt(const CaseStmt &S);
1976 void EmitCaseStmtRange(const CaseStmt &S);
1977 void EmitAsmStmt(const AsmStmt &S);
1979 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
1980 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
1981 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
1982 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
1983 void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
1985 void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
1986 void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
1988 void EmitCXXTryStmt(const CXXTryStmt &S);
1989 void EmitSEHTryStmt(const SEHTryStmt &S);
1990 void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
1991 void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
1992 ArrayRef<const Attr *> Attrs = None);
1994 LValue InitCapturedStruct(const CapturedStmt &S);
1995 llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
1996 void GenerateCapturedStmtFunctionProlog(const CapturedStmt &S);
1997 llvm::Function *GenerateCapturedStmtFunctionEpilog(const CapturedStmt &S);
1998 llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
1999 llvm::Value *GenerateCapturedStmtArgument(const CapturedStmt &S);
2000 void EmitOMPAggregateAssign(LValue OriginalAddr, llvm::Value *PrivateAddr,
2001 const Expr *AssignExpr, QualType Type,
2002 const VarDecl *VDInit);
2003 void EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
2004 OMPPrivateScope &PrivateScope);
2005 void EmitOMPPrivateClause(const OMPExecutableDirective &D,
2006 OMPPrivateScope &PrivateScope);
2008 void EmitOMPParallelDirective(const OMPParallelDirective &S);
2009 void EmitOMPSimdDirective(const OMPSimdDirective &S);
2010 void EmitOMPForDirective(const OMPForDirective &S);
2011 void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
2012 void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
2013 void EmitOMPSectionDirective(const OMPSectionDirective &S);
2014 void EmitOMPSingleDirective(const OMPSingleDirective &S);
2015 void EmitOMPMasterDirective(const OMPMasterDirective &S);
2016 void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
2017 void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
2018 void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
2019 void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
2020 void EmitOMPTaskDirective(const OMPTaskDirective &S);
2021 void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
2022 void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
2023 void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
2024 void EmitOMPFlushDirective(const OMPFlushDirective &S);
2025 void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
2026 void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
2027 void EmitOMPTargetDirective(const OMPTargetDirective &S);
2028 void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
2032 /// Helpers for the OpenMP loop directives.
2033 void EmitOMPLoopBody(const OMPLoopDirective &Directive,
2034 bool SeparateIter = false);
2035 void EmitOMPInnerLoop(const OMPLoopDirective &S, OMPPrivateScope &LoopScope,
2036 bool SeparateIter = false);
2037 void EmitOMPSimdFinal(const OMPLoopDirective &S);
2038 void EmitOMPWorksharingLoop(const OMPLoopDirective &S);
2042 //===--------------------------------------------------------------------===//
2043 // LValue Expression Emission
2044 //===--------------------------------------------------------------------===//
2046 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
2047 RValue GetUndefRValue(QualType Ty);
2049 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
2050 /// and issue an ErrorUnsupported style diagnostic (using the
2052 RValue EmitUnsupportedRValue(const Expr *E,
2055 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
2056 /// an ErrorUnsupported style diagnostic (using the provided Name).
2057 LValue EmitUnsupportedLValue(const Expr *E,
2060 /// EmitLValue - Emit code to compute a designator that specifies the location
2061 /// of the expression.
2063 /// This can return one of two things: a simple address or a bitfield
2064 /// reference. In either case, the LLVM Value* in the LValue structure is
2065 /// guaranteed to be an LLVM pointer type.
2067 /// If this returns a bitfield reference, nothing about the pointee type of
2068 /// the LLVM value is known: For example, it may not be a pointer to an
2071 /// If this returns a normal address, and if the lvalue's C type is fixed
2072 /// size, this method guarantees that the returned pointer type will point to
2073 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
2074 /// variable length type, this is not possible.
2076 LValue EmitLValue(const Expr *E);
2078 /// \brief Same as EmitLValue but additionally we generate checking code to
2079 /// guard against undefined behavior. This is only suitable when we know
2080 /// that the address will be used to access the object.
2081 LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
2083 RValue convertTempToRValue(llvm::Value *addr, QualType type,
2084 SourceLocation Loc);
2086 void EmitAtomicInit(Expr *E, LValue lvalue);
2088 RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
2089 AggValueSlot slot = AggValueSlot::ignored());
2091 void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
2093 std::pair<RValue, RValue> EmitAtomicCompareExchange(
2094 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
2095 llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
2096 llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
2097 bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored());
2099 /// EmitToMemory - Change a scalar value from its value
2100 /// representation to its in-memory representation.
2101 llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
2103 /// EmitFromMemory - Change a scalar value from its memory
2104 /// representation to its value representation.
2105 llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
2107 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2108 /// care to appropriately convert from the memory representation to
2109 /// the LLVM value representation.
2110 llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
2111 unsigned Alignment, QualType Ty,
2113 llvm::MDNode *TBAAInfo = nullptr,
2114 QualType TBAABaseTy = QualType(),
2115 uint64_t TBAAOffset = 0);
2117 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2118 /// care to appropriately convert from the memory representation to
2119 /// the LLVM value representation. The l-value must be a simple
2121 llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
2123 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2124 /// care to appropriately convert from the memory representation to
2125 /// the LLVM value representation.
2126 void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
2127 bool Volatile, unsigned Alignment, QualType Ty,
2128 llvm::MDNode *TBAAInfo = nullptr, bool isInit = false,
2129 QualType TBAABaseTy = QualType(),
2130 uint64_t TBAAOffset = 0);
2132 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2133 /// care to appropriately convert from the memory representation to
2134 /// the LLVM value representation. The l-value must be a simple
2135 /// l-value. The isInit flag indicates whether this is an initialization.
2136 /// If so, atomic qualifiers are ignored and the store is always non-atomic.
2137 void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
2139 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
2140 /// this method emits the address of the lvalue, then loads the result as an
2141 /// rvalue, returning the rvalue.
2142 RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
2143 RValue EmitLoadOfExtVectorElementLValue(LValue V);
2144 RValue EmitLoadOfBitfieldLValue(LValue LV);
2145 RValue EmitLoadOfGlobalRegLValue(LValue LV);
2147 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
2148 /// lvalue, where both are guaranteed to the have the same type, and that type
2150 void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false);
2151 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
2152 void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
2154 /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
2155 /// as EmitStoreThroughLValue.
2157 /// \param Result [out] - If non-null, this will be set to a Value* for the
2158 /// bit-field contents after the store, appropriate for use as the result of
2159 /// an assignment to the bit-field.
2160 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
2161 llvm::Value **Result=nullptr);
2163 /// Emit an l-value for an assignment (simple or compound) of complex type.
2164 LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
2165 LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
2166 LValue EmitScalarCompooundAssignWithComplex(const CompoundAssignOperator *E,
2167 llvm::Value *&Result);
2169 // Note: only available for agg return types
2170 LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
2171 LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
2172 // Note: only available for agg return types
2173 LValue EmitCallExprLValue(const CallExpr *E);
2174 // Note: only available for agg return types
2175 LValue EmitVAArgExprLValue(const VAArgExpr *E);
2176 LValue EmitDeclRefLValue(const DeclRefExpr *E);
2177 LValue EmitReadRegister(const VarDecl *VD);
2178 LValue EmitStringLiteralLValue(const StringLiteral *E);
2179 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
2180 LValue EmitPredefinedLValue(const PredefinedExpr *E);
2181 LValue EmitUnaryOpLValue(const UnaryOperator *E);
2182 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2183 bool Accessed = false);
2184 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
2185 LValue EmitMemberExpr(const MemberExpr *E);
2186 LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
2187 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
2188 LValue EmitInitListLValue(const InitListExpr *E);
2189 LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
2190 LValue EmitCastLValue(const CastExpr *E);
2191 LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
2192 LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
2194 llvm::Value *EmitExtVectorElementLValue(LValue V);
2196 RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
2198 class ConstantEmission {
2199 llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
2200 ConstantEmission(llvm::Constant *C, bool isReference)
2201 : ValueAndIsReference(C, isReference) {}
2203 ConstantEmission() {}
2204 static ConstantEmission forReference(llvm::Constant *C) {
2205 return ConstantEmission(C, true);
2207 static ConstantEmission forValue(llvm::Constant *C) {
2208 return ConstantEmission(C, false);
2211 LLVM_EXPLICIT operator bool() const {
2212 return ValueAndIsReference.getOpaqueValue() != nullptr;
2215 bool isReference() const { return ValueAndIsReference.getInt(); }
2216 LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
2217 assert(isReference());
2218 return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
2219 refExpr->getType());
2222 llvm::Constant *getValue() const {
2223 assert(!isReference());
2224 return ValueAndIsReference.getPointer();
2228 ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
2230 RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
2231 AggValueSlot slot = AggValueSlot::ignored());
2232 LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
2234 llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
2235 const ObjCIvarDecl *Ivar);
2236 LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
2237 LValue EmitLValueForLambdaField(const FieldDecl *Field);
2239 /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
2240 /// if the Field is a reference, this will return the address of the reference
2241 /// and not the address of the value stored in the reference.
2242 LValue EmitLValueForFieldInitialization(LValue Base,
2243 const FieldDecl* Field);
2245 LValue EmitLValueForIvar(QualType ObjectTy,
2246 llvm::Value* Base, const ObjCIvarDecl *Ivar,
2247 unsigned CVRQualifiers);
2249 LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
2250 LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
2251 LValue EmitLambdaLValue(const LambdaExpr *E);
2252 LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
2253 LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
2255 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
2256 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
2257 LValue EmitStmtExprLValue(const StmtExpr *E);
2258 LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
2259 LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
2260 void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
2262 //===--------------------------------------------------------------------===//
2263 // Scalar Expression Emission
2264 //===--------------------------------------------------------------------===//
2266 /// EmitCall - Generate a call of the given function, expecting the given
2267 /// result type, and using the given argument list which specifies both the
2268 /// LLVM arguments and the types they were derived from.
2270 /// \param TargetDecl - If given, the decl of the function in a direct call;
2271 /// used to set attributes on the call (noreturn, etc.).
2272 RValue EmitCall(const CGFunctionInfo &FnInfo,
2273 llvm::Value *Callee,
2274 ReturnValueSlot ReturnValue,
2275 const CallArgList &Args,
2276 const Decl *TargetDecl = nullptr,
2277 llvm::Instruction **callOrInvoke = nullptr);
2279 RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E,
2280 ReturnValueSlot ReturnValue,
2281 const Decl *TargetDecl = nullptr,
2282 llvm::Value *Chain = nullptr);
2283 RValue EmitCallExpr(const CallExpr *E,
2284 ReturnValueSlot ReturnValue = ReturnValueSlot());
2286 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2287 const Twine &name = "");
2288 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2289 ArrayRef<llvm::Value*> args,
2290 const Twine &name = "");
2291 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2292 const Twine &name = "");
2293 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2294 ArrayRef<llvm::Value*> args,
2295 const Twine &name = "");
2297 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2298 ArrayRef<llvm::Value *> Args,
2299 const Twine &Name = "");
2300 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2301 const Twine &Name = "");
2302 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2303 ArrayRef<llvm::Value*> args,
2304 const Twine &name = "");
2305 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2306 const Twine &name = "");
2307 void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2308 ArrayRef<llvm::Value*> args);
2310 llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
2311 NestedNameSpecifier *Qual,
2314 llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2316 const CXXRecordDecl *RD);
2319 EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2320 ReturnValueSlot ReturnValue, llvm::Value *This,
2321 llvm::Value *ImplicitParam,
2322 QualType ImplicitParamTy, const CallExpr *E);
2323 RValue EmitCXXStructorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2324 ReturnValueSlot ReturnValue, llvm::Value *This,
2325 llvm::Value *ImplicitParam,
2326 QualType ImplicitParamTy, const CallExpr *E,
2328 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2329 ReturnValueSlot ReturnValue);
2330 RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE,
2331 const CXXMethodDecl *MD,
2332 ReturnValueSlot ReturnValue,
2334 NestedNameSpecifier *Qualifier,
2335 bool IsArrow, const Expr *Base);
2336 // Compute the object pointer.
2337 RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2338 ReturnValueSlot ReturnValue);
2340 RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
2341 const CXXMethodDecl *MD,
2342 ReturnValueSlot ReturnValue);
2344 RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
2345 ReturnValueSlot ReturnValue);
2348 RValue EmitBuiltinExpr(const FunctionDecl *FD,
2349 unsigned BuiltinID, const CallExpr *E,
2350 ReturnValueSlot ReturnValue);
2352 RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
2354 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
2355 /// is unhandled by the current target.
2356 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2358 llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
2359 const llvm::CmpInst::Predicate Fp,
2360 const llvm::CmpInst::Predicate Ip,
2361 const llvm::Twine &Name = "");
2362 llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2364 llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
2365 unsigned LLVMIntrinsic,
2366 unsigned AltLLVMIntrinsic,
2367 const char *NameHint,
2370 SmallVectorImpl<llvm::Value *> &Ops,
2371 llvm::Value *Align = nullptr);
2372 llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2373 unsigned Modifier, llvm::Type *ArgTy,
2375 llvm::Value *EmitNeonCall(llvm::Function *F,
2376 SmallVectorImpl<llvm::Value*> &O,
2378 unsigned shift = 0, bool rightshift = false);
2379 llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
2380 llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
2381 bool negateForRightShift);
2382 llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
2383 llvm::Type *Ty, bool usgn, const char *name);
2384 // Helper functions for EmitAArch64BuiltinExpr.
2385 llvm::Value *vectorWrapScalar8(llvm::Value *Op);
2386 llvm::Value *vectorWrapScalar16(llvm::Value *Op);
2387 llvm::Value *emitVectorWrappedScalar8Intrinsic(
2388 unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
2389 llvm::Value *emitVectorWrappedScalar16Intrinsic(
2390 unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
2391 llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2392 llvm::Value *EmitNeon64Call(llvm::Function *F,
2393 llvm::SmallVectorImpl<llvm::Value *> &O,
2396 llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
2397 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2398 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2399 llvm::Value *EmitR600BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2401 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
2402 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
2403 llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
2404 llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
2405 llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2406 llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
2407 const ObjCMethodDecl *MethodWithObjects);
2408 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
2409 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2410 ReturnValueSlot Return = ReturnValueSlot());
2412 /// Retrieves the default cleanup kind for an ARC cleanup.
2413 /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2414 CleanupKind getARCCleanupKind() {
2415 return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2416 ? NormalAndEHCleanup : NormalCleanup;
2420 void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
2421 void EmitARCDestroyWeak(llvm::Value *addr);
2422 llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
2423 llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
2424 llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
2426 void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
2427 void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
2428 llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2429 llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
2430 llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
2431 bool resultIgnored);
2432 llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
2433 bool resultIgnored);
2434 llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2435 llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
2436 llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
2437 void EmitARCDestroyStrong(llvm::Value *addr, ARCPreciseLifetime_t precise);
2438 void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
2439 llvm::Value *EmitARCAutorelease(llvm::Value *value);
2440 llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2441 llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2442 llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2444 std::pair<LValue,llvm::Value*>
2445 EmitARCStoreAutoreleasing(const BinaryOperator *e);
2446 std::pair<LValue,llvm::Value*>
2447 EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2449 llvm::Value *EmitObjCThrowOperand(const Expr *expr);
2451 llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2452 llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2453 llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2455 llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
2456 llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2457 llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2459 void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
2461 static Destroyer destroyARCStrongImprecise;
2462 static Destroyer destroyARCStrongPrecise;
2463 static Destroyer destroyARCWeak;
2465 void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2466 llvm::Value *EmitObjCAutoreleasePoolPush();
2467 llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2468 void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2469 void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2471 /// \brief Emits a reference binding to the passed in expression.
2472 RValue EmitReferenceBindingToExpr(const Expr *E);
2474 //===--------------------------------------------------------------------===//
2475 // Expression Emission
2476 //===--------------------------------------------------------------------===//
2478 // Expressions are broken into three classes: scalar, complex, aggregate.
2480 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
2481 /// scalar type, returning the result.
2482 llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
2484 /// EmitScalarConversion - Emit a conversion from the specified type to the
2485 /// specified destination type, both of which are LLVM scalar types.
2486 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
2489 /// EmitComplexToScalarConversion - Emit a conversion from the specified
2490 /// complex type to the specified destination type, where the destination type
2491 /// is an LLVM scalar type.
2492 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
2496 /// EmitAggExpr - Emit the computation of the specified expression
2497 /// of aggregate type. The result is computed into the given slot,
2498 /// which may be null to indicate that the value is not needed.
2499 void EmitAggExpr(const Expr *E, AggValueSlot AS);
2501 /// EmitAggExprToLValue - Emit the computation of the specified expression of
2502 /// aggregate type into a temporary LValue.
2503 LValue EmitAggExprToLValue(const Expr *E);
2505 /// EmitGCMemmoveCollectable - Emit special API for structs with object
2507 void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
2510 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
2511 /// make sure it survives garbage collection until this point.
2512 void EmitExtendGCLifetime(llvm::Value *object);
2514 /// EmitComplexExpr - Emit the computation of the specified expression of
2515 /// complex type, returning the result.
2516 ComplexPairTy EmitComplexExpr(const Expr *E,
2517 bool IgnoreReal = false,
2518 bool IgnoreImag = false);
2520 /// EmitComplexExprIntoLValue - Emit the given expression of complex
2521 /// type and place its result into the specified l-value.
2522 void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
2524 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
2525 void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
2527 /// EmitLoadOfComplex - Load a complex number from the specified l-value.
2528 ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
2530 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
2531 /// global variable that has already been created for it. If the initializer
2532 /// has a different type than GV does, this may free GV and return a different
2533 /// one. Otherwise it just returns GV.
2534 llvm::GlobalVariable *
2535 AddInitializerToStaticVarDecl(const VarDecl &D,
2536 llvm::GlobalVariable *GV);
2539 /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
2540 /// variable with global storage.
2541 void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr,
2544 llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor,
2545 llvm::Constant *Addr);
2547 /// Call atexit() with a function that passes the given argument to
2548 /// the given function.
2549 void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn,
2550 llvm::Constant *addr);
2552 /// Emit code in this function to perform a guarded variable
2553 /// initialization. Guarded initializations are used when it's not
2554 /// possible to prove that an initialization will be done exactly
2555 /// once, e.g. with a static local variable or a static data member
2556 /// of a class template.
2557 void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
2560 /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2562 void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
2563 ArrayRef<llvm::Function *> CXXThreadLocals,
2564 llvm::GlobalVariable *Guard = nullptr);
2566 /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global
2568 void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
2569 const std::vector<std::pair<llvm::WeakVH,
2570 llvm::Constant*> > &DtorsAndObjects);
2572 void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2574 llvm::GlobalVariable *Addr,
2577 void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
2579 void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
2582 void enterFullExpression(const ExprWithCleanups *E) {
2583 if (E->getNumObjects() == 0) return;
2584 enterNonTrivialFullExpression(E);
2586 void enterNonTrivialFullExpression(const ExprWithCleanups *E);
2588 void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
2590 void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
2592 RValue EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest = nullptr);
2594 //===--------------------------------------------------------------------===//
2595 // Annotations Emission
2596 //===--------------------------------------------------------------------===//
2598 /// Emit an annotation call (intrinsic or builtin).
2599 llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn,
2600 llvm::Value *AnnotatedVal,
2601 StringRef AnnotationStr,
2602 SourceLocation Location);
2604 /// Emit local annotations for the local variable V, declared by D.
2605 void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
2607 /// Emit field annotations for the given field & value. Returns the
2608 /// annotation result.
2609 llvm::Value *EmitFieldAnnotations(const FieldDecl *D, llvm::Value *V);
2611 //===--------------------------------------------------------------------===//
2613 //===--------------------------------------------------------------------===//
2615 /// ContainsLabel - Return true if the statement contains a label in it. If
2616 /// this statement is not executed normally, it not containing a label means
2617 /// that we can just remove the code.
2618 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
2620 /// containsBreak - Return true if the statement contains a break out of it.
2621 /// If the statement (recursively) contains a switch or loop with a break
2622 /// inside of it, this is fine.
2623 static bool containsBreak(const Stmt *S);
2625 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2626 /// to a constant, or if it does but contains a label, return false. If it
2627 /// constant folds return true and set the boolean result in Result.
2628 bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
2630 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2631 /// to a constant, or if it does but contains a label, return false. If it
2632 /// constant folds return true and set the folded value.
2633 bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result);
2635 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
2636 /// if statement) to the specified blocks. Based on the condition, this might
2637 /// try to simplify the codegen of the conditional based on the branch.
2638 /// TrueCount should be the number of times we expect the condition to
2639 /// evaluate to true based on PGO data.
2640 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
2641 llvm::BasicBlock *FalseBlock, uint64_t TrueCount);
2643 /// \brief Emit a description of a type in a format suitable for passing to
2644 /// a runtime sanitizer handler.
2645 llvm::Constant *EmitCheckTypeDescriptor(QualType T);
2647 /// \brief Convert a value into a format suitable for passing to a runtime
2648 /// sanitizer handler.
2649 llvm::Value *EmitCheckValue(llvm::Value *V);
2651 /// \brief Emit a description of a source location in a format suitable for
2652 /// passing to a runtime sanitizer handler.
2653 llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
2655 /// \brief Create a basic block that will call a handler function in a
2656 /// sanitizer runtime with the provided arguments, and create a conditional
2658 void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerKind>> Checked,
2659 StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
2660 ArrayRef<llvm::Value *> DynamicArgs);
2662 /// \brief Create a basic block that will call the trap intrinsic, and emit a
2663 /// conditional branch to it, for the -ftrapv checks.
2664 void EmitTrapCheck(llvm::Value *Checked);
2666 /// EmitCallArg - Emit a single call argument.
2667 void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
2669 /// EmitDelegateCallArg - We are performing a delegate call; that
2670 /// is, the current function is delegating to another one. Produce
2671 /// a r-value suitable for passing the given parameter.
2672 void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
2673 SourceLocation loc);
2675 /// SetFPAccuracy - Set the minimum required accuracy of the given floating
2676 /// point operation, expressed as the maximum relative error in ulp.
2677 void SetFPAccuracy(llvm::Value *Val, float Accuracy);
2680 llvm::MDNode *getRangeForLoadFromType(QualType Ty);
2681 void EmitReturnOfRValue(RValue RV, QualType Ty);
2683 void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
2685 llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4>
2686 DeferredReplacements;
2688 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
2689 /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
2691 /// \param AI - The first function argument of the expansion.
2692 void ExpandTypeFromArgs(QualType Ty, LValue Dst,
2693 SmallVectorImpl<llvm::Argument *>::iterator &AI);
2695 /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg
2696 /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
2697 /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
2698 void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
2699 SmallVectorImpl<llvm::Value *> &IRCallArgs,
2700 unsigned &IRCallArgPos);
2702 llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
2703 const Expr *InputExpr, std::string &ConstraintStr);
2705 llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
2706 LValue InputValue, QualType InputType,
2707 std::string &ConstraintStr,
2708 SourceLocation Loc);
2711 /// EmitCallArgs - Emit call arguments for a function.
2712 template <typename T>
2713 void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo,
2714 CallExpr::const_arg_iterator ArgBeg,
2715 CallExpr::const_arg_iterator ArgEnd,
2716 const FunctionDecl *CalleeDecl = nullptr,
2717 unsigned ParamsToSkip = 0, bool ForceColumnInfo = false) {
2718 SmallVector<QualType, 16> ArgTypes;
2719 CallExpr::const_arg_iterator Arg = ArgBeg;
2721 assert((ParamsToSkip == 0 || CallArgTypeInfo) &&
2722 "Can't skip parameters if type info is not provided");
2723 if (CallArgTypeInfo) {
2724 // First, use the argument types that the type info knows about
2725 for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip,
2726 E = CallArgTypeInfo->param_type_end();
2727 I != E; ++I, ++Arg) {
2728 assert(Arg != ArgEnd && "Running over edge of argument list!");
2730 ((*I)->isVariablyModifiedType() ||
2732 .getCanonicalType((*I).getNonReferenceType())
2734 getContext().getCanonicalType(Arg->getType()).getTypePtr()) &&
2735 "type mismatch in call argument!");
2736 ArgTypes.push_back(*I);
2740 // Either we've emitted all the call args, or we have a call to variadic
2743 (Arg == ArgEnd || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) &&
2744 "Extra arguments in non-variadic function!");
2746 // If we still have any arguments, emit them using the type of the argument.
2747 for (; Arg != ArgEnd; ++Arg)
2748 ArgTypes.push_back(getVarArgType(*Arg));
2750 EmitCallArgs(Args, ArgTypes, ArgBeg, ArgEnd, CalleeDecl, ParamsToSkip,
2754 void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes,
2755 CallExpr::const_arg_iterator ArgBeg,
2756 CallExpr::const_arg_iterator ArgEnd,
2757 const FunctionDecl *CalleeDecl = nullptr,
2758 unsigned ParamsToSkip = 0, bool ForceColumnInfo = false);
2761 QualType getVarArgType(const Expr *Arg);
2763 const TargetCodeGenInfo &getTargetHooks() const {
2764 return CGM.getTargetCodeGenInfo();
2767 void EmitDeclMetadata();
2769 CodeGenModule::ByrefHelpers *
2770 buildByrefHelpers(llvm::StructType &byrefType,
2771 const AutoVarEmission &emission);
2773 void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
2775 /// GetPointeeAlignment - Given an expression with a pointer type, emit the
2776 /// value and compute our best estimate of the alignment of the pointee.
2777 std::pair<llvm::Value*, unsigned> EmitPointerWithAlignment(const Expr *Addr);
2779 llvm::Value *GetValueForARMHint(unsigned BuiltinID);
2782 /// Helper class with most of the code for saving a value for a
2783 /// conditional expression cleanup.
2784 struct DominatingLLVMValue {
2785 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
2787 /// Answer whether the given value needs extra work to be saved.
2788 static bool needsSaving(llvm::Value *value) {
2789 // If it's not an instruction, we don't need to save.
2790 if (!isa<llvm::Instruction>(value)) return false;
2792 // If it's an instruction in the entry block, we don't need to save.
2793 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
2794 return (block != &block->getParent()->getEntryBlock());
2797 /// Try to save the given value.
2798 static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
2799 if (!needsSaving(value)) return saved_type(value, false);
2801 // Otherwise we need an alloca.
2802 llvm::Value *alloca =
2803 CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
2804 CGF.Builder.CreateStore(value, alloca);
2806 return saved_type(alloca, true);
2809 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
2810 if (!value.getInt()) return value.getPointer();
2811 return CGF.Builder.CreateLoad(value.getPointer());
2815 /// A partial specialization of DominatingValue for llvm::Values that
2816 /// might be llvm::Instructions.
2817 template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
2819 static type restore(CodeGenFunction &CGF, saved_type value) {
2820 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
2824 /// A specialization of DominatingValue for RValue.
2825 template <> struct DominatingValue<RValue> {
2826 typedef RValue type;
2828 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
2829 AggregateAddress, ComplexAddress };
2833 saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
2836 static bool needsSaving(RValue value);
2837 static saved_type save(CodeGenFunction &CGF, RValue value);
2838 RValue restore(CodeGenFunction &CGF);
2840 // implementations in CGExprCXX.cpp
2843 static bool needsSaving(type value) {
2844 return saved_type::needsSaving(value);
2846 static saved_type save(CodeGenFunction &CGF, type value) {
2847 return saved_type::save(CGF, value);
2849 static type restore(CodeGenFunction &CGF, saved_type value) {
2850 return value.restore(CGF);
2854 } // end namespace CodeGen
2855 } // end namespace clang