blob: c0368aab4d35edf01e8b7d80c6774c6cb6baa5d8 [file] [log] [blame]
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stump0dd9e882009-02-08 23:14:22 +000010// This is the internal per-function state used for llvm translation.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Stephen Hines176edba2014-12-01 14:53:08 -080014#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
15#define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
Daniel Dunbar45d196b2008-11-01 01:53:16 +000017#include "CGBuilder.h"
Eric Christopherc3287792011-10-19 00:43:52 +000018#include "CGDebugInfo.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070019#include "CGLoopInfo.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000020#include "CGValue.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "CodeGenModule.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070022#include "CodeGenPGO.h"
23#include "EHScopeStack.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000024#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"
Ben Langmuir524387a2013-05-09 19:17:11 +000029#include "clang/Basic/CapturedStmt.h"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070030#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000031#include "clang/Basic/TargetInfo.h"
32#include "clang/Frontend/CodeGenOptions.h"
33#include "llvm/ADT/ArrayRef.h"
34#include "llvm/ADT/DenseMap.h"
35#include "llvm/ADT/SmallVector.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070036#include "llvm/IR/ValueHandle.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000037#include "llvm/Support/Debug.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000038
Reid Spencer5f016e22007-07-11 17:01:13 +000039namespace llvm {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070040class BasicBlock;
41class LLVMContext;
42class MDNode;
43class Module;
44class SwitchInst;
45class Twine;
46class Value;
47class CallSite;
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
50namespace clang {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070051class ASTContext;
52class BlockDecl;
53class CXXDestructorDecl;
54class CXXForRangeStmt;
55class CXXTryStmt;
56class Decl;
57class LabelDecl;
58class EnumConstantDecl;
59class FunctionDecl;
60class FunctionProtoType;
61class LabelStmt;
62class ObjCContainerDecl;
63class ObjCInterfaceDecl;
64class ObjCIvarDecl;
65class ObjCMethodDecl;
66class ObjCImplementationDecl;
67class ObjCPropertyImplDecl;
68class TargetInfo;
69class TargetCodeGenInfo;
70class VarDecl;
71class ObjCForCollectionStmt;
72class ObjCAtTryStmt;
73class ObjCAtThrowStmt;
74class ObjCAtSynchronizedStmt;
75class ObjCAutoreleasePoolStmt;
Devang Patelb84a06e2007-10-23 02:10:49 +000076
Reid Spencer5f016e22007-07-11 17:01:13 +000077namespace CodeGen {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070078class CodeGenTypes;
79class CGFunctionInfo;
80class CGRecordLayout;
81class CGBlockInfo;
82class CGCXXABI;
83class BlockFlags;
84class BlockFieldFlags;
Mike Stump0dd9e882009-02-08 23:14:22 +000085
John McCall9d232c82013-03-07 21:37:08 +000086/// The kind of evaluation to perform on values of a particular
87/// type. Basically, is the code in CGExprScalar, CGExprComplex, or
88/// CGExprAgg?
89///
90/// TODO: should vectors maybe be split out into their own thing?
91enum TypeEvaluationKind {
92 TEK_Scalar,
93 TEK_Complex,
94 TEK_Aggregate
95};
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097/// CodeGenFunction - This class organizes the per-function state that is used
98/// while generating LLVM code.
John McCall5936e332011-02-15 09:22:45 +000099class CodeGenFunction : public CodeGenTypeCache {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700100 CodeGenFunction(const CodeGenFunction &) = delete;
101 void operator=(const CodeGenFunction &) = delete;
John McCall4c40d982010-08-31 07:33:07 +0000102
103 friend class CGCXXABI;
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000104public:
John McCallff8e1152010-07-23 21:56:41 +0000105 /// A jump destination is an abstract label, branching to which may
106 /// require a jump out through normal cleanups.
John McCallf1549f62010-07-06 01:34:17 +0000107 struct JumpDest {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700108 JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
John McCallff8e1152010-07-23 21:56:41 +0000109 JumpDest(llvm::BasicBlock *Block,
110 EHScopeStack::stable_iterator Depth,
111 unsigned Index)
112 : Block(Block), ScopeDepth(Depth), Index(Index) {}
113
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700114 bool isValid() const { return Block != nullptr; }
John McCallff8e1152010-07-23 21:56:41 +0000115 llvm::BasicBlock *getBlock() const { return Block; }
116 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
117 unsigned getDestIndex() const { return Index; }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000118
Nadav Rotem495cfa42013-03-23 06:43:35 +0000119 // This should be used cautiously.
120 void setScopeDepth(EHScopeStack::stable_iterator depth) {
121 ScopeDepth = depth;
122 }
123
John McCallff8e1152010-07-23 21:56:41 +0000124 private:
John McCallf1549f62010-07-06 01:34:17 +0000125 llvm::BasicBlock *Block;
126 EHScopeStack::stable_iterator ScopeDepth;
John McCallff8e1152010-07-23 21:56:41 +0000127 unsigned Index;
128 };
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 CodeGenModule &CGM; // Per-module state.
Daniel Dunbar444be732009-11-13 05:51:54 +0000131 const TargetInfo &Target;
Mike Stump0dd9e882009-02-08 23:14:22 +0000132
Chris Lattner58dee102007-08-21 16:57:55 +0000133 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700134 LoopInfoStack LoopStack;
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000135 CGBuilderTy Builder;
Mike Stump0dd9e882009-02-08 23:14:22 +0000136
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700137 /// \brief CGBuilder insert helper. This function is called after an
138 /// instruction is created using Builder.
139 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
140 llvm::BasicBlock *BB,
141 llvm::BasicBlock::iterator InsertPt) const;
142
John McCallf5ebf9b2013-05-03 07:33:41 +0000143 /// CurFuncDecl - Holds the Decl for the current outermost
144 /// non-closure context.
Chris Lattner41110242008-06-17 18:05:57 +0000145 const Decl *CurFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000146 /// CurCodeDecl - This is the inner-most code context, which includes blocks.
147 const Decl *CurCodeDecl;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000148 const CGFunctionInfo *CurFnInfo;
Chris Lattner391d77a2008-03-30 23:03:07 +0000149 QualType FnRetTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 llvm::Function *CurFn;
151
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000152 /// CurGD - The GlobalDecl for the current function being compiled.
153 GlobalDecl CurGD;
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000154
John McCallf85e1932011-06-15 23:02:42 +0000155 /// PrologueCleanupDepth - The cleanup depth enclosing all the
156 /// cleanups associated with the parameters.
157 EHScopeStack::stable_iterator PrologueCleanupDepth;
158
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000159 /// ReturnBlock - Unified return block.
John McCallf1549f62010-07-06 01:34:17 +0000160 JumpDest ReturnBlock;
161
Mike Stump0dd9e882009-02-08 23:14:22 +0000162 /// ReturnValue - The temporary alloca to hold the return value. This is null
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000163 /// iff the function has no return value.
Eli Friedmanb17daf92009-12-04 02:43:40 +0000164 llvm::Value *ReturnValue;
Mike Stump0dd9e882009-02-08 23:14:22 +0000165
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 /// AllocaInsertPoint - This is an instruction in the entry block before which
167 /// we prefer to insert allocas.
Chris Lattner481769b2009-03-31 22:17:44 +0000168 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000169
Ben Langmuir524387a2013-05-09 19:17:11 +0000170 /// \brief API for captured statement code generation.
171 class CGCapturedStmtInfo {
172 public:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700173 explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
174 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
Ben Langmuir524387a2013-05-09 19:17:11 +0000175 explicit CGCapturedStmtInfo(const CapturedStmt &S,
176 CapturedRegionKind K = CR_Default)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700177 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
Ben Langmuir524387a2013-05-09 19:17:11 +0000178
179 RecordDecl::field_iterator Field =
180 S.getCapturedRecordDecl()->field_begin();
181 for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
182 E = S.capture_end();
183 I != E; ++I, ++Field) {
184 if (I->capturesThis())
185 CXXThisFieldDecl = *Field;
Stephen Hines176edba2014-12-01 14:53:08 -0800186 else if (I->capturesVariable())
Ben Langmuir524387a2013-05-09 19:17:11 +0000187 CaptureFields[I->getCapturedVar()] = *Field;
188 }
189 }
190
191 virtual ~CGCapturedStmtInfo();
192
193 CapturedRegionKind getKind() const { return Kind; }
194
195 void setContextValue(llvm::Value *V) { ThisValue = V; }
196 // \brief Retrieve the value of the context parameter.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700197 virtual llvm::Value *getContextValue() const { return ThisValue; }
Ben Langmuir524387a2013-05-09 19:17:11 +0000198
199 /// \brief Lookup the captured field decl for a variable.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700200 virtual const FieldDecl *lookup(const VarDecl *VD) const {
Ben Langmuir524387a2013-05-09 19:17:11 +0000201 return CaptureFields.lookup(VD);
202 }
203
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700204 bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; }
205 virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
Ben Langmuir524387a2013-05-09 19:17:11 +0000206
Stephen Hines176edba2014-12-01 14:53:08 -0800207 static bool classof(const CGCapturedStmtInfo *) {
208 return true;
209 }
210
Ben Langmuir524387a2013-05-09 19:17:11 +0000211 /// \brief Emit the captured statement body.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700212 virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700213 RegionCounter Cnt = CGF.getPGORegionCounter(S);
214 Cnt.beginRegion(CGF.Builder);
Ben Langmuir524387a2013-05-09 19:17:11 +0000215 CGF.EmitStmt(S);
216 }
217
218 /// \brief Get the name of the capture helper.
219 virtual StringRef getHelperName() const { return "__captured_stmt"; }
220
221 private:
222 /// \brief The kind of captured statement being generated.
223 CapturedRegionKind Kind;
224
225 /// \brief Keep the map between VarDecl and FieldDecl.
226 llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
227
228 /// \brief The base address of the captured record, passed in as the first
229 /// argument of the parallel region function.
230 llvm::Value *ThisValue;
231
232 /// \brief Captured 'this' type.
233 FieldDecl *CXXThisFieldDecl;
234 };
235 CGCapturedStmtInfo *CapturedStmtInfo;
236
Nuno Lopesb3198a82012-05-08 22:10:46 +0000237 /// BoundsChecking - Emit run-time bounds checks. Higher values mean
238 /// potentially higher performance penalties.
239 unsigned char BoundsChecking;
240
Stephen Hines176edba2014-12-01 14:53:08 -0800241 /// \brief Sanitizers enabled for this function.
242 SanitizerSet SanOpts;
243
244 /// \brief True if CodeGen currently emits code implementing sanitizer checks.
245 bool IsSanitizerScope;
246
247 /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope.
248 class SanitizerScope {
249 CodeGenFunction *CGF;
250 public:
251 SanitizerScope(CodeGenFunction *CGF);
252 ~SanitizerScope();
253 };
254
255 /// In C++, whether we are code generating a thunk. This controls whether we
256 /// should emit cleanups.
257 bool CurFuncIsThunk;
Will Dietz4f45bc02013-01-18 11:30:38 +0000258
John McCallf85e1932011-06-15 23:02:42 +0000259 /// In ARC, whether we should autorelease the return value.
260 bool AutoreleaseResult;
261
Stephen Hines176edba2014-12-01 14:53:08 -0800262 /// Whether we processed a Microsoft-style asm block during CodeGen. These can
263 /// potentially set the return value.
264 bool SawAsmBlock;
265
John McCalld16c2cf2011-02-08 08:22:06 +0000266 const CodeGen::CGBlockInfo *BlockInfo;
267 llvm::Value *BlockPointer;
268
Eli Friedmancec5ebd2012-02-11 02:57:39 +0000269 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
270 FieldDecl *LambdaThisCaptureField;
271
Douglas Gregor3d91bbc2010-05-17 15:52:46 +0000272 /// \brief A mapping from NRVO variables to the flags used to indicate
273 /// when the NRVO has been applied to this variable.
274 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000275
John McCallf1549f62010-07-06 01:34:17 +0000276 EHScopeStack EHStack;
Richard Smith8a07cd32013-06-12 20:42:33 +0000277 llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700278 llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack;
Richard Smith8a07cd32013-06-12 20:42:33 +0000279
280 /// Header for data within LifetimeExtendedCleanupStack.
281 struct LifetimeExtendedCleanupHeader {
282 /// The size of the following cleanup object.
Stephen Hines176edba2014-12-01 14:53:08 -0800283 unsigned Size : 29;
Richard Smith8a07cd32013-06-12 20:42:33 +0000284 /// The kind of cleanup to push: a value from the CleanupKind enumeration.
285 unsigned Kind : 3;
286
Stephen Hines176edba2014-12-01 14:53:08 -0800287 size_t getSize() const { return size_t(Size); }
Richard Smith8a07cd32013-06-12 20:42:33 +0000288 CleanupKind getKind() const { return static_cast<CleanupKind>(Kind); }
289 };
John McCallf1549f62010-07-06 01:34:17 +0000290
John McCallff8e1152010-07-23 21:56:41 +0000291 /// i32s containing the indexes of the cleanup destinations.
292 llvm::AllocaInst *NormalCleanupDest;
John McCallff8e1152010-07-23 21:56:41 +0000293
294 unsigned NextCleanupDestIndex;
295
John McCall1a343eb2011-11-10 08:15:53 +0000296 /// FirstBlockInfo - The head of a singly-linked-list of block layouts.
297 CGBlockInfo *FirstBlockInfo;
298
John McCall777d6e52011-08-11 02:22:43 +0000299 /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
300 llvm::BasicBlock *EHResumeBlock;
301
Bill Wendling285cfd82011-09-19 20:31:14 +0000302 /// The exception slot. All landing pads write the current exception pointer
303 /// into this alloca.
John McCallf1549f62010-07-06 01:34:17 +0000304 llvm::Value *ExceptionSlot;
305
Bill Wendling285cfd82011-09-19 20:31:14 +0000306 /// The selector slot. Under the MandatoryCleanup model, all landing pads
307 /// write the current selector value into this alloca.
John McCall93c332a2011-05-28 21:13:02 +0000308 llvm::AllocaInst *EHSelectorSlot;
309
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700310 llvm::AllocaInst *AbnormalTerminationSlot;
311
312 /// The implicit parameter to SEH filter functions of type
313 /// 'EXCEPTION_POINTERS*'.
314 ImplicitParamDecl *SEHPointersDecl;
315
John McCallf1549f62010-07-06 01:34:17 +0000316 /// Emits a landing pad for the current EH stack.
317 llvm::BasicBlock *EmitLandingPad();
318
319 llvm::BasicBlock *getInvokeDestImpl();
320
John McCall150b4622011-01-26 04:00:11 +0000321 template <class T>
John McCall804b8072011-01-28 10:53:53 +0000322 typename DominatingValue<T>::saved_type saveValueInCond(T value) {
323 return DominatingValue<T>::save(*this, value);
John McCall150b4622011-01-26 04:00:11 +0000324 }
325
Daniel Dunbar18ccc772008-09-28 01:03:14 +0000326public:
Anders Carlssonfa1f7562009-02-10 06:07:49 +0000327 /// ObjCEHValueStack - Stack of Objective-C exception values, used for
328 /// rethrows.
Chris Lattner686775d2011-07-20 06:58:45 +0000329 SmallVector<llvm::Value*, 8> ObjCEHValueStack;
Mike Stump0dd9e882009-02-08 23:14:22 +0000330
John McCalld768e9d2011-06-22 02:32:12 +0000331 /// A class controlling the emission of a finally block.
332 class FinallyInfo {
333 /// Where the catchall's edge through the cleanup should go.
334 JumpDest RethrowDest;
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000335
John McCalld768e9d2011-06-22 02:32:12 +0000336 /// A function to call to enter the catch.
337 llvm::Constant *BeginCatchFn;
338
339 /// An i1 variable indicating whether or not the @finally is
340 /// running for an exception.
341 llvm::AllocaInst *ForEHVar;
342
343 /// An i8* variable into which the exception pointer to rethrow
344 /// has been saved.
345 llvm::AllocaInst *SavedExnVar;
346
347 public:
348 void enter(CodeGenFunction &CGF, const Stmt *Finally,
349 llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
350 llvm::Constant *rethrowFn);
351 void exit(CodeGenFunction &CGF);
352 };
Mike Stumpd88ea562009-12-09 03:35:49 +0000353
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700354 /// Cleanups can be emitted for two reasons: normal control leaving a region
355 /// exceptional control flow leaving a region.
356 struct SEHFinallyInfo {
357 SEHFinallyInfo()
358 : FinallyBB(nullptr), ContBB(nullptr), ResumeBB(nullptr) {}
359
360 llvm::BasicBlock *FinallyBB;
361 llvm::BasicBlock *ContBB;
362 llvm::BasicBlock *ResumeBB;
363 };
364
365 /// Returns true inside SEH __try blocks.
366 bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); }
367
John McCall150b4622011-01-26 04:00:11 +0000368 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
369 /// current full-expression. Safe against the possibility that
370 /// we're currently inside a conditionally-evaluated expression.
John McCall3ad32c82011-01-28 08:37:24 +0000371 template <class T, class A0>
372 void pushFullExprCleanup(CleanupKind kind, A0 a0) {
373 // If we're not in a conditional branch, or if none of the
374 // arguments requires saving, then use the unconditional cleanup.
John McCallc4a1a842011-07-12 00:15:30 +0000375 if (!isInConditionalBranch())
376 return EHStack.pushCleanup<T>(kind, a0);
John McCall3ad32c82011-01-28 08:37:24 +0000377
John McCall804b8072011-01-28 10:53:53 +0000378 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
John McCall3ad32c82011-01-28 08:37:24 +0000379
380 typedef EHScopeStack::ConditionalCleanup1<T, A0> CleanupType;
381 EHStack.pushCleanup<CleanupType>(kind, a0_saved);
382 initFullExprCleanup();
383 }
384
385 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
386 /// current full-expression. Safe against the possibility that
387 /// we're currently inside a conditionally-evaluated expression.
John McCall150b4622011-01-26 04:00:11 +0000388 template <class T, class A0, class A1>
389 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) {
390 // If we're not in a conditional branch, or if none of the
391 // arguments requires saving, then use the unconditional cleanup.
John McCallc4a1a842011-07-12 00:15:30 +0000392 if (!isInConditionalBranch())
393 return EHStack.pushCleanup<T>(kind, a0, a1);
John McCall150b4622011-01-26 04:00:11 +0000394
John McCall804b8072011-01-28 10:53:53 +0000395 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
396 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
John McCall150b4622011-01-26 04:00:11 +0000397
398 typedef EHScopeStack::ConditionalCleanup2<T, A0, A1> CleanupType;
John McCall3ad32c82011-01-28 08:37:24 +0000399 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved);
400 initFullExprCleanup();
John McCall150b4622011-01-26 04:00:11 +0000401 }
402
Douglas Gregord7b23162011-06-22 16:12:01 +0000403 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
404 /// current full-expression. Safe against the possibility that
405 /// we're currently inside a conditionally-evaluated expression.
406 template <class T, class A0, class A1, class A2>
407 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2) {
408 // If we're not in a conditional branch, or if none of the
409 // arguments requires saving, then use the unconditional cleanup.
410 if (!isInConditionalBranch()) {
John McCallc4a1a842011-07-12 00:15:30 +0000411 return EHStack.pushCleanup<T>(kind, a0, a1, a2);
Douglas Gregord7b23162011-06-22 16:12:01 +0000412 }
413
414 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
415 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
416 typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
417
418 typedef EHScopeStack::ConditionalCleanup3<T, A0, A1, A2> CleanupType;
419 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, a2_saved);
420 initFullExprCleanup();
421 }
422
John McCall9928c482011-07-12 16:41:08 +0000423 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
424 /// current full-expression. Safe against the possibility that
425 /// we're currently inside a conditionally-evaluated expression.
426 template <class T, class A0, class A1, class A2, class A3>
427 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2, A3 a3) {
428 // If we're not in a conditional branch, or if none of the
429 // arguments requires saving, then use the unconditional cleanup.
430 if (!isInConditionalBranch()) {
431 return EHStack.pushCleanup<T>(kind, a0, a1, a2, a3);
432 }
433
434 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
435 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
436 typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
437 typename DominatingValue<A3>::saved_type a3_saved = saveValueInCond(a3);
438
439 typedef EHScopeStack::ConditionalCleanup4<T, A0, A1, A2, A3> CleanupType;
440 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved,
441 a2_saved, a3_saved);
442 initFullExprCleanup();
443 }
444
Richard Smith8a07cd32013-06-12 20:42:33 +0000445 /// \brief Queue a cleanup to be pushed after finishing the current
446 /// full-expression.
447 template <class T, class A0, class A1, class A2, class A3>
448 void pushCleanupAfterFullExpr(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3) {
449 assert(!isInConditionalBranch() && "can't defer conditional cleanup");
450
451 LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind };
452
453 size_t OldSize = LifetimeExtendedCleanupStack.size();
454 LifetimeExtendedCleanupStack.resize(
455 LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size);
456
457 char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
458 new (Buffer) LifetimeExtendedCleanupHeader(Header);
459 new (Buffer + sizeof(Header)) T(a0, a1, a2, a3);
460 }
461
John McCall6f103ba2011-11-10 10:43:54 +0000462 /// Set up the last cleaup that was pushed as a conditional
463 /// full-expression cleanup.
464 void initFullExprCleanup();
465
John McCallf1549f62010-07-06 01:34:17 +0000466 /// PushDestructorCleanup - Push a cleanup to call the
467 /// complete-object destructor of an object of the given type at the
468 /// given address. Does nothing if T is not a C++ class type with a
469 /// non-trivial destructor.
470 void PushDestructorCleanup(QualType T, llvm::Value *Addr);
471
John McCall81407d42010-07-21 06:29:51 +0000472 /// PushDestructorCleanup - Push a cleanup to call the
473 /// complete-object variant of the given destructor on the object at
474 /// the given address.
475 void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
476 llvm::Value *Addr);
477
John McCallf1549f62010-07-06 01:34:17 +0000478 /// PopCleanupBlock - Will pop the cleanup entry on the stack and
479 /// process all branch fixups.
Adrian Prantl1c3db762013-05-16 00:41:26 +0000480 void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
John McCallf1549f62010-07-06 01:34:17 +0000481
John McCall7d8647f2010-09-14 07:57:04 +0000482 /// DeactivateCleanupBlock - Deactivates the given cleanup block.
483 /// The block cannot be reactivated. Pops it if it's the top of the
484 /// stack.
John McCall6f103ba2011-11-10 10:43:54 +0000485 ///
486 /// \param DominatingIP - An instruction which is known to
487 /// dominate the current IP (if set) and which lies along
488 /// all paths of execution between the current IP and the
489 /// the point at which the cleanup comes into scope.
490 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
491 llvm::Instruction *DominatingIP);
John McCall7d8647f2010-09-14 07:57:04 +0000492
493 /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
494 /// Cannot be used to resurrect a deactivated cleanup.
John McCall6f103ba2011-11-10 10:43:54 +0000495 ///
496 /// \param DominatingIP - An instruction which is known to
497 /// dominate the current IP (if set) and which lies along
498 /// all paths of execution between the current IP and the
499 /// the point at which the cleanup comes into scope.
500 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
501 llvm::Instruction *DominatingIP);
John McCallcd2d2b72010-08-13 21:20:51 +0000502
John McCallf1549f62010-07-06 01:34:17 +0000503 /// \brief Enters a new scope for capturing cleanups, all of which
504 /// will be executed once the scope is exited.
505 class RunCleanupsScope {
John McCallf1549f62010-07-06 01:34:17 +0000506 EHScopeStack::stable_iterator CleanupStackDepth;
Richard Smith8a07cd32013-06-12 20:42:33 +0000507 size_t LifetimeExtendedCleanupStackSize;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000508 bool OldDidCallStackSave;
John McCalled383132013-03-01 01:24:35 +0000509 protected:
Douglas Gregor5656e142009-11-24 21:15:44 +0000510 bool PerformCleanup;
John McCalled383132013-03-01 01:24:35 +0000511 private:
Douglas Gregor01234bb2009-11-24 16:43:22 +0000512
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700513 RunCleanupsScope(const RunCleanupsScope &) = delete;
514 void operator=(const RunCleanupsScope &) = delete;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000515
Eric Christopherc3287792011-10-19 00:43:52 +0000516 protected:
517 CodeGenFunction& CGF;
Will Dietz4f45bc02013-01-18 11:30:38 +0000518
Douglas Gregor01234bb2009-11-24 16:43:22 +0000519 public:
520 /// \brief Enter a new cleanup scope.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000521 explicit RunCleanupsScope(CodeGenFunction &CGF)
Eric Christopherc3287792011-10-19 00:43:52 +0000522 : PerformCleanup(true), CGF(CGF)
Douglas Gregor5656e142009-11-24 21:15:44 +0000523 {
John McCallf1549f62010-07-06 01:34:17 +0000524 CleanupStackDepth = CGF.EHStack.stable_begin();
Richard Smith8a07cd32013-06-12 20:42:33 +0000525 LifetimeExtendedCleanupStackSize =
526 CGF.LifetimeExtendedCleanupStack.size();
Douglas Gregor01234bb2009-11-24 16:43:22 +0000527 OldDidCallStackSave = CGF.DidCallStackSave;
Argyrios Kyrtzidis4ada2ca2010-09-14 00:42:34 +0000528 CGF.DidCallStackSave = false;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000529 }
530
531 /// \brief Exit this cleanup scope, emitting any accumulated
532 /// cleanups.
John McCallf1549f62010-07-06 01:34:17 +0000533 ~RunCleanupsScope() {
Douglas Gregor5656e142009-11-24 21:15:44 +0000534 if (PerformCleanup) {
535 CGF.DidCallStackSave = OldDidCallStackSave;
Richard Smith8a07cd32013-06-12 20:42:33 +0000536 CGF.PopCleanupBlocks(CleanupStackDepth,
537 LifetimeExtendedCleanupStackSize);
Douglas Gregor5656e142009-11-24 21:15:44 +0000538 }
539 }
540
541 /// \brief Determine whether this scope requires any cleanups.
542 bool requiresCleanups() const {
John McCallf1549f62010-07-06 01:34:17 +0000543 return CGF.EHStack.stable_begin() != CleanupStackDepth;
Douglas Gregor5656e142009-11-24 21:15:44 +0000544 }
545
546 /// \brief Force the emission of cleanups now, instead of waiting
547 /// until this object is destroyed.
548 void ForceCleanup() {
549 assert(PerformCleanup && "Already forced cleanup");
Douglas Gregor01234bb2009-11-24 16:43:22 +0000550 CGF.DidCallStackSave = OldDidCallStackSave;
Richard Smith8a07cd32013-06-12 20:42:33 +0000551 CGF.PopCleanupBlocks(CleanupStackDepth,
552 LifetimeExtendedCleanupStackSize);
Douglas Gregor5656e142009-11-24 21:15:44 +0000553 PerformCleanup = false;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000554 }
555 };
556
Stephen Hines176edba2014-12-01 14:53:08 -0800557 class LexicalScope : public RunCleanupsScope {
Eric Christopherc3287792011-10-19 00:43:52 +0000558 SourceRange Range;
Nadav Rotem495cfa42013-03-23 06:43:35 +0000559 SmallVector<const LabelDecl*, 4> Labels;
560 LexicalScope *ParentScope;
Eric Christopherc3287792011-10-19 00:43:52 +0000561
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700562 LexicalScope(const LexicalScope &) = delete;
563 void operator=(const LexicalScope &) = delete;
Eric Christopherc3287792011-10-19 00:43:52 +0000564
565 public:
566 /// \brief Enter a new cleanup scope.
567 explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
Nadav Rotem495cfa42013-03-23 06:43:35 +0000568 : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
569 CGF.CurLexicalScope = this;
Eric Christopherc3287792011-10-19 00:43:52 +0000570 if (CGDebugInfo *DI = CGF.getDebugInfo())
571 DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
572 }
573
Nadav Rotem495cfa42013-03-23 06:43:35 +0000574 void addLabel(const LabelDecl *label) {
575 assert(PerformCleanup && "adding label to dead scope?");
576 Labels.push_back(label);
577 }
578
Eric Christopherc3287792011-10-19 00:43:52 +0000579 /// \brief Exit this cleanup scope, emitting any accumulated
580 /// cleanups.
581 ~LexicalScope() {
Adrian Prantlefb72ad2013-04-01 19:02:06 +0000582 if (CGDebugInfo *DI = CGF.getDebugInfo())
583 DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
584
Nadav Rotem495cfa42013-03-23 06:43:35 +0000585 // If we should perform a cleanup, force them now. Note that
586 // this ends the cleanup scope before rescoping any labels.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700587 if (PerformCleanup) {
588 ApplyDebugLocation DL(CGF, Range.getEnd());
589 ForceCleanup();
590 }
Eric Christopherc3287792011-10-19 00:43:52 +0000591 }
592
593 /// \brief Force the emission of cleanups now, instead of waiting
594 /// until this object is destroyed.
595 void ForceCleanup() {
Nadav Rotem495cfa42013-03-23 06:43:35 +0000596 CGF.CurLexicalScope = ParentScope;
Adrian Prantlefb72ad2013-04-01 19:02:06 +0000597 RunCleanupsScope::ForceCleanup();
598
Nadav Rotem495cfa42013-03-23 06:43:35 +0000599 if (!Labels.empty())
600 rescopeLabels();
Eric Christopherc3287792011-10-19 00:43:52 +0000601 }
Nadav Rotem495cfa42013-03-23 06:43:35 +0000602
603 void rescopeLabels();
Eric Christopherc3287792011-10-19 00:43:52 +0000604 };
605
Stephen Hines176edba2014-12-01 14:53:08 -0800606 /// \brief The scope used to remap some variables as private in the OpenMP
607 /// loop body (or other captured region emitted without outlining), and to
608 /// restore old vars back on exit.
609 class OMPPrivateScope : public RunCleanupsScope {
610 typedef llvm::DenseMap<const VarDecl *, llvm::Value *> VarDeclMapTy;
611 VarDeclMapTy SavedLocals;
612 VarDeclMapTy SavedPrivates;
613
614 private:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700615 OMPPrivateScope(const OMPPrivateScope &) = delete;
616 void operator=(const OMPPrivateScope &) = delete;
Stephen Hines176edba2014-12-01 14:53:08 -0800617
618 public:
619 /// \brief Enter a new OpenMP private scope.
620 explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
621
622 /// \brief Registers \a LocalVD variable as a private and apply \a
623 /// PrivateGen function for it to generate corresponding private variable.
624 /// \a PrivateGen returns an address of the generated private variable.
625 /// \return true if the variable is registered as private, false if it has
626 /// been privatized already.
627 bool
628 addPrivate(const VarDecl *LocalVD,
629 const std::function<llvm::Value *()> &PrivateGen) {
630 assert(PerformCleanup && "adding private to dead scope");
Stephen Hines176edba2014-12-01 14:53:08 -0800631 if (SavedLocals.count(LocalVD) > 0) return false;
632 SavedLocals[LocalVD] = CGF.LocalDeclMap.lookup(LocalVD);
633 CGF.LocalDeclMap.erase(LocalVD);
634 SavedPrivates[LocalVD] = PrivateGen();
635 CGF.LocalDeclMap[LocalVD] = SavedLocals[LocalVD];
636 return true;
637 }
638
639 /// \brief Privatizes local variables previously registered as private.
640 /// Registration is separate from the actual privatization to allow
641 /// initializers use values of the original variables, not the private one.
642 /// This is important, for example, if the private variable is a class
643 /// variable initialized by a constructor that references other private
644 /// variables. But at initialization original variables must be used, not
645 /// private copies.
646 /// \return true if at least one variable was privatized, false otherwise.
647 bool Privatize() {
648 for (auto VDPair : SavedPrivates) {
649 CGF.LocalDeclMap[VDPair.first] = VDPair.second;
650 }
651 SavedPrivates.clear();
652 return !SavedLocals.empty();
653 }
654
655 void ForceCleanup() {
656 RunCleanupsScope::ForceCleanup();
657 // Remap vars back to the original values.
658 for (auto I : SavedLocals) {
659 CGF.LocalDeclMap[I.first] = I.second;
660 }
661 SavedLocals.clear();
662 }
663
664 /// \brief Exit scope - all the mapped variables are restored.
665 ~OMPPrivateScope() { ForceCleanup(); }
666 };
Anders Carlsson44ec82b2010-03-30 03:14:41 +0000667
Richard Smith8a07cd32013-06-12 20:42:33 +0000668 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
669 /// that have been added.
Adrian Prantl1c3db762013-05-16 00:41:26 +0000670 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
Anders Carlsson44ec82b2010-03-30 03:14:41 +0000671
Richard Smith8a07cd32013-06-12 20:42:33 +0000672 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
673 /// that have been added, then adds all lifetime-extended cleanups from
674 /// the given position to the stack.
675 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
676 size_t OldLifetimeExtendedStackSize);
677
John McCallff8e1152010-07-23 21:56:41 +0000678 void ResolveBranchFixups(llvm::BasicBlock *Target);
679
John McCallf1549f62010-07-06 01:34:17 +0000680 /// The given basic block lies in the current EH scope, but may be a
681 /// target of a potentially scope-crossing jump; get a stable handle
682 /// to which we can perform this jump later.
John McCallff8e1152010-07-23 21:56:41 +0000683 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
John McCall413e6772010-07-28 01:07:35 +0000684 return JumpDest(Target,
685 EHStack.getInnermostNormalCleanup(),
686 NextCleanupDestIndex++);
John McCallf1549f62010-07-06 01:34:17 +0000687 }
Anders Carlssonc71c8452009-02-07 23:50:39 +0000688
John McCallf1549f62010-07-06 01:34:17 +0000689 /// The given basic block lies in the current EH scope, but may be a
690 /// target of a potentially scope-crossing jump; get a stable handle
691 /// to which we can perform this jump later.
Chris Lattner686775d2011-07-20 06:58:45 +0000692 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
John McCallff8e1152010-07-23 21:56:41 +0000693 return getJumpDestInCurrentScope(createBasicBlock(Name));
John McCallf1549f62010-07-06 01:34:17 +0000694 }
695
696 /// EmitBranchThroughCleanup - Emit a branch from the current insert
697 /// block through the normal cleanup handling code (if any) and then
698 /// on to \arg Dest.
699 void EmitBranchThroughCleanup(JumpDest Dest);
Chris Lattnerb11f9192011-04-17 00:54:30 +0000700
701 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
702 /// specified destination obviously has no cleanups to run. 'false' is always
703 /// a conservatively correct answer for this method.
704 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
John McCallf1549f62010-07-06 01:34:17 +0000705
John McCall777d6e52011-08-11 02:22:43 +0000706 /// popCatchScope - Pops the catch scope at the top of the EHScope
707 /// stack, emitting any required code (other than the catch handlers
708 /// themselves).
709 void popCatchScope();
John McCallff8e1152010-07-23 21:56:41 +0000710
David Chisnallc6860042012-11-07 16:50:40 +0000711 llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
John McCall777d6e52011-08-11 02:22:43 +0000712 llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
Mike Stump0dd9e882009-02-08 23:14:22 +0000713
John McCall150b4622011-01-26 04:00:11 +0000714 /// An object to manage conditionally-evaluated expressions.
715 class ConditionalEvaluation {
716 llvm::BasicBlock *StartBB;
Mike Stump1eb44332009-09-09 15:08:12 +0000717
John McCall150b4622011-01-26 04:00:11 +0000718 public:
719 ConditionalEvaluation(CodeGenFunction &CGF)
720 : StartBB(CGF.Builder.GetInsertBlock()) {}
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000721
John McCall150b4622011-01-26 04:00:11 +0000722 void begin(CodeGenFunction &CGF) {
723 assert(CGF.OutermostConditional != this);
724 if (!CGF.OutermostConditional)
725 CGF.OutermostConditional = this;
726 }
727
728 void end(CodeGenFunction &CGF) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700729 assert(CGF.OutermostConditional != nullptr);
John McCall150b4622011-01-26 04:00:11 +0000730 if (CGF.OutermostConditional == this)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700731 CGF.OutermostConditional = nullptr;
John McCall150b4622011-01-26 04:00:11 +0000732 }
733
734 /// Returns a block which will be executed prior to each
735 /// evaluation of the conditional code.
736 llvm::BasicBlock *getStartingBlock() const {
737 return StartBB;
738 }
739 };
Mike Stump1eb44332009-09-09 15:08:12 +0000740
John McCall3019c442010-09-17 00:50:28 +0000741 /// isInConditionalBranch - Return true if we're currently emitting
742 /// one branch or the other of a conditional expression.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700743 bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
John McCall150b4622011-01-26 04:00:11 +0000744
John McCall6f103ba2011-11-10 10:43:54 +0000745 void setBeforeOutermostConditional(llvm::Value *value, llvm::Value *addr) {
746 assert(isInConditionalBranch());
747 llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
748 new llvm::StoreInst(value, addr, &block->back());
749 }
750
John McCall150b4622011-01-26 04:00:11 +0000751 /// An RAII object to record that we're evaluating a statement
752 /// expression.
753 class StmtExprEvaluation {
754 CodeGenFunction &CGF;
755
756 /// We have to save the outermost conditional: cleanups in a
757 /// statement expression aren't conditional just because the
758 /// StmtExpr is.
759 ConditionalEvaluation *SavedOutermostConditional;
760
761 public:
762 StmtExprEvaluation(CodeGenFunction &CGF)
763 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700764 CGF.OutermostConditional = nullptr;
John McCall150b4622011-01-26 04:00:11 +0000765 }
766
767 ~StmtExprEvaluation() {
768 CGF.OutermostConditional = SavedOutermostConditional;
769 CGF.EnsureInsertPoint();
770 }
771 };
John McCalle996ffd2011-02-16 08:02:54 +0000772
John McCall56ca35d2011-02-17 10:25:35 +0000773 /// An object which temporarily prevents a value from being
774 /// destroyed by aggressive peephole optimizations that assume that
775 /// all uses of a value have been realized in the IR.
776 class PeepholeProtection {
777 llvm::Instruction *Inst;
778 friend class CodeGenFunction;
779
780 public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700781 PeepholeProtection() : Inst(nullptr) {}
John McCall4b9c2d22011-11-06 09:01:30 +0000782 };
John McCall56ca35d2011-02-17 10:25:35 +0000783
John McCall4b9c2d22011-11-06 09:01:30 +0000784 /// A non-RAII class containing all the information about a bound
785 /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for
786 /// this which makes individual mappings very simple; using this
787 /// class directly is useful when you have a variable number of
788 /// opaque values or don't want the RAII functionality for some
789 /// reason.
790 class OpaqueValueMappingData {
John McCalle996ffd2011-02-16 08:02:54 +0000791 const OpaqueValueExpr *OpaqueValue;
John McCall56ca35d2011-02-17 10:25:35 +0000792 bool BoundLValue;
793 CodeGenFunction::PeepholeProtection Protection;
John McCalle996ffd2011-02-16 08:02:54 +0000794
John McCall4b9c2d22011-11-06 09:01:30 +0000795 OpaqueValueMappingData(const OpaqueValueExpr *ov,
796 bool boundLValue)
797 : OpaqueValue(ov), BoundLValue(boundLValue) {}
John McCalle996ffd2011-02-16 08:02:54 +0000798 public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700799 OpaqueValueMappingData() : OpaqueValue(nullptr) {}
John McCall4b9c2d22011-11-06 09:01:30 +0000800
John McCall56ca35d2011-02-17 10:25:35 +0000801 static bool shouldBindAsLValue(const Expr *expr) {
John McCalla5493f82011-11-08 22:54:08 +0000802 // gl-values should be bound as l-values for obvious reasons.
803 // Records should be bound as l-values because IR generation
804 // always keeps them in memory. Expressions of function type
805 // act exactly like l-values but are formally required to be
806 // r-values in C.
807 return expr->isGLValue() ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700808 expr->getType()->isFunctionType() ||
809 hasAggregateEvaluationKind(expr->getType());
John McCall56ca35d2011-02-17 10:25:35 +0000810 }
811
John McCall4b9c2d22011-11-06 09:01:30 +0000812 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
813 const OpaqueValueExpr *ov,
814 const Expr *e) {
815 if (shouldBindAsLValue(ov))
816 return bind(CGF, ov, CGF.EmitLValue(e));
817 return bind(CGF, ov, CGF.EmitAnyExpr(e));
818 }
819
820 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
821 const OpaqueValueExpr *ov,
822 const LValue &lv) {
823 assert(shouldBindAsLValue(ov));
824 CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
825 return OpaqueValueMappingData(ov, true);
826 }
827
828 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
829 const OpaqueValueExpr *ov,
830 const RValue &rv) {
831 assert(!shouldBindAsLValue(ov));
832 CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
833
834 OpaqueValueMappingData data(ov, false);
835
836 // Work around an extremely aggressive peephole optimization in
837 // EmitScalarConversion which assumes that all other uses of a
838 // value are extant.
839 data.Protection = CGF.protectFromPeepholes(rv);
840
841 return data;
842 }
843
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700844 bool isValid() const { return OpaqueValue != nullptr; }
845 void clear() { OpaqueValue = nullptr; }
John McCall4b9c2d22011-11-06 09:01:30 +0000846
847 void unbind(CodeGenFunction &CGF) {
848 assert(OpaqueValue && "no data to unbind!");
849
850 if (BoundLValue) {
851 CGF.OpaqueLValues.erase(OpaqueValue);
852 } else {
853 CGF.OpaqueRValues.erase(OpaqueValue);
854 CGF.unprotectFromPeepholes(Protection);
855 }
856 }
857 };
858
859 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
860 class OpaqueValueMapping {
861 CodeGenFunction &CGF;
862 OpaqueValueMappingData Data;
863
864 public:
865 static bool shouldBindAsLValue(const Expr *expr) {
866 return OpaqueValueMappingData::shouldBindAsLValue(expr);
867 }
868
John McCall56ca35d2011-02-17 10:25:35 +0000869 /// Build the opaque value mapping for the given conditional
870 /// operator if it's the GNU ?: extension. This is a common
871 /// enough pattern that the convenience operator is really
872 /// helpful.
873 ///
874 OpaqueValueMapping(CodeGenFunction &CGF,
875 const AbstractConditionalOperator *op) : CGF(CGF) {
John McCall4b9c2d22011-11-06 09:01:30 +0000876 if (isa<ConditionalOperator>(op))
877 // Leave Data empty.
John McCall56ca35d2011-02-17 10:25:35 +0000878 return;
John McCall56ca35d2011-02-17 10:25:35 +0000879
880 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
John McCall4b9c2d22011-11-06 09:01:30 +0000881 Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
882 e->getCommon());
John McCall56ca35d2011-02-17 10:25:35 +0000883 }
884
John McCalle996ffd2011-02-16 08:02:54 +0000885 OpaqueValueMapping(CodeGenFunction &CGF,
886 const OpaqueValueExpr *opaqueValue,
John McCall56ca35d2011-02-17 10:25:35 +0000887 LValue lvalue)
John McCall4b9c2d22011-11-06 09:01:30 +0000888 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
John McCall56ca35d2011-02-17 10:25:35 +0000889 }
890
891 OpaqueValueMapping(CodeGenFunction &CGF,
892 const OpaqueValueExpr *opaqueValue,
893 RValue rvalue)
John McCall4b9c2d22011-11-06 09:01:30 +0000894 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
John McCalle996ffd2011-02-16 08:02:54 +0000895 }
896
897 void pop() {
John McCall4b9c2d22011-11-06 09:01:30 +0000898 Data.unbind(CGF);
899 Data.clear();
John McCalle996ffd2011-02-16 08:02:54 +0000900 }
901
902 ~OpaqueValueMapping() {
John McCall4b9c2d22011-11-06 09:01:30 +0000903 if (Data.isValid()) Data.unbind(CGF);
John McCalle996ffd2011-02-16 08:02:54 +0000904 }
905 };
Fariborz Jahaniane2204552010-11-16 19:29:39 +0000906
907 /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
908 /// number that holds the value.
909 unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
Fariborz Jahanian52a80e12011-01-26 23:08:27 +0000910
911 /// BuildBlockByrefAddress - Computes address location of the
912 /// variable which is declared as __block.
913 llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
914 const VarDecl *V);
Chris Lattner7f02f722007-08-24 05:35:26 +0000915private:
Chris Lattnerd9becd12009-10-28 23:59:40 +0000916 CGDebugInfo *DebugInfo;
Devang Patelaa112892011-03-07 18:45:56 +0000917 bool DisableDebugInfo;
Mike Stump09429b92009-02-17 17:00:02 +0000918
John McCall93c332a2011-05-28 21:13:02 +0000919 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
920 /// calling llvm.stacksave for multiple VLAs in the same scope.
921 bool DidCallStackSave;
922
Mike Stumpf71d2322009-11-30 20:08:49 +0000923 /// IndirectBranch - The first time an indirect goto is seen we create a block
924 /// with an indirect branch. Every time we see the address of a label taken,
925 /// we add the label to the indirect goto. Every subsequent indirect goto is
926 /// codegen'd as a jump to the IndirectBranch's basic block.
Chris Lattnerd9becd12009-10-28 23:59:40 +0000927 llvm::IndirectBrInst *IndirectBranch;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000928
Mike Stump0dd9e882009-02-08 23:14:22 +0000929 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
930 /// decls.
John McCall6b5a61b2011-02-07 10:33:21 +0000931 typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
932 DeclMapTy LocalDeclMap;
Reid Spencer5f016e22007-07-11 17:01:13 +0000933
934 /// LabelMap - This keeps track of the LLVM basic block for each C label.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000935 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
Mike Stump0dd9e882009-02-08 23:14:22 +0000936
Mike Stump0dd9e882009-02-08 23:14:22 +0000937 // BreakContinueStack - This keeps track of where break and continue
Anders Carlssone4b6d342009-02-10 05:52:02 +0000938 // statements should jump to.
Chris Lattnerda138702007-07-16 21:28:45 +0000939 struct BreakContinue {
John McCallf1549f62010-07-06 01:34:17 +0000940 BreakContinue(JumpDest Break, JumpDest Continue)
941 : BreakBlock(Break), ContinueBlock(Continue) {}
Mike Stump0dd9e882009-02-08 23:14:22 +0000942
John McCallf1549f62010-07-06 01:34:17 +0000943 JumpDest BreakBlock;
944 JumpDest ContinueBlock;
Mike Stump0dd9e882009-02-08 23:14:22 +0000945 };
Chris Lattner686775d2011-07-20 06:58:45 +0000946 SmallVector<BreakContinue, 8> BreakContinueStack;
Daniel Dunbar18ccc772008-09-28 01:03:14 +0000947
Stephen Hines651f13c2014-04-23 16:59:28 -0700948 CodeGenPGO PGO;
949
950public:
951 /// Get a counter for instrumentation of the region associated with the given
952 /// statement.
953 RegionCounter getPGORegionCounter(const Stmt *S) {
954 return RegionCounter(PGO, S);
955 }
956private:
957
Zhongxing Xu170fd492012-01-14 09:08:15 +0000958 /// SwitchInsn - This is nearest current switch instruction. It is null if
Mike Stump0dd9e882009-02-08 23:14:22 +0000959 /// current context is not in a switch.
Devang Patel51b09f22007-10-04 23:45:31 +0000960 llvm::SwitchInst *SwitchInsn;
Stephen Hines651f13c2014-04-23 16:59:28 -0700961 /// The branch weights of SwitchInsn when doing instrumentation based PGO.
962 SmallVector<uint64_t, 16> *SwitchWeights;
Devang Patel51b09f22007-10-04 23:45:31 +0000963
Mike Stump0dd9e882009-02-08 23:14:22 +0000964 /// CaseRangeBlock - This block holds if condition check for last case
Devang Patel80fd5f92007-10-09 17:08:50 +0000965 /// statement range in current switch instruction.
Devang Patelc049e4f2007-10-08 20:57:48 +0000966 llvm::BasicBlock *CaseRangeBlock;
967
John McCall56ca35d2011-02-17 10:25:35 +0000968 /// OpaqueLValues - Keeps track of the current set of opaque value
John McCalle996ffd2011-02-16 08:02:54 +0000969 /// expressions.
John McCall56ca35d2011-02-17 10:25:35 +0000970 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
971 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
John McCalle996ffd2011-02-16 08:02:54 +0000972
Mike Stump0dd9e882009-02-08 23:14:22 +0000973 // VLASizeMap - This keeps track of the associated size for each VLA type.
Eli Friedmanbbed6b92009-08-15 02:50:32 +0000974 // We track this by the size expression rather than the type itself because
975 // in certain situations, like a const qualifier applied to an VLA typedef,
976 // multiple VLA types can share the same size expression.
Mike Stump0dd9e882009-02-08 23:14:22 +0000977 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
978 // enter/leave scopes.
Eli Friedmanbbed6b92009-08-15 02:50:32 +0000979 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
Mike Stump0dd9e882009-02-08 23:14:22 +0000980
John McCallf1549f62010-07-06 01:34:17 +0000981 /// A block containing a single 'unreachable' instruction. Created
982 /// lazily by getUnreachableBlock().
983 llvm::BasicBlock *UnreachableBlock;
Mike Stump0dd9e882009-02-08 23:14:22 +0000984
Adrian Prantld072e592013-05-03 20:11:48 +0000985 /// Counts of the number return expressions in the function.
986 unsigned NumReturnExprs;
Adrian Prantlfa6b0792013-05-02 17:30:20 +0000987
988 /// Count the number of simple (constant) return expressions in the function.
989 unsigned NumSimpleReturnExprs;
990
Adrian Prantld072e592013-05-03 20:11:48 +0000991 /// The last regular (non-return) debug location (breakpoint) in the function.
992 SourceLocation LastStopPoint;
Adrian Prantlfa6b0792013-05-02 17:30:20 +0000993
Richard Smithc3bf52c2013-04-20 22:23:05 +0000994public:
995 /// A scope within which we are constructing the fields of an object which
996 /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
997 /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
998 class FieldConstructionScope {
999 public:
1000 FieldConstructionScope(CodeGenFunction &CGF, llvm::Value *This)
1001 : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
1002 CGF.CXXDefaultInitExprThis = This;
1003 }
1004 ~FieldConstructionScope() {
1005 CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
1006 }
1007
1008 private:
1009 CodeGenFunction &CGF;
1010 llvm::Value *OldCXXDefaultInitExprThis;
1011 };
1012
1013 /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
1014 /// is overridden to be the object under construction.
1015 class CXXDefaultInitExprScope {
1016 public:
1017 CXXDefaultInitExprScope(CodeGenFunction &CGF)
1018 : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue) {
1019 CGF.CXXThisValue = CGF.CXXDefaultInitExprThis;
1020 }
1021 ~CXXDefaultInitExprScope() {
1022 CGF.CXXThisValue = OldCXXThisValue;
1023 }
1024
1025 public:
1026 CodeGenFunction &CGF;
1027 llvm::Value *OldCXXThisValue;
1028 };
1029
1030private:
Anders Carlssonf6c56e22009-11-25 03:15:49 +00001031 /// CXXThisDecl - When generating code for a C++ member function,
1032 /// this will hold the implicit 'this' declaration.
Eli Friedmancec5ebd2012-02-11 02:57:39 +00001033 ImplicitParamDecl *CXXABIThisDecl;
1034 llvm::Value *CXXABIThisValue;
John McCall25049412010-02-16 22:04:33 +00001035 llvm::Value *CXXThisValue;
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Richard Smithc3bf52c2013-04-20 22:23:05 +00001037 /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1038 /// this expression.
1039 llvm::Value *CXXDefaultInitExprThis;
1040
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001041 /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1042 /// destructor, this will hold the implicit argument (e.g. VTT).
1043 ImplicitParamDecl *CXXStructorImplicitParamDecl;
1044 llvm::Value *CXXStructorImplicitParamValue;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001045
John McCall150b4622011-01-26 04:00:11 +00001046 /// OutermostConditional - Points to the outermost active
1047 /// conditional control. This is used so that we know if a
1048 /// temporary should be destroyed conditionally.
1049 ConditionalEvaluation *OutermostConditional;
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Nadav Rotem495cfa42013-03-23 06:43:35 +00001051 /// The current lexical scope.
1052 LexicalScope *CurLexicalScope;
Anders Carlsson7dfa4072009-09-12 02:14:24 +00001053
Adrian Prantl1c3db762013-05-16 00:41:26 +00001054 /// The current source location that should be used for exception
1055 /// handling code.
1056 SourceLocation CurEHLocation;
1057
Anders Carlsson7dfa4072009-09-12 02:14:24 +00001058 /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
1059 /// type as well as the field number that contains the actual data.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001060 llvm::DenseMap<const ValueDecl *, std::pair<llvm::Type *,
Anders Carlsson7dfa4072009-09-12 02:14:24 +00001061 unsigned> > ByRefValueInfo;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001062
John McCallf1549f62010-07-06 01:34:17 +00001063 llvm::BasicBlock *TerminateLandingPad;
Mike Stump182f3832009-12-10 00:02:42 +00001064 llvm::BasicBlock *TerminateHandler;
Chris Lattner83252dc2010-07-20 21:07:09 +00001065 llvm::BasicBlock *TrapBB;
Eli Friedman94067052009-12-10 02:21:21 +00001066
Tanya Lattner0df579e2012-07-09 22:06:01 +00001067 /// Add a kernel metadata node to the named metadata node 'opencl.kernels'.
1068 /// In the kernel metadata node, reference the kernel function and metadata
1069 /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2):
Joey Gouly37453b92013-03-08 09:42:32 +00001070 /// - A node for the vec_type_hint(<type>) qualifier contains string
1071 /// "vec_type_hint", an undefined value of the <type> data type,
1072 /// and a Boolean that is true if the <type> is integer and signed.
Tanya Lattner0df579e2012-07-09 22:06:01 +00001073 /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string
1074 /// "work_group_size_hint", and three 32-bit integers X, Y and Z.
1075 /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string
1076 /// "reqd_work_group_size", and three 32-bit integers X, Y and Z.
1077 void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1078 llvm::Function *Fn);
1079
Reid Spencer5f016e22007-07-11 17:01:13 +00001080public:
Fariborz Jahanian4904bf42012-06-26 16:06:38 +00001081 CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
John McCall1a343eb2011-11-10 08:15:53 +00001082 ~CodeGenFunction();
Mike Stump0dd9e882009-02-08 23:14:22 +00001083
John McCall1e7fe752010-09-02 09:58:18 +00001084 CodeGenTypes &getTypes() const { return CGM.getTypes(); }
John McCallf2aac842011-05-15 02:34:36 +00001085 ASTContext &getContext() const { return CGM.getContext(); }
Devang Patelaa112892011-03-07 18:45:56 +00001086 CGDebugInfo *getDebugInfo() {
1087 if (DisableDebugInfo)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001088 return nullptr;
Devang Patelaa112892011-03-07 18:45:56 +00001089 return DebugInfo;
1090 }
1091 void disableDebugInfo() { DisableDebugInfo = true; }
1092 void enableDebugInfo() { DisableDebugInfo = false; }
1093
John McCallf85e1932011-06-15 23:02:42 +00001094 bool shouldUseFusedARCCalls() {
1095 return CGM.getCodeGenOpts().OptimizationLevel == 0;
1096 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001097
David Blaikie4e4d0842012-03-11 07:00:24 +00001098 const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
John McCalld16c2cf2011-02-08 08:22:06 +00001099
Bill Wendling285cfd82011-09-19 20:31:14 +00001100 /// Returns a pointer to the function's exception object and selector slot,
1101 /// which is assigned in every landing pad.
John McCallf1549f62010-07-06 01:34:17 +00001102 llvm::Value *getExceptionSlot();
John McCall93c332a2011-05-28 21:13:02 +00001103 llvm::Value *getEHSelectorSlot();
John McCallf1549f62010-07-06 01:34:17 +00001104
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001105 /// Stack slot that contains whether a __finally block is being executed as an
1106 /// EH cleanup or as a normal cleanup.
1107 llvm::Value *getAbnormalTerminationSlot();
1108
Bill Wendlingae270592011-09-15 18:57:19 +00001109 /// Returns the contents of the function's exception object and selector
1110 /// slots.
1111 llvm::Value *getExceptionFromSlot();
1112 llvm::Value *getSelectorFromSlot();
1113
John McCallff8e1152010-07-23 21:56:41 +00001114 llvm::Value *getNormalCleanupDestSlot();
John McCallff8e1152010-07-23 21:56:41 +00001115
John McCallf1549f62010-07-06 01:34:17 +00001116 llvm::BasicBlock *getUnreachableBlock() {
1117 if (!UnreachableBlock) {
1118 UnreachableBlock = createBasicBlock("unreachable");
1119 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1120 }
1121 return UnreachableBlock;
1122 }
1123
1124 llvm::BasicBlock *getInvokeDest() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001125 if (!EHStack.requiresLandingPad()) return nullptr;
John McCallf1549f62010-07-06 01:34:17 +00001126 return getInvokeDestImpl();
1127 }
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001128
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001129 bool currentFunctionUsesSEHTry() const {
1130 const auto *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
1131 return FD && FD->usesSEHTry();
1132 }
1133
John McCall64aa4b32013-04-16 22:48:15 +00001134 const TargetInfo &getTarget() const { return Target; }
John McCalld16c2cf2011-02-08 08:22:06 +00001135 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
Owen Anderson69243822009-07-13 04:10:07 +00001136
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001137 //===--------------------------------------------------------------------===//
John McCallbdc4d802011-07-09 01:37:26 +00001138 // Cleanups
1139 //===--------------------------------------------------------------------===//
1140
1141 typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty);
1142
John McCall2673c682011-07-11 08:38:19 +00001143 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1144 llvm::Value *arrayEndPointer,
1145 QualType elementType,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001146 Destroyer *destroyer);
John McCall2673c682011-07-11 08:38:19 +00001147 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1148 llvm::Value *arrayEnd,
1149 QualType elementType,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001150 Destroyer *destroyer);
John McCallbdc4d802011-07-09 01:37:26 +00001151
John McCall9928c482011-07-12 16:41:08 +00001152 void pushDestroy(QualType::DestructionKind dtorKind,
1153 llvm::Value *addr, QualType type);
John McCall074cae02013-02-01 05:11:40 +00001154 void pushEHDestroy(QualType::DestructionKind dtorKind,
1155 llvm::Value *addr, QualType type);
John McCallbdc4d802011-07-09 01:37:26 +00001156 void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001157 Destroyer *destroyer, bool useEHCleanupForArray);
Richard Smith8a07cd32013-06-12 20:42:33 +00001158 void pushLifetimeExtendedDestroy(CleanupKind kind, llvm::Value *addr,
1159 QualType type, Destroyer *destroyer,
1160 bool useEHCleanupForArray);
Stephen Hines176edba2014-12-01 14:53:08 -08001161 void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1162 llvm::Value *CompletePtr,
1163 QualType ElementType);
Stephen Hines651f13c2014-04-23 16:59:28 -07001164 void pushStackRestore(CleanupKind kind, llvm::Value *SPMem);
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001165 void emitDestroy(llvm::Value *addr, QualType type, Destroyer *destroyer,
John McCall2673c682011-07-11 08:38:19 +00001166 bool useEHCleanupForArray);
David Blaikiec7971a92013-08-27 23:57:18 +00001167 llvm::Function *generateDestroyHelper(llvm::Constant *addr, QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001168 Destroyer *destroyer,
David Blaikiec7971a92013-08-27 23:57:18 +00001169 bool useEHCleanupForArray,
1170 const VarDecl *VD);
John McCallbdc4d802011-07-09 01:37:26 +00001171 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001172 QualType type, Destroyer *destroyer,
John McCallfbf780a2011-07-13 08:09:46 +00001173 bool checkZeroLength, bool useEHCleanup);
John McCallbdc4d802011-07-09 01:37:26 +00001174
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001175 Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
John McCall9928c482011-07-12 16:41:08 +00001176
John McCallbdc4d802011-07-09 01:37:26 +00001177 /// Determines whether an EH cleanup is required to destroy a type
1178 /// with the given destruction kind.
1179 bool needsEHCleanup(QualType::DestructionKind kind) {
1180 switch (kind) {
1181 case QualType::DK_none:
1182 return false;
1183 case QualType::DK_cxx_destructor:
1184 case QualType::DK_objc_weak_lifetime:
David Blaikie4e4d0842012-03-11 07:00:24 +00001185 return getLangOpts().Exceptions;
John McCallbdc4d802011-07-09 01:37:26 +00001186 case QualType::DK_objc_strong_lifetime:
David Blaikie4e4d0842012-03-11 07:00:24 +00001187 return getLangOpts().Exceptions &&
John McCallbdc4d802011-07-09 01:37:26 +00001188 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1189 }
1190 llvm_unreachable("bad destruction kind");
1191 }
1192
John McCall9928c482011-07-12 16:41:08 +00001193 CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1194 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1195 }
1196
John McCallbdc4d802011-07-09 01:37:26 +00001197 //===--------------------------------------------------------------------===//
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001198 // Objective-C
1199 //===--------------------------------------------------------------------===//
1200
Chris Lattner391d77a2008-03-30 23:03:07 +00001201 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001202
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001203 void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001204
Mike Stump0dd9e882009-02-08 23:14:22 +00001205 /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +00001206 void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1207 const ObjCPropertyImplDecl *PID);
John McCall1e1f4872011-09-13 03:34:09 +00001208 void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
Fariborz Jahanianb6e5fe32012-01-07 18:56:22 +00001209 const ObjCPropertyImplDecl *propImpl,
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001210 const ObjCMethodDecl *GetterMothodDecl,
Fariborz Jahanianb6e5fe32012-01-07 18:56:22 +00001211 llvm::Constant *AtomicHelperFn);
Fariborz Jahanian2846b972011-02-18 19:15:13 +00001212
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00001213 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1214 ObjCMethodDecl *MD, bool ctor);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001215
Mike Stump0dd9e882009-02-08 23:14:22 +00001216 /// GenerateObjCSetter - Synthesize an Objective-C property setter function
1217 /// for the given property.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +00001218 void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1219 const ObjCPropertyImplDecl *PID);
John McCall71c758d2011-09-10 09:17:20 +00001220 void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
Fariborz Jahaniancd93b962012-01-06 22:33:54 +00001221 const ObjCPropertyImplDecl *propImpl,
1222 llvm::Constant *AtomicHelperFn);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +00001223 bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
Fariborz Jahanian15bd5882010-04-13 18:32:24 +00001224 bool IvarTypeWithAggrGCObjects(QualType Ty);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001225
Mike Stump4e7a1f72009-02-21 20:00:35 +00001226 //===--------------------------------------------------------------------===//
1227 // Block Bits
1228 //===--------------------------------------------------------------------===//
1229
John McCall6b5a61b2011-02-07 10:33:21 +00001230 llvm::Value *EmitBlockLiteral(const BlockExpr *);
John McCall1a343eb2011-11-10 08:15:53 +00001231 llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
1232 static void destroyBlockInfos(CGBlockInfo *info);
Blaine Garst2a7eb282010-02-23 21:51:17 +00001233 llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001234 const CGBlockInfo &Info,
Chris Lattner2acc6e32011-07-18 04:24:23 +00001235 llvm::StructType *,
John McCalld16c2cf2011-02-08 08:22:06 +00001236 llvm::Constant *BlockVarLayout);
Mike Stump4e7a1f72009-02-21 20:00:35 +00001237
Fariborz Jahanian564360b2010-06-24 00:08:06 +00001238 llvm::Function *GenerateBlockFunction(GlobalDecl GD,
John McCall6b5a61b2011-02-07 10:33:21 +00001239 const CGBlockInfo &Info,
Eli Friedman64bee652012-02-25 02:48:22 +00001240 const DeclMapTy &ldm,
1241 bool IsLambdaConversionToBlock);
Mike Stump4e7a1f72009-02-21 20:00:35 +00001242
John McCalld16c2cf2011-02-08 08:22:06 +00001243 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1244 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001245 llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
1246 const ObjCPropertyImplDecl *PID);
1247 llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
1248 const ObjCPropertyImplDecl *PID);
Eli Friedmancae40c42012-02-28 01:08:45 +00001249 llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
John McCalld16c2cf2011-02-08 08:22:06 +00001250
John McCalld16c2cf2011-02-08 08:22:06 +00001251 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1252
John McCall5af02db2011-03-31 01:59:53 +00001253 class AutoVarEmission;
1254
1255 void emitByrefStructureInit(const AutoVarEmission &emission);
1256 void enterByrefCleanup(const AutoVarEmission &emission);
1257
John McCall6b5a61b2011-02-07 10:33:21 +00001258 llvm::Value *LoadBlockStruct() {
1259 assert(BlockPointer && "no block pointer set!");
1260 return BlockPointer;
1261 }
Mike Stump4e7a1f72009-02-21 20:00:35 +00001262
John McCallea1471e2010-05-20 01:18:31 +00001263 void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
John McCallf4b88a42012-03-10 09:33:50 +00001264 void AllocateBlockDecl(const DeclRefExpr *E);
John McCall6b5a61b2011-02-07 10:33:21 +00001265 llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001266 llvm::Type *BuildByRefType(const VarDecl *var);
Mike Stumpdab514f2009-03-04 03:23:46 +00001267
John McCalld26bc762011-03-09 04:27:21 +00001268 void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1269 const CGFunctionInfo &FnInfo);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001270 /// \brief Emit code for the start of a function.
1271 /// \param Loc The location to be associated with the function.
1272 /// \param StartLoc The location of the function body.
John McCallf5ebf9b2013-05-03 07:33:41 +00001273 void StartFunction(GlobalDecl GD,
1274 QualType RetTy,
Daniel Dunbar7c086512008-09-09 23:14:03 +00001275 llvm::Function *Fn,
John McCalld26bc762011-03-09 04:27:21 +00001276 const CGFunctionInfo &FnInfo,
Daniel Dunbar2284ac92008-10-18 18:22:23 +00001277 const FunctionArgList &Args,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001278 SourceLocation Loc = SourceLocation(),
1279 SourceLocation StartLoc = SourceLocation());
Daniel Dunbara448fb22008-11-11 23:11:34 +00001280
John McCall9fc6a772010-02-19 09:25:03 +00001281 void EmitConstructorBody(FunctionArgList &Args);
1282 void EmitDestructorBody(FunctionArgList &Args);
Lang Hames56c00c42013-02-17 07:22:09 +00001283 void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
Richard Smith3cebc732013-11-05 09:12:18 +00001284 void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body);
Stephen Hines651f13c2014-04-23 16:59:28 -07001285 void EmitBlockWithFallThrough(llvm::BasicBlock *BB, RegionCounter &Cnt);
John McCalla355e072010-02-18 03:17:58 +00001286
Faisal Valid6992ab2013-09-29 08:45:24 +00001287 void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
Eli Friedman64bee652012-02-25 02:48:22 +00001288 CallArgList &CallArgs);
Eli Friedmanbd89f8c2012-02-16 01:37:33 +00001289 void EmitLambdaToBlockPointerBody(FunctionArgList &Args);
Eli Friedman64bee652012-02-25 02:48:22 +00001290 void EmitLambdaBlockInvokeBody();
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001291 void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
1292 void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD);
Stephen Hines176edba2014-12-01 14:53:08 -08001293 void EmitAsanPrologueOrEpilogue(bool Prologue);
Eli Friedmanbd89f8c2012-02-16 01:37:33 +00001294
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001295 /// \brief Emit the unified return block, trying to avoid its emission when
1296 /// possible.
1297 /// \return The debug location of the user written return statement if the
1298 /// return block is is avoided.
1299 llvm::DebugLoc EmitReturnBlock();
Daniel Dunbar1c1d6072009-01-26 23:27:52 +00001300
Mike Stump0dd9e882009-02-08 23:14:22 +00001301 /// FinishFunction - Complete IR generation of the current function. It is
1302 /// legal to call this function even if there is no current insertion point.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001303 void FinishFunction(SourceLocation EndLoc=SourceLocation());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001304
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001305 void StartThunk(llvm::Function *Fn, GlobalDecl GD,
1306 const CGFunctionInfo &FnInfo);
Hans Wennborg93b717a2013-11-15 17:24:45 +00001307
Stephen Hines176edba2014-12-01 14:53:08 -08001308 void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk);
1309
1310 /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
1311 void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr,
1312 llvm::Value *Callee);
Hans Wennborg93b717a2013-11-15 17:24:45 +00001313
Anders Carlsson519c3282010-03-24 00:39:18 +00001314 /// GenerateThunk - Generate a thunk for the given method.
John McCalld26bc762011-03-09 04:27:21 +00001315 void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1316 GlobalDecl GD, const ThunkInfo &Thunk);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001317
Eli Friedman7dcdf5b2011-05-06 17:27:27 +00001318 void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1319 GlobalDecl GD, const ThunkInfo &Thunk);
1320
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001321 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1322 FunctionArgList &Args);
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Eli Friedmanb74ed082012-02-14 02:31:03 +00001324 void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init,
1325 ArrayRef<VarDecl *> ArrayIndexes);
1326
Anders Carlssond103f9f2010-03-28 19:40:00 +00001327 /// InitializeVTablePointer - Initialize the vtable pointer of the given
1328 /// subobject.
1329 ///
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001330 void InitializeVTablePointer(BaseSubobject Base,
Anders Carlssonb3b772e2010-04-20 05:22:15 +00001331 const CXXRecordDecl *NearestVBase,
Ken Dyckd6fb21f2011-03-23 01:04:18 +00001332 CharUnits OffsetFromNearestVBase,
Anders Carlssond103f9f2010-03-28 19:40:00 +00001333 const CXXRecordDecl *VTableClass);
1334
1335 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001336 void InitializeVTablePointers(BaseSubobject Base,
Anders Carlssonb3b772e2010-04-20 05:22:15 +00001337 const CXXRecordDecl *NearestVBase,
Ken Dyckd6fb21f2011-03-23 01:04:18 +00001338 CharUnits OffsetFromNearestVBase,
Anders Carlsson603d6d12010-03-28 21:07:49 +00001339 bool BaseIsNonVirtualPrimaryBase,
Anders Carlsson603d6d12010-03-28 21:07:49 +00001340 const CXXRecordDecl *VTableClass,
1341 VisitedVirtualBasesSetTy& VBases);
Eli Friedman77a259c2009-12-08 06:46:18 +00001342
Anders Carlsson603d6d12010-03-28 21:07:49 +00001343 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
Anders Carlssond103f9f2010-03-28 19:40:00 +00001344
Dan Gohman043fb9a2010-10-26 18:44:08 +00001345 /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1346 /// to by This.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001347 llvm::Value *GetVTablePtr(llvm::Value *This, llvm::Type *Ty);
Anders Carlssond103f9f2010-03-28 19:40:00 +00001348
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001349 /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
1350 /// If vptr CFI is enabled, emit a check that VTable is valid.
1351 void EmitVTablePtrCheckForCall(const CXXMethodDecl *MD, llvm::Value *VTable);
Benjamin Kramer9581ed02013-08-25 22:46:27 +00001352
1353 /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
1354 /// expr can be devirtualized.
1355 bool CanDevirtualizeMemberFunctionCall(const Expr *Base,
1356 const CXXMethodDecl *MD);
1357
John McCall50da2ca2010-07-21 05:30:47 +00001358 /// EnterDtorCleanups - Enter the cleanups necessary to complete the
1359 /// given phase of destruction for a destructor. The end result
1360 /// should call destructors on members and base classes in reverse
1361 /// order of their construction.
1362 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Chris Lattner7255a2d2010-06-22 00:03:40 +00001364 /// ShouldInstrumentFunction - Return true if the current function should be
1365 /// instrumented with __cyg_profile_func_* calls
1366 bool ShouldInstrumentFunction();
1367
1368 /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1369 /// instrumentation function with the current function and the call site, if
1370 /// function instrumentation is enabled.
1371 void EmitFunctionInstrumentation(const char *Fn);
1372
Roman Divackybe4c8702011-02-10 16:52:03 +00001373 /// EmitMCountInstrumentation - Emit call to .mcount.
1374 void EmitMCountInstrumentation();
1375
Mike Stump0dd9e882009-02-08 23:14:22 +00001376 /// EmitFunctionProlog - Emit the target specific LLVM code to load the
1377 /// arguments for the given function. This is also responsible for naming the
1378 /// LLVM function arguments.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001379 void EmitFunctionProlog(const CGFunctionInfo &FI,
1380 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001381 const FunctionArgList &Args);
1382
Mike Stump0dd9e882009-02-08 23:14:22 +00001383 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
1384 /// given temporary.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001385 void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
1386 SourceLocation EndLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001387
Mike Stumpcce3d4f2009-12-07 23:38:24 +00001388 /// EmitStartEHSpec - Emit the start of the exception spec.
1389 void EmitStartEHSpec(const Decl *D);
1390
1391 /// EmitEndEHSpec - Emit the end of the exception spec.
1392 void EmitEndEHSpec(const Decl *D);
1393
John McCallf1549f62010-07-06 01:34:17 +00001394 /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1395 llvm::BasicBlock *getTerminateLandingPad();
1396
1397 /// getTerminateHandler - Return a handler (not a landing pad, just
1398 /// a catch handler) that just calls terminate. This is used when
1399 /// a terminate scope encloses a try.
Mike Stump9b39c512009-12-09 22:59:31 +00001400 llvm::BasicBlock *getTerminateHandler();
1401
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001402 llvm::Type *ConvertTypeForMem(QualType T);
1403 llvm::Type *ConvertType(QualType T);
1404 llvm::Type *ConvertType(const TypeDecl *T) {
John McCallbff225e2010-02-16 04:15:37 +00001405 return ConvertType(getContext().getTypeDeclType(T));
1406 }
Chris Lattnerc8aa5f12008-04-04 04:07:35 +00001407
Mike Stump0dd9e882009-02-08 23:14:22 +00001408 /// LoadObjCSelf - Load the value of self. This function is only valid while
1409 /// generating code for an Objective-C method.
Chris Lattnerc8aa5f12008-04-04 04:07:35 +00001410 llvm::Value *LoadObjCSelf();
Mike Stump0dd9e882009-02-08 23:14:22 +00001411
1412 /// TypeOfSelfObject - Return type of object that this self represents.
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001413 QualType TypeOfSelfObject();
Chris Lattner41110242008-06-17 18:05:57 +00001414
Reid Spencer5f016e22007-07-11 17:01:13 +00001415 /// hasAggregateLLVMType - Return true if the specified AST type will map into
1416 /// an aggregate LLVM type or is void.
John McCall9d232c82013-03-07 21:37:08 +00001417 static TypeEvaluationKind getEvaluationKind(QualType T);
1418
1419 static bool hasScalarEvaluationKind(QualType T) {
1420 return getEvaluationKind(T) == TEK_Scalar;
1421 }
1422
1423 static bool hasAggregateEvaluationKind(QualType T) {
1424 return getEvaluationKind(T) == TEK_Aggregate;
1425 }
Daniel Dunbar55e87422008-11-11 02:29:29 +00001426
1427 /// createBasicBlock - Create an LLVM basic block.
Richard Smith4def70d2012-10-09 19:52:38 +00001428 llvm::BasicBlock *createBasicBlock(const Twine &name = "",
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001429 llvm::Function *parent = nullptr,
1430 llvm::BasicBlock *before = nullptr) {
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001431#ifdef NDEBUG
John McCalld16c2cf2011-02-08 08:22:06 +00001432 return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001433#else
John McCalld16c2cf2011-02-08 08:22:06 +00001434 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001435#endif
Daniel Dunbar55e87422008-11-11 02:29:29 +00001436 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001437
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
1439 /// label maps to.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001440 JumpDest getJumpDestForLabel(const LabelDecl *S);
Mike Stump0dd9e882009-02-08 23:14:22 +00001441
Mike Stumpf71d2322009-11-30 20:08:49 +00001442 /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1443 /// another basic block, simplify it. This assumes that no other code could
1444 /// potentially reference the basic block.
Daniel Dunbaraa5bd872009-04-01 04:37:47 +00001445 void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1446
Mike Stump0dd9e882009-02-08 23:14:22 +00001447 /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
1448 /// adding a fall-through branch from the current insert block if
1449 /// necessary. It is legal to call this function even if there is no current
1450 /// insertion point.
Daniel Dunbara0c21a82008-11-13 01:24:05 +00001451 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00001452 /// IsFinished - If true, indicates that the caller has finished emitting
1453 /// branches to the given block and does not expect to emit code into it. This
1454 /// means the block can be ignored if it is unreachable.
Daniel Dunbara0c21a82008-11-13 01:24:05 +00001455 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
Daniel Dunbar824e3bd2008-11-11 04:34:23 +00001456
John McCall777d6e52011-08-11 02:22:43 +00001457 /// EmitBlockAfterUses - Emit the given block somewhere hopefully
1458 /// near its uses, and leave the insertion point in it.
1459 void EmitBlockAfterUses(llvm::BasicBlock *BB);
1460
Mike Stump0dd9e882009-02-08 23:14:22 +00001461 /// EmitBranch - Emit a branch to the specified basic block from the current
1462 /// insert block, taking care to avoid creation of branches from dummy
1463 /// blocks. It is legal to call this function even if there is no current
1464 /// insertion point.
Daniel Dunbar5e08ad32008-11-11 22:06:59 +00001465 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00001466 /// This function clears the current insertion point. The caller should follow
1467 /// calls to this function with calls to Emit*Block prior to generation new
1468 /// code.
Daniel Dunbard57a8712008-11-11 09:41:28 +00001469 void EmitBranch(llvm::BasicBlock *Block);
1470
Mike Stump0dd9e882009-02-08 23:14:22 +00001471 /// HaveInsertPoint - True if an insertion point is defined. If not, this
1472 /// indicates that the current code being emitted is unreachable.
1473 bool HaveInsertPoint() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001474 return Builder.GetInsertBlock() != nullptr;
Daniel Dunbara448fb22008-11-11 23:11:34 +00001475 }
1476
Mike Stump0dd9e882009-02-08 23:14:22 +00001477 /// EnsureInsertPoint - Ensure that an insertion point is defined so that
1478 /// emitted IR has a place to go. Note that by definition, if this function
1479 /// creates a block then that block is unreachable; callers may do better to
1480 /// detect when no insertion point is defined and simply skip IR generation.
Daniel Dunbara448fb22008-11-11 23:11:34 +00001481 void EnsureInsertPoint() {
1482 if (!HaveInsertPoint())
1483 EmitBlock(createBasicBlock());
1484 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001485
Daniel Dunbar488e9932008-08-16 00:56:44 +00001486 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerdc5e8262007-12-02 01:43:38 +00001487 /// specified stmt yet.
David Blaikie0a1c8622013-08-19 21:02:26 +00001488 void ErrorUnsupported(const Stmt *S, const char *Type);
Reid Spencer5f016e22007-07-11 17:01:13 +00001489
1490 //===--------------------------------------------------------------------===//
1491 // Helpers
1492 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00001493
Eli Friedman6da2c712011-12-03 04:14:32 +00001494 LValue MakeAddrLValue(llvm::Value *V, QualType T,
1495 CharUnits Alignment = CharUnits()) {
Dan Gohman3d5aff52010-10-14 23:06:10 +00001496 return LValue::MakeAddr(V, T, Alignment, getContext(),
1497 CGM.getTBAAInfo(T));
Daniel Dunbar5cf8bfe2010-08-21 02:53:44 +00001498 }
John McCalle0c11682012-07-02 23:58:38 +00001499
Stephen Hines176edba2014-12-01 14:53:08 -08001500 LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
Daniel Dunbar5cf8bfe2010-08-21 02:53:44 +00001501
Reid Spencer5f016e22007-07-11 17:01:13 +00001502 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
Daniel Dunbar195337d2010-02-09 02:48:28 +00001503 /// block. The caller is responsible for setting an appropriate alignment on
1504 /// the alloca.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001505 llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
Chris Lattner8cc488f2011-07-20 07:06:53 +00001506 const Twine &Name = "tmp");
Mike Stump0dd9e882009-02-08 23:14:22 +00001507
John McCallac418162010-04-22 01:10:34 +00001508 /// InitTempAlloca - Provide an initial value for the given alloca.
1509 void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1510
Daniel Dunbar9bd4da22010-02-16 19:44:13 +00001511 /// CreateIRTemp - Create a temporary IR object of the given type, with
1512 /// appropriate alignment. This routine should only be used when an temporary
1513 /// value needs to be stored into an alloca (for example, to avoid explicit
1514 /// PHI construction), but the type is the IR type, not the type appropriate
1515 /// for storing in memory.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001516 llvm::AllocaInst *CreateIRTemp(QualType T, const Twine &Name = "tmp");
Daniel Dunbar9bd4da22010-02-16 19:44:13 +00001517
Daniel Dunbar195337d2010-02-09 02:48:28 +00001518 /// CreateMemTemp - Create a temporary memory object of the given type, with
1519 /// appropriate alignment.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001520 llvm::AllocaInst *CreateMemTemp(QualType T, const Twine &Name = "tmp");
Daniel Dunbar195337d2010-02-09 02:48:28 +00001521
John McCall558d2ab2010-09-15 10:14:12 +00001522 /// CreateAggTemp - Create a temporary memory object for the given
1523 /// aggregate type.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001524 AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
Eli Friedmand7722d92011-12-03 02:13:40 +00001525 CharUnits Alignment = getContext().getTypeAlignInChars(T);
Eli Friedmanf3940782011-12-03 00:54:26 +00001526 return AggValueSlot::forAddr(CreateMemTemp(T, Name), Alignment,
1527 T.getQualifiers(),
John McCall7c2349b2011-08-25 20:40:09 +00001528 AggValueSlot::IsNotDestructed,
John McCall410ffb22011-08-25 23:04:34 +00001529 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier649b4a12012-03-29 17:37:10 +00001530 AggValueSlot::IsNotAliased);
John McCall558d2ab2010-09-15 10:14:12 +00001531 }
1532
Stephen Hines651f13c2014-04-23 16:59:28 -07001533 /// CreateInAllocaTmp - Create a temporary memory object for the given
1534 /// aggregate type.
1535 AggValueSlot CreateInAllocaTmp(QualType T, const Twine &Name = "inalloca");
1536
John McCalld16c2cf2011-02-08 08:22:06 +00001537 /// Emit a cast to void* in the appropriate address space.
1538 llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1539
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
1541 /// expression and compare the result against zero, returning an Int1Ty value.
1542 llvm::Value *EvaluateExprAsBool(const Expr *E);
1543
John McCall2a416372010-12-05 02:00:02 +00001544 /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
1545 void EmitIgnoredExpr(const Expr *E);
1546
Chris Lattner9b655512007-08-31 22:49:20 +00001547 /// EmitAnyExpr - Emit code to compute the specified expression which can have
1548 /// any type. The result is returned as an RValue struct. If this is an
1549 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
1550 /// the result should be returned.
Mike Stump49d1cd52009-05-26 22:03:21 +00001551 ///
Dmitri Gribenko70517ca2012-08-23 17:58:28 +00001552 /// \param ignoreResult True if the resulting value isn't used.
John McCall558d2ab2010-09-15 10:14:12 +00001553 RValue EmitAnyExpr(const Expr *E,
John McCalle0c11682012-07-02 23:58:38 +00001554 AggValueSlot aggSlot = AggValueSlot::ignored(),
1555 bool ignoreResult = false);
Devang Pateld9363c32007-09-28 21:49:18 +00001556
Mike Stump0dd9e882009-02-08 23:14:22 +00001557 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
1558 // or the value of the expression, depending on how va_list is defined.
Eli Friedman4fd0aa52009-01-20 17:46:04 +00001559 llvm::Value *EmitVAListRef(const Expr *E);
1560
Mike Stump0dd9e882009-02-08 23:14:22 +00001561 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
1562 /// always be accessible even if no aggregate location is provided.
John McCall558d2ab2010-09-15 10:14:12 +00001563 RValue EmitAnyExprToTemp(const Expr *E);
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001564
John McCall60d33652011-03-08 09:11:50 +00001565 /// EmitAnyExprToMem - Emits the code necessary to evaluate an
Chad Rosier649b4a12012-03-29 17:37:10 +00001566 /// arbitrary expression into the given memory location.
John McCall3d3ec1c2010-04-21 10:05:39 +00001567 void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
Chad Rosier649b4a12012-03-29 17:37:10 +00001568 Qualifiers Quals, bool IsInitializer);
John McCall3d3ec1c2010-04-21 10:05:39 +00001569
John McCall60d33652011-03-08 09:11:50 +00001570 /// EmitExprAsInit - Emits the code necessary to initialize a
1571 /// location in memory with the given initializer.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001572 void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1573 bool capturedByInit);
John McCall60d33652011-03-08 09:11:50 +00001574
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +00001575 /// hasVolatileMember - returns true if aggregate type has a volatile
1576 /// member.
1577 bool hasVolatileMember(QualType T) {
1578 if (const RecordType *RT = T->getAs<RecordType>()) {
1579 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
1580 return RD->hasVolatileMember();
1581 }
1582 return false;
1583 }
Arnaud A. de Grandmaison8fcbb8d2013-02-05 09:06:17 +00001584 /// EmitAggregateCopy - Emit an aggregate assignment.
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001585 ///
1586 /// The difference to EmitAggregateCopy is that tail padding is not copied.
1587 /// This is required for correctness when assigning non-POD structures in C++.
1588 void EmitAggregateAssign(llvm::Value *DestPtr, llvm::Value *SrcPtr,
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +00001589 QualType EltTy) {
1590 bool IsVolatile = hasVolatileMember(EltTy);
1591 EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, CharUnits::Zero(),
1592 true);
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001593 }
1594
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001595 void EmitAggregateCopyCtor(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1596 QualType DestTy, QualType SrcTy) {
1597 CharUnits DestTypeAlign = getContext().getTypeAlignInChars(DestTy);
1598 CharUnits SrcTypeAlign = getContext().getTypeAlignInChars(SrcTy);
1599 EmitAggregateCopy(DestPtr, SrcPtr, SrcTy, /*IsVolatile=*/false,
1600 std::min(DestTypeAlign, SrcTypeAlign),
1601 /*IsAssignment=*/false);
1602 }
1603
Arnaud A. de Grandmaison8fcbb8d2013-02-05 09:06:17 +00001604 /// EmitAggregateCopy - Emit an aggregate copy.
Mike Stump27fe2e62009-05-23 22:29:41 +00001605 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001606 /// \param isVolatile - True iff either the source or the destination is
Mike Stump27fe2e62009-05-23 22:29:41 +00001607 /// volatile.
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001608 /// \param isAssignment - If false, allow padding to be copied. This often
1609 /// yields more efficient.
Daniel Dunbar7482d122008-09-09 20:49:46 +00001610 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
Eli Friedmanbd7d8282011-12-05 22:23:28 +00001611 QualType EltTy, bool isVolatile=false,
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001612 CharUnits Alignment = CharUnits::Zero(),
1613 bool isAssignment = false);
Daniel Dunbar7482d122008-09-09 20:49:46 +00001614
Devang Patel51b09f22007-10-04 23:45:31 +00001615 /// StartBlock - Start new block named N. If insert block is a dummy block
1616 /// then reuse it.
1617 void StartBlock(const char *N);
1618
Anders Carlssondde0a942008-09-11 09:15:33 +00001619 /// GetAddrOfLocalVar - Return the address of a local variable.
John McCall4c40d982010-08-31 07:33:07 +00001620 llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
1621 llvm::Value *Res = LocalDeclMap[VD];
1622 assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
1623 return Res;
1624 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001625
John McCall56ca35d2011-02-17 10:25:35 +00001626 /// getOpaqueLValueMapping - Given an opaque value expression (which
1627 /// must be mapped to an l-value), return its mapping.
1628 const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
1629 assert(OpaqueValueMapping::shouldBindAsLValue(e));
1630
1631 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
1632 it = OpaqueLValues.find(e);
1633 assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
1634 return it->second;
1635 }
1636
1637 /// getOpaqueRValueMapping - Given an opaque value expression (which
1638 /// must be mapped to an r-value), return its mapping.
1639 const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
1640 assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1641
1642 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
1643 it = OpaqueRValues.find(e);
1644 assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
John McCalle996ffd2011-02-16 08:02:54 +00001645 return it->second;
1646 }
1647
Dan Gohman4f8d1232008-05-22 00:50:06 +00001648 /// getAccessedFieldNo - Given an encoded value and a result number, return
1649 /// the input field number being accessed.
1650 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
1651
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001652 llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
Chris Lattner3d00fdc2009-10-13 06:55:33 +00001653 llvm::BasicBlock *GetIndirectGotoBlock();
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00001654
Anders Carlsson1884eb02010-05-22 17:35:42 +00001655 /// EmitNullInitialization - Generate code to set a value of the given type to
1656 /// null, If the type contains data member pointers, they will be initialized
1657 /// to -1 in accordance with the Itanium C++ ABI.
1658 void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
Anders Carlssonddf7cac2008-11-04 05:30:00 +00001659
1660 // EmitVAArg - Generate code to get an argument from the passed in pointer
1661 // and update it accordingly. The return value is a pointer to the argument.
1662 // FIXME: We should be able to get rid of this method and use the va_arg
Mike Stump0dd9e882009-02-08 23:14:22 +00001663 // instruction in LLVM instead once it works well enough.
Anders Carlssonddf7cac2008-11-04 05:30:00 +00001664 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
Anders Carlssonf666b772008-12-20 20:27:15 +00001665
John McCallbdc4d802011-07-09 01:37:26 +00001666 /// emitArrayLength - Compute the length of an array, even if it's a
1667 /// VLA, and drill down to the base element type.
1668 llvm::Value *emitArrayLength(const ArrayType *arrayType,
1669 QualType &baseType,
1670 llvm::Value *&addr);
1671
John McCallbc8d40d2011-06-24 21:55:10 +00001672 /// EmitVLASize - Capture all the sizes for the VLA expressions in
1673 /// the given variably-modified type and store them in the VLASizeMap.
Daniel Dunbard286f052009-07-19 06:58:07 +00001674 ///
1675 /// This function can be called with a null (unreachable) insert point.
John McCallbc8d40d2011-06-24 21:55:10 +00001676 void EmitVariablyModifiedType(QualType Ty);
Mike Stump0dd9e882009-02-08 23:14:22 +00001677
John McCallbc8d40d2011-06-24 21:55:10 +00001678 /// getVLASize - Returns an LLVM value that corresponds to the size,
1679 /// in non-variably-sized elements, of a variable length array type,
1680 /// plus that largest non-variably-sized element type. Assumes that
1681 /// the type has already been emitted with EmitVariablyModifiedType.
1682 std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1683 std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
Anders Carlssondcc90d82008-12-12 07:19:02 +00001684
Anders Carlsson5f4307b2009-04-14 16:58:56 +00001685 /// LoadCXXThis - Load the value of 'this'. This function is only valid while
1686 /// generating code for an C++ member function.
John McCall25049412010-02-16 22:04:33 +00001687 llvm::Value *LoadCXXThis() {
1688 assert(CXXThisValue && "no 'this' value for this function");
1689 return CXXThisValue;
1690 }
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Anders Carlssonc997d422010-01-02 01:01:18 +00001692 /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1693 /// virtual bases.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001694 // FIXME: Every place that calls LoadCXXVTT is something
1695 // that needs to be abstracted properly.
John McCall25049412010-02-16 22:04:33 +00001696 llvm::Value *LoadCXXVTT() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001697 assert(CXXStructorImplicitParamValue && "no VTT value for this function");
1698 return CXXStructorImplicitParamValue;
1699 }
1700
1701 /// LoadCXXStructorImplicitParam - Load the implicit parameter
1702 /// for a constructor/destructor.
1703 llvm::Value *LoadCXXStructorImplicitParam() {
1704 assert(CXXStructorImplicitParamValue &&
1705 "no implicit argument value for this function");
1706 return CXXStructorImplicitParamValue;
John McCall25049412010-02-16 22:04:33 +00001707 }
John McCallbff225e2010-02-16 04:15:37 +00001708
1709 /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
Anders Carlsson8561a862010-04-24 23:01:49 +00001710 /// complete class to the given direct base.
1711 llvm::Value *
1712 GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
1713 const CXXRecordDecl *Derived,
1714 const CXXRecordDecl *Base,
1715 bool BaseIsVirtual);
Anders Carlssona88ad562010-04-24 21:51:08 +00001716
Mike Stumpf71d2322009-11-30 20:08:49 +00001717 /// GetAddressOfBaseClass - This function will add the necessary delta to the
1718 /// load of 'this' and returns address of the base class.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001719 llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
Anders Carlsson8561a862010-04-24 23:01:49 +00001720 const CXXRecordDecl *Derived,
John McCallf871d0c2010-08-07 06:22:56 +00001721 CastExpr::path_const_iterator PathBegin,
1722 CastExpr::path_const_iterator PathEnd,
Stephen Hines176edba2014-12-01 14:53:08 -08001723 bool NullCheckValue, SourceLocation Loc);
Anders Carlsson34a2d382010-04-24 21:06:20 +00001724
Anders Carlssona3697c92009-11-23 17:57:54 +00001725 llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
Anders Carlsson8561a862010-04-24 23:01:49 +00001726 const CXXRecordDecl *Derived,
John McCallf871d0c2010-08-07 06:22:56 +00001727 CastExpr::path_const_iterator PathBegin,
1728 CastExpr::path_const_iterator PathEnd,
Anders Carlssona3697c92009-11-23 17:57:54 +00001729 bool NullCheckValue);
1730
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001731 /// GetVTTParameter - Return the VTT parameter that should be passed to a
1732 /// base constructor/destructor with virtual bases.
1733 /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
1734 /// to ItaniumCXXABI.cpp together with all the references to VTT.
1735 llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
1736 bool Delegating);
1737
John McCallc0bf4622010-02-23 00:48:20 +00001738 void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1739 CXXCtorType CtorType,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001740 const FunctionArgList &Args,
1741 SourceLocation Loc);
Sean Hunt059ce0d2011-05-01 07:04:31 +00001742 // It's important not to confuse this and the previous function. Delegating
1743 // constructors are the C++0x feature. The constructor delegate optimization
1744 // is used to reduce duplication in the base and complete consturctors where
1745 // they are substantially the same.
1746 void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1747 const FunctionArgList &Args);
Anders Carlsson155ed4a2010-05-02 23:20:53 +00001748 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
Douglas Gregor378e1e72013-01-31 05:50:40 +00001749 bool ForVirtualBase, bool Delegating,
Stephen Hines176edba2014-12-01 14:53:08 -08001750 llvm::Value *This, const CXXConstructExpr *E);
1751
Fariborz Jahanian34999872010-11-13 21:53:34 +00001752 void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1753 llvm::Value *This, llvm::Value *Src,
Stephen Hines176edba2014-12-01 14:53:08 -08001754 const CXXConstructExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001756 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +00001757 const ConstantArrayType *ArrayTy,
Anders Carlsson5d4d9462009-11-24 18:43:52 +00001758 llvm::Value *ArrayPtr,
Stephen Hines176edba2014-12-01 14:53:08 -08001759 const CXXConstructExpr *E,
Douglas Gregor59174c02010-07-21 01:10:17 +00001760 bool ZeroInitialization = false);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001761
Anders Carlsson569c1f42009-09-23 02:45:36 +00001762 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1763 llvm::Value *NumElements,
Anders Carlsson5d4d9462009-11-24 18:43:52 +00001764 llvm::Value *ArrayPtr,
Stephen Hines176edba2014-12-01 14:53:08 -08001765 const CXXConstructExpr *E,
Douglas Gregor59174c02010-07-21 01:10:17 +00001766 bool ZeroInitialization = false);
Anders Carlssonb14095a2009-04-17 00:06:03 +00001767
John McCallbdc4d802011-07-09 01:37:26 +00001768 static Destroyer destroyCXXObject;
1769
Anders Carlsson7267c162009-05-29 21:03:38 +00001770 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
Douglas Gregor378e1e72013-01-31 05:50:40 +00001771 bool ForVirtualBase, bool Delegating,
1772 llvm::Value *This);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001773
John McCall19705672011-09-15 06:49:18 +00001774 void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001775 llvm::Value *NewPtr, llvm::Value *NumElements,
1776 llvm::Value *AllocSizeWithoutCookie);
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Peter Collingbourne86811602011-11-27 22:09:22 +00001778 void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
1779 llvm::Value *Ptr);
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Anders Carlssona00703d2009-05-31 01:40:14 +00001781 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
Anders Carlsson60e282c2009-08-16 21:13:42 +00001782 void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Eli Friedman4bf81522009-11-18 00:57:03 +00001784 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
1785 QualType DeleteTy);
1786
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001787 RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
1788 const Expr *Arg, bool IsDelete);
1789
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001790 llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
Mike Stumpc849c052009-11-16 06:50:58 +00001791 llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
Nico Weberc5f80462012-10-11 10:13:44 +00001792 llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001793
Richard Smith2c9f87c2012-08-24 00:54:33 +00001794 /// \brief Situations in which we might emit a check for the suitability of a
1795 /// pointer or glvalue.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001796 enum TypeCheckKind {
Richard Smith2c9f87c2012-08-24 00:54:33 +00001797 /// Checking the operand of a load. Must be suitably sized and aligned.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001798 TCK_Load,
Richard Smith2c9f87c2012-08-24 00:54:33 +00001799 /// Checking the destination of a store. Must be suitably sized and aligned.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001800 TCK_Store,
Richard Smith2c9f87c2012-08-24 00:54:33 +00001801 /// Checking the bound value in a reference binding. Must be suitably sized
1802 /// and aligned, but is not required to refer to an object (until the
1803 /// reference is used), per core issue 453.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001804 TCK_ReferenceBinding,
Richard Smith2c9f87c2012-08-24 00:54:33 +00001805 /// Checking the object expression in a non-static data member access. Must
1806 /// be an object within its lifetime.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001807 TCK_MemberAccess,
Richard Smith2c9f87c2012-08-24 00:54:33 +00001808 /// Checking the 'this' pointer for a call to a non-static member function.
1809 /// Must be an object within its lifetime.
Richard Smith8e1cee62012-10-25 02:14:12 +00001810 TCK_MemberCall,
1811 /// Checking the 'this' pointer for a constructor call.
Richard Smithc7648302013-02-13 21:18:23 +00001812 TCK_ConstructorCall,
1813 /// Checking the operand of a static_cast to a derived pointer type. Must be
1814 /// null or an object within its lifetime.
1815 TCK_DowncastPointer,
1816 /// Checking the operand of a static_cast to a derived reference type. Must
1817 /// be an object within its lifetime.
Stephen Hines176edba2014-12-01 14:53:08 -08001818 TCK_DowncastReference,
1819 /// Checking the operand of a cast to a base object. Must be suitably sized
1820 /// and aligned.
1821 TCK_Upcast,
1822 /// Checking the operand of a cast to a virtual base object. Must be an
1823 /// object within its lifetime.
1824 TCK_UpcastToVirtualBase
Richard Smith2c9f87c2012-08-24 00:54:33 +00001825 };
1826
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001827 /// \brief Whether any type-checking sanitizers are enabled. If \c false,
1828 /// calls to EmitTypeCheck can be skipped.
1829 bool sanitizePerformTypeCheck() const;
1830
Richard Smith7ac9ef12012-09-08 02:08:36 +00001831 /// \brief Emit a check that \p V is the address of storage of the
Richard Smith2c9f87c2012-08-24 00:54:33 +00001832 /// appropriate size and alignment for an object of type \p Type.
Richard Smith4def70d2012-10-09 19:52:38 +00001833 void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
Stephen Hines176edba2014-12-01 14:53:08 -08001834 QualType Type, CharUnits Alignment = CharUnits::Zero(),
1835 bool SkipNullCheck = false);
Mike Stumpb14e62d2009-12-16 02:57:00 +00001836
Richard Smitha0a628f2013-02-23 02:53:19 +00001837 /// \brief Emit a check that \p Base points into an array object, which
1838 /// we can access at index \p Index. \p Accessed should be \c false if we
1839 /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
1840 void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
1841 QualType IndexType, bool Accessed);
1842
Chris Lattnerdd36d322010-01-09 21:40:03 +00001843 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1844 bool isInc, bool isPre);
1845 ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1846 bool isInc, bool isPre);
Stephen Hines176edba2014-12-01 14:53:08 -08001847
1848 void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment,
1849 llvm::Value *OffsetValue = nullptr) {
1850 Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
1851 OffsetValue);
1852 }
1853
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 //===--------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 // Declaration Emission
1856 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00001857
Daniel Dunbard286f052009-07-19 06:58:07 +00001858 /// EmitDecl - Emit a declaration.
1859 ///
1860 /// This function can be called with a null (unreachable) insert point.
Reid Spencer5f016e22007-07-11 17:01:13 +00001861 void EmitDecl(const Decl &D);
Daniel Dunbard286f052009-07-19 06:58:07 +00001862
John McCallb6bbcc92010-10-15 04:57:14 +00001863 /// EmitVarDecl - Emit a local variable declaration.
Daniel Dunbard286f052009-07-19 06:58:07 +00001864 ///
1865 /// This function can be called with a null (unreachable) insert point.
John McCallb6bbcc92010-10-15 04:57:14 +00001866 void EmitVarDecl(const VarDecl &D);
Daniel Dunbard286f052009-07-19 06:58:07 +00001867
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001868 void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1869 bool capturedByInit);
John McCall7acddac2011-06-17 06:42:21 +00001870 void EmitScalarInit(llvm::Value *init, LValue lvalue);
John McCallf85e1932011-06-15 23:02:42 +00001871
John McCallf1549f62010-07-06 01:34:17 +00001872 typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1873 llvm::Value *Address);
1874
Stephen Hines176edba2014-12-01 14:53:08 -08001875 /// \brief Determine whether the given initializer is trivial in the sense
1876 /// that it requires no code to be generated.
1877 bool isTrivialInitializer(const Expr *Init);
1878
John McCallb6bbcc92010-10-15 04:57:14 +00001879 /// EmitAutoVarDecl - Emit an auto variable declaration.
Daniel Dunbard286f052009-07-19 06:58:07 +00001880 ///
1881 /// This function can be called with a null (unreachable) insert point.
John McCall34695852011-02-22 06:44:22 +00001882 void EmitAutoVarDecl(const VarDecl &D);
1883
1884 class AutoVarEmission {
1885 friend class CodeGenFunction;
1886
John McCall57b3b6a2011-02-22 07:16:58 +00001887 const VarDecl *Variable;
John McCall34695852011-02-22 06:44:22 +00001888
1889 /// The alignment of the variable.
1890 CharUnits Alignment;
1891
1892 /// The address of the alloca. Null if the variable was emitted
1893 /// as a global constant.
1894 llvm::Value *Address;
1895
1896 llvm::Value *NRVOFlag;
1897
1898 /// True if the variable is a __block variable.
1899 bool IsByRef;
1900
1901 /// True if the variable is of aggregate type and has a constant
1902 /// initializer.
1903 bool IsConstantAggregate;
1904
Nadav Rotem495cfa42013-03-23 06:43:35 +00001905 /// Non-null if we should use lifetime annotations.
1906 llvm::Value *SizeForLifetimeMarkers;
1907
John McCall57b3b6a2011-02-22 07:16:58 +00001908 struct Invalid {};
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001909 AutoVarEmission(Invalid) : Variable(nullptr) {}
John McCall57b3b6a2011-02-22 07:16:58 +00001910
John McCall34695852011-02-22 06:44:22 +00001911 AutoVarEmission(const VarDecl &variable)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001912 : Variable(&variable), Address(nullptr), NRVOFlag(nullptr),
Nadav Rotem495cfa42013-03-23 06:43:35 +00001913 IsByRef(false), IsConstantAggregate(false),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001914 SizeForLifetimeMarkers(nullptr) {}
John McCall34695852011-02-22 06:44:22 +00001915
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001916 bool wasEmittedAsGlobal() const { return Address == nullptr; }
John McCall34695852011-02-22 06:44:22 +00001917
1918 public:
John McCall57b3b6a2011-02-22 07:16:58 +00001919 static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
1920
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001921 bool useLifetimeMarkers() const {
1922 return SizeForLifetimeMarkers != nullptr;
1923 }
Nadav Rotem495cfa42013-03-23 06:43:35 +00001924 llvm::Value *getSizeForLifetimeMarkers() const {
1925 assert(useLifetimeMarkers());
1926 return SizeForLifetimeMarkers;
1927 }
1928
1929 /// Returns the raw, allocated address, which is not necessarily
1930 /// the address of the object itself.
1931 llvm::Value *getAllocatedAddress() const {
1932 return Address;
1933 }
1934
John McCall34695852011-02-22 06:44:22 +00001935 /// Returns the address of the object within this declaration.
1936 /// Note that this does not chase the forwarding pointer for
1937 /// __block decls.
1938 llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
1939 if (!IsByRef) return Address;
1940
1941 return CGF.Builder.CreateStructGEP(Address,
John McCall57b3b6a2011-02-22 07:16:58 +00001942 CGF.getByRefValueLLVMField(Variable),
1943 Variable->getNameAsString());
John McCall34695852011-02-22 06:44:22 +00001944 }
1945 };
1946 AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
1947 void EmitAutoVarInit(const AutoVarEmission &emission);
1948 void EmitAutoVarCleanups(const AutoVarEmission &emission);
John McCallbdc4d802011-07-09 01:37:26 +00001949 void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
1950 QualType::DestructionKind dtorKind);
Daniel Dunbard286f052009-07-19 06:58:07 +00001951
John McCallb6bbcc92010-10-15 04:57:14 +00001952 void EmitStaticVarDecl(const VarDecl &D,
1953 llvm::GlobalValue::LinkageTypes Linkage);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001954
1955 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Stephen Hines651f13c2014-04-23 16:59:28 -07001956 void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, bool ArgIsPointer,
1957 unsigned ArgNo);
Mike Stump0dd9e882009-02-08 23:14:22 +00001958
John McCall56ca35d2011-02-17 10:25:35 +00001959 /// protectFromPeepholes - Protect a value that we're intending to
1960 /// store to the side, but which will probably be used later, from
1961 /// aggressive peepholing optimizations that might delete it.
1962 ///
1963 /// Pass the result to unprotectFromPeepholes to declare that
1964 /// protection is no longer required.
1965 ///
1966 /// There's no particular reason why this shouldn't apply to
1967 /// l-values, it's just that no existing peepholes work on pointers.
1968 PeepholeProtection protectFromPeepholes(RValue rvalue);
1969 void unprotectFromPeepholes(PeepholeProtection protection);
1970
Reid Spencer5f016e22007-07-11 17:01:13 +00001971 //===--------------------------------------------------------------------===//
1972 // Statement Emission
1973 //===--------------------------------------------------------------------===//
1974
Mike Stump0dd9e882009-02-08 23:14:22 +00001975 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
Daniel Dunbar09124252008-11-12 08:21:33 +00001976 void EmitStopPoint(const Stmt *S);
1977
Mike Stump0dd9e882009-02-08 23:14:22 +00001978 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
1979 /// this function even if there is no current insertion point.
1980 ///
1981 /// This function may clear the current insertion point; callers should use
1982 /// EnsureInsertPoint if they wish to subsequently generate code without first
1983 /// calling EmitBlock, EmitBranch, or EmitStmt.
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 void EmitStmt(const Stmt *S);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001985
Daniel Dunbar09124252008-11-12 08:21:33 +00001986 /// EmitSimpleStmt - Try to emit a "simple" statement which does not
Mike Stump0dd9e882009-02-08 23:14:22 +00001987 /// necessarily require an insertion point or debug information; typically
1988 /// because the statement amounts to a jump or a container of other
1989 /// statements.
Daniel Dunbar09124252008-11-12 08:21:33 +00001990 ///
1991 /// \return True if the statement was handled.
1992 bool EmitSimpleStmt(const Stmt *S);
1993
Eli Friedman2ac2fa72013-06-10 22:04:49 +00001994 llvm::Value *EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
1995 AggValueSlot AVS = AggValueSlot::ignored());
1996 llvm::Value *EmitCompoundStmtWithoutScope(const CompoundStmt &S,
1997 bool GetLast = false,
1998 AggValueSlot AVS =
1999 AggValueSlot::ignored());
Daniel Dunbara448fb22008-11-11 23:11:34 +00002000
Mike Stump0dd9e882009-02-08 23:14:22 +00002001 /// EmitLabel - Emit the block for the given label. It is legal to call this
2002 /// function even if there is no current insertion point.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002003 void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
Daniel Dunbara448fb22008-11-11 23:11:34 +00002004
Reid Spencer5f016e22007-07-11 17:01:13 +00002005 void EmitLabelStmt(const LabelStmt &S);
Richard Smith534986f2012-04-14 00:33:13 +00002006 void EmitAttributedStmt(const AttributedStmt &S);
Reid Spencer5f016e22007-07-11 17:01:13 +00002007 void EmitGotoStmt(const GotoStmt &S);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00002008 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
Reid Spencer5f016e22007-07-11 17:01:13 +00002009 void EmitIfStmt(const IfStmt &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002010
2011 void EmitCondBrHints(llvm::LLVMContext &Context, llvm::BranchInst *CondBr,
Stephen Hines176edba2014-12-01 14:53:08 -08002012 ArrayRef<const Attr *> Attrs);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002013 void EmitWhileStmt(const WhileStmt &S,
Stephen Hines176edba2014-12-01 14:53:08 -08002014 ArrayRef<const Attr *> Attrs = None);
2015 void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002016 void EmitForStmt(const ForStmt &S,
Stephen Hines176edba2014-12-01 14:53:08 -08002017 ArrayRef<const Attr *> Attrs = None);
Reid Spencer5f016e22007-07-11 17:01:13 +00002018 void EmitReturnStmt(const ReturnStmt &S);
2019 void EmitDeclStmt(const DeclStmt &S);
Daniel Dunbar09124252008-11-12 08:21:33 +00002020 void EmitBreakStmt(const BreakStmt &S);
2021 void EmitContinueStmt(const ContinueStmt &S);
Devang Patel51b09f22007-10-04 23:45:31 +00002022 void EmitSwitchStmt(const SwitchStmt &S);
2023 void EmitDefaultStmt(const DefaultStmt &S);
2024 void EmitCaseStmt(const CaseStmt &S);
Devang Patelc049e4f2007-10-08 20:57:48 +00002025 void EmitCaseStmtRange(const CaseStmt &S);
Chad Rosiera23b91d2012-08-28 18:54:39 +00002026 void EmitAsmStmt(const AsmStmt &S);
Mike Stump0dd9e882009-02-08 23:14:22 +00002027
Anders Carlsson3d8400d2008-08-30 19:51:14 +00002028 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002029 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
2030 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
Chris Lattner10cac6f2008-11-15 21:26:17 +00002031 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
John McCallf85e1932011-06-15 23:02:42 +00002032 void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
Mike Stump0dd9e882009-02-08 23:14:22 +00002033
John McCall59a70002010-07-07 06:56:46 +00002034 void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
2035 void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
John McCall9fc6a772010-02-19 09:25:03 +00002036
Anders Carlsson6815e942009-09-27 18:58:34 +00002037 void EmitCXXTryStmt(const CXXTryStmt &S);
Reid Kleckner98592d92013-09-16 21:46:30 +00002038 void EmitSEHTryStmt(const SEHTryStmt &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002039 void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002040 void EnterSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI);
2041 void ExitSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI);
2042
2043 llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
2044 const SEHExceptStmt &Except);
2045
2046 void EmitSEHExceptionCodeSave();
2047 llvm::Value *EmitSEHExceptionCode();
2048 llvm::Value *EmitSEHExceptionInfo();
2049 llvm::Value *EmitSEHAbnormalTermination();
2050
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002051 void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
Stephen Hines176edba2014-12-01 14:53:08 -08002052 ArrayRef<const Attr *> Attrs = None);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002053
Stephen Hines176edba2014-12-01 14:53:08 -08002054 LValue InitCapturedStruct(const CapturedStmt &S);
Ben Langmuir524387a2013-05-09 19:17:11 +00002055 llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
Stephen Hines176edba2014-12-01 14:53:08 -08002056 void GenerateCapturedStmtFunctionProlog(const CapturedStmt &S);
2057 llvm::Function *GenerateCapturedStmtFunctionEpilog(const CapturedStmt &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002058 llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002059 llvm::Value *GenerateCapturedStmtArgument(const CapturedStmt &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002060 void EmitOMPAggregateAssign(LValue OriginalAddr, llvm::Value *PrivateAddr,
2061 const Expr *AssignExpr, QualType Type,
2062 const VarDecl *VDInit);
2063 void EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
2064 OMPPrivateScope &PrivateScope);
2065 void EmitOMPPrivateClause(const OMPExecutableDirective &D,
2066 OMPPrivateScope &PrivateScope);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002067
2068 void EmitOMPParallelDirective(const OMPParallelDirective &S);
2069 void EmitOMPSimdDirective(const OMPSimdDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002070 void EmitOMPForDirective(const OMPForDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002071 void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002072 void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
2073 void EmitOMPSectionDirective(const OMPSectionDirective &S);
2074 void EmitOMPSingleDirective(const OMPSingleDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002075 void EmitOMPMasterDirective(const OMPMasterDirective &S);
2076 void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002077 void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002078 void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002079 void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002080 void EmitOMPTaskDirective(const OMPTaskDirective &S);
2081 void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
2082 void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
2083 void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
2084 void EmitOMPFlushDirective(const OMPFlushDirective &S);
2085 void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
2086 void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
2087 void EmitOMPTargetDirective(const OMPTargetDirective &S);
2088 void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
2089
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002090private:
2091
2092 /// Helpers for the OpenMP loop directives.
Stephen Hines176edba2014-12-01 14:53:08 -08002093 void EmitOMPLoopBody(const OMPLoopDirective &Directive,
2094 bool SeparateIter = false);
2095 void EmitOMPInnerLoop(const OMPLoopDirective &S, OMPPrivateScope &LoopScope,
2096 bool SeparateIter = false);
2097 void EmitOMPSimdFinal(const OMPLoopDirective &S);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002098 void EmitOMPWorksharingLoop(const OMPLoopDirective &S);
2099 void EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind,
2100 const OMPLoopDirective &S,
2101 OMPPrivateScope &LoopScope, llvm::Value *LB,
2102 llvm::Value *UB, llvm::Value *ST, llvm::Value *IL,
2103 llvm::Value *Chunk);
2104
2105public:
Ben Langmuir524387a2013-05-09 19:17:11 +00002106
Reid Spencer5f016e22007-07-11 17:01:13 +00002107 //===--------------------------------------------------------------------===//
2108 // LValue Expression Emission
2109 //===--------------------------------------------------------------------===//
2110
Daniel Dunbar13e81732009-02-05 07:09:07 +00002111 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
2112 RValue GetUndefRValue(QualType Ty);
2113
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00002114 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
2115 /// and issue an ErrorUnsupported style diagnostic (using the
2116 /// provided Name).
2117 RValue EmitUnsupportedRValue(const Expr *E,
2118 const char *Name);
2119
Mike Stump0dd9e882009-02-08 23:14:22 +00002120 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
2121 /// an ErrorUnsupported style diagnostic (using the provided Name).
Daniel Dunbar6ba82a42008-08-25 20:45:57 +00002122 LValue EmitUnsupportedLValue(const Expr *E,
2123 const char *Name);
2124
Reid Spencer5f016e22007-07-11 17:01:13 +00002125 /// EmitLValue - Emit code to compute a designator that specifies the location
2126 /// of the expression.
2127 ///
2128 /// This can return one of two things: a simple address or a bitfield
2129 /// reference. In either case, the LLVM Value* in the LValue structure is
2130 /// guaranteed to be an LLVM pointer type.
2131 ///
2132 /// If this returns a bitfield reference, nothing about the pointee type of
2133 /// the LLVM value is known: For example, it may not be a pointer to an
2134 /// integer.
2135 ///
2136 /// If this returns a normal address, and if the lvalue's C type is fixed
2137 /// size, this method guarantees that the returned pointer type will point to
2138 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
2139 /// variable length type, this is not possible.
2140 ///
2141 LValue EmitLValue(const Expr *E);
Mike Stump0dd9e882009-02-08 23:14:22 +00002142
Richard Smith7ac9ef12012-09-08 02:08:36 +00002143 /// \brief Same as EmitLValue but additionally we generate checking code to
2144 /// guard against undefined behavior. This is only suitable when we know
2145 /// that the address will be used to access the object.
2146 LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
Mike Stumpb14e62d2009-12-16 02:57:00 +00002147
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002148 RValue convertTempToRValue(llvm::Value *addr, QualType type,
2149 SourceLocation Loc);
John McCall9d232c82013-03-07 21:37:08 +00002150
John McCall9eda3ab2013-03-07 21:37:17 +00002151 void EmitAtomicInit(Expr *E, LValue lvalue);
2152
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002153 bool LValueIsSuitableForInlineAtomic(LValue Src);
2154 bool typeIsSuitableForInlineAtomic(QualType Ty, bool IsVolatile) const;
2155
2156 RValue EmitAtomicLoad(LValue LV, SourceLocation SL,
2157 AggValueSlot Slot = AggValueSlot::ignored());
2158
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002159 RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002160 llvm::AtomicOrdering AO, bool IsVolatile = false,
John McCall9eda3ab2013-03-07 21:37:17 +00002161 AggValueSlot slot = AggValueSlot::ignored());
2162
2163 void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
2164
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002165 void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO,
2166 bool IsVolatile, bool isInit);
2167
2168 std::pair<RValue, RValue> EmitAtomicCompareExchange(
2169 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
2170 llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
2171 llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
2172 bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored());
2173
John McCall26815d92010-10-27 20:58:56 +00002174 /// EmitToMemory - Change a scalar value from its value
2175 /// representation to its in-memory representation.
2176 llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
2177
2178 /// EmitFromMemory - Change a scalar value from its memory
2179 /// representation to its value representation.
2180 llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
2181
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00002182 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2183 /// care to appropriately convert from the memory representation to
2184 /// the LLVM value representation.
Mike Stump09429b92009-02-17 17:00:02 +00002185 llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
Dan Gohman3d5aff52010-10-14 23:06:10 +00002186 unsigned Alignment, QualType Ty,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002187 SourceLocation Loc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002188 llvm::MDNode *TBAAInfo = nullptr,
Manman Renb37a73d2013-04-04 21:53:22 +00002189 QualType TBAABaseTy = QualType(),
2190 uint64_t TBAAOffset = 0);
John McCall545d9962011-06-25 02:11:03 +00002191
2192 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2193 /// care to appropriately convert from the memory representation to
2194 /// the LLVM value representation. The l-value must be a simple
2195 /// l-value.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002196 llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00002197
2198 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2199 /// care to appropriately convert from the memory representation to
2200 /// the LLVM value representation.
Mike Stump09429b92009-02-17 17:00:02 +00002201 void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Dan Gohman3d5aff52010-10-14 23:06:10 +00002202 bool Volatile, unsigned Alignment, QualType Ty,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002203 llvm::MDNode *TBAAInfo = nullptr, bool isInit = false,
Manman Renb37a73d2013-04-04 21:53:22 +00002204 QualType TBAABaseTy = QualType(),
2205 uint64_t TBAAOffset = 0);
John McCall545d9962011-06-25 02:11:03 +00002206
2207 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2208 /// care to appropriately convert from the memory representation to
2209 /// the LLVM value representation. The l-value must be a simple
David Chisnall7a7ee302012-01-16 17:27:18 +00002210 /// l-value. The isInit flag indicates whether this is an initialization.
2211 /// If so, atomic qualifiers are ignored and the store is always non-atomic.
2212 void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00002213
Reid Spencer5f016e22007-07-11 17:01:13 +00002214 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
2215 /// this method emits the address of the lvalue, then loads the result as an
2216 /// rvalue, returning the rvalue.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002217 RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
John McCall545d9962011-06-25 02:11:03 +00002218 RValue EmitLoadOfExtVectorElementLValue(LValue V);
2219 RValue EmitLoadOfBitfieldLValue(LValue LV);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002220 RValue EmitLoadOfGlobalRegLValue(LValue LV);
Mike Stump0dd9e882009-02-08 23:14:22 +00002221
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
2223 /// lvalue, where both are guaranteed to the have the same type, and that type
2224 /// is 'Ty'.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002225 void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false);
John McCall545d9962011-06-25 02:11:03 +00002226 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002227 void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
Daniel Dunbared3849b2008-11-19 09:36:46 +00002228
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002229 /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
2230 /// as EmitStoreThroughLValue.
Daniel Dunbared3849b2008-11-19 09:36:46 +00002231 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00002232 /// \param Result [out] - If non-null, this will be set to a Value* for the
2233 /// bit-field contents after the store, appropriate for use as the result of
2234 /// an assignment to the bit-field.
John McCall545d9962011-06-25 02:11:03 +00002235 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002236 llvm::Value **Result=nullptr);
Mike Stump0dd9e882009-02-08 23:14:22 +00002237
John McCall83ce9d42010-11-16 23:07:28 +00002238 /// Emit an l-value for an assignment (simple or compound) of complex type.
2239 LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
John McCall2a416372010-12-05 02:00:02 +00002240 LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002241 LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
2242 llvm::Value *&Result);
John McCall83ce9d42010-11-16 23:07:28 +00002243
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002244 // Note: only available for agg return types
Daniel Dunbar80e62c22008-09-04 03:20:13 +00002245 LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
John McCall2a416372010-12-05 02:00:02 +00002246 LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00002247 // Note: only available for agg return types
Christopher Lamb22c940e2007-12-29 05:02:41 +00002248 LValue EmitCallExprLValue(const CallExpr *E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00002249 // Note: only available for agg return types
2250 LValue EmitVAArgExprLValue(const VAArgExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00002251 LValue EmitDeclRefLValue(const DeclRefExpr *E);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002252 LValue EmitReadRegister(const VarDecl *VD);
Reid Spencer5f016e22007-07-11 17:01:13 +00002253 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002254 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
Chris Lattnerd9f69102008-08-10 01:53:14 +00002255 LValue EmitPredefinedLValue(const PredefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00002256 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Richard Smitha0a628f2013-02-23 02:53:19 +00002257 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2258 bool Accessed = false);
Nate Begeman213541a2008-04-18 23:10:10 +00002259 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
Devang Patelb84a06e2007-10-23 02:10:49 +00002260 LValue EmitMemberExpr(const MemberExpr *E);
Fariborz Jahanian820bca42009-12-09 23:35:29 +00002261 LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
Eli Friedman06e863f2008-05-13 23:18:27 +00002262 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
Richard Smith13ec9102012-05-14 21:57:21 +00002263 LValue EmitInitListLValue(const InitListExpr *E);
John McCall56ca35d2011-02-17 10:25:35 +00002264 LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
Chris Lattner75dfeda2009-03-18 18:28:57 +00002265 LValue EmitCastLValue(const CastExpr *E);
Douglas Gregor03e80032011-06-21 17:03:29 +00002266 LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
John McCalle996ffd2011-02-16 08:02:54 +00002267 LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
Stephen Hines176edba2014-12-01 14:53:08 -08002268
2269 llvm::Value *EmitExtVectorElementLValue(LValue V);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002270
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002271 RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002272
John McCalldd2ecee2012-03-10 03:05:10 +00002273 class ConstantEmission {
2274 llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
2275 ConstantEmission(llvm::Constant *C, bool isReference)
2276 : ValueAndIsReference(C, isReference) {}
2277 public:
2278 ConstantEmission() {}
2279 static ConstantEmission forReference(llvm::Constant *C) {
2280 return ConstantEmission(C, true);
2281 }
2282 static ConstantEmission forValue(llvm::Constant *C) {
2283 return ConstantEmission(C, false);
2284 }
2285
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002286 explicit operator bool() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002287 return ValueAndIsReference.getOpaqueValue() != nullptr;
2288 }
John McCalldd2ecee2012-03-10 03:05:10 +00002289
2290 bool isReference() const { return ValueAndIsReference.getInt(); }
2291 LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
2292 assert(isReference());
2293 return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
2294 refExpr->getType());
2295 }
2296
2297 llvm::Constant *getValue() const {
2298 assert(!isReference());
2299 return ValueAndIsReference.getPointer();
2300 }
2301 };
2302
John McCallf4b88a42012-03-10 09:33:50 +00002303 ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
John McCalldd2ecee2012-03-10 03:05:10 +00002304
John McCall4b9c2d22011-11-06 09:01:30 +00002305 RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
2306 AggValueSlot slot = AggValueSlot::ignored());
2307 LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
2308
Daniel Dunbar2a031922009-04-22 05:08:15 +00002309 llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002310 const ObjCIvarDecl *Ivar);
Eli Friedman377ecc72012-04-16 03:54:45 +00002311 LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
John McCallf5ebf9b2013-05-03 07:33:41 +00002312 LValue EmitLValueForLambdaField(const FieldDecl *Field);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002313
Anders Carlsson06a29702010-01-29 05:24:29 +00002314 /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
2315 /// if the Field is a reference, this will return the address of the reference
2316 /// and not the address of the value stored in the reference.
Eli Friedman377ecc72012-04-16 03:54:45 +00002317 LValue EmitLValueForFieldInitialization(LValue Base,
2318 const FieldDecl* Field);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002319
Fariborz Jahanian45012a72009-02-03 00:09:52 +00002320 LValue EmitLValueForIvar(QualType ObjectTy,
2321 llvm::Value* Base, const ObjCIvarDecl *Ivar,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002322 unsigned CVRQualifiers);
2323
Anders Carlssonb58d0172009-05-30 23:23:33 +00002324 LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
Anders Carlssone61c9e82009-05-30 23:30:54 +00002325 LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
Eli Friedman31a37022012-02-08 05:34:55 +00002326 LValue EmitLambdaLValue(const LambdaExpr *E);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00002327 LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
Nico Weberc5f80462012-10-11 10:13:44 +00002328 LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002329
Daniel Dunbar0a04d772008-08-23 10:51:21 +00002330 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
Chris Lattner391d77a2008-03-30 23:03:07 +00002331 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
Chris Lattner65459942009-04-25 19:35:26 +00002332 LValue EmitStmtExprLValue(const StmtExpr *E);
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00002333 LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
Fariborz Jahanian03b29602010-06-17 19:56:20 +00002334 LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
Yunzhong Gao3b8e0b72013-08-30 08:53:09 +00002335 void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
John McCall56ca35d2011-02-17 10:25:35 +00002336
Reid Spencer5f016e22007-07-11 17:01:13 +00002337 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +00002338 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +00002339 //===--------------------------------------------------------------------===//
2340
Mike Stump0dd9e882009-02-08 23:14:22 +00002341 /// EmitCall - Generate a call of the given function, expecting the given
2342 /// result type, and using the given argument list which specifies both the
2343 /// LLVM arguments and the types they were derived from.
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002344 ///
Mike Stumpf71d2322009-11-30 20:08:49 +00002345 /// \param TargetDecl - If given, the decl of the function in a direct call;
2346 /// used to set attributes on the call (noreturn, etc.).
Daniel Dunbar88b53962009-02-02 22:03:45 +00002347 RValue EmitCall(const CGFunctionInfo &FnInfo,
2348 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00002349 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002350 const CallArgList &Args,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002351 const Decl *TargetDecl = nullptr,
2352 llvm::Instruction **callOrInvoke = nullptr);
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Stephen Hines176edba2014-12-01 14:53:08 -08002354 RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E,
Anders Carlssond2490a92009-12-24 20:40:36 +00002355 ReturnValueSlot ReturnValue,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002356 const Decl *TargetDecl = nullptr,
2357 llvm::Value *Chain = nullptr);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002358 RValue EmitCallExpr(const CallExpr *E,
Anders Carlssond2490a92009-12-24 20:40:36 +00002359 ReturnValueSlot ReturnValue = ReturnValueSlot());
Mike Stump1eb44332009-09-09 15:08:12 +00002360
John McCallbd7370a2013-02-28 19:01:20 +00002361 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2362 const Twine &name = "");
2363 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2364 ArrayRef<llvm::Value*> args,
2365 const Twine &name = "");
2366 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2367 const Twine &name = "");
2368 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2369 ArrayRef<llvm::Value*> args,
2370 const Twine &name = "");
2371
John McCallf1549f62010-07-06 01:34:17 +00002372 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +00002373 ArrayRef<llvm::Value *> Args,
Chris Lattner8cc488f2011-07-20 07:06:53 +00002374 const Twine &Name = "");
Jay Foad4c7d9f12011-07-15 08:37:34 +00002375 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
Chris Lattner8cc488f2011-07-20 07:06:53 +00002376 const Twine &Name = "");
John McCallbd7370a2013-02-28 19:01:20 +00002377 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2378 ArrayRef<llvm::Value*> args,
2379 const Twine &name = "");
2380 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2381 const Twine &name = "");
2382 void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2383 ArrayRef<llvm::Value*> args);
John McCallf1549f62010-07-06 01:34:17 +00002384
Fariborz Jahanian27262672011-01-20 17:19:02 +00002385 llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
2386 NestedNameSpecifier *Qual,
Chris Lattner2acc6e32011-07-18 04:24:23 +00002387 llvm::Type *Ty);
Fariborz Jahanianccd52592011-02-01 23:22:34 +00002388
2389 llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2390 CXXDtorType Type,
Fariborz Jahanian771c6782011-02-03 19:27:17 +00002391 const CXXRecordDecl *RD);
Anders Carlsson566abee2009-11-13 04:45:41 +00002392
Stephen Hines176edba2014-12-01 14:53:08 -08002393 RValue
2394 EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2395 ReturnValueSlot ReturnValue, llvm::Value *This,
2396 llvm::Value *ImplicitParam,
2397 QualType ImplicitParamTy, const CallExpr *E);
2398 RValue EmitCXXStructorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2399 ReturnValueSlot ReturnValue, llvm::Value *This,
2400 llvm::Value *ImplicitParam,
2401 QualType ImplicitParamTy, const CallExpr *E,
2402 StructorType Type);
Anders Carlssona1736c02009-12-24 21:13:40 +00002403 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2404 ReturnValueSlot ReturnValue);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002405 RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE,
2406 const CXXMethodDecl *MD,
2407 ReturnValueSlot ReturnValue,
2408 bool HasQualifier,
2409 NestedNameSpecifier *Qualifier,
2410 bool IsArrow, const Expr *Base);
2411 // Compute the object pointer.
Anders Carlssona1736c02009-12-24 21:13:40 +00002412 RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2413 ReturnValueSlot ReturnValue);
Ted Kremenek55499762008-06-17 02:43:46 +00002414
Anders Carlsson0f294632009-05-27 04:18:27 +00002415 RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
Anders Carlssona1736c02009-12-24 21:13:40 +00002416 const CXXMethodDecl *MD,
2417 ReturnValueSlot ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00002418
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +00002419 RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
2420 ReturnValueSlot ReturnValue);
2421
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002422
Mike Stump1eb44332009-09-09 15:08:12 +00002423 RValue EmitBuiltinExpr(const FunctionDecl *FD,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002424 unsigned BuiltinID, const CallExpr *E,
2425 ReturnValueSlot ReturnValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00002426
Anders Carlssona1736c02009-12-24 21:13:40 +00002427 RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
Mike Stump09429b92009-02-17 17:00:02 +00002428
Mike Stump0dd9e882009-02-08 23:14:22 +00002429 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
2430 /// is unhandled by the current target.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00002431 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2432
Kevin Qin8137a602013-11-14 02:45:18 +00002433 llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
2434 const llvm::CmpInst::Predicate Fp,
2435 const llvm::CmpInst::Predicate Ip,
2436 const llvm::Twine &Name = "");
Chris Lattner2752c012010-03-03 19:03:45 +00002437 llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Stephen Hines651f13c2014-04-23 16:59:28 -07002438
2439 llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
2440 unsigned LLVMIntrinsic,
2441 unsigned AltLLVMIntrinsic,
2442 const char *NameHint,
2443 unsigned Modifier,
2444 const CallExpr *E,
2445 SmallVectorImpl<llvm::Value *> &Ops,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002446 llvm::Value *Align = nullptr);
Stephen Hines651f13c2014-04-23 16:59:28 -07002447 llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2448 unsigned Modifier, llvm::Type *ArgTy,
2449 const CallExpr *E);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002450 llvm::Value *EmitNeonCall(llvm::Function *F,
Chris Lattner686775d2011-07-20 06:58:45 +00002451 SmallVectorImpl<llvm::Value*> &O,
Bob Wilsondb3d4d02010-12-08 22:37:56 +00002452 const char *name,
Nate Begeman61eecf52010-06-14 05:21:25 +00002453 unsigned shift = 0, bool rightshift = false);
Bob Wilsoncf556522010-12-07 22:40:02 +00002454 llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002455 llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
Nate Begeman61eecf52010-06-14 05:21:25 +00002456 bool negateForRightShift);
Amaury de la Vieuville7f0ff702013-10-04 13:13:15 +00002457 llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
2458 llvm::Type *Ty, bool usgn, const char *name);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002459 // Helper functions for EmitAArch64BuiltinExpr.
Stephen Hines651f13c2014-04-23 16:59:28 -07002460 llvm::Value *vectorWrapScalar8(llvm::Value *Op);
2461 llvm::Value *vectorWrapScalar16(llvm::Value *Op);
2462 llvm::Value *emitVectorWrappedScalar8Intrinsic(
2463 unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
2464 llvm::Value *emitVectorWrappedScalar16Intrinsic(
2465 unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002466 llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Stephen Hines651f13c2014-04-23 16:59:28 -07002467 llvm::Value *EmitNeon64Call(llvm::Function *F,
2468 llvm::SmallVectorImpl<llvm::Value *> &O,
2469 const char *name);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002470
Bill Wendling795b1002012-02-22 09:30:11 +00002471 llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
Anders Carlsson564f1de2007-12-09 23:17:02 +00002472 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2473 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002474 llvm::Value *EmitR600BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Mike Stump0dd9e882009-02-08 23:14:22 +00002475
Daniel Dunbared7c6182008-08-20 00:28:19 +00002476 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
Chris Lattner7f02f722007-08-24 05:35:26 +00002477 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
Patrick Beardeb382ec2012-04-19 00:25:12 +00002478 llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002479 llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
2480 llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2481 llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
2482 const ObjCMethodDecl *MethodWithObjects);
Chris Lattner8fdf3282008-06-24 17:04:18 +00002483 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
John McCallef072fd2010-05-22 01:48:05 +00002484 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2485 ReturnValueSlot Return = ReturnValueSlot());
Chris Lattner8fdf3282008-06-24 17:04:18 +00002486
John McCallf85e1932011-06-15 23:02:42 +00002487 /// Retrieves the default cleanup kind for an ARC cleanup.
2488 /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2489 CleanupKind getARCCleanupKind() {
2490 return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2491 ? NormalAndEHCleanup : NormalCleanup;
2492 }
2493
2494 // ARC primitives.
2495 void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
2496 void EmitARCDestroyWeak(llvm::Value *addr);
2497 llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
2498 llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
2499 llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
2500 bool ignored);
2501 void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
2502 void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
2503 llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2504 llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
John McCall545d9962011-06-25 02:11:03 +00002505 llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
John McCall5b07e802013-03-13 03:10:54 +00002506 bool resultIgnored);
John McCallf85e1932011-06-15 23:02:42 +00002507 llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
John McCall5b07e802013-03-13 03:10:54 +00002508 bool resultIgnored);
John McCallf85e1932011-06-15 23:02:42 +00002509 llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2510 llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
John McCall348f16f2011-10-04 06:23:45 +00002511 llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
John McCall5b07e802013-03-13 03:10:54 +00002512 void EmitARCDestroyStrong(llvm::Value *addr, ARCPreciseLifetime_t precise);
2513 void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
John McCallf85e1932011-06-15 23:02:42 +00002514 llvm::Value *EmitARCAutorelease(llvm::Value *value);
2515 llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2516 llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2517 llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2518
2519 std::pair<LValue,llvm::Value*>
2520 EmitARCStoreAutoreleasing(const BinaryOperator *e);
2521 std::pair<LValue,llvm::Value*>
2522 EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2523
John McCall2b014d62011-10-01 10:32:24 +00002524 llvm::Value *EmitObjCThrowOperand(const Expr *expr);
2525
John McCallf85e1932011-06-15 23:02:42 +00002526 llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2527 llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2528 llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2529
John McCall348f16f2011-10-04 06:23:45 +00002530 llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
John McCallf85e1932011-06-15 23:02:42 +00002531 llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2532 llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2533
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002534 void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
John McCallb6a60792013-03-23 02:35:54 +00002535
John McCallbdc4d802011-07-09 01:37:26 +00002536 static Destroyer destroyARCStrongImprecise;
2537 static Destroyer destroyARCStrongPrecise;
2538 static Destroyer destroyARCWeak;
2539
John McCallf85e1932011-06-15 23:02:42 +00002540 void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2541 llvm::Value *EmitObjCAutoreleasePoolPush();
2542 llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2543 void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2544 void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2545
Richard Smithd4ec5622013-06-12 23:38:09 +00002546 /// \brief Emits a reference binding to the passed in expression.
2547 RValue EmitReferenceBindingToExpr(const Expr *E);
Anders Carlsson3aba0932010-01-31 18:34:51 +00002548
Chris Lattner883f6a72007-08-11 00:04:45 +00002549 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +00002550 // Expression Emission
Chris Lattner883f6a72007-08-11 00:04:45 +00002551 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +00002552
2553 // Expressions are broken into three classes: scalar, complex, aggregate.
Mike Stump0dd9e882009-02-08 23:14:22 +00002554
2555 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
2556 /// scalar type, returning the result.
Anders Carlsson14c5cbf2009-08-16 07:36:22 +00002557 llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002558
Chris Lattner3707b252007-08-26 06:48:56 +00002559 /// EmitScalarConversion - Emit a conversion from the specified type to the
2560 /// specified destination type, both of which are LLVM scalar types.
2561 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
2562 QualType DstTy);
Mike Stump0dd9e882009-02-08 23:14:22 +00002563
Chris Lattner4f1a7b32007-08-26 16:34:22 +00002564 /// EmitComplexToScalarConversion - Emit a conversion from the specified
Mike Stump0dd9e882009-02-08 23:14:22 +00002565 /// complex type to the specified destination type, where the destination type
2566 /// is an LLVM scalar type.
Chris Lattner4f1a7b32007-08-26 16:34:22 +00002567 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
2568 QualType DstTy);
Mike Stump0dd9e882009-02-08 23:14:22 +00002569
2570
John McCall558d2ab2010-09-15 10:14:12 +00002571 /// EmitAggExpr - Emit the computation of the specified expression
2572 /// of aggregate type. The result is computed into the given slot,
2573 /// which may be null to indicate that the value is not needed.
John McCalle0c11682012-07-02 23:58:38 +00002574 void EmitAggExpr(const Expr *E, AggValueSlot AS);
Mike Stump0dd9e882009-02-08 23:14:22 +00002575
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00002576 /// EmitAggExprToLValue - Emit the computation of the specified expression of
2577 /// aggregate type into a temporary LValue.
2578 LValue EmitAggExprToLValue(const Expr *E);
2579
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002580 /// EmitGCMemmoveCollectable - Emit special API for structs with object
2581 /// pointers.
2582 void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
Fariborz Jahanian08c32132009-08-31 19:33:16 +00002583 QualType Ty);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002584
John McCall0c24c802011-06-24 23:21:27 +00002585 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
2586 /// make sure it survives garbage collection until this point.
2587 void EmitExtendGCLifetime(llvm::Value *object);
2588
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00002589 /// EmitComplexExpr - Emit the computation of the specified expression of
Chris Lattner23b1cdb2007-08-23 23:43:33 +00002590 /// complex type, returning the result.
John McCallb418d742010-11-16 10:08:07 +00002591 ComplexPairTy EmitComplexExpr(const Expr *E,
2592 bool IgnoreReal = false,
2593 bool IgnoreImag = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002594
John McCall9d232c82013-03-07 21:37:08 +00002595 /// EmitComplexExprIntoLValue - Emit the given expression of complex
2596 /// type and place its result into the specified l-value.
2597 void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00002598
John McCall9d232c82013-03-07 21:37:08 +00002599 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
2600 void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
2601
2602 /// EmitLoadOfComplex - Load a complex number from the specified l-value.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002603 ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
Chris Lattner2621fd12008-05-08 05:58:21 +00002604
Chandler Carruth0f30a122012-03-30 19:44:53 +00002605 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
2606 /// global variable that has already been created for it. If the initializer
2607 /// has a different type than GV does, this may free GV and return a different
2608 /// one. Otherwise it just returns GV.
2609 llvm::GlobalVariable *
2610 AddInitializerToStaticVarDecl(const VarDecl &D,
2611 llvm::GlobalVariable *GV);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002612
Daniel Dunbar0096acf2009-02-25 19:24:29 +00002613
Anders Carlsson3b2e16b2009-08-08 21:45:14 +00002614 /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
2615 /// variable with global storage.
Richard Smith7ca48502012-02-13 22:16:19 +00002616 void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr,
2617 bool PerformInit);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +00002618
Stephen Hines176edba2014-12-01 14:53:08 -08002619 llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor,
2620 llvm::Constant *Addr);
2621
John McCall20bb1752012-05-01 06:13:13 +00002622 /// Call atexit() with a function that passes the given argument to
2623 /// the given function.
David Blaikiec7971a92013-08-27 23:57:18 +00002624 void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn,
2625 llvm::Constant *addr);
Mike Stump1eb44332009-09-09 15:08:12 +00002626
John McCall3030eb82010-11-06 09:44:32 +00002627 /// Emit code in this function to perform a guarded variable
2628 /// initialization. Guarded initializations are used when it's not
2629 /// possible to prove that an initialization will be done exactly
2630 /// once, e.g. with a static local variable or a static data member
2631 /// of a class template.
Chandler Carruth0f30a122012-03-30 19:44:53 +00002632 void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
Richard Smith7ca48502012-02-13 22:16:19 +00002633 bool PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +00002634
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002635 /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2636 /// variables.
2637 void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
Stephen Hines176edba2014-12-01 14:53:08 -08002638 ArrayRef<llvm::Function *> CXXThreadLocals,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002639 llvm::GlobalVariable *Guard = nullptr);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002640
John McCall3f88f682012-04-06 18:21:03 +00002641 /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002642 /// variables.
John McCall3f88f682012-04-06 18:21:03 +00002643 void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
2644 const std::vector<std::pair<llvm::WeakVH,
2645 llvm::Constant*> > &DtorsAndObjects);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002646
John McCalld26bc762011-03-09 04:27:21 +00002647 void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2648 const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +00002649 llvm::GlobalVariable *Addr,
2650 bool PerformInit);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002651
John McCall558d2ab2010-09-15 10:14:12 +00002652 void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
Fariborz Jahanian34999872010-11-13 21:53:34 +00002653
2654 void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
Fariborz Jahanian830937b2010-12-02 17:02:11 +00002655 const Expr *Exp);
Mike Stump1eb44332009-09-09 15:08:12 +00002656
John McCall1a343eb2011-11-10 08:15:53 +00002657 void enterFullExpression(const ExprWithCleanups *E) {
2658 if (E->getNumObjects() == 0) return;
2659 enterNonTrivialFullExpression(E);
2660 }
2661 void enterNonTrivialFullExpression(const ExprWithCleanups *E);
Mike Stump1eb44332009-09-09 15:08:12 +00002662
Richard Smith4c71b8c2013-05-07 21:53:22 +00002663 void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
Douglas Gregor1eb2e592010-05-16 00:44:00 +00002664
Eli Friedman4c5d8af2012-02-09 03:32:31 +00002665 void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
2666
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002667 RValue EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest = nullptr);
Eli Friedman276b0612011-10-11 02:20:01 +00002668
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00002669 //===--------------------------------------------------------------------===//
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002670 // Annotations Emission
2671 //===--------------------------------------------------------------------===//
2672
2673 /// Emit an annotation call (intrinsic or builtin).
2674 llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn,
2675 llvm::Value *AnnotatedVal,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002676 StringRef AnnotationStr,
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002677 SourceLocation Location);
2678
2679 /// Emit local annotations for the local variable V, declared by D.
2680 void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
2681
2682 /// Emit field annotations for the given field & value. Returns the
2683 /// annotation result.
2684 llvm::Value *EmitFieldAnnotations(const FieldDecl *D, llvm::Value *V);
2685
2686 //===--------------------------------------------------------------------===//
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00002687 // Internal Helpers
2688 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00002689
Chris Lattner0946ccd2008-11-11 07:41:27 +00002690 /// ContainsLabel - Return true if the statement contains a label in it. If
2691 /// this statement is not executed normally, it not containing a label means
2692 /// that we can just remove the code.
2693 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002694
Chris Lattneref425a62011-02-28 00:18:40 +00002695 /// containsBreak - Return true if the statement contains a break out of it.
2696 /// If the statement (recursively) contains a switch or loop with a break
2697 /// inside of it, this is fine.
2698 static bool containsBreak(const Stmt *S);
2699
Daniel Dunbar4bc04552008-11-12 10:12:14 +00002700 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
Chris Lattnerc2c90012011-02-27 23:02:32 +00002701 /// to a constant, or if it does but contains a label, return false. If it
2702 /// constant folds return true and set the boolean result in Result.
2703 bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
Mike Stump0dd9e882009-02-08 23:14:22 +00002704
Chris Lattneref425a62011-02-28 00:18:40 +00002705 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2706 /// to a constant, or if it does but contains a label, return false. If it
2707 /// constant folds return true and set the folded value.
Richard Trieue1ecdc12012-07-23 20:21:35 +00002708 bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result);
Chris Lattneref425a62011-02-28 00:18:40 +00002709
Chris Lattner31a09842008-11-12 08:04:58 +00002710 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
2711 /// if statement) to the specified blocks. Based on the condition, this might
2712 /// try to simplify the codegen of the conditional based on the branch.
Stephen Hines651f13c2014-04-23 16:59:28 -07002713 /// TrueCount should be the number of times we expect the condition to
2714 /// evaluate to true based on PGO data.
Chris Lattner9bc47e22008-11-12 07:46:33 +00002715 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
Stephen Hines651f13c2014-04-23 16:59:28 -07002716 llvm::BasicBlock *FalseBlock, uint64_t TrueCount);
Mike Stumpbe07f602009-12-14 21:58:14 +00002717
Richard Smith4def70d2012-10-09 19:52:38 +00002718 /// \brief Emit a description of a type in a format suitable for passing to
2719 /// a runtime sanitizer handler.
2720 llvm::Constant *EmitCheckTypeDescriptor(QualType T);
2721
2722 /// \brief Convert a value into a format suitable for passing to a runtime
2723 /// sanitizer handler.
2724 llvm::Value *EmitCheckValue(llvm::Value *V);
2725
2726 /// \brief Emit a description of a source location in a format suitable for
2727 /// passing to a runtime sanitizer handler.
2728 llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
2729
Richard Smithcc305612012-11-01 22:15:34 +00002730 /// \brief Create a basic block that will call a handler function in a
2731 /// sanitizer runtime with the provided arguments, and create a conditional
2732 /// branch to it.
Stephen Hines176edba2014-12-01 14:53:08 -08002733 void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerKind>> Checked,
2734 StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
2735 ArrayRef<llvm::Value *> DynamicArgs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002736
Richard Smithcc305612012-11-01 22:15:34 +00002737 /// \brief Create a basic block that will call the trap intrinsic, and emit a
2738 /// conditional branch to it, for the -ftrapv checks.
Chad Rosier78d85b12013-01-29 23:31:22 +00002739 void EmitTrapCheck(llvm::Value *Checked);
Richard Smithcc305612012-11-01 22:15:34 +00002740
Anders Carlsson21c9ad92010-03-30 03:27:09 +00002741 /// EmitCallArg - Emit a single call argument.
John McCall413ebdb2011-03-11 20:59:21 +00002742 void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
Anders Carlsson21c9ad92010-03-30 03:27:09 +00002743
John McCall27360712010-05-26 22:34:26 +00002744 /// EmitDelegateCallArg - We are performing a delegate call; that
2745 /// is, the current function is delegating to another one. Produce
2746 /// a r-value suitable for passing the given parameter.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002747 void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
2748 SourceLocation loc);
John McCall27360712010-05-26 22:34:26 +00002749
Peter Collingbournec5096cb2011-10-27 19:19:51 +00002750 /// SetFPAccuracy - Set the minimum required accuracy of the given floating
2751 /// point operation, expressed as the maximum relative error in ulp.
Duncan Sands82500162012-04-10 08:23:07 +00002752 void SetFPAccuracy(llvm::Value *Val, float Accuracy);
Peter Collingbournec5096cb2011-10-27 19:19:51 +00002753
Chris Lattner31a09842008-11-12 08:04:58 +00002754private:
Rafael Espindolac3f89552012-03-24 16:50:34 +00002755 llvm::MDNode *getRangeForLoadFromType(QualType Ty);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002756 void EmitReturnOfRValue(RValue RV, QualType Ty);
2757
Stephen Hines651f13c2014-04-23 16:59:28 -07002758 void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
2759
2760 llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4>
2761 DeferredReplacements;
2762
Daniel Dunbar56273772008-09-17 00:51:38 +00002763 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
2764 /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
2765 ///
2766 /// \param AI - The first function argument of the expansion.
Stephen Hines176edba2014-12-01 14:53:08 -08002767 void ExpandTypeFromArgs(QualType Ty, LValue Dst,
2768 SmallVectorImpl<llvm::Argument *>::iterator &AI);
Daniel Dunbar56273772008-09-17 00:51:38 +00002769
Stephen Hines176edba2014-12-01 14:53:08 -08002770 /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg
2771 /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
2772 /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
2773 void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
2774 SmallVectorImpl<llvm::Value *> &IRCallArgs,
2775 unsigned &IRCallArgPos);
Anders Carlssonc8c7b182009-01-11 19:40:10 +00002776
Chad Rosier42b60552012-08-23 20:00:18 +00002777 llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
Anders Carlssonc8c7b182009-01-11 19:40:10 +00002778 const Expr *InputExpr, std::string &ConstraintStr);
Mike Stump0dd9e882009-02-08 23:14:22 +00002779
Chad Rosier42b60552012-08-23 20:00:18 +00002780 llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
Eli Friedman6d7cfd72010-07-16 00:55:21 +00002781 LValue InputValue, QualType InputType,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002782 std::string &ConstraintStr,
2783 SourceLocation Loc);
Eli Friedman6d7cfd72010-07-16 00:55:21 +00002784
Stephen Hines651f13c2014-04-23 16:59:28 -07002785public:
Anders Carlsson0139bb92009-04-08 20:47:54 +00002786 /// EmitCallArgs - Emit call arguments for a function.
Stephen Hines651f13c2014-04-23 16:59:28 -07002787 template <typename T>
2788 void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo,
Anders Carlsson0139bb92009-04-08 20:47:54 +00002789 CallExpr::const_arg_iterator ArgBeg,
Adrian Prantlb0e603c2013-07-26 20:42:57 +00002790 CallExpr::const_arg_iterator ArgEnd,
Stephen Hines176edba2014-12-01 14:53:08 -08002791 const FunctionDecl *CalleeDecl = nullptr,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002792 unsigned ParamsToSkip = 0) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002793 SmallVector<QualType, 16> ArgTypes;
2794 CallExpr::const_arg_iterator Arg = ArgBeg;
2795
Stephen Hines176edba2014-12-01 14:53:08 -08002796 assert((ParamsToSkip == 0 || CallArgTypeInfo) &&
2797 "Can't skip parameters if type info is not provided");
2798 if (CallArgTypeInfo) {
2799 // First, use the argument types that the type info knows about
2800 for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip,
2801 E = CallArgTypeInfo->param_type_end();
2802 I != E; ++I, ++Arg) {
2803 assert(Arg != ArgEnd && "Running over edge of argument list!");
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002804 assert(
2805 ((*I)->isVariablyModifiedType() ||
2806 getContext()
2807 .getCanonicalType((*I).getNonReferenceType())
2808 .getTypePtr() ==
2809 getContext().getCanonicalType(Arg->getType()).getTypePtr()) &&
2810 "type mismatch in call argument!");
Stephen Hines176edba2014-12-01 14:53:08 -08002811 ArgTypes.push_back(*I);
2812 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002813 }
2814
2815 // Either we've emitted all the call args, or we have a call to variadic
Stephen Hines176edba2014-12-01 14:53:08 -08002816 // function.
2817 assert(
2818 (Arg == ArgEnd || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) &&
2819 "Extra arguments in non-variadic function!");
Stephen Hines651f13c2014-04-23 16:59:28 -07002820
2821 // If we still have any arguments, emit them using the type of the argument.
2822 for (; Arg != ArgEnd; ++Arg)
Stephen Hines176edba2014-12-01 14:53:08 -08002823 ArgTypes.push_back(getVarArgType(*Arg));
Stephen Hines651f13c2014-04-23 16:59:28 -07002824
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002825 EmitCallArgs(Args, ArgTypes, ArgBeg, ArgEnd, CalleeDecl, ParamsToSkip);
Stephen Hines651f13c2014-04-23 16:59:28 -07002826 }
2827
2828 void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes,
2829 CallExpr::const_arg_iterator ArgBeg,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002830 CallExpr::const_arg_iterator ArgEnd,
Stephen Hines176edba2014-12-01 14:53:08 -08002831 const FunctionDecl *CalleeDecl = nullptr,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002832 unsigned ParamsToSkip = 0);
Stephen Hines651f13c2014-04-23 16:59:28 -07002833
2834private:
Stephen Hines176edba2014-12-01 14:53:08 -08002835 QualType getVarArgType(const Expr *Arg);
2836
John McCall492c4f92010-03-03 04:15:11 +00002837 const TargetCodeGenInfo &getTargetHooks() const {
2838 return CGM.getTargetCodeGenInfo();
2839 }
John McCall744016d2010-07-06 23:57:41 +00002840
2841 void EmitDeclMetadata();
John McCallf0c11f72011-03-31 08:03:29 +00002842
2843 CodeGenModule::ByrefHelpers *
Chris Lattner2acc6e32011-07-18 04:24:23 +00002844 buildByrefHelpers(llvm::StructType &byrefType,
John McCallf0c11f72011-03-31 08:03:29 +00002845 const AutoVarEmission &emission);
Dan Gohmanb49bd272012-02-16 00:57:37 +00002846
2847 void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
Jay Foadf4c3db12012-03-02 18:34:30 +00002848
Eli Friedmanea93e402012-08-23 03:10:17 +00002849 /// GetPointeeAlignment - Given an expression with a pointer type, emit the
2850 /// value and compute our best estimate of the alignment of the pointee.
2851 std::pair<llvm::Value*, unsigned> EmitPointerWithAlignment(const Expr *Addr);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002852
2853 llvm::Value *GetValueForARMHint(unsigned BuiltinID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002854};
Mike Stump1eb44332009-09-09 15:08:12 +00002855
John McCall150b4622011-01-26 04:00:11 +00002856/// Helper class with most of the code for saving a value for a
2857/// conditional expression cleanup.
John McCall804b8072011-01-28 10:53:53 +00002858struct DominatingLLVMValue {
John McCall150b4622011-01-26 04:00:11 +00002859 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
2860
2861 /// Answer whether the given value needs extra work to be saved.
2862 static bool needsSaving(llvm::Value *value) {
2863 // If it's not an instruction, we don't need to save.
2864 if (!isa<llvm::Instruction>(value)) return false;
2865
2866 // If it's an instruction in the entry block, we don't need to save.
2867 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
2868 return (block != &block->getParent()->getEntryBlock());
2869 }
2870
2871 /// Try to save the given value.
2872 static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
2873 if (!needsSaving(value)) return saved_type(value, false);
2874
2875 // Otherwise we need an alloca.
2876 llvm::Value *alloca =
2877 CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
2878 CGF.Builder.CreateStore(value, alloca);
2879
2880 return saved_type(alloca, true);
2881 }
2882
2883 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
2884 if (!value.getInt()) return value.getPointer();
2885 return CGF.Builder.CreateLoad(value.getPointer());
2886 }
2887};
2888
John McCall804b8072011-01-28 10:53:53 +00002889/// A partial specialization of DominatingValue for llvm::Values that
2890/// might be llvm::Instructions.
2891template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
2892 typedef T *type;
John McCall150b4622011-01-26 04:00:11 +00002893 static type restore(CodeGenFunction &CGF, saved_type value) {
John McCall804b8072011-01-28 10:53:53 +00002894 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
2895 }
2896};
2897
2898/// A specialization of DominatingValue for RValue.
2899template <> struct DominatingValue<RValue> {
2900 typedef RValue type;
2901 class saved_type {
2902 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
2903 AggregateAddress, ComplexAddress };
2904
2905 llvm::Value *Value;
2906 Kind K;
2907 saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
2908
2909 public:
2910 static bool needsSaving(RValue value);
2911 static saved_type save(CodeGenFunction &CGF, RValue value);
2912 RValue restore(CodeGenFunction &CGF);
2913
2914 // implementations in CGExprCXX.cpp
2915 };
2916
2917 static bool needsSaving(type value) {
2918 return saved_type::needsSaving(value);
2919 }
2920 static saved_type save(CodeGenFunction &CGF, type value) {
2921 return saved_type::save(CGF, value);
2922 }
2923 static type restore(CodeGenFunction &CGF, saved_type value) {
2924 return value.restore(CGF);
John McCall150b4622011-01-26 04:00:11 +00002925 }
2926};
2927
Reid Spencer5f016e22007-07-11 17:01:13 +00002928} // end namespace CodeGen
2929} // end namespace clang
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00002930
Reid Spencer5f016e22007-07-11 17:01:13 +00002931#endif