blob: 4575e31dbd2fc1e98f7e8bdc49d8ae5be7a8bd9b [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "clang/Basic/TargetInfo.h"
31#include "clang/Frontend/CodeGenOptions.h"
32#include "llvm/ADT/ArrayRef.h"
33#include "llvm/ADT/DenseMap.h"
34#include "llvm/ADT/SmallVector.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070035#include "llvm/IR/ValueHandle.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000036#include "llvm/Support/Debug.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000037
Reid Spencer5f016e22007-07-11 17:01:13 +000038namespace llvm {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070039class BasicBlock;
40class LLVMContext;
41class MDNode;
42class Module;
43class SwitchInst;
44class Twine;
45class Value;
46class CallSite;
Reid Spencer5f016e22007-07-11 17:01:13 +000047}
48
49namespace clang {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070050class ASTContext;
51class BlockDecl;
52class CXXDestructorDecl;
53class CXXForRangeStmt;
54class CXXTryStmt;
55class Decl;
56class LabelDecl;
57class EnumConstantDecl;
58class FunctionDecl;
59class FunctionProtoType;
60class LabelStmt;
61class ObjCContainerDecl;
62class ObjCInterfaceDecl;
63class ObjCIvarDecl;
64class ObjCMethodDecl;
65class ObjCImplementationDecl;
66class ObjCPropertyImplDecl;
67class TargetInfo;
68class TargetCodeGenInfo;
69class VarDecl;
70class ObjCForCollectionStmt;
71class ObjCAtTryStmt;
72class ObjCAtThrowStmt;
73class ObjCAtSynchronizedStmt;
74class ObjCAutoreleasePoolStmt;
Devang Patelb84a06e2007-10-23 02:10:49 +000075
Reid Spencer5f016e22007-07-11 17:01:13 +000076namespace CodeGen {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070077class CodeGenTypes;
78class CGFunctionInfo;
79class CGRecordLayout;
80class CGBlockInfo;
81class CGCXXABI;
82class BlockFlags;
83class BlockFieldFlags;
Mike Stump0dd9e882009-02-08 23:14:22 +000084
John McCall9d232c82013-03-07 21:37:08 +000085/// The kind of evaluation to perform on values of a particular
86/// type. Basically, is the code in CGExprScalar, CGExprComplex, or
87/// CGExprAgg?
88///
89/// TODO: should vectors maybe be split out into their own thing?
90enum TypeEvaluationKind {
91 TEK_Scalar,
92 TEK_Complex,
93 TEK_Aggregate
94};
95
Stephen Hines176edba2014-12-01 14:53:08 -080096class SuppressDebugLocation {
97 llvm::DebugLoc CurLoc;
98 llvm::IRBuilderBase &Builder;
99public:
100 SuppressDebugLocation(llvm::IRBuilderBase &Builder)
101 : CurLoc(Builder.getCurrentDebugLocation()), Builder(Builder) {
102 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
103 }
104 ~SuppressDebugLocation() {
105 Builder.SetCurrentDebugLocation(CurLoc);
106 }
107};
108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109/// CodeGenFunction - This class organizes the per-function state that is used
110/// while generating LLVM code.
John McCall5936e332011-02-15 09:22:45 +0000111class CodeGenFunction : public CodeGenTypeCache {
Dmitri Gribenkof56faa02012-09-15 20:20:27 +0000112 CodeGenFunction(const CodeGenFunction &) LLVM_DELETED_FUNCTION;
113 void operator=(const CodeGenFunction &) LLVM_DELETED_FUNCTION;
John McCall4c40d982010-08-31 07:33:07 +0000114
115 friend class CGCXXABI;
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000116public:
John McCallff8e1152010-07-23 21:56:41 +0000117 /// A jump destination is an abstract label, branching to which may
118 /// require a jump out through normal cleanups.
John McCallf1549f62010-07-06 01:34:17 +0000119 struct JumpDest {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700120 JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
John McCallff8e1152010-07-23 21:56:41 +0000121 JumpDest(llvm::BasicBlock *Block,
122 EHScopeStack::stable_iterator Depth,
123 unsigned Index)
124 : Block(Block), ScopeDepth(Depth), Index(Index) {}
125
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700126 bool isValid() const { return Block != nullptr; }
John McCallff8e1152010-07-23 21:56:41 +0000127 llvm::BasicBlock *getBlock() const { return Block; }
128 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
129 unsigned getDestIndex() const { return Index; }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000130
Nadav Rotem495cfa42013-03-23 06:43:35 +0000131 // This should be used cautiously.
132 void setScopeDepth(EHScopeStack::stable_iterator depth) {
133 ScopeDepth = depth;
134 }
135
John McCallff8e1152010-07-23 21:56:41 +0000136 private:
John McCallf1549f62010-07-06 01:34:17 +0000137 llvm::BasicBlock *Block;
138 EHScopeStack::stable_iterator ScopeDepth;
John McCallff8e1152010-07-23 21:56:41 +0000139 unsigned Index;
140 };
141
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 CodeGenModule &CGM; // Per-module state.
Daniel Dunbar444be732009-11-13 05:51:54 +0000143 const TargetInfo &Target;
Mike Stump0dd9e882009-02-08 23:14:22 +0000144
Chris Lattner58dee102007-08-21 16:57:55 +0000145 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700146 LoopInfoStack LoopStack;
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000147 CGBuilderTy Builder;
Mike Stump0dd9e882009-02-08 23:14:22 +0000148
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700149 /// \brief CGBuilder insert helper. This function is called after an
150 /// instruction is created using Builder.
151 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
152 llvm::BasicBlock *BB,
153 llvm::BasicBlock::iterator InsertPt) const;
154
John McCallf5ebf9b2013-05-03 07:33:41 +0000155 /// CurFuncDecl - Holds the Decl for the current outermost
156 /// non-closure context.
Chris Lattner41110242008-06-17 18:05:57 +0000157 const Decl *CurFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000158 /// CurCodeDecl - This is the inner-most code context, which includes blocks.
159 const Decl *CurCodeDecl;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000160 const CGFunctionInfo *CurFnInfo;
Chris Lattner391d77a2008-03-30 23:03:07 +0000161 QualType FnRetTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 llvm::Function *CurFn;
163
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000164 /// CurGD - The GlobalDecl for the current function being compiled.
165 GlobalDecl CurGD;
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000166
John McCallf85e1932011-06-15 23:02:42 +0000167 /// PrologueCleanupDepth - The cleanup depth enclosing all the
168 /// cleanups associated with the parameters.
169 EHScopeStack::stable_iterator PrologueCleanupDepth;
170
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000171 /// ReturnBlock - Unified return block.
John McCallf1549f62010-07-06 01:34:17 +0000172 JumpDest ReturnBlock;
173
Mike Stump0dd9e882009-02-08 23:14:22 +0000174 /// ReturnValue - The temporary alloca to hold the return value. This is null
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000175 /// iff the function has no return value.
Eli Friedmanb17daf92009-12-04 02:43:40 +0000176 llvm::Value *ReturnValue;
Mike Stump0dd9e882009-02-08 23:14:22 +0000177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 /// AllocaInsertPoint - This is an instruction in the entry block before which
179 /// we prefer to insert allocas.
Chris Lattner481769b2009-03-31 22:17:44 +0000180 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000181
Ben Langmuir524387a2013-05-09 19:17:11 +0000182 /// \brief API for captured statement code generation.
183 class CGCapturedStmtInfo {
184 public:
185 explicit CGCapturedStmtInfo(const CapturedStmt &S,
186 CapturedRegionKind K = CR_Default)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700187 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
Ben Langmuir524387a2013-05-09 19:17:11 +0000188
189 RecordDecl::field_iterator Field =
190 S.getCapturedRecordDecl()->field_begin();
191 for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
192 E = S.capture_end();
193 I != E; ++I, ++Field) {
194 if (I->capturesThis())
195 CXXThisFieldDecl = *Field;
Stephen Hines176edba2014-12-01 14:53:08 -0800196 else if (I->capturesVariable())
Ben Langmuir524387a2013-05-09 19:17:11 +0000197 CaptureFields[I->getCapturedVar()] = *Field;
198 }
199 }
200
201 virtual ~CGCapturedStmtInfo();
202
203 CapturedRegionKind getKind() const { return Kind; }
204
205 void setContextValue(llvm::Value *V) { ThisValue = V; }
206 // \brief Retrieve the value of the context parameter.
207 llvm::Value *getContextValue() const { return ThisValue; }
208
209 /// \brief Lookup the captured field decl for a variable.
210 const FieldDecl *lookup(const VarDecl *VD) const {
211 return CaptureFields.lookup(VD);
212 }
213
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700214 bool isCXXThisExprCaptured() const { return CXXThisFieldDecl != nullptr; }
Ben Langmuir524387a2013-05-09 19:17:11 +0000215 FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
216
Stephen Hines176edba2014-12-01 14:53:08 -0800217 static bool classof(const CGCapturedStmtInfo *) {
218 return true;
219 }
220
Ben Langmuir524387a2013-05-09 19:17:11 +0000221 /// \brief Emit the captured statement body.
222 virtual void EmitBody(CodeGenFunction &CGF, Stmt *S) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700223 RegionCounter Cnt = CGF.getPGORegionCounter(S);
224 Cnt.beginRegion(CGF.Builder);
Ben Langmuir524387a2013-05-09 19:17:11 +0000225 CGF.EmitStmt(S);
226 }
227
228 /// \brief Get the name of the capture helper.
229 virtual StringRef getHelperName() const { return "__captured_stmt"; }
230
231 private:
232 /// \brief The kind of captured statement being generated.
233 CapturedRegionKind Kind;
234
235 /// \brief Keep the map between VarDecl and FieldDecl.
236 llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
237
238 /// \brief The base address of the captured record, passed in as the first
239 /// argument of the parallel region function.
240 llvm::Value *ThisValue;
241
242 /// \brief Captured 'this' type.
243 FieldDecl *CXXThisFieldDecl;
244 };
245 CGCapturedStmtInfo *CapturedStmtInfo;
246
Nuno Lopesb3198a82012-05-08 22:10:46 +0000247 /// BoundsChecking - Emit run-time bounds checks. Higher values mean
248 /// potentially higher performance penalties.
249 unsigned char BoundsChecking;
250
Stephen Hines176edba2014-12-01 14:53:08 -0800251 /// \brief Sanitizers enabled for this function.
252 SanitizerSet SanOpts;
253
254 /// \brief True if CodeGen currently emits code implementing sanitizer checks.
255 bool IsSanitizerScope;
256
257 /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope.
258 class SanitizerScope {
259 CodeGenFunction *CGF;
260 public:
261 SanitizerScope(CodeGenFunction *CGF);
262 ~SanitizerScope();
263 };
264
265 /// In C++, whether we are code generating a thunk. This controls whether we
266 /// should emit cleanups.
267 bool CurFuncIsThunk;
Will Dietz4f45bc02013-01-18 11:30:38 +0000268
John McCallf85e1932011-06-15 23:02:42 +0000269 /// In ARC, whether we should autorelease the return value.
270 bool AutoreleaseResult;
271
Stephen Hines176edba2014-12-01 14:53:08 -0800272 /// Whether we processed a Microsoft-style asm block during CodeGen. These can
273 /// potentially set the return value.
274 bool SawAsmBlock;
275
John McCalld16c2cf2011-02-08 08:22:06 +0000276 const CodeGen::CGBlockInfo *BlockInfo;
277 llvm::Value *BlockPointer;
278
Eli Friedmancec5ebd2012-02-11 02:57:39 +0000279 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
280 FieldDecl *LambdaThisCaptureField;
281
Douglas Gregor3d91bbc2010-05-17 15:52:46 +0000282 /// \brief A mapping from NRVO variables to the flags used to indicate
283 /// when the NRVO has been applied to this variable.
284 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000285
John McCallf1549f62010-07-06 01:34:17 +0000286 EHScopeStack EHStack;
Richard Smith8a07cd32013-06-12 20:42:33 +0000287 llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
288
289 /// Header for data within LifetimeExtendedCleanupStack.
290 struct LifetimeExtendedCleanupHeader {
291 /// The size of the following cleanup object.
Stephen Hines176edba2014-12-01 14:53:08 -0800292 unsigned Size : 29;
Richard Smith8a07cd32013-06-12 20:42:33 +0000293 /// The kind of cleanup to push: a value from the CleanupKind enumeration.
294 unsigned Kind : 3;
295
Stephen Hines176edba2014-12-01 14:53:08 -0800296 size_t getSize() const { return size_t(Size); }
Richard Smith8a07cd32013-06-12 20:42:33 +0000297 CleanupKind getKind() const { return static_cast<CleanupKind>(Kind); }
298 };
John McCallf1549f62010-07-06 01:34:17 +0000299
John McCallff8e1152010-07-23 21:56:41 +0000300 /// i32s containing the indexes of the cleanup destinations.
301 llvm::AllocaInst *NormalCleanupDest;
John McCallff8e1152010-07-23 21:56:41 +0000302
303 unsigned NextCleanupDestIndex;
304
John McCall1a343eb2011-11-10 08:15:53 +0000305 /// FirstBlockInfo - The head of a singly-linked-list of block layouts.
306 CGBlockInfo *FirstBlockInfo;
307
John McCall777d6e52011-08-11 02:22:43 +0000308 /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
309 llvm::BasicBlock *EHResumeBlock;
310
Bill Wendling285cfd82011-09-19 20:31:14 +0000311 /// The exception slot. All landing pads write the current exception pointer
312 /// into this alloca.
John McCallf1549f62010-07-06 01:34:17 +0000313 llvm::Value *ExceptionSlot;
314
Bill Wendling285cfd82011-09-19 20:31:14 +0000315 /// The selector slot. Under the MandatoryCleanup model, all landing pads
316 /// write the current selector value into this alloca.
John McCall93c332a2011-05-28 21:13:02 +0000317 llvm::AllocaInst *EHSelectorSlot;
318
John McCallf1549f62010-07-06 01:34:17 +0000319 /// Emits a landing pad for the current EH stack.
320 llvm::BasicBlock *EmitLandingPad();
321
322 llvm::BasicBlock *getInvokeDestImpl();
323
John McCall150b4622011-01-26 04:00:11 +0000324 template <class T>
John McCall804b8072011-01-28 10:53:53 +0000325 typename DominatingValue<T>::saved_type saveValueInCond(T value) {
326 return DominatingValue<T>::save(*this, value);
John McCall150b4622011-01-26 04:00:11 +0000327 }
328
Daniel Dunbar18ccc772008-09-28 01:03:14 +0000329public:
Anders Carlssonfa1f7562009-02-10 06:07:49 +0000330 /// ObjCEHValueStack - Stack of Objective-C exception values, used for
331 /// rethrows.
Chris Lattner686775d2011-07-20 06:58:45 +0000332 SmallVector<llvm::Value*, 8> ObjCEHValueStack;
Mike Stump0dd9e882009-02-08 23:14:22 +0000333
John McCalld768e9d2011-06-22 02:32:12 +0000334 /// A class controlling the emission of a finally block.
335 class FinallyInfo {
336 /// Where the catchall's edge through the cleanup should go.
337 JumpDest RethrowDest;
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000338
John McCalld768e9d2011-06-22 02:32:12 +0000339 /// A function to call to enter the catch.
340 llvm::Constant *BeginCatchFn;
341
342 /// An i1 variable indicating whether or not the @finally is
343 /// running for an exception.
344 llvm::AllocaInst *ForEHVar;
345
346 /// An i8* variable into which the exception pointer to rethrow
347 /// has been saved.
348 llvm::AllocaInst *SavedExnVar;
349
350 public:
351 void enter(CodeGenFunction &CGF, const Stmt *Finally,
352 llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
353 llvm::Constant *rethrowFn);
354 void exit(CodeGenFunction &CGF);
355 };
Mike Stumpd88ea562009-12-09 03:35:49 +0000356
John McCall150b4622011-01-26 04:00:11 +0000357 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
358 /// current full-expression. Safe against the possibility that
359 /// we're currently inside a conditionally-evaluated expression.
John McCall3ad32c82011-01-28 08:37:24 +0000360 template <class T, class A0>
361 void pushFullExprCleanup(CleanupKind kind, A0 a0) {
362 // If we're not in a conditional branch, or if none of the
363 // arguments requires saving, then use the unconditional cleanup.
John McCallc4a1a842011-07-12 00:15:30 +0000364 if (!isInConditionalBranch())
365 return EHStack.pushCleanup<T>(kind, a0);
John McCall3ad32c82011-01-28 08:37:24 +0000366
John McCall804b8072011-01-28 10:53:53 +0000367 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
John McCall3ad32c82011-01-28 08:37:24 +0000368
369 typedef EHScopeStack::ConditionalCleanup1<T, A0> CleanupType;
370 EHStack.pushCleanup<CleanupType>(kind, a0_saved);
371 initFullExprCleanup();
372 }
373
374 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
375 /// current full-expression. Safe against the possibility that
376 /// we're currently inside a conditionally-evaluated expression.
John McCall150b4622011-01-26 04:00:11 +0000377 template <class T, class A0, class A1>
378 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) {
379 // If we're not in a conditional branch, or if none of the
380 // arguments requires saving, then use the unconditional cleanup.
John McCallc4a1a842011-07-12 00:15:30 +0000381 if (!isInConditionalBranch())
382 return EHStack.pushCleanup<T>(kind, a0, a1);
John McCall150b4622011-01-26 04:00:11 +0000383
John McCall804b8072011-01-28 10:53:53 +0000384 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
385 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
John McCall150b4622011-01-26 04:00:11 +0000386
387 typedef EHScopeStack::ConditionalCleanup2<T, A0, A1> CleanupType;
John McCall3ad32c82011-01-28 08:37:24 +0000388 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved);
389 initFullExprCleanup();
John McCall150b4622011-01-26 04:00:11 +0000390 }
391
Douglas Gregord7b23162011-06-22 16:12:01 +0000392 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
393 /// current full-expression. Safe against the possibility that
394 /// we're currently inside a conditionally-evaluated expression.
395 template <class T, class A0, class A1, class A2>
396 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2) {
397 // If we're not in a conditional branch, or if none of the
398 // arguments requires saving, then use the unconditional cleanup.
399 if (!isInConditionalBranch()) {
John McCallc4a1a842011-07-12 00:15:30 +0000400 return EHStack.pushCleanup<T>(kind, a0, a1, a2);
Douglas Gregord7b23162011-06-22 16:12:01 +0000401 }
402
403 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
404 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
405 typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
406
407 typedef EHScopeStack::ConditionalCleanup3<T, A0, A1, A2> CleanupType;
408 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, a2_saved);
409 initFullExprCleanup();
410 }
411
John McCall9928c482011-07-12 16:41:08 +0000412 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
413 /// current full-expression. Safe against the possibility that
414 /// we're currently inside a conditionally-evaluated expression.
415 template <class T, class A0, class A1, class A2, class A3>
416 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2, A3 a3) {
417 // If we're not in a conditional branch, or if none of the
418 // arguments requires saving, then use the unconditional cleanup.
419 if (!isInConditionalBranch()) {
420 return EHStack.pushCleanup<T>(kind, a0, a1, a2, a3);
421 }
422
423 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
424 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
425 typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
426 typename DominatingValue<A3>::saved_type a3_saved = saveValueInCond(a3);
427
428 typedef EHScopeStack::ConditionalCleanup4<T, A0, A1, A2, A3> CleanupType;
429 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved,
430 a2_saved, a3_saved);
431 initFullExprCleanup();
432 }
433
Richard Smith8a07cd32013-06-12 20:42:33 +0000434 /// \brief Queue a cleanup to be pushed after finishing the current
435 /// full-expression.
436 template <class T, class A0, class A1, class A2, class A3>
437 void pushCleanupAfterFullExpr(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3) {
438 assert(!isInConditionalBranch() && "can't defer conditional cleanup");
439
440 LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind };
441
442 size_t OldSize = LifetimeExtendedCleanupStack.size();
443 LifetimeExtendedCleanupStack.resize(
444 LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size);
445
446 char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
447 new (Buffer) LifetimeExtendedCleanupHeader(Header);
448 new (Buffer + sizeof(Header)) T(a0, a1, a2, a3);
449 }
450
John McCall6f103ba2011-11-10 10:43:54 +0000451 /// Set up the last cleaup that was pushed as a conditional
452 /// full-expression cleanup.
453 void initFullExprCleanup();
454
John McCallf1549f62010-07-06 01:34:17 +0000455 /// PushDestructorCleanup - Push a cleanup to call the
456 /// complete-object destructor of an object of the given type at the
457 /// given address. Does nothing if T is not a C++ class type with a
458 /// non-trivial destructor.
459 void PushDestructorCleanup(QualType T, llvm::Value *Addr);
460
John McCall81407d42010-07-21 06:29:51 +0000461 /// PushDestructorCleanup - Push a cleanup to call the
462 /// complete-object variant of the given destructor on the object at
463 /// the given address.
464 void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
465 llvm::Value *Addr);
466
John McCallf1549f62010-07-06 01:34:17 +0000467 /// PopCleanupBlock - Will pop the cleanup entry on the stack and
468 /// process all branch fixups.
Adrian Prantl1c3db762013-05-16 00:41:26 +0000469 void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
John McCallf1549f62010-07-06 01:34:17 +0000470
John McCall7d8647f2010-09-14 07:57:04 +0000471 /// DeactivateCleanupBlock - Deactivates the given cleanup block.
472 /// The block cannot be reactivated. Pops it if it's the top of the
473 /// stack.
John McCall6f103ba2011-11-10 10:43:54 +0000474 ///
475 /// \param DominatingIP - An instruction which is known to
476 /// dominate the current IP (if set) and which lies along
477 /// all paths of execution between the current IP and the
478 /// the point at which the cleanup comes into scope.
479 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
480 llvm::Instruction *DominatingIP);
John McCall7d8647f2010-09-14 07:57:04 +0000481
482 /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
483 /// Cannot be used to resurrect a deactivated cleanup.
John McCall6f103ba2011-11-10 10:43:54 +0000484 ///
485 /// \param DominatingIP - An instruction which is known to
486 /// dominate the current IP (if set) and which lies along
487 /// all paths of execution between the current IP and the
488 /// the point at which the cleanup comes into scope.
489 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
490 llvm::Instruction *DominatingIP);
John McCallcd2d2b72010-08-13 21:20:51 +0000491
John McCallf1549f62010-07-06 01:34:17 +0000492 /// \brief Enters a new scope for capturing cleanups, all of which
493 /// will be executed once the scope is exited.
494 class RunCleanupsScope {
John McCallf1549f62010-07-06 01:34:17 +0000495 EHScopeStack::stable_iterator CleanupStackDepth;
Richard Smith8a07cd32013-06-12 20:42:33 +0000496 size_t LifetimeExtendedCleanupStackSize;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000497 bool OldDidCallStackSave;
John McCalled383132013-03-01 01:24:35 +0000498 protected:
Douglas Gregor5656e142009-11-24 21:15:44 +0000499 bool PerformCleanup;
John McCalled383132013-03-01 01:24:35 +0000500 private:
Douglas Gregor01234bb2009-11-24 16:43:22 +0000501
Dmitri Gribenkof56faa02012-09-15 20:20:27 +0000502 RunCleanupsScope(const RunCleanupsScope &) LLVM_DELETED_FUNCTION;
503 void operator=(const RunCleanupsScope &) LLVM_DELETED_FUNCTION;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000504
Eric Christopherc3287792011-10-19 00:43:52 +0000505 protected:
506 CodeGenFunction& CGF;
Will Dietz4f45bc02013-01-18 11:30:38 +0000507
Douglas Gregor01234bb2009-11-24 16:43:22 +0000508 public:
509 /// \brief Enter a new cleanup scope.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000510 explicit RunCleanupsScope(CodeGenFunction &CGF)
Eric Christopherc3287792011-10-19 00:43:52 +0000511 : PerformCleanup(true), CGF(CGF)
Douglas Gregor5656e142009-11-24 21:15:44 +0000512 {
John McCallf1549f62010-07-06 01:34:17 +0000513 CleanupStackDepth = CGF.EHStack.stable_begin();
Richard Smith8a07cd32013-06-12 20:42:33 +0000514 LifetimeExtendedCleanupStackSize =
515 CGF.LifetimeExtendedCleanupStack.size();
Douglas Gregor01234bb2009-11-24 16:43:22 +0000516 OldDidCallStackSave = CGF.DidCallStackSave;
Argyrios Kyrtzidis4ada2ca2010-09-14 00:42:34 +0000517 CGF.DidCallStackSave = false;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000518 }
519
520 /// \brief Exit this cleanup scope, emitting any accumulated
521 /// cleanups.
John McCallf1549f62010-07-06 01:34:17 +0000522 ~RunCleanupsScope() {
Douglas Gregor5656e142009-11-24 21:15:44 +0000523 if (PerformCleanup) {
524 CGF.DidCallStackSave = OldDidCallStackSave;
Richard Smith8a07cd32013-06-12 20:42:33 +0000525 CGF.PopCleanupBlocks(CleanupStackDepth,
526 LifetimeExtendedCleanupStackSize);
Douglas Gregor5656e142009-11-24 21:15:44 +0000527 }
528 }
529
530 /// \brief Determine whether this scope requires any cleanups.
531 bool requiresCleanups() const {
John McCallf1549f62010-07-06 01:34:17 +0000532 return CGF.EHStack.stable_begin() != CleanupStackDepth;
Douglas Gregor5656e142009-11-24 21:15:44 +0000533 }
534
535 /// \brief Force the emission of cleanups now, instead of waiting
536 /// until this object is destroyed.
537 void ForceCleanup() {
538 assert(PerformCleanup && "Already forced cleanup");
Douglas Gregor01234bb2009-11-24 16:43:22 +0000539 CGF.DidCallStackSave = OldDidCallStackSave;
Richard Smith8a07cd32013-06-12 20:42:33 +0000540 CGF.PopCleanupBlocks(CleanupStackDepth,
541 LifetimeExtendedCleanupStackSize);
Douglas Gregor5656e142009-11-24 21:15:44 +0000542 PerformCleanup = false;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000543 }
544 };
545
Stephen Hines176edba2014-12-01 14:53:08 -0800546 class LexicalScope : public RunCleanupsScope {
Eric Christopherc3287792011-10-19 00:43:52 +0000547 SourceRange Range;
Nadav Rotem495cfa42013-03-23 06:43:35 +0000548 SmallVector<const LabelDecl*, 4> Labels;
549 LexicalScope *ParentScope;
Eric Christopherc3287792011-10-19 00:43:52 +0000550
Dmitri Gribenkof56faa02012-09-15 20:20:27 +0000551 LexicalScope(const LexicalScope &) LLVM_DELETED_FUNCTION;
552 void operator=(const LexicalScope &) LLVM_DELETED_FUNCTION;
Eric Christopherc3287792011-10-19 00:43:52 +0000553
554 public:
555 /// \brief Enter a new cleanup scope.
556 explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
Nadav Rotem495cfa42013-03-23 06:43:35 +0000557 : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
558 CGF.CurLexicalScope = this;
Eric Christopherc3287792011-10-19 00:43:52 +0000559 if (CGDebugInfo *DI = CGF.getDebugInfo())
560 DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
561 }
562
Nadav Rotem495cfa42013-03-23 06:43:35 +0000563 void addLabel(const LabelDecl *label) {
564 assert(PerformCleanup && "adding label to dead scope?");
565 Labels.push_back(label);
566 }
567
Eric Christopherc3287792011-10-19 00:43:52 +0000568 /// \brief Exit this cleanup scope, emitting any accumulated
569 /// cleanups.
570 ~LexicalScope() {
Adrian Prantlefb72ad2013-04-01 19:02:06 +0000571 if (CGDebugInfo *DI = CGF.getDebugInfo())
572 DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
573
Nadav Rotem495cfa42013-03-23 06:43:35 +0000574 // If we should perform a cleanup, force them now. Note that
575 // this ends the cleanup scope before rescoping any labels.
576 if (PerformCleanup) ForceCleanup();
Eric Christopherc3287792011-10-19 00:43:52 +0000577 }
578
579 /// \brief Force the emission of cleanups now, instead of waiting
580 /// until this object is destroyed.
581 void ForceCleanup() {
Nadav Rotem495cfa42013-03-23 06:43:35 +0000582 CGF.CurLexicalScope = ParentScope;
Adrian Prantlefb72ad2013-04-01 19:02:06 +0000583 RunCleanupsScope::ForceCleanup();
584
Nadav Rotem495cfa42013-03-23 06:43:35 +0000585 if (!Labels.empty())
586 rescopeLabels();
Eric Christopherc3287792011-10-19 00:43:52 +0000587 }
Nadav Rotem495cfa42013-03-23 06:43:35 +0000588
589 void rescopeLabels();
Eric Christopherc3287792011-10-19 00:43:52 +0000590 };
591
Stephen Hines176edba2014-12-01 14:53:08 -0800592 /// \brief The scope used to remap some variables as private in the OpenMP
593 /// loop body (or other captured region emitted without outlining), and to
594 /// restore old vars back on exit.
595 class OMPPrivateScope : public RunCleanupsScope {
596 typedef llvm::DenseMap<const VarDecl *, llvm::Value *> VarDeclMapTy;
597 VarDeclMapTy SavedLocals;
598 VarDeclMapTy SavedPrivates;
599
600 private:
601 OMPPrivateScope(const OMPPrivateScope &) LLVM_DELETED_FUNCTION;
602 void operator=(const OMPPrivateScope &) LLVM_DELETED_FUNCTION;
603
604 public:
605 /// \brief Enter a new OpenMP private scope.
606 explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
607
608 /// \brief Registers \a LocalVD variable as a private and apply \a
609 /// PrivateGen function for it to generate corresponding private variable.
610 /// \a PrivateGen returns an address of the generated private variable.
611 /// \return true if the variable is registered as private, false if it has
612 /// been privatized already.
613 bool
614 addPrivate(const VarDecl *LocalVD,
615 const std::function<llvm::Value *()> &PrivateGen) {
616 assert(PerformCleanup && "adding private to dead scope");
617 assert(LocalVD->isLocalVarDecl() && "privatizing non-local variable");
618 if (SavedLocals.count(LocalVD) > 0) return false;
619 SavedLocals[LocalVD] = CGF.LocalDeclMap.lookup(LocalVD);
620 CGF.LocalDeclMap.erase(LocalVD);
621 SavedPrivates[LocalVD] = PrivateGen();
622 CGF.LocalDeclMap[LocalVD] = SavedLocals[LocalVD];
623 return true;
624 }
625
626 /// \brief Privatizes local variables previously registered as private.
627 /// Registration is separate from the actual privatization to allow
628 /// initializers use values of the original variables, not the private one.
629 /// This is important, for example, if the private variable is a class
630 /// variable initialized by a constructor that references other private
631 /// variables. But at initialization original variables must be used, not
632 /// private copies.
633 /// \return true if at least one variable was privatized, false otherwise.
634 bool Privatize() {
635 for (auto VDPair : SavedPrivates) {
636 CGF.LocalDeclMap[VDPair.first] = VDPair.second;
637 }
638 SavedPrivates.clear();
639 return !SavedLocals.empty();
640 }
641
642 void ForceCleanup() {
643 RunCleanupsScope::ForceCleanup();
644 // Remap vars back to the original values.
645 for (auto I : SavedLocals) {
646 CGF.LocalDeclMap[I.first] = I.second;
647 }
648 SavedLocals.clear();
649 }
650
651 /// \brief Exit scope - all the mapped variables are restored.
652 ~OMPPrivateScope() { ForceCleanup(); }
653 };
Anders Carlsson44ec82b2010-03-30 03:14:41 +0000654
Richard Smith8a07cd32013-06-12 20:42:33 +0000655 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
656 /// that have been added.
Adrian Prantl1c3db762013-05-16 00:41:26 +0000657 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
Anders Carlsson44ec82b2010-03-30 03:14:41 +0000658
Richard Smith8a07cd32013-06-12 20:42:33 +0000659 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
660 /// that have been added, then adds all lifetime-extended cleanups from
661 /// the given position to the stack.
662 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
663 size_t OldLifetimeExtendedStackSize);
664
John McCallff8e1152010-07-23 21:56:41 +0000665 void ResolveBranchFixups(llvm::BasicBlock *Target);
666
John McCallf1549f62010-07-06 01:34:17 +0000667 /// The given basic block lies in the current EH scope, but may be a
668 /// target of a potentially scope-crossing jump; get a stable handle
669 /// to which we can perform this jump later.
John McCallff8e1152010-07-23 21:56:41 +0000670 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
John McCall413e6772010-07-28 01:07:35 +0000671 return JumpDest(Target,
672 EHStack.getInnermostNormalCleanup(),
673 NextCleanupDestIndex++);
John McCallf1549f62010-07-06 01:34:17 +0000674 }
Anders Carlssonc71c8452009-02-07 23:50:39 +0000675
John McCallf1549f62010-07-06 01:34:17 +0000676 /// The given basic block lies in the current EH scope, but may be a
677 /// target of a potentially scope-crossing jump; get a stable handle
678 /// to which we can perform this jump later.
Chris Lattner686775d2011-07-20 06:58:45 +0000679 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
John McCallff8e1152010-07-23 21:56:41 +0000680 return getJumpDestInCurrentScope(createBasicBlock(Name));
John McCallf1549f62010-07-06 01:34:17 +0000681 }
682
683 /// EmitBranchThroughCleanup - Emit a branch from the current insert
684 /// block through the normal cleanup handling code (if any) and then
685 /// on to \arg Dest.
686 void EmitBranchThroughCleanup(JumpDest Dest);
Chris Lattnerb11f9192011-04-17 00:54:30 +0000687
688 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
689 /// specified destination obviously has no cleanups to run. 'false' is always
690 /// a conservatively correct answer for this method.
691 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
John McCallf1549f62010-07-06 01:34:17 +0000692
John McCall777d6e52011-08-11 02:22:43 +0000693 /// popCatchScope - Pops the catch scope at the top of the EHScope
694 /// stack, emitting any required code (other than the catch handlers
695 /// themselves).
696 void popCatchScope();
John McCallff8e1152010-07-23 21:56:41 +0000697
David Chisnallc6860042012-11-07 16:50:40 +0000698 llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
John McCall777d6e52011-08-11 02:22:43 +0000699 llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
Mike Stump0dd9e882009-02-08 23:14:22 +0000700
John McCall150b4622011-01-26 04:00:11 +0000701 /// An object to manage conditionally-evaluated expressions.
702 class ConditionalEvaluation {
703 llvm::BasicBlock *StartBB;
Mike Stump1eb44332009-09-09 15:08:12 +0000704
John McCall150b4622011-01-26 04:00:11 +0000705 public:
706 ConditionalEvaluation(CodeGenFunction &CGF)
707 : StartBB(CGF.Builder.GetInsertBlock()) {}
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000708
John McCall150b4622011-01-26 04:00:11 +0000709 void begin(CodeGenFunction &CGF) {
710 assert(CGF.OutermostConditional != this);
711 if (!CGF.OutermostConditional)
712 CGF.OutermostConditional = this;
713 }
714
715 void end(CodeGenFunction &CGF) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700716 assert(CGF.OutermostConditional != nullptr);
John McCall150b4622011-01-26 04:00:11 +0000717 if (CGF.OutermostConditional == this)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700718 CGF.OutermostConditional = nullptr;
John McCall150b4622011-01-26 04:00:11 +0000719 }
720
721 /// Returns a block which will be executed prior to each
722 /// evaluation of the conditional code.
723 llvm::BasicBlock *getStartingBlock() const {
724 return StartBB;
725 }
726 };
Mike Stump1eb44332009-09-09 15:08:12 +0000727
John McCall3019c442010-09-17 00:50:28 +0000728 /// isInConditionalBranch - Return true if we're currently emitting
729 /// one branch or the other of a conditional expression.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700730 bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
John McCall150b4622011-01-26 04:00:11 +0000731
John McCall6f103ba2011-11-10 10:43:54 +0000732 void setBeforeOutermostConditional(llvm::Value *value, llvm::Value *addr) {
733 assert(isInConditionalBranch());
734 llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
735 new llvm::StoreInst(value, addr, &block->back());
736 }
737
John McCall150b4622011-01-26 04:00:11 +0000738 /// An RAII object to record that we're evaluating a statement
739 /// expression.
740 class StmtExprEvaluation {
741 CodeGenFunction &CGF;
742
743 /// We have to save the outermost conditional: cleanups in a
744 /// statement expression aren't conditional just because the
745 /// StmtExpr is.
746 ConditionalEvaluation *SavedOutermostConditional;
747
748 public:
749 StmtExprEvaluation(CodeGenFunction &CGF)
750 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700751 CGF.OutermostConditional = nullptr;
John McCall150b4622011-01-26 04:00:11 +0000752 }
753
754 ~StmtExprEvaluation() {
755 CGF.OutermostConditional = SavedOutermostConditional;
756 CGF.EnsureInsertPoint();
757 }
758 };
John McCalle996ffd2011-02-16 08:02:54 +0000759
John McCall56ca35d2011-02-17 10:25:35 +0000760 /// An object which temporarily prevents a value from being
761 /// destroyed by aggressive peephole optimizations that assume that
762 /// all uses of a value have been realized in the IR.
763 class PeepholeProtection {
764 llvm::Instruction *Inst;
765 friend class CodeGenFunction;
766
767 public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700768 PeepholeProtection() : Inst(nullptr) {}
John McCall4b9c2d22011-11-06 09:01:30 +0000769 };
John McCall56ca35d2011-02-17 10:25:35 +0000770
John McCall4b9c2d22011-11-06 09:01:30 +0000771 /// A non-RAII class containing all the information about a bound
772 /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for
773 /// this which makes individual mappings very simple; using this
774 /// class directly is useful when you have a variable number of
775 /// opaque values or don't want the RAII functionality for some
776 /// reason.
777 class OpaqueValueMappingData {
John McCalle996ffd2011-02-16 08:02:54 +0000778 const OpaqueValueExpr *OpaqueValue;
John McCall56ca35d2011-02-17 10:25:35 +0000779 bool BoundLValue;
780 CodeGenFunction::PeepholeProtection Protection;
John McCalle996ffd2011-02-16 08:02:54 +0000781
John McCall4b9c2d22011-11-06 09:01:30 +0000782 OpaqueValueMappingData(const OpaqueValueExpr *ov,
783 bool boundLValue)
784 : OpaqueValue(ov), BoundLValue(boundLValue) {}
John McCalle996ffd2011-02-16 08:02:54 +0000785 public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700786 OpaqueValueMappingData() : OpaqueValue(nullptr) {}
John McCall4b9c2d22011-11-06 09:01:30 +0000787
John McCall56ca35d2011-02-17 10:25:35 +0000788 static bool shouldBindAsLValue(const Expr *expr) {
John McCalla5493f82011-11-08 22:54:08 +0000789 // gl-values should be bound as l-values for obvious reasons.
790 // Records should be bound as l-values because IR generation
791 // always keeps them in memory. Expressions of function type
792 // act exactly like l-values but are formally required to be
793 // r-values in C.
794 return expr->isGLValue() ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700795 expr->getType()->isFunctionType() ||
796 hasAggregateEvaluationKind(expr->getType());
John McCall56ca35d2011-02-17 10:25:35 +0000797 }
798
John McCall4b9c2d22011-11-06 09:01:30 +0000799 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
800 const OpaqueValueExpr *ov,
801 const Expr *e) {
802 if (shouldBindAsLValue(ov))
803 return bind(CGF, ov, CGF.EmitLValue(e));
804 return bind(CGF, ov, CGF.EmitAnyExpr(e));
805 }
806
807 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
808 const OpaqueValueExpr *ov,
809 const LValue &lv) {
810 assert(shouldBindAsLValue(ov));
811 CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
812 return OpaqueValueMappingData(ov, true);
813 }
814
815 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
816 const OpaqueValueExpr *ov,
817 const RValue &rv) {
818 assert(!shouldBindAsLValue(ov));
819 CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
820
821 OpaqueValueMappingData data(ov, false);
822
823 // Work around an extremely aggressive peephole optimization in
824 // EmitScalarConversion which assumes that all other uses of a
825 // value are extant.
826 data.Protection = CGF.protectFromPeepholes(rv);
827
828 return data;
829 }
830
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700831 bool isValid() const { return OpaqueValue != nullptr; }
832 void clear() { OpaqueValue = nullptr; }
John McCall4b9c2d22011-11-06 09:01:30 +0000833
834 void unbind(CodeGenFunction &CGF) {
835 assert(OpaqueValue && "no data to unbind!");
836
837 if (BoundLValue) {
838 CGF.OpaqueLValues.erase(OpaqueValue);
839 } else {
840 CGF.OpaqueRValues.erase(OpaqueValue);
841 CGF.unprotectFromPeepholes(Protection);
842 }
843 }
844 };
845
846 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
847 class OpaqueValueMapping {
848 CodeGenFunction &CGF;
849 OpaqueValueMappingData Data;
850
851 public:
852 static bool shouldBindAsLValue(const Expr *expr) {
853 return OpaqueValueMappingData::shouldBindAsLValue(expr);
854 }
855
John McCall56ca35d2011-02-17 10:25:35 +0000856 /// Build the opaque value mapping for the given conditional
857 /// operator if it's the GNU ?: extension. This is a common
858 /// enough pattern that the convenience operator is really
859 /// helpful.
860 ///
861 OpaqueValueMapping(CodeGenFunction &CGF,
862 const AbstractConditionalOperator *op) : CGF(CGF) {
John McCall4b9c2d22011-11-06 09:01:30 +0000863 if (isa<ConditionalOperator>(op))
864 // Leave Data empty.
John McCall56ca35d2011-02-17 10:25:35 +0000865 return;
John McCall56ca35d2011-02-17 10:25:35 +0000866
867 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
John McCall4b9c2d22011-11-06 09:01:30 +0000868 Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
869 e->getCommon());
John McCall56ca35d2011-02-17 10:25:35 +0000870 }
871
John McCalle996ffd2011-02-16 08:02:54 +0000872 OpaqueValueMapping(CodeGenFunction &CGF,
873 const OpaqueValueExpr *opaqueValue,
John McCall56ca35d2011-02-17 10:25:35 +0000874 LValue lvalue)
John McCall4b9c2d22011-11-06 09:01:30 +0000875 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
John McCall56ca35d2011-02-17 10:25:35 +0000876 }
877
878 OpaqueValueMapping(CodeGenFunction &CGF,
879 const OpaqueValueExpr *opaqueValue,
880 RValue rvalue)
John McCall4b9c2d22011-11-06 09:01:30 +0000881 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
John McCalle996ffd2011-02-16 08:02:54 +0000882 }
883
884 void pop() {
John McCall4b9c2d22011-11-06 09:01:30 +0000885 Data.unbind(CGF);
886 Data.clear();
John McCalle996ffd2011-02-16 08:02:54 +0000887 }
888
889 ~OpaqueValueMapping() {
John McCall4b9c2d22011-11-06 09:01:30 +0000890 if (Data.isValid()) Data.unbind(CGF);
John McCalle996ffd2011-02-16 08:02:54 +0000891 }
892 };
Fariborz Jahaniane2204552010-11-16 19:29:39 +0000893
894 /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
895 /// number that holds the value.
896 unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
Fariborz Jahanian52a80e12011-01-26 23:08:27 +0000897
898 /// BuildBlockByrefAddress - Computes address location of the
899 /// variable which is declared as __block.
900 llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
901 const VarDecl *V);
Chris Lattner7f02f722007-08-24 05:35:26 +0000902private:
Chris Lattnerd9becd12009-10-28 23:59:40 +0000903 CGDebugInfo *DebugInfo;
Devang Patelaa112892011-03-07 18:45:56 +0000904 bool DisableDebugInfo;
Mike Stump09429b92009-02-17 17:00:02 +0000905
John McCall93c332a2011-05-28 21:13:02 +0000906 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
907 /// calling llvm.stacksave for multiple VLAs in the same scope.
908 bool DidCallStackSave;
909
Mike Stumpf71d2322009-11-30 20:08:49 +0000910 /// IndirectBranch - The first time an indirect goto is seen we create a block
911 /// with an indirect branch. Every time we see the address of a label taken,
912 /// we add the label to the indirect goto. Every subsequent indirect goto is
913 /// codegen'd as a jump to the IndirectBranch's basic block.
Chris Lattnerd9becd12009-10-28 23:59:40 +0000914 llvm::IndirectBrInst *IndirectBranch;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000915
Mike Stump0dd9e882009-02-08 23:14:22 +0000916 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
917 /// decls.
John McCall6b5a61b2011-02-07 10:33:21 +0000918 typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
919 DeclMapTy LocalDeclMap;
Reid Spencer5f016e22007-07-11 17:01:13 +0000920
921 /// LabelMap - This keeps track of the LLVM basic block for each C label.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000922 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
Mike Stump0dd9e882009-02-08 23:14:22 +0000923
Mike Stump0dd9e882009-02-08 23:14:22 +0000924 // BreakContinueStack - This keeps track of where break and continue
Anders Carlssone4b6d342009-02-10 05:52:02 +0000925 // statements should jump to.
Chris Lattnerda138702007-07-16 21:28:45 +0000926 struct BreakContinue {
John McCallf1549f62010-07-06 01:34:17 +0000927 BreakContinue(JumpDest Break, JumpDest Continue)
928 : BreakBlock(Break), ContinueBlock(Continue) {}
Mike Stump0dd9e882009-02-08 23:14:22 +0000929
John McCallf1549f62010-07-06 01:34:17 +0000930 JumpDest BreakBlock;
931 JumpDest ContinueBlock;
Mike Stump0dd9e882009-02-08 23:14:22 +0000932 };
Chris Lattner686775d2011-07-20 06:58:45 +0000933 SmallVector<BreakContinue, 8> BreakContinueStack;
Daniel Dunbar18ccc772008-09-28 01:03:14 +0000934
Stephen Hines651f13c2014-04-23 16:59:28 -0700935 CodeGenPGO PGO;
936
937public:
938 /// Get a counter for instrumentation of the region associated with the given
939 /// statement.
940 RegionCounter getPGORegionCounter(const Stmt *S) {
941 return RegionCounter(PGO, S);
942 }
943private:
944
Zhongxing Xu170fd492012-01-14 09:08:15 +0000945 /// SwitchInsn - This is nearest current switch instruction. It is null if
Mike Stump0dd9e882009-02-08 23:14:22 +0000946 /// current context is not in a switch.
Devang Patel51b09f22007-10-04 23:45:31 +0000947 llvm::SwitchInst *SwitchInsn;
Stephen Hines651f13c2014-04-23 16:59:28 -0700948 /// The branch weights of SwitchInsn when doing instrumentation based PGO.
949 SmallVector<uint64_t, 16> *SwitchWeights;
Devang Patel51b09f22007-10-04 23:45:31 +0000950
Mike Stump0dd9e882009-02-08 23:14:22 +0000951 /// CaseRangeBlock - This block holds if condition check for last case
Devang Patel80fd5f92007-10-09 17:08:50 +0000952 /// statement range in current switch instruction.
Devang Patelc049e4f2007-10-08 20:57:48 +0000953 llvm::BasicBlock *CaseRangeBlock;
954
John McCall56ca35d2011-02-17 10:25:35 +0000955 /// OpaqueLValues - Keeps track of the current set of opaque value
John McCalle996ffd2011-02-16 08:02:54 +0000956 /// expressions.
John McCall56ca35d2011-02-17 10:25:35 +0000957 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
958 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
John McCalle996ffd2011-02-16 08:02:54 +0000959
Mike Stump0dd9e882009-02-08 23:14:22 +0000960 // VLASizeMap - This keeps track of the associated size for each VLA type.
Eli Friedmanbbed6b92009-08-15 02:50:32 +0000961 // We track this by the size expression rather than the type itself because
962 // in certain situations, like a const qualifier applied to an VLA typedef,
963 // multiple VLA types can share the same size expression.
Mike Stump0dd9e882009-02-08 23:14:22 +0000964 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
965 // enter/leave scopes.
Eli Friedmanbbed6b92009-08-15 02:50:32 +0000966 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
Mike Stump0dd9e882009-02-08 23:14:22 +0000967
John McCallf1549f62010-07-06 01:34:17 +0000968 /// A block containing a single 'unreachable' instruction. Created
969 /// lazily by getUnreachableBlock().
970 llvm::BasicBlock *UnreachableBlock;
Mike Stump0dd9e882009-02-08 23:14:22 +0000971
Adrian Prantld072e592013-05-03 20:11:48 +0000972 /// Counts of the number return expressions in the function.
973 unsigned NumReturnExprs;
Adrian Prantlfa6b0792013-05-02 17:30:20 +0000974
975 /// Count the number of simple (constant) return expressions in the function.
976 unsigned NumSimpleReturnExprs;
977
Adrian Prantld072e592013-05-03 20:11:48 +0000978 /// The last regular (non-return) debug location (breakpoint) in the function.
979 SourceLocation LastStopPoint;
Adrian Prantlfa6b0792013-05-02 17:30:20 +0000980
Richard Smithc3bf52c2013-04-20 22:23:05 +0000981public:
982 /// A scope within which we are constructing the fields of an object which
983 /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
984 /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
985 class FieldConstructionScope {
986 public:
987 FieldConstructionScope(CodeGenFunction &CGF, llvm::Value *This)
988 : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
989 CGF.CXXDefaultInitExprThis = This;
990 }
991 ~FieldConstructionScope() {
992 CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
993 }
994
995 private:
996 CodeGenFunction &CGF;
997 llvm::Value *OldCXXDefaultInitExprThis;
998 };
999
1000 /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
1001 /// is overridden to be the object under construction.
1002 class CXXDefaultInitExprScope {
1003 public:
1004 CXXDefaultInitExprScope(CodeGenFunction &CGF)
1005 : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue) {
1006 CGF.CXXThisValue = CGF.CXXDefaultInitExprThis;
1007 }
1008 ~CXXDefaultInitExprScope() {
1009 CGF.CXXThisValue = OldCXXThisValue;
1010 }
1011
1012 public:
1013 CodeGenFunction &CGF;
1014 llvm::Value *OldCXXThisValue;
1015 };
1016
1017private:
Anders Carlssonf6c56e22009-11-25 03:15:49 +00001018 /// CXXThisDecl - When generating code for a C++ member function,
1019 /// this will hold the implicit 'this' declaration.
Eli Friedmancec5ebd2012-02-11 02:57:39 +00001020 ImplicitParamDecl *CXXABIThisDecl;
1021 llvm::Value *CXXABIThisValue;
John McCall25049412010-02-16 22:04:33 +00001022 llvm::Value *CXXThisValue;
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Richard Smithc3bf52c2013-04-20 22:23:05 +00001024 /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1025 /// this expression.
1026 llvm::Value *CXXDefaultInitExprThis;
1027
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001028 /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1029 /// destructor, this will hold the implicit argument (e.g. VTT).
1030 ImplicitParamDecl *CXXStructorImplicitParamDecl;
1031 llvm::Value *CXXStructorImplicitParamValue;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001032
John McCall150b4622011-01-26 04:00:11 +00001033 /// OutermostConditional - Points to the outermost active
1034 /// conditional control. This is used so that we know if a
1035 /// temporary should be destroyed conditionally.
1036 ConditionalEvaluation *OutermostConditional;
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Nadav Rotem495cfa42013-03-23 06:43:35 +00001038 /// The current lexical scope.
1039 LexicalScope *CurLexicalScope;
Anders Carlsson7dfa4072009-09-12 02:14:24 +00001040
Adrian Prantl1c3db762013-05-16 00:41:26 +00001041 /// The current source location that should be used for exception
1042 /// handling code.
1043 SourceLocation CurEHLocation;
1044
Anders Carlsson7dfa4072009-09-12 02:14:24 +00001045 /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
1046 /// type as well as the field number that contains the actual data.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001047 llvm::DenseMap<const ValueDecl *, std::pair<llvm::Type *,
Anders Carlsson7dfa4072009-09-12 02:14:24 +00001048 unsigned> > ByRefValueInfo;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001049
John McCallf1549f62010-07-06 01:34:17 +00001050 llvm::BasicBlock *TerminateLandingPad;
Mike Stump182f3832009-12-10 00:02:42 +00001051 llvm::BasicBlock *TerminateHandler;
Chris Lattner83252dc2010-07-20 21:07:09 +00001052 llvm::BasicBlock *TrapBB;
Eli Friedman94067052009-12-10 02:21:21 +00001053
Tanya Lattner0df579e2012-07-09 22:06:01 +00001054 /// Add a kernel metadata node to the named metadata node 'opencl.kernels'.
1055 /// In the kernel metadata node, reference the kernel function and metadata
1056 /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2):
Joey Gouly37453b92013-03-08 09:42:32 +00001057 /// - A node for the vec_type_hint(<type>) qualifier contains string
1058 /// "vec_type_hint", an undefined value of the <type> data type,
1059 /// and a Boolean that is true if the <type> is integer and signed.
Tanya Lattner0df579e2012-07-09 22:06:01 +00001060 /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string
1061 /// "work_group_size_hint", and three 32-bit integers X, Y and Z.
1062 /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string
1063 /// "reqd_work_group_size", and three 32-bit integers X, Y and Z.
1064 void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1065 llvm::Function *Fn);
1066
Reid Spencer5f016e22007-07-11 17:01:13 +00001067public:
Fariborz Jahanian4904bf42012-06-26 16:06:38 +00001068 CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
John McCall1a343eb2011-11-10 08:15:53 +00001069 ~CodeGenFunction();
Mike Stump0dd9e882009-02-08 23:14:22 +00001070
John McCall1e7fe752010-09-02 09:58:18 +00001071 CodeGenTypes &getTypes() const { return CGM.getTypes(); }
John McCallf2aac842011-05-15 02:34:36 +00001072 ASTContext &getContext() const { return CGM.getContext(); }
Devang Patelaa112892011-03-07 18:45:56 +00001073 CGDebugInfo *getDebugInfo() {
1074 if (DisableDebugInfo)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001075 return nullptr;
Devang Patelaa112892011-03-07 18:45:56 +00001076 return DebugInfo;
1077 }
1078 void disableDebugInfo() { DisableDebugInfo = true; }
1079 void enableDebugInfo() { DisableDebugInfo = false; }
1080
John McCallf85e1932011-06-15 23:02:42 +00001081 bool shouldUseFusedARCCalls() {
1082 return CGM.getCodeGenOpts().OptimizationLevel == 0;
1083 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001084
David Blaikie4e4d0842012-03-11 07:00:24 +00001085 const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
John McCalld16c2cf2011-02-08 08:22:06 +00001086
Bill Wendling285cfd82011-09-19 20:31:14 +00001087 /// Returns a pointer to the function's exception object and selector slot,
1088 /// which is assigned in every landing pad.
John McCallf1549f62010-07-06 01:34:17 +00001089 llvm::Value *getExceptionSlot();
John McCall93c332a2011-05-28 21:13:02 +00001090 llvm::Value *getEHSelectorSlot();
John McCallf1549f62010-07-06 01:34:17 +00001091
Bill Wendlingae270592011-09-15 18:57:19 +00001092 /// Returns the contents of the function's exception object and selector
1093 /// slots.
1094 llvm::Value *getExceptionFromSlot();
1095 llvm::Value *getSelectorFromSlot();
1096
John McCallff8e1152010-07-23 21:56:41 +00001097 llvm::Value *getNormalCleanupDestSlot();
John McCallff8e1152010-07-23 21:56:41 +00001098
John McCallf1549f62010-07-06 01:34:17 +00001099 llvm::BasicBlock *getUnreachableBlock() {
1100 if (!UnreachableBlock) {
1101 UnreachableBlock = createBasicBlock("unreachable");
1102 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1103 }
1104 return UnreachableBlock;
1105 }
1106
1107 llvm::BasicBlock *getInvokeDest() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001108 if (!EHStack.requiresLandingPad()) return nullptr;
John McCallf1549f62010-07-06 01:34:17 +00001109 return getInvokeDestImpl();
1110 }
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001111
John McCall64aa4b32013-04-16 22:48:15 +00001112 const TargetInfo &getTarget() const { return Target; }
John McCalld16c2cf2011-02-08 08:22:06 +00001113 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
Owen Anderson69243822009-07-13 04:10:07 +00001114
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001115 //===--------------------------------------------------------------------===//
John McCallbdc4d802011-07-09 01:37:26 +00001116 // Cleanups
1117 //===--------------------------------------------------------------------===//
1118
1119 typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty);
1120
John McCall2673c682011-07-11 08:38:19 +00001121 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1122 llvm::Value *arrayEndPointer,
1123 QualType elementType,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001124 Destroyer *destroyer);
John McCall2673c682011-07-11 08:38:19 +00001125 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1126 llvm::Value *arrayEnd,
1127 QualType elementType,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001128 Destroyer *destroyer);
John McCallbdc4d802011-07-09 01:37:26 +00001129
John McCall9928c482011-07-12 16:41:08 +00001130 void pushDestroy(QualType::DestructionKind dtorKind,
1131 llvm::Value *addr, QualType type);
John McCall074cae02013-02-01 05:11:40 +00001132 void pushEHDestroy(QualType::DestructionKind dtorKind,
1133 llvm::Value *addr, QualType type);
John McCallbdc4d802011-07-09 01:37:26 +00001134 void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001135 Destroyer *destroyer, bool useEHCleanupForArray);
Richard Smith8a07cd32013-06-12 20:42:33 +00001136 void pushLifetimeExtendedDestroy(CleanupKind kind, llvm::Value *addr,
1137 QualType type, Destroyer *destroyer,
1138 bool useEHCleanupForArray);
Stephen Hines176edba2014-12-01 14:53:08 -08001139 void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1140 llvm::Value *CompletePtr,
1141 QualType ElementType);
Stephen Hines651f13c2014-04-23 16:59:28 -07001142 void pushStackRestore(CleanupKind kind, llvm::Value *SPMem);
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001143 void emitDestroy(llvm::Value *addr, QualType type, Destroyer *destroyer,
John McCall2673c682011-07-11 08:38:19 +00001144 bool useEHCleanupForArray);
David Blaikiec7971a92013-08-27 23:57:18 +00001145 llvm::Function *generateDestroyHelper(llvm::Constant *addr, QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001146 Destroyer *destroyer,
David Blaikiec7971a92013-08-27 23:57:18 +00001147 bool useEHCleanupForArray,
1148 const VarDecl *VD);
John McCallbdc4d802011-07-09 01:37:26 +00001149 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001150 QualType type, Destroyer *destroyer,
John McCallfbf780a2011-07-13 08:09:46 +00001151 bool checkZeroLength, bool useEHCleanup);
John McCallbdc4d802011-07-09 01:37:26 +00001152
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001153 Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
John McCall9928c482011-07-12 16:41:08 +00001154
John McCallbdc4d802011-07-09 01:37:26 +00001155 /// Determines whether an EH cleanup is required to destroy a type
1156 /// with the given destruction kind.
1157 bool needsEHCleanup(QualType::DestructionKind kind) {
1158 switch (kind) {
1159 case QualType::DK_none:
1160 return false;
1161 case QualType::DK_cxx_destructor:
1162 case QualType::DK_objc_weak_lifetime:
David Blaikie4e4d0842012-03-11 07:00:24 +00001163 return getLangOpts().Exceptions;
John McCallbdc4d802011-07-09 01:37:26 +00001164 case QualType::DK_objc_strong_lifetime:
David Blaikie4e4d0842012-03-11 07:00:24 +00001165 return getLangOpts().Exceptions &&
John McCallbdc4d802011-07-09 01:37:26 +00001166 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1167 }
1168 llvm_unreachable("bad destruction kind");
1169 }
1170
John McCall9928c482011-07-12 16:41:08 +00001171 CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1172 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1173 }
1174
John McCallbdc4d802011-07-09 01:37:26 +00001175 //===--------------------------------------------------------------------===//
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001176 // Objective-C
1177 //===--------------------------------------------------------------------===//
1178
Chris Lattner391d77a2008-03-30 23:03:07 +00001179 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001180
Mike Stump0dd9e882009-02-08 23:14:22 +00001181 void StartObjCMethod(const ObjCMethodDecl *MD,
Devang Patel8d3f8972011-05-19 23:37:41 +00001182 const ObjCContainerDecl *CD,
1183 SourceLocation StartLoc);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001184
Mike Stump0dd9e882009-02-08 23:14:22 +00001185 /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +00001186 void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1187 const ObjCPropertyImplDecl *PID);
John McCall1e1f4872011-09-13 03:34:09 +00001188 void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
Fariborz Jahanianb6e5fe32012-01-07 18:56:22 +00001189 const ObjCPropertyImplDecl *propImpl,
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001190 const ObjCMethodDecl *GetterMothodDecl,
Fariborz Jahanianb6e5fe32012-01-07 18:56:22 +00001191 llvm::Constant *AtomicHelperFn);
Fariborz Jahanian2846b972011-02-18 19:15:13 +00001192
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00001193 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1194 ObjCMethodDecl *MD, bool ctor);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001195
Mike Stump0dd9e882009-02-08 23:14:22 +00001196 /// GenerateObjCSetter - Synthesize an Objective-C property setter function
1197 /// for the given property.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +00001198 void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1199 const ObjCPropertyImplDecl *PID);
John McCall71c758d2011-09-10 09:17:20 +00001200 void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
Fariborz Jahaniancd93b962012-01-06 22:33:54 +00001201 const ObjCPropertyImplDecl *propImpl,
1202 llvm::Constant *AtomicHelperFn);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +00001203 bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
Fariborz Jahanian15bd5882010-04-13 18:32:24 +00001204 bool IvarTypeWithAggrGCObjects(QualType Ty);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001205
Mike Stump4e7a1f72009-02-21 20:00:35 +00001206 //===--------------------------------------------------------------------===//
1207 // Block Bits
1208 //===--------------------------------------------------------------------===//
1209
John McCall6b5a61b2011-02-07 10:33:21 +00001210 llvm::Value *EmitBlockLiteral(const BlockExpr *);
John McCall1a343eb2011-11-10 08:15:53 +00001211 llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
1212 static void destroyBlockInfos(CGBlockInfo *info);
Blaine Garst2a7eb282010-02-23 21:51:17 +00001213 llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001214 const CGBlockInfo &Info,
Chris Lattner2acc6e32011-07-18 04:24:23 +00001215 llvm::StructType *,
John McCalld16c2cf2011-02-08 08:22:06 +00001216 llvm::Constant *BlockVarLayout);
Mike Stump4e7a1f72009-02-21 20:00:35 +00001217
Fariborz Jahanian564360b2010-06-24 00:08:06 +00001218 llvm::Function *GenerateBlockFunction(GlobalDecl GD,
John McCall6b5a61b2011-02-07 10:33:21 +00001219 const CGBlockInfo &Info,
Eli Friedman64bee652012-02-25 02:48:22 +00001220 const DeclMapTy &ldm,
1221 bool IsLambdaConversionToBlock);
Mike Stump4e7a1f72009-02-21 20:00:35 +00001222
John McCalld16c2cf2011-02-08 08:22:06 +00001223 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1224 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001225 llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
1226 const ObjCPropertyImplDecl *PID);
1227 llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
1228 const ObjCPropertyImplDecl *PID);
Eli Friedmancae40c42012-02-28 01:08:45 +00001229 llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
John McCalld16c2cf2011-02-08 08:22:06 +00001230
John McCalld16c2cf2011-02-08 08:22:06 +00001231 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1232
John McCall5af02db2011-03-31 01:59:53 +00001233 class AutoVarEmission;
1234
1235 void emitByrefStructureInit(const AutoVarEmission &emission);
1236 void enterByrefCleanup(const AutoVarEmission &emission);
1237
John McCall6b5a61b2011-02-07 10:33:21 +00001238 llvm::Value *LoadBlockStruct() {
1239 assert(BlockPointer && "no block pointer set!");
1240 return BlockPointer;
1241 }
Mike Stump4e7a1f72009-02-21 20:00:35 +00001242
John McCallea1471e2010-05-20 01:18:31 +00001243 void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
John McCallf4b88a42012-03-10 09:33:50 +00001244 void AllocateBlockDecl(const DeclRefExpr *E);
John McCall6b5a61b2011-02-07 10:33:21 +00001245 llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001246 llvm::Type *BuildByRefType(const VarDecl *var);
Mike Stumpdab514f2009-03-04 03:23:46 +00001247
John McCalld26bc762011-03-09 04:27:21 +00001248 void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1249 const CGFunctionInfo &FnInfo);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001250 /// \brief Emit code for the start of a function.
1251 /// \param Loc The location to be associated with the function.
1252 /// \param StartLoc The location of the function body.
John McCallf5ebf9b2013-05-03 07:33:41 +00001253 void StartFunction(GlobalDecl GD,
1254 QualType RetTy,
Daniel Dunbar7c086512008-09-09 23:14:03 +00001255 llvm::Function *Fn,
John McCalld26bc762011-03-09 04:27:21 +00001256 const CGFunctionInfo &FnInfo,
Daniel Dunbar2284ac92008-10-18 18:22:23 +00001257 const FunctionArgList &Args,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001258 SourceLocation Loc = SourceLocation(),
1259 SourceLocation StartLoc = SourceLocation());
Daniel Dunbara448fb22008-11-11 23:11:34 +00001260
John McCall9fc6a772010-02-19 09:25:03 +00001261 void EmitConstructorBody(FunctionArgList &Args);
1262 void EmitDestructorBody(FunctionArgList &Args);
Lang Hames56c00c42013-02-17 07:22:09 +00001263 void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
Richard Smith3cebc732013-11-05 09:12:18 +00001264 void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body);
Stephen Hines651f13c2014-04-23 16:59:28 -07001265 void EmitBlockWithFallThrough(llvm::BasicBlock *BB, RegionCounter &Cnt);
John McCalla355e072010-02-18 03:17:58 +00001266
Faisal Valid6992ab2013-09-29 08:45:24 +00001267 void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
Eli Friedman64bee652012-02-25 02:48:22 +00001268 CallArgList &CallArgs);
Eli Friedmanbd89f8c2012-02-16 01:37:33 +00001269 void EmitLambdaToBlockPointerBody(FunctionArgList &Args);
Eli Friedman64bee652012-02-25 02:48:22 +00001270 void EmitLambdaBlockInvokeBody();
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001271 void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
1272 void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD);
Stephen Hines176edba2014-12-01 14:53:08 -08001273 void EmitAsanPrologueOrEpilogue(bool Prologue);
Eli Friedmanbd89f8c2012-02-16 01:37:33 +00001274
Mike Stump0dd9e882009-02-08 23:14:22 +00001275 /// EmitReturnBlock - Emit the unified return block, trying to avoid its
1276 /// emission when possible.
David Blaikie0a0f93c2013-02-01 19:09:49 +00001277 void EmitReturnBlock();
Daniel Dunbar1c1d6072009-01-26 23:27:52 +00001278
Mike Stump0dd9e882009-02-08 23:14:22 +00001279 /// FinishFunction - Complete IR generation of the current function. It is
1280 /// legal to call this function even if there is no current insertion point.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001281 void FinishFunction(SourceLocation EndLoc=SourceLocation());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001282
Hans Wennborg93b717a2013-11-15 17:24:45 +00001283 void StartThunk(llvm::Function *Fn, GlobalDecl GD, const CGFunctionInfo &FnInfo);
1284
Stephen Hines176edba2014-12-01 14:53:08 -08001285 void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk);
1286
1287 /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
1288 void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr,
1289 llvm::Value *Callee);
Hans Wennborg93b717a2013-11-15 17:24:45 +00001290
Anders Carlsson519c3282010-03-24 00:39:18 +00001291 /// GenerateThunk - Generate a thunk for the given method.
John McCalld26bc762011-03-09 04:27:21 +00001292 void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1293 GlobalDecl GD, const ThunkInfo &Thunk);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001294
Eli Friedman7dcdf5b2011-05-06 17:27:27 +00001295 void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1296 GlobalDecl GD, const ThunkInfo &Thunk);
1297
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001298 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1299 FunctionArgList &Args);
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Eli Friedmanb74ed082012-02-14 02:31:03 +00001301 void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init,
1302 ArrayRef<VarDecl *> ArrayIndexes);
1303
Anders Carlssond103f9f2010-03-28 19:40:00 +00001304 /// InitializeVTablePointer - Initialize the vtable pointer of the given
1305 /// subobject.
1306 ///
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001307 void InitializeVTablePointer(BaseSubobject Base,
Anders Carlssonb3b772e2010-04-20 05:22:15 +00001308 const CXXRecordDecl *NearestVBase,
Ken Dyckd6fb21f2011-03-23 01:04:18 +00001309 CharUnits OffsetFromNearestVBase,
Anders Carlssond103f9f2010-03-28 19:40:00 +00001310 const CXXRecordDecl *VTableClass);
1311
1312 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001313 void InitializeVTablePointers(BaseSubobject Base,
Anders Carlssonb3b772e2010-04-20 05:22:15 +00001314 const CXXRecordDecl *NearestVBase,
Ken Dyckd6fb21f2011-03-23 01:04:18 +00001315 CharUnits OffsetFromNearestVBase,
Anders Carlsson603d6d12010-03-28 21:07:49 +00001316 bool BaseIsNonVirtualPrimaryBase,
Anders Carlsson603d6d12010-03-28 21:07:49 +00001317 const CXXRecordDecl *VTableClass,
1318 VisitedVirtualBasesSetTy& VBases);
Eli Friedman77a259c2009-12-08 06:46:18 +00001319
Anders Carlsson603d6d12010-03-28 21:07:49 +00001320 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
Anders Carlssond103f9f2010-03-28 19:40:00 +00001321
Dan Gohman043fb9a2010-10-26 18:44:08 +00001322 /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1323 /// to by This.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001324 llvm::Value *GetVTablePtr(llvm::Value *This, llvm::Type *Ty);
Anders Carlssond103f9f2010-03-28 19:40:00 +00001325
Benjamin Kramer9581ed02013-08-25 22:46:27 +00001326
1327 /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
1328 /// expr can be devirtualized.
1329 bool CanDevirtualizeMemberFunctionCall(const Expr *Base,
1330 const CXXMethodDecl *MD);
1331
John McCall50da2ca2010-07-21 05:30:47 +00001332 /// EnterDtorCleanups - Enter the cleanups necessary to complete the
1333 /// given phase of destruction for a destructor. The end result
1334 /// should call destructors on members and base classes in reverse
1335 /// order of their construction.
1336 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Chris Lattner7255a2d2010-06-22 00:03:40 +00001338 /// ShouldInstrumentFunction - Return true if the current function should be
1339 /// instrumented with __cyg_profile_func_* calls
1340 bool ShouldInstrumentFunction();
1341
1342 /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1343 /// instrumentation function with the current function and the call site, if
1344 /// function instrumentation is enabled.
1345 void EmitFunctionInstrumentation(const char *Fn);
1346
Roman Divackybe4c8702011-02-10 16:52:03 +00001347 /// EmitMCountInstrumentation - Emit call to .mcount.
1348 void EmitMCountInstrumentation();
1349
Mike Stump0dd9e882009-02-08 23:14:22 +00001350 /// EmitFunctionProlog - Emit the target specific LLVM code to load the
1351 /// arguments for the given function. This is also responsible for naming the
1352 /// LLVM function arguments.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001353 void EmitFunctionProlog(const CGFunctionInfo &FI,
1354 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001355 const FunctionArgList &Args);
1356
Mike Stump0dd9e882009-02-08 23:14:22 +00001357 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
1358 /// given temporary.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001359 void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
1360 SourceLocation EndLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001361
Mike Stumpcce3d4f2009-12-07 23:38:24 +00001362 /// EmitStartEHSpec - Emit the start of the exception spec.
1363 void EmitStartEHSpec(const Decl *D);
1364
1365 /// EmitEndEHSpec - Emit the end of the exception spec.
1366 void EmitEndEHSpec(const Decl *D);
1367
John McCallf1549f62010-07-06 01:34:17 +00001368 /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1369 llvm::BasicBlock *getTerminateLandingPad();
1370
1371 /// getTerminateHandler - Return a handler (not a landing pad, just
1372 /// a catch handler) that just calls terminate. This is used when
1373 /// a terminate scope encloses a try.
Mike Stump9b39c512009-12-09 22:59:31 +00001374 llvm::BasicBlock *getTerminateHandler();
1375
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001376 llvm::Type *ConvertTypeForMem(QualType T);
1377 llvm::Type *ConvertType(QualType T);
1378 llvm::Type *ConvertType(const TypeDecl *T) {
John McCallbff225e2010-02-16 04:15:37 +00001379 return ConvertType(getContext().getTypeDeclType(T));
1380 }
Chris Lattnerc8aa5f12008-04-04 04:07:35 +00001381
Mike Stump0dd9e882009-02-08 23:14:22 +00001382 /// LoadObjCSelf - Load the value of self. This function is only valid while
1383 /// generating code for an Objective-C method.
Chris Lattnerc8aa5f12008-04-04 04:07:35 +00001384 llvm::Value *LoadObjCSelf();
Mike Stump0dd9e882009-02-08 23:14:22 +00001385
1386 /// TypeOfSelfObject - Return type of object that this self represents.
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001387 QualType TypeOfSelfObject();
Chris Lattner41110242008-06-17 18:05:57 +00001388
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 /// hasAggregateLLVMType - Return true if the specified AST type will map into
1390 /// an aggregate LLVM type or is void.
John McCall9d232c82013-03-07 21:37:08 +00001391 static TypeEvaluationKind getEvaluationKind(QualType T);
1392
1393 static bool hasScalarEvaluationKind(QualType T) {
1394 return getEvaluationKind(T) == TEK_Scalar;
1395 }
1396
1397 static bool hasAggregateEvaluationKind(QualType T) {
1398 return getEvaluationKind(T) == TEK_Aggregate;
1399 }
Daniel Dunbar55e87422008-11-11 02:29:29 +00001400
1401 /// createBasicBlock - Create an LLVM basic block.
Richard Smith4def70d2012-10-09 19:52:38 +00001402 llvm::BasicBlock *createBasicBlock(const Twine &name = "",
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001403 llvm::Function *parent = nullptr,
1404 llvm::BasicBlock *before = nullptr) {
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001405#ifdef NDEBUG
John McCalld16c2cf2011-02-08 08:22:06 +00001406 return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001407#else
John McCalld16c2cf2011-02-08 08:22:06 +00001408 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001409#endif
Daniel Dunbar55e87422008-11-11 02:29:29 +00001410 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001411
Reid Spencer5f016e22007-07-11 17:01:13 +00001412 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
1413 /// label maps to.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001414 JumpDest getJumpDestForLabel(const LabelDecl *S);
Mike Stump0dd9e882009-02-08 23:14:22 +00001415
Mike Stumpf71d2322009-11-30 20:08:49 +00001416 /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1417 /// another basic block, simplify it. This assumes that no other code could
1418 /// potentially reference the basic block.
Daniel Dunbaraa5bd872009-04-01 04:37:47 +00001419 void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1420
Mike Stump0dd9e882009-02-08 23:14:22 +00001421 /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
1422 /// adding a fall-through branch from the current insert block if
1423 /// necessary. It is legal to call this function even if there is no current
1424 /// insertion point.
Daniel Dunbara0c21a82008-11-13 01:24:05 +00001425 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00001426 /// IsFinished - If true, indicates that the caller has finished emitting
1427 /// branches to the given block and does not expect to emit code into it. This
1428 /// means the block can be ignored if it is unreachable.
Daniel Dunbara0c21a82008-11-13 01:24:05 +00001429 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
Daniel Dunbar824e3bd2008-11-11 04:34:23 +00001430
John McCall777d6e52011-08-11 02:22:43 +00001431 /// EmitBlockAfterUses - Emit the given block somewhere hopefully
1432 /// near its uses, and leave the insertion point in it.
1433 void EmitBlockAfterUses(llvm::BasicBlock *BB);
1434
Mike Stump0dd9e882009-02-08 23:14:22 +00001435 /// EmitBranch - Emit a branch to the specified basic block from the current
1436 /// insert block, taking care to avoid creation of branches from dummy
1437 /// blocks. It is legal to call this function even if there is no current
1438 /// insertion point.
Daniel Dunbar5e08ad32008-11-11 22:06:59 +00001439 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00001440 /// This function clears the current insertion point. The caller should follow
1441 /// calls to this function with calls to Emit*Block prior to generation new
1442 /// code.
Daniel Dunbard57a8712008-11-11 09:41:28 +00001443 void EmitBranch(llvm::BasicBlock *Block);
1444
Mike Stump0dd9e882009-02-08 23:14:22 +00001445 /// HaveInsertPoint - True if an insertion point is defined. If not, this
1446 /// indicates that the current code being emitted is unreachable.
1447 bool HaveInsertPoint() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001448 return Builder.GetInsertBlock() != nullptr;
Daniel Dunbara448fb22008-11-11 23:11:34 +00001449 }
1450
Mike Stump0dd9e882009-02-08 23:14:22 +00001451 /// EnsureInsertPoint - Ensure that an insertion point is defined so that
1452 /// emitted IR has a place to go. Note that by definition, if this function
1453 /// creates a block then that block is unreachable; callers may do better to
1454 /// detect when no insertion point is defined and simply skip IR generation.
Daniel Dunbara448fb22008-11-11 23:11:34 +00001455 void EnsureInsertPoint() {
1456 if (!HaveInsertPoint())
1457 EmitBlock(createBasicBlock());
1458 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001459
Daniel Dunbar488e9932008-08-16 00:56:44 +00001460 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerdc5e8262007-12-02 01:43:38 +00001461 /// specified stmt yet.
David Blaikie0a1c8622013-08-19 21:02:26 +00001462 void ErrorUnsupported(const Stmt *S, const char *Type);
Reid Spencer5f016e22007-07-11 17:01:13 +00001463
1464 //===--------------------------------------------------------------------===//
1465 // Helpers
1466 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00001467
Eli Friedman6da2c712011-12-03 04:14:32 +00001468 LValue MakeAddrLValue(llvm::Value *V, QualType T,
1469 CharUnits Alignment = CharUnits()) {
Dan Gohman3d5aff52010-10-14 23:06:10 +00001470 return LValue::MakeAddr(V, T, Alignment, getContext(),
1471 CGM.getTBAAInfo(T));
Daniel Dunbar5cf8bfe2010-08-21 02:53:44 +00001472 }
John McCalle0c11682012-07-02 23:58:38 +00001473
Stephen Hines176edba2014-12-01 14:53:08 -08001474 LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
Daniel Dunbar5cf8bfe2010-08-21 02:53:44 +00001475
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
Daniel Dunbar195337d2010-02-09 02:48:28 +00001477 /// block. The caller is responsible for setting an appropriate alignment on
1478 /// the alloca.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001479 llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
Chris Lattner8cc488f2011-07-20 07:06:53 +00001480 const Twine &Name = "tmp");
Mike Stump0dd9e882009-02-08 23:14:22 +00001481
John McCallac418162010-04-22 01:10:34 +00001482 /// InitTempAlloca - Provide an initial value for the given alloca.
1483 void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1484
Daniel Dunbar9bd4da22010-02-16 19:44:13 +00001485 /// CreateIRTemp - Create a temporary IR object of the given type, with
1486 /// appropriate alignment. This routine should only be used when an temporary
1487 /// value needs to be stored into an alloca (for example, to avoid explicit
1488 /// PHI construction), but the type is the IR type, not the type appropriate
1489 /// for storing in memory.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001490 llvm::AllocaInst *CreateIRTemp(QualType T, const Twine &Name = "tmp");
Daniel Dunbar9bd4da22010-02-16 19:44:13 +00001491
Daniel Dunbar195337d2010-02-09 02:48:28 +00001492 /// CreateMemTemp - Create a temporary memory object of the given type, with
1493 /// appropriate alignment.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001494 llvm::AllocaInst *CreateMemTemp(QualType T, const Twine &Name = "tmp");
Daniel Dunbar195337d2010-02-09 02:48:28 +00001495
John McCall558d2ab2010-09-15 10:14:12 +00001496 /// CreateAggTemp - Create a temporary memory object for the given
1497 /// aggregate type.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001498 AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
Eli Friedmand7722d92011-12-03 02:13:40 +00001499 CharUnits Alignment = getContext().getTypeAlignInChars(T);
Eli Friedmanf3940782011-12-03 00:54:26 +00001500 return AggValueSlot::forAddr(CreateMemTemp(T, Name), Alignment,
1501 T.getQualifiers(),
John McCall7c2349b2011-08-25 20:40:09 +00001502 AggValueSlot::IsNotDestructed,
John McCall410ffb22011-08-25 23:04:34 +00001503 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier649b4a12012-03-29 17:37:10 +00001504 AggValueSlot::IsNotAliased);
John McCall558d2ab2010-09-15 10:14:12 +00001505 }
1506
Stephen Hines651f13c2014-04-23 16:59:28 -07001507 /// CreateInAllocaTmp - Create a temporary memory object for the given
1508 /// aggregate type.
1509 AggValueSlot CreateInAllocaTmp(QualType T, const Twine &Name = "inalloca");
1510
John McCalld16c2cf2011-02-08 08:22:06 +00001511 /// Emit a cast to void* in the appropriate address space.
1512 llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1513
Reid Spencer5f016e22007-07-11 17:01:13 +00001514 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
1515 /// expression and compare the result against zero, returning an Int1Ty value.
1516 llvm::Value *EvaluateExprAsBool(const Expr *E);
1517
John McCall2a416372010-12-05 02:00:02 +00001518 /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
1519 void EmitIgnoredExpr(const Expr *E);
1520
Chris Lattner9b655512007-08-31 22:49:20 +00001521 /// EmitAnyExpr - Emit code to compute the specified expression which can have
1522 /// any type. The result is returned as an RValue struct. If this is an
1523 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
1524 /// the result should be returned.
Mike Stump49d1cd52009-05-26 22:03:21 +00001525 ///
Dmitri Gribenko70517ca2012-08-23 17:58:28 +00001526 /// \param ignoreResult True if the resulting value isn't used.
John McCall558d2ab2010-09-15 10:14:12 +00001527 RValue EmitAnyExpr(const Expr *E,
John McCalle0c11682012-07-02 23:58:38 +00001528 AggValueSlot aggSlot = AggValueSlot::ignored(),
1529 bool ignoreResult = false);
Devang Pateld9363c32007-09-28 21:49:18 +00001530
Mike Stump0dd9e882009-02-08 23:14:22 +00001531 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
1532 // or the value of the expression, depending on how va_list is defined.
Eli Friedman4fd0aa52009-01-20 17:46:04 +00001533 llvm::Value *EmitVAListRef(const Expr *E);
1534
Mike Stump0dd9e882009-02-08 23:14:22 +00001535 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
1536 /// always be accessible even if no aggregate location is provided.
John McCall558d2ab2010-09-15 10:14:12 +00001537 RValue EmitAnyExprToTemp(const Expr *E);
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001538
John McCall60d33652011-03-08 09:11:50 +00001539 /// EmitAnyExprToMem - Emits the code necessary to evaluate an
Chad Rosier649b4a12012-03-29 17:37:10 +00001540 /// arbitrary expression into the given memory location.
John McCall3d3ec1c2010-04-21 10:05:39 +00001541 void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
Chad Rosier649b4a12012-03-29 17:37:10 +00001542 Qualifiers Quals, bool IsInitializer);
John McCall3d3ec1c2010-04-21 10:05:39 +00001543
John McCall60d33652011-03-08 09:11:50 +00001544 /// EmitExprAsInit - Emits the code necessary to initialize a
1545 /// location in memory with the given initializer.
John McCallf85e1932011-06-15 23:02:42 +00001546 void EmitExprAsInit(const Expr *init, const ValueDecl *D,
John McCalla07398e2011-06-16 04:16:24 +00001547 LValue lvalue, bool capturedByInit);
John McCall60d33652011-03-08 09:11:50 +00001548
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +00001549 /// hasVolatileMember - returns true if aggregate type has a volatile
1550 /// member.
1551 bool hasVolatileMember(QualType T) {
1552 if (const RecordType *RT = T->getAs<RecordType>()) {
1553 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
1554 return RD->hasVolatileMember();
1555 }
1556 return false;
1557 }
Arnaud A. de Grandmaison8fcbb8d2013-02-05 09:06:17 +00001558 /// EmitAggregateCopy - Emit an aggregate assignment.
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001559 ///
1560 /// The difference to EmitAggregateCopy is that tail padding is not copied.
1561 /// This is required for correctness when assigning non-POD structures in C++.
1562 void EmitAggregateAssign(llvm::Value *DestPtr, llvm::Value *SrcPtr,
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +00001563 QualType EltTy) {
1564 bool IsVolatile = hasVolatileMember(EltTy);
1565 EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, CharUnits::Zero(),
1566 true);
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001567 }
1568
Arnaud A. de Grandmaison8fcbb8d2013-02-05 09:06:17 +00001569 /// EmitAggregateCopy - Emit an aggregate copy.
Mike Stump27fe2e62009-05-23 22:29:41 +00001570 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001571 /// \param isVolatile - True iff either the source or the destination is
Mike Stump27fe2e62009-05-23 22:29:41 +00001572 /// volatile.
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001573 /// \param isAssignment - If false, allow padding to be copied. This often
1574 /// yields more efficient.
Daniel Dunbar7482d122008-09-09 20:49:46 +00001575 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
Eli Friedmanbd7d8282011-12-05 22:23:28 +00001576 QualType EltTy, bool isVolatile=false,
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001577 CharUnits Alignment = CharUnits::Zero(),
1578 bool isAssignment = false);
Daniel Dunbar7482d122008-09-09 20:49:46 +00001579
Devang Patel51b09f22007-10-04 23:45:31 +00001580 /// StartBlock - Start new block named N. If insert block is a dummy block
1581 /// then reuse it.
1582 void StartBlock(const char *N);
1583
Anders Carlssondde0a942008-09-11 09:15:33 +00001584 /// GetAddrOfLocalVar - Return the address of a local variable.
John McCall4c40d982010-08-31 07:33:07 +00001585 llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
1586 llvm::Value *Res = LocalDeclMap[VD];
1587 assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
1588 return Res;
1589 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001590
John McCall56ca35d2011-02-17 10:25:35 +00001591 /// getOpaqueLValueMapping - Given an opaque value expression (which
1592 /// must be mapped to an l-value), return its mapping.
1593 const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
1594 assert(OpaqueValueMapping::shouldBindAsLValue(e));
1595
1596 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
1597 it = OpaqueLValues.find(e);
1598 assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
1599 return it->second;
1600 }
1601
1602 /// getOpaqueRValueMapping - Given an opaque value expression (which
1603 /// must be mapped to an r-value), return its mapping.
1604 const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
1605 assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1606
1607 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
1608 it = OpaqueRValues.find(e);
1609 assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
John McCalle996ffd2011-02-16 08:02:54 +00001610 return it->second;
1611 }
1612
Dan Gohman4f8d1232008-05-22 00:50:06 +00001613 /// getAccessedFieldNo - Given an encoded value and a result number, return
1614 /// the input field number being accessed.
1615 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
1616
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001617 llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
Chris Lattner3d00fdc2009-10-13 06:55:33 +00001618 llvm::BasicBlock *GetIndirectGotoBlock();
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00001619
Anders Carlsson1884eb02010-05-22 17:35:42 +00001620 /// EmitNullInitialization - Generate code to set a value of the given type to
1621 /// null, If the type contains data member pointers, they will be initialized
1622 /// to -1 in accordance with the Itanium C++ ABI.
1623 void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
Anders Carlssonddf7cac2008-11-04 05:30:00 +00001624
1625 // EmitVAArg - Generate code to get an argument from the passed in pointer
1626 // and update it accordingly. The return value is a pointer to the argument.
1627 // FIXME: We should be able to get rid of this method and use the va_arg
Mike Stump0dd9e882009-02-08 23:14:22 +00001628 // instruction in LLVM instead once it works well enough.
Anders Carlssonddf7cac2008-11-04 05:30:00 +00001629 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
Anders Carlssonf666b772008-12-20 20:27:15 +00001630
John McCallbdc4d802011-07-09 01:37:26 +00001631 /// emitArrayLength - Compute the length of an array, even if it's a
1632 /// VLA, and drill down to the base element type.
1633 llvm::Value *emitArrayLength(const ArrayType *arrayType,
1634 QualType &baseType,
1635 llvm::Value *&addr);
1636
John McCallbc8d40d2011-06-24 21:55:10 +00001637 /// EmitVLASize - Capture all the sizes for the VLA expressions in
1638 /// the given variably-modified type and store them in the VLASizeMap.
Daniel Dunbard286f052009-07-19 06:58:07 +00001639 ///
1640 /// This function can be called with a null (unreachable) insert point.
John McCallbc8d40d2011-06-24 21:55:10 +00001641 void EmitVariablyModifiedType(QualType Ty);
Mike Stump0dd9e882009-02-08 23:14:22 +00001642
John McCallbc8d40d2011-06-24 21:55:10 +00001643 /// getVLASize - Returns an LLVM value that corresponds to the size,
1644 /// in non-variably-sized elements, of a variable length array type,
1645 /// plus that largest non-variably-sized element type. Assumes that
1646 /// the type has already been emitted with EmitVariablyModifiedType.
1647 std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1648 std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
Anders Carlssondcc90d82008-12-12 07:19:02 +00001649
Anders Carlsson5f4307b2009-04-14 16:58:56 +00001650 /// LoadCXXThis - Load the value of 'this'. This function is only valid while
1651 /// generating code for an C++ member function.
John McCall25049412010-02-16 22:04:33 +00001652 llvm::Value *LoadCXXThis() {
1653 assert(CXXThisValue && "no 'this' value for this function");
1654 return CXXThisValue;
1655 }
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Anders Carlssonc997d422010-01-02 01:01:18 +00001657 /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1658 /// virtual bases.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001659 // FIXME: Every place that calls LoadCXXVTT is something
1660 // that needs to be abstracted properly.
John McCall25049412010-02-16 22:04:33 +00001661 llvm::Value *LoadCXXVTT() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001662 assert(CXXStructorImplicitParamValue && "no VTT value for this function");
1663 return CXXStructorImplicitParamValue;
1664 }
1665
1666 /// LoadCXXStructorImplicitParam - Load the implicit parameter
1667 /// for a constructor/destructor.
1668 llvm::Value *LoadCXXStructorImplicitParam() {
1669 assert(CXXStructorImplicitParamValue &&
1670 "no implicit argument value for this function");
1671 return CXXStructorImplicitParamValue;
John McCall25049412010-02-16 22:04:33 +00001672 }
John McCallbff225e2010-02-16 04:15:37 +00001673
1674 /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
Anders Carlsson8561a862010-04-24 23:01:49 +00001675 /// complete class to the given direct base.
1676 llvm::Value *
1677 GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
1678 const CXXRecordDecl *Derived,
1679 const CXXRecordDecl *Base,
1680 bool BaseIsVirtual);
Anders Carlssona88ad562010-04-24 21:51:08 +00001681
Mike Stumpf71d2322009-11-30 20:08:49 +00001682 /// GetAddressOfBaseClass - This function will add the necessary delta to the
1683 /// load of 'this' and returns address of the base class.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001684 llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
Anders Carlsson8561a862010-04-24 23:01:49 +00001685 const CXXRecordDecl *Derived,
John McCallf871d0c2010-08-07 06:22:56 +00001686 CastExpr::path_const_iterator PathBegin,
1687 CastExpr::path_const_iterator PathEnd,
Stephen Hines176edba2014-12-01 14:53:08 -08001688 bool NullCheckValue, SourceLocation Loc);
Anders Carlsson34a2d382010-04-24 21:06:20 +00001689
Anders Carlssona3697c92009-11-23 17:57:54 +00001690 llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
Anders Carlsson8561a862010-04-24 23:01:49 +00001691 const CXXRecordDecl *Derived,
John McCallf871d0c2010-08-07 06:22:56 +00001692 CastExpr::path_const_iterator PathBegin,
1693 CastExpr::path_const_iterator PathEnd,
Anders Carlssona3697c92009-11-23 17:57:54 +00001694 bool NullCheckValue);
1695
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001696 /// GetVTTParameter - Return the VTT parameter that should be passed to a
1697 /// base constructor/destructor with virtual bases.
1698 /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
1699 /// to ItaniumCXXABI.cpp together with all the references to VTT.
1700 llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
1701 bool Delegating);
1702
John McCallc0bf4622010-02-23 00:48:20 +00001703 void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1704 CXXCtorType CtorType,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001705 const FunctionArgList &Args,
1706 SourceLocation Loc);
Sean Hunt059ce0d2011-05-01 07:04:31 +00001707 // It's important not to confuse this and the previous function. Delegating
1708 // constructors are the C++0x feature. The constructor delegate optimization
1709 // is used to reduce duplication in the base and complete consturctors where
1710 // they are substantially the same.
1711 void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1712 const FunctionArgList &Args);
Anders Carlsson155ed4a2010-05-02 23:20:53 +00001713 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
Douglas Gregor378e1e72013-01-31 05:50:40 +00001714 bool ForVirtualBase, bool Delegating,
Stephen Hines176edba2014-12-01 14:53:08 -08001715 llvm::Value *This, const CXXConstructExpr *E);
1716
Fariborz Jahanian34999872010-11-13 21:53:34 +00001717 void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1718 llvm::Value *This, llvm::Value *Src,
Stephen Hines176edba2014-12-01 14:53:08 -08001719 const CXXConstructExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001721 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +00001722 const ConstantArrayType *ArrayTy,
Anders Carlsson5d4d9462009-11-24 18:43:52 +00001723 llvm::Value *ArrayPtr,
Stephen Hines176edba2014-12-01 14:53:08 -08001724 const CXXConstructExpr *E,
Douglas Gregor59174c02010-07-21 01:10:17 +00001725 bool ZeroInitialization = false);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001726
Anders Carlsson569c1f42009-09-23 02:45:36 +00001727 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1728 llvm::Value *NumElements,
Anders Carlsson5d4d9462009-11-24 18:43:52 +00001729 llvm::Value *ArrayPtr,
Stephen Hines176edba2014-12-01 14:53:08 -08001730 const CXXConstructExpr *E,
Douglas Gregor59174c02010-07-21 01:10:17 +00001731 bool ZeroInitialization = false);
Anders Carlssonb14095a2009-04-17 00:06:03 +00001732
John McCallbdc4d802011-07-09 01:37:26 +00001733 static Destroyer destroyCXXObject;
1734
Anders Carlsson7267c162009-05-29 21:03:38 +00001735 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
Douglas Gregor378e1e72013-01-31 05:50:40 +00001736 bool ForVirtualBase, bool Delegating,
1737 llvm::Value *This);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001738
John McCall19705672011-09-15 06:49:18 +00001739 void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001740 llvm::Value *NewPtr, llvm::Value *NumElements,
1741 llvm::Value *AllocSizeWithoutCookie);
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Peter Collingbourne86811602011-11-27 22:09:22 +00001743 void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
1744 llvm::Value *Ptr);
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Anders Carlssona00703d2009-05-31 01:40:14 +00001746 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
Anders Carlsson60e282c2009-08-16 21:13:42 +00001747 void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Eli Friedman4bf81522009-11-18 00:57:03 +00001749 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
1750 QualType DeleteTy);
1751
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001752 RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
1753 const Expr *Arg, bool IsDelete);
1754
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001755 llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
Mike Stumpc849c052009-11-16 06:50:58 +00001756 llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
Nico Weberc5f80462012-10-11 10:13:44 +00001757 llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001758
Richard Smith2c9f87c2012-08-24 00:54:33 +00001759 /// \brief Situations in which we might emit a check for the suitability of a
1760 /// pointer or glvalue.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001761 enum TypeCheckKind {
Richard Smith2c9f87c2012-08-24 00:54:33 +00001762 /// Checking the operand of a load. Must be suitably sized and aligned.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001763 TCK_Load,
Richard Smith2c9f87c2012-08-24 00:54:33 +00001764 /// Checking the destination of a store. Must be suitably sized and aligned.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001765 TCK_Store,
Richard Smith2c9f87c2012-08-24 00:54:33 +00001766 /// Checking the bound value in a reference binding. Must be suitably sized
1767 /// and aligned, but is not required to refer to an object (until the
1768 /// reference is used), per core issue 453.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001769 TCK_ReferenceBinding,
Richard Smith2c9f87c2012-08-24 00:54:33 +00001770 /// Checking the object expression in a non-static data member access. Must
1771 /// be an object within its lifetime.
Richard Smith7ac9ef12012-09-08 02:08:36 +00001772 TCK_MemberAccess,
Richard Smith2c9f87c2012-08-24 00:54:33 +00001773 /// Checking the 'this' pointer for a call to a non-static member function.
1774 /// Must be an object within its lifetime.
Richard Smith8e1cee62012-10-25 02:14:12 +00001775 TCK_MemberCall,
1776 /// Checking the 'this' pointer for a constructor call.
Richard Smithc7648302013-02-13 21:18:23 +00001777 TCK_ConstructorCall,
1778 /// Checking the operand of a static_cast to a derived pointer type. Must be
1779 /// null or an object within its lifetime.
1780 TCK_DowncastPointer,
1781 /// Checking the operand of a static_cast to a derived reference type. Must
1782 /// be an object within its lifetime.
Stephen Hines176edba2014-12-01 14:53:08 -08001783 TCK_DowncastReference,
1784 /// Checking the operand of a cast to a base object. Must be suitably sized
1785 /// and aligned.
1786 TCK_Upcast,
1787 /// Checking the operand of a cast to a virtual base object. Must be an
1788 /// object within its lifetime.
1789 TCK_UpcastToVirtualBase
Richard Smith2c9f87c2012-08-24 00:54:33 +00001790 };
1791
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001792 /// \brief Whether any type-checking sanitizers are enabled. If \c false,
1793 /// calls to EmitTypeCheck can be skipped.
1794 bool sanitizePerformTypeCheck() const;
1795
Richard Smith7ac9ef12012-09-08 02:08:36 +00001796 /// \brief Emit a check that \p V is the address of storage of the
Richard Smith2c9f87c2012-08-24 00:54:33 +00001797 /// appropriate size and alignment for an object of type \p Type.
Richard Smith4def70d2012-10-09 19:52:38 +00001798 void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
Stephen Hines176edba2014-12-01 14:53:08 -08001799 QualType Type, CharUnits Alignment = CharUnits::Zero(),
1800 bool SkipNullCheck = false);
Mike Stumpb14e62d2009-12-16 02:57:00 +00001801
Richard Smitha0a628f2013-02-23 02:53:19 +00001802 /// \brief Emit a check that \p Base points into an array object, which
1803 /// we can access at index \p Index. \p Accessed should be \c false if we
1804 /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
1805 void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
1806 QualType IndexType, bool Accessed);
1807
Chris Lattnerdd36d322010-01-09 21:40:03 +00001808 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1809 bool isInc, bool isPre);
1810 ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1811 bool isInc, bool isPre);
Stephen Hines176edba2014-12-01 14:53:08 -08001812
1813 void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment,
1814 llvm::Value *OffsetValue = nullptr) {
1815 Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
1816 OffsetValue);
1817 }
1818
Reid Spencer5f016e22007-07-11 17:01:13 +00001819 //===--------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 // Declaration Emission
1821 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00001822
Daniel Dunbard286f052009-07-19 06:58:07 +00001823 /// EmitDecl - Emit a declaration.
1824 ///
1825 /// This function can be called with a null (unreachable) insert point.
Reid Spencer5f016e22007-07-11 17:01:13 +00001826 void EmitDecl(const Decl &D);
Daniel Dunbard286f052009-07-19 06:58:07 +00001827
John McCallb6bbcc92010-10-15 04:57:14 +00001828 /// EmitVarDecl - Emit a local variable declaration.
Daniel Dunbard286f052009-07-19 06:58:07 +00001829 ///
1830 /// This function can be called with a null (unreachable) insert point.
John McCallb6bbcc92010-10-15 04:57:14 +00001831 void EmitVarDecl(const VarDecl &D);
Daniel Dunbard286f052009-07-19 06:58:07 +00001832
John McCallf85e1932011-06-15 23:02:42 +00001833 void EmitScalarInit(const Expr *init, const ValueDecl *D,
John McCalla07398e2011-06-16 04:16:24 +00001834 LValue lvalue, bool capturedByInit);
John McCall7acddac2011-06-17 06:42:21 +00001835 void EmitScalarInit(llvm::Value *init, LValue lvalue);
John McCallf85e1932011-06-15 23:02:42 +00001836
John McCallf1549f62010-07-06 01:34:17 +00001837 typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1838 llvm::Value *Address);
1839
Stephen Hines176edba2014-12-01 14:53:08 -08001840 /// \brief Determine whether the given initializer is trivial in the sense
1841 /// that it requires no code to be generated.
1842 bool isTrivialInitializer(const Expr *Init);
1843
John McCallb6bbcc92010-10-15 04:57:14 +00001844 /// EmitAutoVarDecl - Emit an auto variable declaration.
Daniel Dunbard286f052009-07-19 06:58:07 +00001845 ///
1846 /// This function can be called with a null (unreachable) insert point.
John McCall34695852011-02-22 06:44:22 +00001847 void EmitAutoVarDecl(const VarDecl &D);
1848
1849 class AutoVarEmission {
1850 friend class CodeGenFunction;
1851
John McCall57b3b6a2011-02-22 07:16:58 +00001852 const VarDecl *Variable;
John McCall34695852011-02-22 06:44:22 +00001853
1854 /// The alignment of the variable.
1855 CharUnits Alignment;
1856
1857 /// The address of the alloca. Null if the variable was emitted
1858 /// as a global constant.
1859 llvm::Value *Address;
1860
1861 llvm::Value *NRVOFlag;
1862
1863 /// True if the variable is a __block variable.
1864 bool IsByRef;
1865
1866 /// True if the variable is of aggregate type and has a constant
1867 /// initializer.
1868 bool IsConstantAggregate;
1869
Nadav Rotem495cfa42013-03-23 06:43:35 +00001870 /// Non-null if we should use lifetime annotations.
1871 llvm::Value *SizeForLifetimeMarkers;
1872
John McCall57b3b6a2011-02-22 07:16:58 +00001873 struct Invalid {};
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001874 AutoVarEmission(Invalid) : Variable(nullptr) {}
John McCall57b3b6a2011-02-22 07:16:58 +00001875
John McCall34695852011-02-22 06:44:22 +00001876 AutoVarEmission(const VarDecl &variable)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001877 : Variable(&variable), Address(nullptr), NRVOFlag(nullptr),
Nadav Rotem495cfa42013-03-23 06:43:35 +00001878 IsByRef(false), IsConstantAggregate(false),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001879 SizeForLifetimeMarkers(nullptr) {}
John McCall34695852011-02-22 06:44:22 +00001880
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001881 bool wasEmittedAsGlobal() const { return Address == nullptr; }
John McCall34695852011-02-22 06:44:22 +00001882
1883 public:
John McCall57b3b6a2011-02-22 07:16:58 +00001884 static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
1885
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001886 bool useLifetimeMarkers() const {
1887 return SizeForLifetimeMarkers != nullptr;
1888 }
Nadav Rotem495cfa42013-03-23 06:43:35 +00001889 llvm::Value *getSizeForLifetimeMarkers() const {
1890 assert(useLifetimeMarkers());
1891 return SizeForLifetimeMarkers;
1892 }
1893
1894 /// Returns the raw, allocated address, which is not necessarily
1895 /// the address of the object itself.
1896 llvm::Value *getAllocatedAddress() const {
1897 return Address;
1898 }
1899
John McCall34695852011-02-22 06:44:22 +00001900 /// Returns the address of the object within this declaration.
1901 /// Note that this does not chase the forwarding pointer for
1902 /// __block decls.
1903 llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
1904 if (!IsByRef) return Address;
1905
1906 return CGF.Builder.CreateStructGEP(Address,
John McCall57b3b6a2011-02-22 07:16:58 +00001907 CGF.getByRefValueLLVMField(Variable),
1908 Variable->getNameAsString());
John McCall34695852011-02-22 06:44:22 +00001909 }
1910 };
1911 AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
1912 void EmitAutoVarInit(const AutoVarEmission &emission);
1913 void EmitAutoVarCleanups(const AutoVarEmission &emission);
John McCallbdc4d802011-07-09 01:37:26 +00001914 void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
1915 QualType::DestructionKind dtorKind);
Daniel Dunbard286f052009-07-19 06:58:07 +00001916
John McCallb6bbcc92010-10-15 04:57:14 +00001917 void EmitStaticVarDecl(const VarDecl &D,
1918 llvm::GlobalValue::LinkageTypes Linkage);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001919
1920 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Stephen Hines651f13c2014-04-23 16:59:28 -07001921 void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, bool ArgIsPointer,
1922 unsigned ArgNo);
Mike Stump0dd9e882009-02-08 23:14:22 +00001923
John McCall56ca35d2011-02-17 10:25:35 +00001924 /// protectFromPeepholes - Protect a value that we're intending to
1925 /// store to the side, but which will probably be used later, from
1926 /// aggressive peepholing optimizations that might delete it.
1927 ///
1928 /// Pass the result to unprotectFromPeepholes to declare that
1929 /// protection is no longer required.
1930 ///
1931 /// There's no particular reason why this shouldn't apply to
1932 /// l-values, it's just that no existing peepholes work on pointers.
1933 PeepholeProtection protectFromPeepholes(RValue rvalue);
1934 void unprotectFromPeepholes(PeepholeProtection protection);
1935
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 //===--------------------------------------------------------------------===//
1937 // Statement Emission
1938 //===--------------------------------------------------------------------===//
1939
Mike Stump0dd9e882009-02-08 23:14:22 +00001940 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
Daniel Dunbar09124252008-11-12 08:21:33 +00001941 void EmitStopPoint(const Stmt *S);
1942
Mike Stump0dd9e882009-02-08 23:14:22 +00001943 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
1944 /// this function even if there is no current insertion point.
1945 ///
1946 /// This function may clear the current insertion point; callers should use
1947 /// EnsureInsertPoint if they wish to subsequently generate code without first
1948 /// calling EmitBlock, EmitBranch, or EmitStmt.
Reid Spencer5f016e22007-07-11 17:01:13 +00001949 void EmitStmt(const Stmt *S);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001950
Daniel Dunbar09124252008-11-12 08:21:33 +00001951 /// EmitSimpleStmt - Try to emit a "simple" statement which does not
Mike Stump0dd9e882009-02-08 23:14:22 +00001952 /// necessarily require an insertion point or debug information; typically
1953 /// because the statement amounts to a jump or a container of other
1954 /// statements.
Daniel Dunbar09124252008-11-12 08:21:33 +00001955 ///
1956 /// \return True if the statement was handled.
1957 bool EmitSimpleStmt(const Stmt *S);
1958
Eli Friedman2ac2fa72013-06-10 22:04:49 +00001959 llvm::Value *EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
1960 AggValueSlot AVS = AggValueSlot::ignored());
1961 llvm::Value *EmitCompoundStmtWithoutScope(const CompoundStmt &S,
1962 bool GetLast = false,
1963 AggValueSlot AVS =
1964 AggValueSlot::ignored());
Daniel Dunbara448fb22008-11-11 23:11:34 +00001965
Mike Stump0dd9e882009-02-08 23:14:22 +00001966 /// EmitLabel - Emit the block for the given label. It is legal to call this
1967 /// function even if there is no current insertion point.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001968 void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
Daniel Dunbara448fb22008-11-11 23:11:34 +00001969
Reid Spencer5f016e22007-07-11 17:01:13 +00001970 void EmitLabelStmt(const LabelStmt &S);
Richard Smith534986f2012-04-14 00:33:13 +00001971 void EmitAttributedStmt(const AttributedStmt &S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001972 void EmitGotoStmt(const GotoStmt &S);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00001973 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001974 void EmitIfStmt(const IfStmt &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001975
1976 void EmitCondBrHints(llvm::LLVMContext &Context, llvm::BranchInst *CondBr,
Stephen Hines176edba2014-12-01 14:53:08 -08001977 ArrayRef<const Attr *> Attrs);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001978 void EmitWhileStmt(const WhileStmt &S,
Stephen Hines176edba2014-12-01 14:53:08 -08001979 ArrayRef<const Attr *> Attrs = None);
1980 void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001981 void EmitForStmt(const ForStmt &S,
Stephen Hines176edba2014-12-01 14:53:08 -08001982 ArrayRef<const Attr *> Attrs = None);
Reid Spencer5f016e22007-07-11 17:01:13 +00001983 void EmitReturnStmt(const ReturnStmt &S);
1984 void EmitDeclStmt(const DeclStmt &S);
Daniel Dunbar09124252008-11-12 08:21:33 +00001985 void EmitBreakStmt(const BreakStmt &S);
1986 void EmitContinueStmt(const ContinueStmt &S);
Devang Patel51b09f22007-10-04 23:45:31 +00001987 void EmitSwitchStmt(const SwitchStmt &S);
1988 void EmitDefaultStmt(const DefaultStmt &S);
1989 void EmitCaseStmt(const CaseStmt &S);
Devang Patelc049e4f2007-10-08 20:57:48 +00001990 void EmitCaseStmtRange(const CaseStmt &S);
Chad Rosiera23b91d2012-08-28 18:54:39 +00001991 void EmitAsmStmt(const AsmStmt &S);
Mike Stump0dd9e882009-02-08 23:14:22 +00001992
Anders Carlsson3d8400d2008-08-30 19:51:14 +00001993 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001994 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
1995 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
Chris Lattner10cac6f2008-11-15 21:26:17 +00001996 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
John McCallf85e1932011-06-15 23:02:42 +00001997 void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
Mike Stump0dd9e882009-02-08 23:14:22 +00001998
John McCall59a70002010-07-07 06:56:46 +00001999 void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
2000 void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
John McCall9fc6a772010-02-19 09:25:03 +00002001
Anders Carlsson6815e942009-09-27 18:58:34 +00002002 void EmitCXXTryStmt(const CXXTryStmt &S);
Reid Kleckner98592d92013-09-16 21:46:30 +00002003 void EmitSEHTryStmt(const SEHTryStmt &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002004 void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
2005 void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
Stephen Hines176edba2014-12-01 14:53:08 -08002006 ArrayRef<const Attr *> Attrs = None);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002007
Stephen Hines176edba2014-12-01 14:53:08 -08002008 LValue InitCapturedStruct(const CapturedStmt &S);
Ben Langmuir524387a2013-05-09 19:17:11 +00002009 llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
Stephen Hines176edba2014-12-01 14:53:08 -08002010 void GenerateCapturedStmtFunctionProlog(const CapturedStmt &S);
2011 llvm::Function *GenerateCapturedStmtFunctionEpilog(const CapturedStmt &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002012 llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002013 llvm::Value *GenerateCapturedStmtArgument(const CapturedStmt &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002014 void EmitOMPAggregateAssign(LValue OriginalAddr, llvm::Value *PrivateAddr,
2015 const Expr *AssignExpr, QualType Type,
2016 const VarDecl *VDInit);
2017 void EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
2018 OMPPrivateScope &PrivateScope);
2019 void EmitOMPPrivateClause(const OMPExecutableDirective &D,
2020 OMPPrivateScope &PrivateScope);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002021
2022 void EmitOMPParallelDirective(const OMPParallelDirective &S);
2023 void EmitOMPSimdDirective(const OMPSimdDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002024 void EmitOMPForDirective(const OMPForDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002025 void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002026 void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
2027 void EmitOMPSectionDirective(const OMPSectionDirective &S);
2028 void EmitOMPSingleDirective(const OMPSingleDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002029 void EmitOMPMasterDirective(const OMPMasterDirective &S);
2030 void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002031 void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002032 void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002033 void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002034 void EmitOMPTaskDirective(const OMPTaskDirective &S);
2035 void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
2036 void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
2037 void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
2038 void EmitOMPFlushDirective(const OMPFlushDirective &S);
2039 void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
2040 void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
2041 void EmitOMPTargetDirective(const OMPTargetDirective &S);
2042 void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
2043
2044 /// Helpers for 'omp simd' directive.
2045 void EmitOMPLoopBody(const OMPLoopDirective &Directive,
2046 bool SeparateIter = false);
2047 void EmitOMPInnerLoop(const OMPLoopDirective &S, OMPPrivateScope &LoopScope,
2048 bool SeparateIter = false);
2049 void EmitOMPSimdFinal(const OMPLoopDirective &S);
Ben Langmuir524387a2013-05-09 19:17:11 +00002050
Reid Spencer5f016e22007-07-11 17:01:13 +00002051 //===--------------------------------------------------------------------===//
2052 // LValue Expression Emission
2053 //===--------------------------------------------------------------------===//
2054
Daniel Dunbar13e81732009-02-05 07:09:07 +00002055 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
2056 RValue GetUndefRValue(QualType Ty);
2057
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00002058 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
2059 /// and issue an ErrorUnsupported style diagnostic (using the
2060 /// provided Name).
2061 RValue EmitUnsupportedRValue(const Expr *E,
2062 const char *Name);
2063
Mike Stump0dd9e882009-02-08 23:14:22 +00002064 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
2065 /// an ErrorUnsupported style diagnostic (using the provided Name).
Daniel Dunbar6ba82a42008-08-25 20:45:57 +00002066 LValue EmitUnsupportedLValue(const Expr *E,
2067 const char *Name);
2068
Reid Spencer5f016e22007-07-11 17:01:13 +00002069 /// EmitLValue - Emit code to compute a designator that specifies the location
2070 /// of the expression.
2071 ///
2072 /// This can return one of two things: a simple address or a bitfield
2073 /// reference. In either case, the LLVM Value* in the LValue structure is
2074 /// guaranteed to be an LLVM pointer type.
2075 ///
2076 /// If this returns a bitfield reference, nothing about the pointee type of
2077 /// the LLVM value is known: For example, it may not be a pointer to an
2078 /// integer.
2079 ///
2080 /// If this returns a normal address, and if the lvalue's C type is fixed
2081 /// size, this method guarantees that the returned pointer type will point to
2082 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
2083 /// variable length type, this is not possible.
2084 ///
2085 LValue EmitLValue(const Expr *E);
Mike Stump0dd9e882009-02-08 23:14:22 +00002086
Richard Smith7ac9ef12012-09-08 02:08:36 +00002087 /// \brief Same as EmitLValue but additionally we generate checking code to
2088 /// guard against undefined behavior. This is only suitable when we know
2089 /// that the address will be used to access the object.
2090 LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
Mike Stumpb14e62d2009-12-16 02:57:00 +00002091
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002092 RValue convertTempToRValue(llvm::Value *addr, QualType type,
2093 SourceLocation Loc);
John McCall9d232c82013-03-07 21:37:08 +00002094
John McCall9eda3ab2013-03-07 21:37:17 +00002095 void EmitAtomicInit(Expr *E, LValue lvalue);
2096
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002097 RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
John McCall9eda3ab2013-03-07 21:37:17 +00002098 AggValueSlot slot = AggValueSlot::ignored());
2099
2100 void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
2101
John McCall26815d92010-10-27 20:58:56 +00002102 /// EmitToMemory - Change a scalar value from its value
2103 /// representation to its in-memory representation.
2104 llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
2105
2106 /// EmitFromMemory - Change a scalar value from its memory
2107 /// representation to its value representation.
2108 llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
2109
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00002110 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2111 /// care to appropriately convert from the memory representation to
2112 /// the LLVM value representation.
Mike Stump09429b92009-02-17 17:00:02 +00002113 llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
Dan Gohman3d5aff52010-10-14 23:06:10 +00002114 unsigned Alignment, QualType Ty,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002115 SourceLocation Loc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002116 llvm::MDNode *TBAAInfo = nullptr,
Manman Renb37a73d2013-04-04 21:53:22 +00002117 QualType TBAABaseTy = QualType(),
2118 uint64_t TBAAOffset = 0);
John McCall545d9962011-06-25 02:11:03 +00002119
2120 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2121 /// care to appropriately convert from the memory representation to
2122 /// the LLVM value representation. The l-value must be a simple
2123 /// l-value.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002124 llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00002125
2126 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2127 /// care to appropriately convert from the memory representation to
2128 /// the LLVM value representation.
Mike Stump09429b92009-02-17 17:00:02 +00002129 void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Dan Gohman3d5aff52010-10-14 23:06:10 +00002130 bool Volatile, unsigned Alignment, QualType Ty,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002131 llvm::MDNode *TBAAInfo = nullptr, bool isInit = false,
Manman Renb37a73d2013-04-04 21:53:22 +00002132 QualType TBAABaseTy = QualType(),
2133 uint64_t TBAAOffset = 0);
John McCall545d9962011-06-25 02:11:03 +00002134
2135 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2136 /// care to appropriately convert from the memory representation to
2137 /// the LLVM value representation. The l-value must be a simple
David Chisnall7a7ee302012-01-16 17:27:18 +00002138 /// l-value. The isInit flag indicates whether this is an initialization.
2139 /// If so, atomic qualifiers are ignored and the store is always non-atomic.
2140 void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00002141
Reid Spencer5f016e22007-07-11 17:01:13 +00002142 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
2143 /// this method emits the address of the lvalue, then loads the result as an
2144 /// rvalue, returning the rvalue.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002145 RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
John McCall545d9962011-06-25 02:11:03 +00002146 RValue EmitLoadOfExtVectorElementLValue(LValue V);
2147 RValue EmitLoadOfBitfieldLValue(LValue LV);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002148 RValue EmitLoadOfGlobalRegLValue(LValue LV);
Mike Stump0dd9e882009-02-08 23:14:22 +00002149
Reid Spencer5f016e22007-07-11 17:01:13 +00002150 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
2151 /// lvalue, where both are guaranteed to the have the same type, and that type
2152 /// is 'Ty'.
David Chisnall7a7ee302012-01-16 17:27:18 +00002153 void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false);
John McCall545d9962011-06-25 02:11:03 +00002154 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002155 void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
Daniel Dunbared3849b2008-11-19 09:36:46 +00002156
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002157 /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
2158 /// as EmitStoreThroughLValue.
Daniel Dunbared3849b2008-11-19 09:36:46 +00002159 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00002160 /// \param Result [out] - If non-null, this will be set to a Value* for the
2161 /// bit-field contents after the store, appropriate for use as the result of
2162 /// an assignment to the bit-field.
John McCall545d9962011-06-25 02:11:03 +00002163 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002164 llvm::Value **Result=nullptr);
Mike Stump0dd9e882009-02-08 23:14:22 +00002165
John McCall83ce9d42010-11-16 23:07:28 +00002166 /// Emit an l-value for an assignment (simple or compound) of complex type.
2167 LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
John McCall2a416372010-12-05 02:00:02 +00002168 LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
Eli Friedman0934e182013-06-12 01:40:06 +00002169 LValue EmitScalarCompooundAssignWithComplex(const CompoundAssignOperator *E,
2170 llvm::Value *&Result);
John McCall83ce9d42010-11-16 23:07:28 +00002171
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002172 // Note: only available for agg return types
Daniel Dunbar80e62c22008-09-04 03:20:13 +00002173 LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
John McCall2a416372010-12-05 02:00:02 +00002174 LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00002175 // Note: only available for agg return types
Christopher Lamb22c940e2007-12-29 05:02:41 +00002176 LValue EmitCallExprLValue(const CallExpr *E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00002177 // Note: only available for agg return types
2178 LValue EmitVAArgExprLValue(const VAArgExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 LValue EmitDeclRefLValue(const DeclRefExpr *E);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002180 LValue EmitReadRegister(const VarDecl *VD);
Reid Spencer5f016e22007-07-11 17:01:13 +00002181 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002182 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
Chris Lattnerd9f69102008-08-10 01:53:14 +00002183 LValue EmitPredefinedLValue(const PredefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00002184 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Richard Smitha0a628f2013-02-23 02:53:19 +00002185 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2186 bool Accessed = false);
Nate Begeman213541a2008-04-18 23:10:10 +00002187 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
Devang Patelb84a06e2007-10-23 02:10:49 +00002188 LValue EmitMemberExpr(const MemberExpr *E);
Fariborz Jahanian820bca42009-12-09 23:35:29 +00002189 LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
Eli Friedman06e863f2008-05-13 23:18:27 +00002190 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
Richard Smith13ec9102012-05-14 21:57:21 +00002191 LValue EmitInitListLValue(const InitListExpr *E);
John McCall56ca35d2011-02-17 10:25:35 +00002192 LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
Chris Lattner75dfeda2009-03-18 18:28:57 +00002193 LValue EmitCastLValue(const CastExpr *E);
Douglas Gregor03e80032011-06-21 17:03:29 +00002194 LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
John McCalle996ffd2011-02-16 08:02:54 +00002195 LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
Stephen Hines176edba2014-12-01 14:53:08 -08002196
2197 llvm::Value *EmitExtVectorElementLValue(LValue V);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002198
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002199 RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002200
John McCalldd2ecee2012-03-10 03:05:10 +00002201 class ConstantEmission {
2202 llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
2203 ConstantEmission(llvm::Constant *C, bool isReference)
2204 : ValueAndIsReference(C, isReference) {}
2205 public:
2206 ConstantEmission() {}
2207 static ConstantEmission forReference(llvm::Constant *C) {
2208 return ConstantEmission(C, true);
2209 }
2210 static ConstantEmission forValue(llvm::Constant *C) {
2211 return ConstantEmission(C, false);
2212 }
2213
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002214 LLVM_EXPLICIT operator bool() const {
2215 return ValueAndIsReference.getOpaqueValue() != nullptr;
2216 }
John McCalldd2ecee2012-03-10 03:05:10 +00002217
2218 bool isReference() const { return ValueAndIsReference.getInt(); }
2219 LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
2220 assert(isReference());
2221 return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
2222 refExpr->getType());
2223 }
2224
2225 llvm::Constant *getValue() const {
2226 assert(!isReference());
2227 return ValueAndIsReference.getPointer();
2228 }
2229 };
2230
John McCallf4b88a42012-03-10 09:33:50 +00002231 ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
John McCalldd2ecee2012-03-10 03:05:10 +00002232
John McCall4b9c2d22011-11-06 09:01:30 +00002233 RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
2234 AggValueSlot slot = AggValueSlot::ignored());
2235 LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
2236
Daniel Dunbar2a031922009-04-22 05:08:15 +00002237 llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002238 const ObjCIvarDecl *Ivar);
Eli Friedman377ecc72012-04-16 03:54:45 +00002239 LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
John McCallf5ebf9b2013-05-03 07:33:41 +00002240 LValue EmitLValueForLambdaField(const FieldDecl *Field);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002241
Anders Carlsson06a29702010-01-29 05:24:29 +00002242 /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
2243 /// if the Field is a reference, this will return the address of the reference
2244 /// and not the address of the value stored in the reference.
Eli Friedman377ecc72012-04-16 03:54:45 +00002245 LValue EmitLValueForFieldInitialization(LValue Base,
2246 const FieldDecl* Field);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002247
Fariborz Jahanian45012a72009-02-03 00:09:52 +00002248 LValue EmitLValueForIvar(QualType ObjectTy,
2249 llvm::Value* Base, const ObjCIvarDecl *Ivar,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002250 unsigned CVRQualifiers);
2251
Anders Carlssonb58d0172009-05-30 23:23:33 +00002252 LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
Anders Carlssone61c9e82009-05-30 23:30:54 +00002253 LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
Eli Friedman31a37022012-02-08 05:34:55 +00002254 LValue EmitLambdaLValue(const LambdaExpr *E);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00002255 LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
Nico Weberc5f80462012-10-11 10:13:44 +00002256 LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002257
Daniel Dunbar0a04d772008-08-23 10:51:21 +00002258 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
Chris Lattner391d77a2008-03-30 23:03:07 +00002259 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
Chris Lattner65459942009-04-25 19:35:26 +00002260 LValue EmitStmtExprLValue(const StmtExpr *E);
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00002261 LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
Fariborz Jahanian03b29602010-06-17 19:56:20 +00002262 LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
Yunzhong Gao3b8e0b72013-08-30 08:53:09 +00002263 void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
John McCall56ca35d2011-02-17 10:25:35 +00002264
Reid Spencer5f016e22007-07-11 17:01:13 +00002265 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +00002266 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +00002267 //===--------------------------------------------------------------------===//
2268
Mike Stump0dd9e882009-02-08 23:14:22 +00002269 /// EmitCall - Generate a call of the given function, expecting the given
2270 /// result type, and using the given argument list which specifies both the
2271 /// LLVM arguments and the types they were derived from.
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002272 ///
Mike Stumpf71d2322009-11-30 20:08:49 +00002273 /// \param TargetDecl - If given, the decl of the function in a direct call;
2274 /// used to set attributes on the call (noreturn, etc.).
Daniel Dunbar88b53962009-02-02 22:03:45 +00002275 RValue EmitCall(const CGFunctionInfo &FnInfo,
2276 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00002277 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002278 const CallArgList &Args,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002279 const Decl *TargetDecl = nullptr,
2280 llvm::Instruction **callOrInvoke = nullptr);
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Stephen Hines176edba2014-12-01 14:53:08 -08002282 RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E,
Anders Carlssond2490a92009-12-24 20:40:36 +00002283 ReturnValueSlot ReturnValue,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002284 const Decl *TargetDecl = nullptr);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002285 RValue EmitCallExpr(const CallExpr *E,
Anders Carlssond2490a92009-12-24 20:40:36 +00002286 ReturnValueSlot ReturnValue = ReturnValueSlot());
Mike Stump1eb44332009-09-09 15:08:12 +00002287
John McCallbd7370a2013-02-28 19:01:20 +00002288 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2289 const Twine &name = "");
2290 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2291 ArrayRef<llvm::Value*> args,
2292 const Twine &name = "");
2293 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2294 const Twine &name = "");
2295 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2296 ArrayRef<llvm::Value*> args,
2297 const Twine &name = "");
2298
John McCallf1549f62010-07-06 01:34:17 +00002299 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +00002300 ArrayRef<llvm::Value *> Args,
Chris Lattner8cc488f2011-07-20 07:06:53 +00002301 const Twine &Name = "");
Jay Foad4c7d9f12011-07-15 08:37:34 +00002302 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
Chris Lattner8cc488f2011-07-20 07:06:53 +00002303 const Twine &Name = "");
John McCallbd7370a2013-02-28 19:01:20 +00002304 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2305 ArrayRef<llvm::Value*> args,
2306 const Twine &name = "");
2307 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2308 const Twine &name = "");
2309 void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2310 ArrayRef<llvm::Value*> args);
John McCallf1549f62010-07-06 01:34:17 +00002311
Fariborz Jahanian27262672011-01-20 17:19:02 +00002312 llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
2313 NestedNameSpecifier *Qual,
Chris Lattner2acc6e32011-07-18 04:24:23 +00002314 llvm::Type *Ty);
Fariborz Jahanianccd52592011-02-01 23:22:34 +00002315
2316 llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2317 CXXDtorType Type,
Fariborz Jahanian771c6782011-02-03 19:27:17 +00002318 const CXXRecordDecl *RD);
Anders Carlsson566abee2009-11-13 04:45:41 +00002319
Stephen Hines176edba2014-12-01 14:53:08 -08002320 RValue
2321 EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2322 ReturnValueSlot ReturnValue, llvm::Value *This,
2323 llvm::Value *ImplicitParam,
2324 QualType ImplicitParamTy, const CallExpr *E);
2325 RValue EmitCXXStructorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2326 ReturnValueSlot ReturnValue, llvm::Value *This,
2327 llvm::Value *ImplicitParam,
2328 QualType ImplicitParamTy, const CallExpr *E,
2329 StructorType Type);
Anders Carlssona1736c02009-12-24 21:13:40 +00002330 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2331 ReturnValueSlot ReturnValue);
2332 RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2333 ReturnValueSlot ReturnValue);
Ted Kremenek55499762008-06-17 02:43:46 +00002334
Anders Carlssona2447e02011-05-08 20:32:23 +00002335 llvm::Value *EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E,
2336 const CXXMethodDecl *MD,
2337 llvm::Value *This);
Anders Carlsson0f294632009-05-27 04:18:27 +00002338 RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
Anders Carlssona1736c02009-12-24 21:13:40 +00002339 const CXXMethodDecl *MD,
2340 ReturnValueSlot ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00002341
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +00002342 RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
2343 ReturnValueSlot ReturnValue);
2344
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002345
Mike Stump1eb44332009-09-09 15:08:12 +00002346 RValue EmitBuiltinExpr(const FunctionDecl *FD,
Daniel Dunbaref2abfe2009-02-16 22:43:43 +00002347 unsigned BuiltinID, const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00002348
Anders Carlssona1736c02009-12-24 21:13:40 +00002349 RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
Mike Stump09429b92009-02-17 17:00:02 +00002350
Mike Stump0dd9e882009-02-08 23:14:22 +00002351 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
2352 /// is unhandled by the current target.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00002353 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2354
Kevin Qin8137a602013-11-14 02:45:18 +00002355 llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
2356 const llvm::CmpInst::Predicate Fp,
2357 const llvm::CmpInst::Predicate Ip,
2358 const llvm::Twine &Name = "");
Chris Lattner2752c012010-03-03 19:03:45 +00002359 llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Stephen Hines651f13c2014-04-23 16:59:28 -07002360
2361 llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
2362 unsigned LLVMIntrinsic,
2363 unsigned AltLLVMIntrinsic,
2364 const char *NameHint,
2365 unsigned Modifier,
2366 const CallExpr *E,
2367 SmallVectorImpl<llvm::Value *> &Ops,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002368 llvm::Value *Align = nullptr);
Stephen Hines651f13c2014-04-23 16:59:28 -07002369 llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2370 unsigned Modifier, llvm::Type *ArgTy,
2371 const CallExpr *E);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002372 llvm::Value *EmitNeonCall(llvm::Function *F,
Chris Lattner686775d2011-07-20 06:58:45 +00002373 SmallVectorImpl<llvm::Value*> &O,
Bob Wilsondb3d4d02010-12-08 22:37:56 +00002374 const char *name,
Nate Begeman61eecf52010-06-14 05:21:25 +00002375 unsigned shift = 0, bool rightshift = false);
Bob Wilsoncf556522010-12-07 22:40:02 +00002376 llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002377 llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
Nate Begeman61eecf52010-06-14 05:21:25 +00002378 bool negateForRightShift);
Amaury de la Vieuville7f0ff702013-10-04 13:13:15 +00002379 llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
2380 llvm::Type *Ty, bool usgn, const char *name);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002381 // Helper functions for EmitAArch64BuiltinExpr.
Stephen Hines651f13c2014-04-23 16:59:28 -07002382 llvm::Value *vectorWrapScalar8(llvm::Value *Op);
2383 llvm::Value *vectorWrapScalar16(llvm::Value *Op);
2384 llvm::Value *emitVectorWrappedScalar8Intrinsic(
2385 unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
2386 llvm::Value *emitVectorWrappedScalar16Intrinsic(
2387 unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002388 llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Stephen Hines651f13c2014-04-23 16:59:28 -07002389 llvm::Value *EmitNeon64Call(llvm::Function *F,
2390 llvm::SmallVectorImpl<llvm::Value *> &O,
2391 const char *name);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002392
Bill Wendling795b1002012-02-22 09:30:11 +00002393 llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
Anders Carlsson564f1de2007-12-09 23:17:02 +00002394 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2395 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002396 llvm::Value *EmitR600BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Mike Stump0dd9e882009-02-08 23:14:22 +00002397
Daniel Dunbared7c6182008-08-20 00:28:19 +00002398 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
Chris Lattner7f02f722007-08-24 05:35:26 +00002399 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
Patrick Beardeb382ec2012-04-19 00:25:12 +00002400 llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002401 llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
2402 llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2403 llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
2404 const ObjCMethodDecl *MethodWithObjects);
Chris Lattner8fdf3282008-06-24 17:04:18 +00002405 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
John McCallef072fd2010-05-22 01:48:05 +00002406 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2407 ReturnValueSlot Return = ReturnValueSlot());
Chris Lattner8fdf3282008-06-24 17:04:18 +00002408
John McCallf85e1932011-06-15 23:02:42 +00002409 /// Retrieves the default cleanup kind for an ARC cleanup.
2410 /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2411 CleanupKind getARCCleanupKind() {
2412 return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2413 ? NormalAndEHCleanup : NormalCleanup;
2414 }
2415
2416 // ARC primitives.
2417 void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
2418 void EmitARCDestroyWeak(llvm::Value *addr);
2419 llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
2420 llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
2421 llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
2422 bool ignored);
2423 void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
2424 void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
2425 llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2426 llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
John McCall545d9962011-06-25 02:11:03 +00002427 llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
John McCall5b07e802013-03-13 03:10:54 +00002428 bool resultIgnored);
John McCallf85e1932011-06-15 23:02:42 +00002429 llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
John McCall5b07e802013-03-13 03:10:54 +00002430 bool resultIgnored);
John McCallf85e1932011-06-15 23:02:42 +00002431 llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2432 llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
John McCall348f16f2011-10-04 06:23:45 +00002433 llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
John McCall5b07e802013-03-13 03:10:54 +00002434 void EmitARCDestroyStrong(llvm::Value *addr, ARCPreciseLifetime_t precise);
2435 void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
John McCallf85e1932011-06-15 23:02:42 +00002436 llvm::Value *EmitARCAutorelease(llvm::Value *value);
2437 llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2438 llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2439 llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2440
2441 std::pair<LValue,llvm::Value*>
2442 EmitARCStoreAutoreleasing(const BinaryOperator *e);
2443 std::pair<LValue,llvm::Value*>
2444 EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2445
John McCall2b014d62011-10-01 10:32:24 +00002446 llvm::Value *EmitObjCThrowOperand(const Expr *expr);
2447
John McCallf85e1932011-06-15 23:02:42 +00002448 llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2449 llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2450 llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2451
John McCall348f16f2011-10-04 06:23:45 +00002452 llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
John McCallf85e1932011-06-15 23:02:42 +00002453 llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2454 llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2455
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002456 void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
John McCallb6a60792013-03-23 02:35:54 +00002457
John McCallbdc4d802011-07-09 01:37:26 +00002458 static Destroyer destroyARCStrongImprecise;
2459 static Destroyer destroyARCStrongPrecise;
2460 static Destroyer destroyARCWeak;
2461
John McCallf85e1932011-06-15 23:02:42 +00002462 void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2463 llvm::Value *EmitObjCAutoreleasePoolPush();
2464 llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2465 void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2466 void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2467
Richard Smithd4ec5622013-06-12 23:38:09 +00002468 /// \brief Emits a reference binding to the passed in expression.
2469 RValue EmitReferenceBindingToExpr(const Expr *E);
Anders Carlsson3aba0932010-01-31 18:34:51 +00002470
Chris Lattner883f6a72007-08-11 00:04:45 +00002471 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +00002472 // Expression Emission
Chris Lattner883f6a72007-08-11 00:04:45 +00002473 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +00002474
2475 // Expressions are broken into three classes: scalar, complex, aggregate.
Mike Stump0dd9e882009-02-08 23:14:22 +00002476
2477 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
2478 /// scalar type, returning the result.
Anders Carlsson14c5cbf2009-08-16 07:36:22 +00002479 llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002480
Chris Lattner3707b252007-08-26 06:48:56 +00002481 /// EmitScalarConversion - Emit a conversion from the specified type to the
2482 /// specified destination type, both of which are LLVM scalar types.
2483 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
2484 QualType DstTy);
Mike Stump0dd9e882009-02-08 23:14:22 +00002485
Chris Lattner4f1a7b32007-08-26 16:34:22 +00002486 /// EmitComplexToScalarConversion - Emit a conversion from the specified
Mike Stump0dd9e882009-02-08 23:14:22 +00002487 /// complex type to the specified destination type, where the destination type
2488 /// is an LLVM scalar type.
Chris Lattner4f1a7b32007-08-26 16:34:22 +00002489 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
2490 QualType DstTy);
Mike Stump0dd9e882009-02-08 23:14:22 +00002491
2492
John McCall558d2ab2010-09-15 10:14:12 +00002493 /// EmitAggExpr - Emit the computation of the specified expression
2494 /// of aggregate type. The result is computed into the given slot,
2495 /// which may be null to indicate that the value is not needed.
John McCalle0c11682012-07-02 23:58:38 +00002496 void EmitAggExpr(const Expr *E, AggValueSlot AS);
Mike Stump0dd9e882009-02-08 23:14:22 +00002497
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00002498 /// EmitAggExprToLValue - Emit the computation of the specified expression of
2499 /// aggregate type into a temporary LValue.
2500 LValue EmitAggExprToLValue(const Expr *E);
2501
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002502 /// EmitGCMemmoveCollectable - Emit special API for structs with object
2503 /// pointers.
2504 void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
Fariborz Jahanian08c32132009-08-31 19:33:16 +00002505 QualType Ty);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002506
John McCall0c24c802011-06-24 23:21:27 +00002507 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
2508 /// make sure it survives garbage collection until this point.
2509 void EmitExtendGCLifetime(llvm::Value *object);
2510
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00002511 /// EmitComplexExpr - Emit the computation of the specified expression of
Chris Lattner23b1cdb2007-08-23 23:43:33 +00002512 /// complex type, returning the result.
John McCallb418d742010-11-16 10:08:07 +00002513 ComplexPairTy EmitComplexExpr(const Expr *E,
2514 bool IgnoreReal = false,
2515 bool IgnoreImag = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002516
John McCall9d232c82013-03-07 21:37:08 +00002517 /// EmitComplexExprIntoLValue - Emit the given expression of complex
2518 /// type and place its result into the specified l-value.
2519 void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00002520
John McCall9d232c82013-03-07 21:37:08 +00002521 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
2522 void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
2523
2524 /// EmitLoadOfComplex - Load a complex number from the specified l-value.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002525 ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
Chris Lattner2621fd12008-05-08 05:58:21 +00002526
Chandler Carruth0f30a122012-03-30 19:44:53 +00002527 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
2528 /// global variable that has already been created for it. If the initializer
2529 /// has a different type than GV does, this may free GV and return a different
2530 /// one. Otherwise it just returns GV.
2531 llvm::GlobalVariable *
2532 AddInitializerToStaticVarDecl(const VarDecl &D,
2533 llvm::GlobalVariable *GV);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002534
Daniel Dunbar0096acf2009-02-25 19:24:29 +00002535
Anders Carlsson3b2e16b2009-08-08 21:45:14 +00002536 /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
2537 /// variable with global storage.
Richard Smith7ca48502012-02-13 22:16:19 +00002538 void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr,
2539 bool PerformInit);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +00002540
Stephen Hines176edba2014-12-01 14:53:08 -08002541 llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor,
2542 llvm::Constant *Addr);
2543
John McCall20bb1752012-05-01 06:13:13 +00002544 /// Call atexit() with a function that passes the given argument to
2545 /// the given function.
David Blaikiec7971a92013-08-27 23:57:18 +00002546 void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn,
2547 llvm::Constant *addr);
Mike Stump1eb44332009-09-09 15:08:12 +00002548
John McCall3030eb82010-11-06 09:44:32 +00002549 /// Emit code in this function to perform a guarded variable
2550 /// initialization. Guarded initializations are used when it's not
2551 /// possible to prove that an initialization will be done exactly
2552 /// once, e.g. with a static local variable or a static data member
2553 /// of a class template.
Chandler Carruth0f30a122012-03-30 19:44:53 +00002554 void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
Richard Smith7ca48502012-02-13 22:16:19 +00002555 bool PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +00002556
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002557 /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2558 /// variables.
2559 void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
Stephen Hines176edba2014-12-01 14:53:08 -08002560 ArrayRef<llvm::Function *> CXXThreadLocals,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002561 llvm::GlobalVariable *Guard = nullptr);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002562
John McCall3f88f682012-04-06 18:21:03 +00002563 /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002564 /// variables.
John McCall3f88f682012-04-06 18:21:03 +00002565 void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
2566 const std::vector<std::pair<llvm::WeakVH,
2567 llvm::Constant*> > &DtorsAndObjects);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002568
John McCalld26bc762011-03-09 04:27:21 +00002569 void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2570 const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +00002571 llvm::GlobalVariable *Addr,
2572 bool PerformInit);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002573
John McCall558d2ab2010-09-15 10:14:12 +00002574 void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
Fariborz Jahanian34999872010-11-13 21:53:34 +00002575
2576 void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
Fariborz Jahanian830937b2010-12-02 17:02:11 +00002577 const Expr *Exp);
Mike Stump1eb44332009-09-09 15:08:12 +00002578
John McCall1a343eb2011-11-10 08:15:53 +00002579 void enterFullExpression(const ExprWithCleanups *E) {
2580 if (E->getNumObjects() == 0) return;
2581 enterNonTrivialFullExpression(E);
2582 }
2583 void enterNonTrivialFullExpression(const ExprWithCleanups *E);
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Richard Smith4c71b8c2013-05-07 21:53:22 +00002585 void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
Douglas Gregor1eb2e592010-05-16 00:44:00 +00002586
Eli Friedman4c5d8af2012-02-09 03:32:31 +00002587 void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
2588
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002589 RValue EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest = nullptr);
Eli Friedman276b0612011-10-11 02:20:01 +00002590
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00002591 //===--------------------------------------------------------------------===//
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002592 // Annotations Emission
2593 //===--------------------------------------------------------------------===//
2594
2595 /// Emit an annotation call (intrinsic or builtin).
2596 llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn,
2597 llvm::Value *AnnotatedVal,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002598 StringRef AnnotationStr,
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002599 SourceLocation Location);
2600
2601 /// Emit local annotations for the local variable V, declared by D.
2602 void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
2603
2604 /// Emit field annotations for the given field & value. Returns the
2605 /// annotation result.
2606 llvm::Value *EmitFieldAnnotations(const FieldDecl *D, llvm::Value *V);
2607
2608 //===--------------------------------------------------------------------===//
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00002609 // Internal Helpers
2610 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00002611
Chris Lattner0946ccd2008-11-11 07:41:27 +00002612 /// ContainsLabel - Return true if the statement contains a label in it. If
2613 /// this statement is not executed normally, it not containing a label means
2614 /// that we can just remove the code.
2615 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002616
Chris Lattneref425a62011-02-28 00:18:40 +00002617 /// containsBreak - Return true if the statement contains a break out of it.
2618 /// If the statement (recursively) contains a switch or loop with a break
2619 /// inside of it, this is fine.
2620 static bool containsBreak(const Stmt *S);
2621
Daniel Dunbar4bc04552008-11-12 10:12:14 +00002622 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
Chris Lattnerc2c90012011-02-27 23:02:32 +00002623 /// to a constant, or if it does but contains a label, return false. If it
2624 /// constant folds return true and set the boolean result in Result.
2625 bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
Mike Stump0dd9e882009-02-08 23:14:22 +00002626
Chris Lattneref425a62011-02-28 00:18:40 +00002627 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2628 /// to a constant, or if it does but contains a label, return false. If it
2629 /// constant folds return true and set the folded value.
Richard Trieue1ecdc12012-07-23 20:21:35 +00002630 bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result);
Chris Lattneref425a62011-02-28 00:18:40 +00002631
Chris Lattner31a09842008-11-12 08:04:58 +00002632 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
2633 /// if statement) to the specified blocks. Based on the condition, this might
2634 /// try to simplify the codegen of the conditional based on the branch.
Stephen Hines651f13c2014-04-23 16:59:28 -07002635 /// TrueCount should be the number of times we expect the condition to
2636 /// evaluate to true based on PGO data.
Chris Lattner9bc47e22008-11-12 07:46:33 +00002637 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
Stephen Hines651f13c2014-04-23 16:59:28 -07002638 llvm::BasicBlock *FalseBlock, uint64_t TrueCount);
Mike Stumpbe07f602009-12-14 21:58:14 +00002639
Richard Smith4def70d2012-10-09 19:52:38 +00002640 /// \brief Emit a description of a type in a format suitable for passing to
2641 /// a runtime sanitizer handler.
2642 llvm::Constant *EmitCheckTypeDescriptor(QualType T);
2643
2644 /// \brief Convert a value into a format suitable for passing to a runtime
2645 /// sanitizer handler.
2646 llvm::Value *EmitCheckValue(llvm::Value *V);
2647
2648 /// \brief Emit a description of a source location in a format suitable for
2649 /// passing to a runtime sanitizer handler.
2650 llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
2651
Richard Smithcc305612012-11-01 22:15:34 +00002652 /// \brief Create a basic block that will call a handler function in a
2653 /// sanitizer runtime with the provided arguments, and create a conditional
2654 /// branch to it.
Stephen Hines176edba2014-12-01 14:53:08 -08002655 void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerKind>> Checked,
2656 StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
2657 ArrayRef<llvm::Value *> DynamicArgs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002658
Richard Smithcc305612012-11-01 22:15:34 +00002659 /// \brief Create a basic block that will call the trap intrinsic, and emit a
2660 /// conditional branch to it, for the -ftrapv checks.
Chad Rosier78d85b12013-01-29 23:31:22 +00002661 void EmitTrapCheck(llvm::Value *Checked);
Richard Smithcc305612012-11-01 22:15:34 +00002662
Anders Carlsson21c9ad92010-03-30 03:27:09 +00002663 /// EmitCallArg - Emit a single call argument.
John McCall413ebdb2011-03-11 20:59:21 +00002664 void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
Anders Carlsson21c9ad92010-03-30 03:27:09 +00002665
John McCall27360712010-05-26 22:34:26 +00002666 /// EmitDelegateCallArg - We are performing a delegate call; that
2667 /// is, the current function is delegating to another one. Produce
2668 /// a r-value suitable for passing the given parameter.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002669 void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
2670 SourceLocation loc);
John McCall27360712010-05-26 22:34:26 +00002671
Peter Collingbournec5096cb2011-10-27 19:19:51 +00002672 /// SetFPAccuracy - Set the minimum required accuracy of the given floating
2673 /// point operation, expressed as the maximum relative error in ulp.
Duncan Sands82500162012-04-10 08:23:07 +00002674 void SetFPAccuracy(llvm::Value *Val, float Accuracy);
Peter Collingbournec5096cb2011-10-27 19:19:51 +00002675
Chris Lattner31a09842008-11-12 08:04:58 +00002676private:
Rafael Espindolac3f89552012-03-24 16:50:34 +00002677 llvm::MDNode *getRangeForLoadFromType(QualType Ty);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002678 void EmitReturnOfRValue(RValue RV, QualType Ty);
2679
Stephen Hines651f13c2014-04-23 16:59:28 -07002680 void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
2681
2682 llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4>
2683 DeferredReplacements;
2684
Daniel Dunbar56273772008-09-17 00:51:38 +00002685 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
2686 /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
2687 ///
2688 /// \param AI - The first function argument of the expansion.
Stephen Hines176edba2014-12-01 14:53:08 -08002689 void ExpandTypeFromArgs(QualType Ty, LValue Dst,
2690 SmallVectorImpl<llvm::Argument *>::iterator &AI);
Daniel Dunbar56273772008-09-17 00:51:38 +00002691
Stephen Hines176edba2014-12-01 14:53:08 -08002692 /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg
2693 /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
2694 /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
2695 void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
2696 SmallVectorImpl<llvm::Value *> &IRCallArgs,
2697 unsigned &IRCallArgPos);
Anders Carlssonc8c7b182009-01-11 19:40:10 +00002698
Chad Rosier42b60552012-08-23 20:00:18 +00002699 llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
Anders Carlssonc8c7b182009-01-11 19:40:10 +00002700 const Expr *InputExpr, std::string &ConstraintStr);
Mike Stump0dd9e882009-02-08 23:14:22 +00002701
Chad Rosier42b60552012-08-23 20:00:18 +00002702 llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
Eli Friedman6d7cfd72010-07-16 00:55:21 +00002703 LValue InputValue, QualType InputType,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002704 std::string &ConstraintStr,
2705 SourceLocation Loc);
Eli Friedman6d7cfd72010-07-16 00:55:21 +00002706
Stephen Hines651f13c2014-04-23 16:59:28 -07002707public:
Anders Carlsson0139bb92009-04-08 20:47:54 +00002708 /// EmitCallArgs - Emit call arguments for a function.
Stephen Hines651f13c2014-04-23 16:59:28 -07002709 template <typename T>
2710 void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo,
Anders Carlsson0139bb92009-04-08 20:47:54 +00002711 CallExpr::const_arg_iterator ArgBeg,
Adrian Prantlb0e603c2013-07-26 20:42:57 +00002712 CallExpr::const_arg_iterator ArgEnd,
Stephen Hines176edba2014-12-01 14:53:08 -08002713 const FunctionDecl *CalleeDecl = nullptr,
2714 unsigned ParamsToSkip = 0, bool ForceColumnInfo = false) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002715 SmallVector<QualType, 16> ArgTypes;
2716 CallExpr::const_arg_iterator Arg = ArgBeg;
2717
Stephen Hines176edba2014-12-01 14:53:08 -08002718 assert((ParamsToSkip == 0 || CallArgTypeInfo) &&
2719 "Can't skip parameters if type info is not provided");
2720 if (CallArgTypeInfo) {
2721 // First, use the argument types that the type info knows about
2722 for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip,
2723 E = CallArgTypeInfo->param_type_end();
2724 I != E; ++I, ++Arg) {
2725 assert(Arg != ArgEnd && "Running over edge of argument list!");
Stephen Hines651f13c2014-04-23 16:59:28 -07002726#ifndef NDEBUG
Stephen Hines176edba2014-12-01 14:53:08 -08002727 QualType ArgType = *I;
2728 QualType ActualArgType = Arg->getType();
2729 if (ArgType->isPointerType() && ActualArgType->isPointerType()) {
2730 QualType ActualBaseType =
2731 ActualArgType->getAs<PointerType>()->getPointeeType();
2732 QualType ArgBaseType =
2733 ArgType->getAs<PointerType>()->getPointeeType();
2734 if (ArgBaseType->isVariableArrayType()) {
2735 if (const VariableArrayType *VAT =
2736 getContext().getAsVariableArrayType(ActualBaseType)) {
2737 if (!VAT->getSizeExpr())
2738 ActualArgType = ArgType;
2739 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002740 }
2741 }
Stephen Hines176edba2014-12-01 14:53:08 -08002742 assert(getContext()
2743 .getCanonicalType(ArgType.getNonReferenceType())
2744 .getTypePtr() ==
2745 getContext().getCanonicalType(ActualArgType).getTypePtr() &&
2746 "type mismatch in call argument!");
Stephen Hines651f13c2014-04-23 16:59:28 -07002747#endif
Stephen Hines176edba2014-12-01 14:53:08 -08002748 ArgTypes.push_back(*I);
2749 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002750 }
2751
2752 // Either we've emitted all the call args, or we have a call to variadic
Stephen Hines176edba2014-12-01 14:53:08 -08002753 // function.
2754 assert(
2755 (Arg == ArgEnd || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) &&
2756 "Extra arguments in non-variadic function!");
Stephen Hines651f13c2014-04-23 16:59:28 -07002757
2758 // If we still have any arguments, emit them using the type of the argument.
2759 for (; Arg != ArgEnd; ++Arg)
Stephen Hines176edba2014-12-01 14:53:08 -08002760 ArgTypes.push_back(getVarArgType(*Arg));
Stephen Hines651f13c2014-04-23 16:59:28 -07002761
Stephen Hines176edba2014-12-01 14:53:08 -08002762 EmitCallArgs(Args, ArgTypes, ArgBeg, ArgEnd, CalleeDecl, ParamsToSkip,
2763 ForceColumnInfo);
Stephen Hines651f13c2014-04-23 16:59:28 -07002764 }
2765
2766 void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes,
2767 CallExpr::const_arg_iterator ArgBeg,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002768 CallExpr::const_arg_iterator ArgEnd,
Stephen Hines176edba2014-12-01 14:53:08 -08002769 const FunctionDecl *CalleeDecl = nullptr,
2770 unsigned ParamsToSkip = 0, bool ForceColumnInfo = false);
Stephen Hines651f13c2014-04-23 16:59:28 -07002771
2772private:
Stephen Hines176edba2014-12-01 14:53:08 -08002773 QualType getVarArgType(const Expr *Arg);
2774
John McCall492c4f92010-03-03 04:15:11 +00002775 const TargetCodeGenInfo &getTargetHooks() const {
2776 return CGM.getTargetCodeGenInfo();
2777 }
John McCall744016d2010-07-06 23:57:41 +00002778
2779 void EmitDeclMetadata();
John McCallf0c11f72011-03-31 08:03:29 +00002780
2781 CodeGenModule::ByrefHelpers *
Chris Lattner2acc6e32011-07-18 04:24:23 +00002782 buildByrefHelpers(llvm::StructType &byrefType,
John McCallf0c11f72011-03-31 08:03:29 +00002783 const AutoVarEmission &emission);
Dan Gohmanb49bd272012-02-16 00:57:37 +00002784
2785 void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
Jay Foadf4c3db12012-03-02 18:34:30 +00002786
Eli Friedmanea93e402012-08-23 03:10:17 +00002787 /// GetPointeeAlignment - Given an expression with a pointer type, emit the
2788 /// value and compute our best estimate of the alignment of the pointee.
2789 std::pair<llvm::Value*, unsigned> EmitPointerWithAlignment(const Expr *Addr);
Reid Spencer5f016e22007-07-11 17:01:13 +00002790};
Mike Stump1eb44332009-09-09 15:08:12 +00002791
John McCall150b4622011-01-26 04:00:11 +00002792/// Helper class with most of the code for saving a value for a
2793/// conditional expression cleanup.
John McCall804b8072011-01-28 10:53:53 +00002794struct DominatingLLVMValue {
John McCall150b4622011-01-26 04:00:11 +00002795 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
2796
2797 /// Answer whether the given value needs extra work to be saved.
2798 static bool needsSaving(llvm::Value *value) {
2799 // If it's not an instruction, we don't need to save.
2800 if (!isa<llvm::Instruction>(value)) return false;
2801
2802 // If it's an instruction in the entry block, we don't need to save.
2803 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
2804 return (block != &block->getParent()->getEntryBlock());
2805 }
2806
2807 /// Try to save the given value.
2808 static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
2809 if (!needsSaving(value)) return saved_type(value, false);
2810
2811 // Otherwise we need an alloca.
2812 llvm::Value *alloca =
2813 CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
2814 CGF.Builder.CreateStore(value, alloca);
2815
2816 return saved_type(alloca, true);
2817 }
2818
2819 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
2820 if (!value.getInt()) return value.getPointer();
2821 return CGF.Builder.CreateLoad(value.getPointer());
2822 }
2823};
2824
John McCall804b8072011-01-28 10:53:53 +00002825/// A partial specialization of DominatingValue for llvm::Values that
2826/// might be llvm::Instructions.
2827template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
2828 typedef T *type;
John McCall150b4622011-01-26 04:00:11 +00002829 static type restore(CodeGenFunction &CGF, saved_type value) {
John McCall804b8072011-01-28 10:53:53 +00002830 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
2831 }
2832};
2833
2834/// A specialization of DominatingValue for RValue.
2835template <> struct DominatingValue<RValue> {
2836 typedef RValue type;
2837 class saved_type {
2838 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
2839 AggregateAddress, ComplexAddress };
2840
2841 llvm::Value *Value;
2842 Kind K;
2843 saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
2844
2845 public:
2846 static bool needsSaving(RValue value);
2847 static saved_type save(CodeGenFunction &CGF, RValue value);
2848 RValue restore(CodeGenFunction &CGF);
2849
2850 // implementations in CGExprCXX.cpp
2851 };
2852
2853 static bool needsSaving(type value) {
2854 return saved_type::needsSaving(value);
2855 }
2856 static saved_type save(CodeGenFunction &CGF, type value) {
2857 return saved_type::save(CGF, value);
2858 }
2859 static type restore(CodeGenFunction &CGF, saved_type value) {
2860 return value.restore(CGF);
John McCall150b4622011-01-26 04:00:11 +00002861 }
2862};
2863
Reid Spencer5f016e22007-07-11 17:01:13 +00002864} // end namespace CodeGen
2865} // end namespace clang
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00002866
Reid Spencer5f016e22007-07-11 17:01:13 +00002867#endif