blob: 45068fa554442c7daeef490f6ffbcab5a3827b2a [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"
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080027#include "clang/AST/ExprOpenMP.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000028#include "clang/AST/Type.h"
29#include "clang/Basic/ABI.h"
Ben Langmuir524387a2013-05-09 19:17:11 +000030#include "clang/Basic/CapturedStmt.h"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070031#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000032#include "clang/Basic/TargetInfo.h"
33#include "clang/Frontend/CodeGenOptions.h"
34#include "llvm/ADT/ArrayRef.h"
35#include "llvm/ADT/DenseMap.h"
36#include "llvm/ADT/SmallVector.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070037#include "llvm/IR/ValueHandle.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000038#include "llvm/Support/Debug.h"
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070039#include "llvm/Transforms/Utils/SanitizerStats.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000040
Reid Spencer5f016e22007-07-11 17:01:13 +000041namespace llvm {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070042class BasicBlock;
43class LLVMContext;
44class MDNode;
45class Module;
46class SwitchInst;
47class Twine;
48class Value;
49class CallSite;
Reid Spencer5f016e22007-07-11 17:01:13 +000050}
51
52namespace clang {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070053class ASTContext;
54class BlockDecl;
55class CXXDestructorDecl;
56class CXXForRangeStmt;
57class CXXTryStmt;
58class Decl;
59class LabelDecl;
60class EnumConstantDecl;
61class FunctionDecl;
62class FunctionProtoType;
63class LabelStmt;
64class ObjCContainerDecl;
65class ObjCInterfaceDecl;
66class ObjCIvarDecl;
67class ObjCMethodDecl;
68class ObjCImplementationDecl;
69class ObjCPropertyImplDecl;
70class TargetInfo;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070071class VarDecl;
72class ObjCForCollectionStmt;
73class ObjCAtTryStmt;
74class ObjCAtThrowStmt;
75class ObjCAtSynchronizedStmt;
76class ObjCAutoreleasePoolStmt;
Devang Patelb84a06e2007-10-23 02:10:49 +000077
Reid Spencer5f016e22007-07-11 17:01:13 +000078namespace CodeGen {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070079class CodeGenTypes;
80class CGFunctionInfo;
81class CGRecordLayout;
82class CGBlockInfo;
83class CGCXXABI;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080084class BlockByrefHelpers;
85class BlockByrefInfo;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070086class BlockFlags;
87class BlockFieldFlags;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070088class RegionCodeGenTy;
89class TargetCodeGenInfo;
90struct OMPTaskDataTy;
Mike Stump0dd9e882009-02-08 23:14:22 +000091
John McCall9d232c82013-03-07 21:37:08 +000092/// The kind of evaluation to perform on values of a particular
93/// type. Basically, is the code in CGExprScalar, CGExprComplex, or
94/// CGExprAgg?
95///
96/// TODO: should vectors maybe be split out into their own thing?
97enum TypeEvaluationKind {
98 TEK_Scalar,
99 TEK_Complex,
100 TEK_Aggregate
101};
102
Reid Spencer5f016e22007-07-11 17:01:13 +0000103/// CodeGenFunction - This class organizes the per-function state that is used
104/// while generating LLVM code.
John McCall5936e332011-02-15 09:22:45 +0000105class CodeGenFunction : public CodeGenTypeCache {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700106 CodeGenFunction(const CodeGenFunction &) = delete;
107 void operator=(const CodeGenFunction &) = delete;
John McCall4c40d982010-08-31 07:33:07 +0000108
109 friend class CGCXXABI;
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000110public:
John McCallff8e1152010-07-23 21:56:41 +0000111 /// A jump destination is an abstract label, branching to which may
112 /// require a jump out through normal cleanups.
John McCallf1549f62010-07-06 01:34:17 +0000113 struct JumpDest {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700114 JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
John McCallff8e1152010-07-23 21:56:41 +0000115 JumpDest(llvm::BasicBlock *Block,
116 EHScopeStack::stable_iterator Depth,
117 unsigned Index)
118 : Block(Block), ScopeDepth(Depth), Index(Index) {}
119
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700120 bool isValid() const { return Block != nullptr; }
John McCallff8e1152010-07-23 21:56:41 +0000121 llvm::BasicBlock *getBlock() const { return Block; }
122 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
123 unsigned getDestIndex() const { return Index; }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000124
Nadav Rotem495cfa42013-03-23 06:43:35 +0000125 // This should be used cautiously.
126 void setScopeDepth(EHScopeStack::stable_iterator depth) {
127 ScopeDepth = depth;
128 }
129
John McCallff8e1152010-07-23 21:56:41 +0000130 private:
John McCallf1549f62010-07-06 01:34:17 +0000131 llvm::BasicBlock *Block;
132 EHScopeStack::stable_iterator ScopeDepth;
John McCallff8e1152010-07-23 21:56:41 +0000133 unsigned Index;
134 };
135
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 CodeGenModule &CGM; // Per-module state.
Daniel Dunbar444be732009-11-13 05:51:54 +0000137 const TargetInfo &Target;
Mike Stump0dd9e882009-02-08 23:14:22 +0000138
Chris Lattner58dee102007-08-21 16:57:55 +0000139 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700140 LoopInfoStack LoopStack;
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000141 CGBuilderTy Builder;
Mike Stump0dd9e882009-02-08 23:14:22 +0000142
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700143 /// \brief CGBuilder insert helper. This function is called after an
144 /// instruction is created using Builder.
145 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
146 llvm::BasicBlock *BB,
147 llvm::BasicBlock::iterator InsertPt) const;
148
John McCallf5ebf9b2013-05-03 07:33:41 +0000149 /// CurFuncDecl - Holds the Decl for the current outermost
150 /// non-closure context.
Chris Lattner41110242008-06-17 18:05:57 +0000151 const Decl *CurFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000152 /// CurCodeDecl - This is the inner-most code context, which includes blocks.
153 const Decl *CurCodeDecl;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000154 const CGFunctionInfo *CurFnInfo;
Chris Lattner391d77a2008-03-30 23:03:07 +0000155 QualType FnRetTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 llvm::Function *CurFn;
157
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000158 /// CurGD - The GlobalDecl for the current function being compiled.
159 GlobalDecl CurGD;
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000160
John McCallf85e1932011-06-15 23:02:42 +0000161 /// PrologueCleanupDepth - The cleanup depth enclosing all the
162 /// cleanups associated with the parameters.
163 EHScopeStack::stable_iterator PrologueCleanupDepth;
164
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000165 /// ReturnBlock - Unified return block.
John McCallf1549f62010-07-06 01:34:17 +0000166 JumpDest ReturnBlock;
167
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800168 /// ReturnValue - The temporary alloca to hold the return
169 /// value. This is invalid iff the function has no return value.
170 Address ReturnValue;
Mike Stump0dd9e882009-02-08 23:14:22 +0000171
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 /// AllocaInsertPoint - This is an instruction in the entry block before which
173 /// we prefer to insert allocas.
Chris Lattner481769b2009-03-31 22:17:44 +0000174 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000175
Ben Langmuir524387a2013-05-09 19:17:11 +0000176 /// \brief API for captured statement code generation.
177 class CGCapturedStmtInfo {
178 public:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700179 explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
180 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
Ben Langmuir524387a2013-05-09 19:17:11 +0000181 explicit CGCapturedStmtInfo(const CapturedStmt &S,
182 CapturedRegionKind K = CR_Default)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700183 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
Ben Langmuir524387a2013-05-09 19:17:11 +0000184
185 RecordDecl::field_iterator Field =
186 S.getCapturedRecordDecl()->field_begin();
187 for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
188 E = S.capture_end();
189 I != E; ++I, ++Field) {
190 if (I->capturesThis())
191 CXXThisFieldDecl = *Field;
Stephen Hines176edba2014-12-01 14:53:08 -0800192 else if (I->capturesVariable())
Ben Langmuir524387a2013-05-09 19:17:11 +0000193 CaptureFields[I->getCapturedVar()] = *Field;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700194 else if (I->capturesVariableByCopy())
195 CaptureFields[I->getCapturedVar()] = *Field;
Ben Langmuir524387a2013-05-09 19:17:11 +0000196 }
197 }
198
199 virtual ~CGCapturedStmtInfo();
200
201 CapturedRegionKind getKind() const { return Kind; }
202
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700203 virtual void setContextValue(llvm::Value *V) { ThisValue = V; }
Ben Langmuir524387a2013-05-09 19:17:11 +0000204 // \brief Retrieve the value of the context parameter.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700205 virtual llvm::Value *getContextValue() const { return ThisValue; }
Ben Langmuir524387a2013-05-09 19:17:11 +0000206
207 /// \brief Lookup the captured field decl for a variable.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700208 virtual const FieldDecl *lookup(const VarDecl *VD) const {
Ben Langmuir524387a2013-05-09 19:17:11 +0000209 return CaptureFields.lookup(VD);
210 }
211
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700212 bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; }
213 virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
Ben Langmuir524387a2013-05-09 19:17:11 +0000214
Stephen Hines176edba2014-12-01 14:53:08 -0800215 static bool classof(const CGCapturedStmtInfo *) {
216 return true;
217 }
218
Ben Langmuir524387a2013-05-09 19:17:11 +0000219 /// \brief Emit the captured statement body.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700220 virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700221 CGF.incrementProfileCounter(S);
Ben Langmuir524387a2013-05-09 19:17:11 +0000222 CGF.EmitStmt(S);
223 }
224
225 /// \brief Get the name of the capture helper.
226 virtual StringRef getHelperName() const { return "__captured_stmt"; }
227
228 private:
229 /// \brief The kind of captured statement being generated.
230 CapturedRegionKind Kind;
231
232 /// \brief Keep the map between VarDecl and FieldDecl.
233 llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
234
235 /// \brief The base address of the captured record, passed in as the first
236 /// argument of the parallel region function.
237 llvm::Value *ThisValue;
238
239 /// \brief Captured 'this' type.
240 FieldDecl *CXXThisFieldDecl;
241 };
242 CGCapturedStmtInfo *CapturedStmtInfo;
243
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800244 /// \brief RAII for correct setting/restoring of CapturedStmtInfo.
245 class CGCapturedStmtRAII {
246 private:
247 CodeGenFunction &CGF;
248 CGCapturedStmtInfo *PrevCapturedStmtInfo;
249 public:
250 CGCapturedStmtRAII(CodeGenFunction &CGF,
251 CGCapturedStmtInfo *NewCapturedStmtInfo)
252 : CGF(CGF), PrevCapturedStmtInfo(CGF.CapturedStmtInfo) {
253 CGF.CapturedStmtInfo = NewCapturedStmtInfo;
254 }
255 ~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; }
256 };
Nuno Lopesb3198a82012-05-08 22:10:46 +0000257
Stephen Hines176edba2014-12-01 14:53:08 -0800258 /// \brief Sanitizers enabled for this function.
259 SanitizerSet SanOpts;
260
261 /// \brief True if CodeGen currently emits code implementing sanitizer checks.
262 bool IsSanitizerScope;
263
264 /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope.
265 class SanitizerScope {
266 CodeGenFunction *CGF;
267 public:
268 SanitizerScope(CodeGenFunction *CGF);
269 ~SanitizerScope();
270 };
271
272 /// In C++, whether we are code generating a thunk. This controls whether we
273 /// should emit cleanups.
274 bool CurFuncIsThunk;
Will Dietz4f45bc02013-01-18 11:30:38 +0000275
John McCallf85e1932011-06-15 23:02:42 +0000276 /// In ARC, whether we should autorelease the return value.
277 bool AutoreleaseResult;
278
Stephen Hines176edba2014-12-01 14:53:08 -0800279 /// Whether we processed a Microsoft-style asm block during CodeGen. These can
280 /// potentially set the return value.
281 bool SawAsmBlock;
282
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700283 const FunctionDecl *CurSEHParent = nullptr;
284
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700285 /// True if the current function is an outlined SEH helper. This can be a
286 /// finally block or filter expression.
287 bool IsOutlinedSEHHelper;
288
John McCalld16c2cf2011-02-08 08:22:06 +0000289 const CodeGen::CGBlockInfo *BlockInfo;
290 llvm::Value *BlockPointer;
291
Eli Friedmancec5ebd2012-02-11 02:57:39 +0000292 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
293 FieldDecl *LambdaThisCaptureField;
294
Douglas Gregor3d91bbc2010-05-17 15:52:46 +0000295 /// \brief A mapping from NRVO variables to the flags used to indicate
296 /// when the NRVO has been applied to this variable.
297 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000298
John McCallf1549f62010-07-06 01:34:17 +0000299 EHScopeStack EHStack;
Richard Smith8a07cd32013-06-12 20:42:33 +0000300 llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700301 llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack;
Richard Smith8a07cd32013-06-12 20:42:33 +0000302
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800303 llvm::Instruction *CurrentFuncletPad = nullptr;
304
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700305 class CallLifetimeEnd final : public EHScopeStack::Cleanup {
306 llvm::Value *Addr;
307 llvm::Value *Size;
308
309 public:
310 CallLifetimeEnd(Address addr, llvm::Value *size)
311 : Addr(addr.getPointer()), Size(size) {}
312
313 void Emit(CodeGenFunction &CGF, Flags flags) override {
314 CGF.EmitLifetimeEnd(Size, Addr);
315 }
316 };
317
Richard Smith8a07cd32013-06-12 20:42:33 +0000318 /// Header for data within LifetimeExtendedCleanupStack.
319 struct LifetimeExtendedCleanupHeader {
320 /// The size of the following cleanup object.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800321 unsigned Size;
Richard Smith8a07cd32013-06-12 20:42:33 +0000322 /// The kind of cleanup to push: a value from the CleanupKind enumeration.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800323 CleanupKind Kind;
Richard Smith8a07cd32013-06-12 20:42:33 +0000324
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800325 size_t getSize() const { return Size; }
326 CleanupKind getKind() const { return Kind; }
Richard Smith8a07cd32013-06-12 20:42:33 +0000327 };
John McCallf1549f62010-07-06 01:34:17 +0000328
John McCallff8e1152010-07-23 21:56:41 +0000329 /// i32s containing the indexes of the cleanup destinations.
330 llvm::AllocaInst *NormalCleanupDest;
John McCallff8e1152010-07-23 21:56:41 +0000331
332 unsigned NextCleanupDestIndex;
333
John McCall1a343eb2011-11-10 08:15:53 +0000334 /// FirstBlockInfo - The head of a singly-linked-list of block layouts.
335 CGBlockInfo *FirstBlockInfo;
336
John McCall777d6e52011-08-11 02:22:43 +0000337 /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
338 llvm::BasicBlock *EHResumeBlock;
339
Bill Wendling285cfd82011-09-19 20:31:14 +0000340 /// The exception slot. All landing pads write the current exception pointer
341 /// into this alloca.
John McCallf1549f62010-07-06 01:34:17 +0000342 llvm::Value *ExceptionSlot;
343
Bill Wendling285cfd82011-09-19 20:31:14 +0000344 /// The selector slot. Under the MandatoryCleanup model, all landing pads
345 /// write the current selector value into this alloca.
John McCall93c332a2011-05-28 21:13:02 +0000346 llvm::AllocaInst *EHSelectorSlot;
347
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800348 /// A stack of exception code slots. Entering an __except block pushes a slot
349 /// on the stack and leaving pops one. The __exception_code() intrinsic loads
350 /// a value from the top of the stack.
351 SmallVector<Address, 1> SEHCodeSlotStack;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700352
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800353 /// Value returned by __exception_info intrinsic.
354 llvm::Value *SEHInfo = nullptr;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700355
John McCallf1549f62010-07-06 01:34:17 +0000356 /// Emits a landing pad for the current EH stack.
357 llvm::BasicBlock *EmitLandingPad();
358
359 llvm::BasicBlock *getInvokeDestImpl();
360
John McCall150b4622011-01-26 04:00:11 +0000361 template <class T>
John McCall804b8072011-01-28 10:53:53 +0000362 typename DominatingValue<T>::saved_type saveValueInCond(T value) {
363 return DominatingValue<T>::save(*this, value);
John McCall150b4622011-01-26 04:00:11 +0000364 }
365
Daniel Dunbar18ccc772008-09-28 01:03:14 +0000366public:
Anders Carlssonfa1f7562009-02-10 06:07:49 +0000367 /// ObjCEHValueStack - Stack of Objective-C exception values, used for
368 /// rethrows.
Chris Lattner686775d2011-07-20 06:58:45 +0000369 SmallVector<llvm::Value*, 8> ObjCEHValueStack;
Mike Stump0dd9e882009-02-08 23:14:22 +0000370
John McCalld768e9d2011-06-22 02:32:12 +0000371 /// A class controlling the emission of a finally block.
372 class FinallyInfo {
373 /// Where the catchall's edge through the cleanup should go.
374 JumpDest RethrowDest;
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000375
John McCalld768e9d2011-06-22 02:32:12 +0000376 /// A function to call to enter the catch.
377 llvm::Constant *BeginCatchFn;
378
379 /// An i1 variable indicating whether or not the @finally is
380 /// running for an exception.
381 llvm::AllocaInst *ForEHVar;
382
383 /// An i8* variable into which the exception pointer to rethrow
384 /// has been saved.
385 llvm::AllocaInst *SavedExnVar;
386
387 public:
388 void enter(CodeGenFunction &CGF, const Stmt *Finally,
389 llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
390 llvm::Constant *rethrowFn);
391 void exit(CodeGenFunction &CGF);
392 };
Mike Stumpd88ea562009-12-09 03:35:49 +0000393
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700394 /// Returns true inside SEH __try blocks.
395 bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); }
396
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800397 /// Returns true while emitting a cleanuppad.
398 bool isCleanupPadScope() const {
399 return CurrentFuncletPad && isa<llvm::CleanupPadInst>(CurrentFuncletPad);
400 }
401
John McCall150b4622011-01-26 04:00:11 +0000402 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
403 /// current full-expression. Safe against the possibility that
404 /// we're currently inside a conditionally-evaluated expression.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700405 template <class T, class... As>
406 void pushFullExprCleanup(CleanupKind kind, As... A) {
John McCall3ad32c82011-01-28 08:37:24 +0000407 // If we're not in a conditional branch, or if none of the
408 // arguments requires saving, then use the unconditional cleanup.
John McCallc4a1a842011-07-12 00:15:30 +0000409 if (!isInConditionalBranch())
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700410 return EHStack.pushCleanup<T>(kind, A...);
John McCall3ad32c82011-01-28 08:37:24 +0000411
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700412 // Stash values in a tuple so we can guarantee the order of saves.
413 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
414 SavedTuple Saved{saveValueInCond(A)...};
John McCall3ad32c82011-01-28 08:37:24 +0000415
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700416 typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
417 EHStack.pushCleanupTuple<CleanupType>(kind, Saved);
John McCall9928c482011-07-12 16:41:08 +0000418 initFullExprCleanup();
419 }
420
Richard Smith8a07cd32013-06-12 20:42:33 +0000421 /// \brief Queue a cleanup to be pushed after finishing the current
422 /// full-expression.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700423 template <class T, class... As>
424 void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) {
Richard Smith8a07cd32013-06-12 20:42:33 +0000425 assert(!isInConditionalBranch() && "can't defer conditional cleanup");
426
427 LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind };
428
429 size_t OldSize = LifetimeExtendedCleanupStack.size();
430 LifetimeExtendedCleanupStack.resize(
431 LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size);
432
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800433 static_assert(sizeof(Header) % llvm::AlignOf<T>::Alignment == 0,
434 "Cleanup will be allocated on misaligned address");
Richard Smith8a07cd32013-06-12 20:42:33 +0000435 char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
436 new (Buffer) LifetimeExtendedCleanupHeader(Header);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700437 new (Buffer + sizeof(Header)) T(A...);
Richard Smith8a07cd32013-06-12 20:42:33 +0000438 }
439
John McCall6f103ba2011-11-10 10:43:54 +0000440 /// Set up the last cleaup that was pushed as a conditional
441 /// full-expression cleanup.
442 void initFullExprCleanup();
443
John McCallf1549f62010-07-06 01:34:17 +0000444 /// PushDestructorCleanup - Push a cleanup to call the
445 /// complete-object destructor of an object of the given type at the
446 /// given address. Does nothing if T is not a C++ class type with a
447 /// non-trivial destructor.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800448 void PushDestructorCleanup(QualType T, Address Addr);
John McCallf1549f62010-07-06 01:34:17 +0000449
John McCall81407d42010-07-21 06:29:51 +0000450 /// PushDestructorCleanup - Push a cleanup to call the
451 /// complete-object variant of the given destructor on the object at
452 /// the given address.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800453 void PushDestructorCleanup(const CXXDestructorDecl *Dtor, Address Addr);
John McCall81407d42010-07-21 06:29:51 +0000454
John McCallf1549f62010-07-06 01:34:17 +0000455 /// PopCleanupBlock - Will pop the cleanup entry on the stack and
456 /// process all branch fixups.
Adrian Prantl1c3db762013-05-16 00:41:26 +0000457 void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
John McCallf1549f62010-07-06 01:34:17 +0000458
John McCall7d8647f2010-09-14 07:57:04 +0000459 /// DeactivateCleanupBlock - Deactivates the given cleanup block.
460 /// The block cannot be reactivated. Pops it if it's the top of the
461 /// stack.
John McCall6f103ba2011-11-10 10:43:54 +0000462 ///
463 /// \param DominatingIP - An instruction which is known to
464 /// dominate the current IP (if set) and which lies along
465 /// all paths of execution between the current IP and the
466 /// the point at which the cleanup comes into scope.
467 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
468 llvm::Instruction *DominatingIP);
John McCall7d8647f2010-09-14 07:57:04 +0000469
470 /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
471 /// Cannot be used to resurrect a deactivated cleanup.
John McCall6f103ba2011-11-10 10:43:54 +0000472 ///
473 /// \param DominatingIP - An instruction which is known to
474 /// dominate the current IP (if set) and which lies along
475 /// all paths of execution between the current IP and the
476 /// the point at which the cleanup comes into scope.
477 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
478 llvm::Instruction *DominatingIP);
John McCallcd2d2b72010-08-13 21:20:51 +0000479
John McCallf1549f62010-07-06 01:34:17 +0000480 /// \brief Enters a new scope for capturing cleanups, all of which
481 /// will be executed once the scope is exited.
482 class RunCleanupsScope {
John McCallf1549f62010-07-06 01:34:17 +0000483 EHScopeStack::stable_iterator CleanupStackDepth;
Richard Smith8a07cd32013-06-12 20:42:33 +0000484 size_t LifetimeExtendedCleanupStackSize;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000485 bool OldDidCallStackSave;
John McCalled383132013-03-01 01:24:35 +0000486 protected:
Douglas Gregor5656e142009-11-24 21:15:44 +0000487 bool PerformCleanup;
John McCalled383132013-03-01 01:24:35 +0000488 private:
Douglas Gregor01234bb2009-11-24 16:43:22 +0000489
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700490 RunCleanupsScope(const RunCleanupsScope &) = delete;
491 void operator=(const RunCleanupsScope &) = delete;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000492
Eric Christopherc3287792011-10-19 00:43:52 +0000493 protected:
494 CodeGenFunction& CGF;
Will Dietz4f45bc02013-01-18 11:30:38 +0000495
Douglas Gregor01234bb2009-11-24 16:43:22 +0000496 public:
497 /// \brief Enter a new cleanup scope.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000498 explicit RunCleanupsScope(CodeGenFunction &CGF)
Eric Christopherc3287792011-10-19 00:43:52 +0000499 : PerformCleanup(true), CGF(CGF)
Douglas Gregor5656e142009-11-24 21:15:44 +0000500 {
John McCallf1549f62010-07-06 01:34:17 +0000501 CleanupStackDepth = CGF.EHStack.stable_begin();
Richard Smith8a07cd32013-06-12 20:42:33 +0000502 LifetimeExtendedCleanupStackSize =
503 CGF.LifetimeExtendedCleanupStack.size();
Douglas Gregor01234bb2009-11-24 16:43:22 +0000504 OldDidCallStackSave = CGF.DidCallStackSave;
Argyrios Kyrtzidis4ada2ca2010-09-14 00:42:34 +0000505 CGF.DidCallStackSave = false;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000506 }
507
508 /// \brief Exit this cleanup scope, emitting any accumulated
509 /// cleanups.
John McCallf1549f62010-07-06 01:34:17 +0000510 ~RunCleanupsScope() {
Douglas Gregor5656e142009-11-24 21:15:44 +0000511 if (PerformCleanup) {
512 CGF.DidCallStackSave = OldDidCallStackSave;
Richard Smith8a07cd32013-06-12 20:42:33 +0000513 CGF.PopCleanupBlocks(CleanupStackDepth,
514 LifetimeExtendedCleanupStackSize);
Douglas Gregor5656e142009-11-24 21:15:44 +0000515 }
516 }
517
518 /// \brief Determine whether this scope requires any cleanups.
519 bool requiresCleanups() const {
John McCallf1549f62010-07-06 01:34:17 +0000520 return CGF.EHStack.stable_begin() != CleanupStackDepth;
Douglas Gregor5656e142009-11-24 21:15:44 +0000521 }
522
523 /// \brief Force the emission of cleanups now, instead of waiting
524 /// until this object is destroyed.
525 void ForceCleanup() {
526 assert(PerformCleanup && "Already forced cleanup");
Douglas Gregor01234bb2009-11-24 16:43:22 +0000527 CGF.DidCallStackSave = OldDidCallStackSave;
Richard Smith8a07cd32013-06-12 20:42:33 +0000528 CGF.PopCleanupBlocks(CleanupStackDepth,
529 LifetimeExtendedCleanupStackSize);
Douglas Gregor5656e142009-11-24 21:15:44 +0000530 PerformCleanup = false;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000531 }
532 };
533
Stephen Hines176edba2014-12-01 14:53:08 -0800534 class LexicalScope : public RunCleanupsScope {
Eric Christopherc3287792011-10-19 00:43:52 +0000535 SourceRange Range;
Nadav Rotem495cfa42013-03-23 06:43:35 +0000536 SmallVector<const LabelDecl*, 4> Labels;
537 LexicalScope *ParentScope;
Eric Christopherc3287792011-10-19 00:43:52 +0000538
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700539 LexicalScope(const LexicalScope &) = delete;
540 void operator=(const LexicalScope &) = delete;
Eric Christopherc3287792011-10-19 00:43:52 +0000541
542 public:
543 /// \brief Enter a new cleanup scope.
544 explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
Nadav Rotem495cfa42013-03-23 06:43:35 +0000545 : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
546 CGF.CurLexicalScope = this;
Eric Christopherc3287792011-10-19 00:43:52 +0000547 if (CGDebugInfo *DI = CGF.getDebugInfo())
548 DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
549 }
550
Nadav Rotem495cfa42013-03-23 06:43:35 +0000551 void addLabel(const LabelDecl *label) {
552 assert(PerformCleanup && "adding label to dead scope?");
553 Labels.push_back(label);
554 }
555
Eric Christopherc3287792011-10-19 00:43:52 +0000556 /// \brief Exit this cleanup scope, emitting any accumulated
557 /// cleanups.
558 ~LexicalScope() {
Adrian Prantlefb72ad2013-04-01 19:02:06 +0000559 if (CGDebugInfo *DI = CGF.getDebugInfo())
560 DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
561
Nadav Rotem495cfa42013-03-23 06:43:35 +0000562 // If we should perform a cleanup, force them now. Note that
563 // this ends the cleanup scope before rescoping any labels.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700564 if (PerformCleanup) {
565 ApplyDebugLocation DL(CGF, Range.getEnd());
566 ForceCleanup();
567 }
Eric Christopherc3287792011-10-19 00:43:52 +0000568 }
569
570 /// \brief Force the emission of cleanups now, instead of waiting
571 /// until this object is destroyed.
572 void ForceCleanup() {
Nadav Rotem495cfa42013-03-23 06:43:35 +0000573 CGF.CurLexicalScope = ParentScope;
Adrian Prantlefb72ad2013-04-01 19:02:06 +0000574 RunCleanupsScope::ForceCleanup();
575
Nadav Rotem495cfa42013-03-23 06:43:35 +0000576 if (!Labels.empty())
577 rescopeLabels();
Eric Christopherc3287792011-10-19 00:43:52 +0000578 }
Nadav Rotem495cfa42013-03-23 06:43:35 +0000579
580 void rescopeLabels();
Eric Christopherc3287792011-10-19 00:43:52 +0000581 };
582
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800583 typedef llvm::DenseMap<const Decl *, Address> DeclMapTy;
584
Stephen Hines176edba2014-12-01 14:53:08 -0800585 /// \brief The scope used to remap some variables as private in the OpenMP
586 /// loop body (or other captured region emitted without outlining), and to
587 /// restore old vars back on exit.
588 class OMPPrivateScope : public RunCleanupsScope {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800589 DeclMapTy SavedLocals;
590 DeclMapTy SavedPrivates;
Stephen Hines176edba2014-12-01 14:53:08 -0800591
592 private:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700593 OMPPrivateScope(const OMPPrivateScope &) = delete;
594 void operator=(const OMPPrivateScope &) = delete;
Stephen Hines176edba2014-12-01 14:53:08 -0800595
596 public:
597 /// \brief Enter a new OpenMP private scope.
598 explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
599
600 /// \brief Registers \a LocalVD variable as a private and apply \a
601 /// PrivateGen function for it to generate corresponding private variable.
602 /// \a PrivateGen returns an address of the generated private variable.
603 /// \return true if the variable is registered as private, false if it has
604 /// been privatized already.
605 bool
606 addPrivate(const VarDecl *LocalVD,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800607 llvm::function_ref<Address()> PrivateGen) {
Stephen Hines176edba2014-12-01 14:53:08 -0800608 assert(PerformCleanup && "adding private to dead scope");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800609
610 // Only save it once.
611 if (SavedLocals.count(LocalVD)) return false;
612
613 // Copy the existing local entry to SavedLocals.
614 auto it = CGF.LocalDeclMap.find(LocalVD);
615 if (it != CGF.LocalDeclMap.end()) {
616 SavedLocals.insert({LocalVD, it->second});
617 } else {
618 SavedLocals.insert({LocalVD, Address::invalid()});
619 }
620
621 // Generate the private entry.
622 Address Addr = PrivateGen();
623 QualType VarTy = LocalVD->getType();
624 if (VarTy->isReferenceType()) {
625 Address Temp = CGF.CreateMemTemp(VarTy);
626 CGF.Builder.CreateStore(Addr.getPointer(), Temp);
627 Addr = Temp;
628 }
629 SavedPrivates.insert({LocalVD, Addr});
630
Stephen Hines176edba2014-12-01 14:53:08 -0800631 return true;
632 }
633
634 /// \brief Privatizes local variables previously registered as private.
635 /// Registration is separate from the actual privatization to allow
636 /// initializers use values of the original variables, not the private one.
637 /// This is important, for example, if the private variable is a class
638 /// variable initialized by a constructor that references other private
639 /// variables. But at initialization original variables must be used, not
640 /// private copies.
641 /// \return true if at least one variable was privatized, false otherwise.
642 bool Privatize() {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800643 copyInto(SavedPrivates, CGF.LocalDeclMap);
Stephen Hines176edba2014-12-01 14:53:08 -0800644 SavedPrivates.clear();
645 return !SavedLocals.empty();
646 }
647
648 void ForceCleanup() {
649 RunCleanupsScope::ForceCleanup();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800650 copyInto(SavedLocals, CGF.LocalDeclMap);
Stephen Hines176edba2014-12-01 14:53:08 -0800651 SavedLocals.clear();
652 }
653
654 /// \brief Exit scope - all the mapped variables are restored.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700655 ~OMPPrivateScope() {
656 if (PerformCleanup)
657 ForceCleanup();
658 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800659
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700660 /// Checks if the global variable is captured in current function.
661 bool isGlobalVarCaptured(const VarDecl *VD) const {
662 return !VD->isLocalVarDeclOrParm() && CGF.LocalDeclMap.count(VD) > 0;
663 }
664
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800665 private:
666 /// Copy all the entries in the source map over the corresponding
667 /// entries in the destination, which must exist.
668 static void copyInto(const DeclMapTy &src, DeclMapTy &dest) {
669 for (auto &pair : src) {
670 if (!pair.second.isValid()) {
671 dest.erase(pair.first);
672 continue;
673 }
674
675 auto it = dest.find(pair.first);
676 if (it != dest.end()) {
677 it->second = pair.second;
678 } else {
679 dest.insert(pair);
680 }
681 }
682 }
Stephen Hines176edba2014-12-01 14:53:08 -0800683 };
Anders Carlsson44ec82b2010-03-30 03:14:41 +0000684
Richard Smith8a07cd32013-06-12 20:42:33 +0000685 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
686 /// that have been added.
Adrian Prantl1c3db762013-05-16 00:41:26 +0000687 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
Anders Carlsson44ec82b2010-03-30 03:14:41 +0000688
Richard Smith8a07cd32013-06-12 20:42:33 +0000689 /// \brief Takes the old cleanup stack size and emits the cleanup blocks
690 /// that have been added, then adds all lifetime-extended cleanups from
691 /// the given position to the stack.
692 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
693 size_t OldLifetimeExtendedStackSize);
694
John McCallff8e1152010-07-23 21:56:41 +0000695 void ResolveBranchFixups(llvm::BasicBlock *Target);
696
John McCallf1549f62010-07-06 01:34:17 +0000697 /// The given basic block lies in the current EH scope, but may be a
698 /// target of a potentially scope-crossing jump; get a stable handle
699 /// to which we can perform this jump later.
John McCallff8e1152010-07-23 21:56:41 +0000700 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
John McCall413e6772010-07-28 01:07:35 +0000701 return JumpDest(Target,
702 EHStack.getInnermostNormalCleanup(),
703 NextCleanupDestIndex++);
John McCallf1549f62010-07-06 01:34:17 +0000704 }
Anders Carlssonc71c8452009-02-07 23:50:39 +0000705
John McCallf1549f62010-07-06 01:34:17 +0000706 /// The given basic block lies in the current EH scope, but may be a
707 /// target of a potentially scope-crossing jump; get a stable handle
708 /// to which we can perform this jump later.
Chris Lattner686775d2011-07-20 06:58:45 +0000709 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
John McCallff8e1152010-07-23 21:56:41 +0000710 return getJumpDestInCurrentScope(createBasicBlock(Name));
John McCallf1549f62010-07-06 01:34:17 +0000711 }
712
713 /// EmitBranchThroughCleanup - Emit a branch from the current insert
714 /// block through the normal cleanup handling code (if any) and then
715 /// on to \arg Dest.
716 void EmitBranchThroughCleanup(JumpDest Dest);
Chris Lattnerb11f9192011-04-17 00:54:30 +0000717
718 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
719 /// specified destination obviously has no cleanups to run. 'false' is always
720 /// a conservatively correct answer for this method.
721 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
John McCallf1549f62010-07-06 01:34:17 +0000722
John McCall777d6e52011-08-11 02:22:43 +0000723 /// popCatchScope - Pops the catch scope at the top of the EHScope
724 /// stack, emitting any required code (other than the catch handlers
725 /// themselves).
726 void popCatchScope();
John McCallff8e1152010-07-23 21:56:41 +0000727
David Chisnallc6860042012-11-07 16:50:40 +0000728 llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
John McCall777d6e52011-08-11 02:22:43 +0000729 llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800730 llvm::BasicBlock *getMSVCDispatchBlock(EHScopeStack::stable_iterator scope);
Mike Stump0dd9e882009-02-08 23:14:22 +0000731
John McCall150b4622011-01-26 04:00:11 +0000732 /// An object to manage conditionally-evaluated expressions.
733 class ConditionalEvaluation {
734 llvm::BasicBlock *StartBB;
Mike Stump1eb44332009-09-09 15:08:12 +0000735
John McCall150b4622011-01-26 04:00:11 +0000736 public:
737 ConditionalEvaluation(CodeGenFunction &CGF)
738 : StartBB(CGF.Builder.GetInsertBlock()) {}
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000739
John McCall150b4622011-01-26 04:00:11 +0000740 void begin(CodeGenFunction &CGF) {
741 assert(CGF.OutermostConditional != this);
742 if (!CGF.OutermostConditional)
743 CGF.OutermostConditional = this;
744 }
745
746 void end(CodeGenFunction &CGF) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700747 assert(CGF.OutermostConditional != nullptr);
John McCall150b4622011-01-26 04:00:11 +0000748 if (CGF.OutermostConditional == this)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700749 CGF.OutermostConditional = nullptr;
John McCall150b4622011-01-26 04:00:11 +0000750 }
751
752 /// Returns a block which will be executed prior to each
753 /// evaluation of the conditional code.
754 llvm::BasicBlock *getStartingBlock() const {
755 return StartBB;
756 }
757 };
Mike Stump1eb44332009-09-09 15:08:12 +0000758
John McCall3019c442010-09-17 00:50:28 +0000759 /// isInConditionalBranch - Return true if we're currently emitting
760 /// one branch or the other of a conditional expression.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700761 bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
John McCall150b4622011-01-26 04:00:11 +0000762
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800763 void setBeforeOutermostConditional(llvm::Value *value, Address addr) {
John McCall6f103ba2011-11-10 10:43:54 +0000764 assert(isInConditionalBranch());
765 llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800766 auto store = new llvm::StoreInst(value, addr.getPointer(), &block->back());
767 store->setAlignment(addr.getAlignment().getQuantity());
John McCall6f103ba2011-11-10 10:43:54 +0000768 }
769
John McCall150b4622011-01-26 04:00:11 +0000770 /// An RAII object to record that we're evaluating a statement
771 /// expression.
772 class StmtExprEvaluation {
773 CodeGenFunction &CGF;
774
775 /// We have to save the outermost conditional: cleanups in a
776 /// statement expression aren't conditional just because the
777 /// StmtExpr is.
778 ConditionalEvaluation *SavedOutermostConditional;
779
780 public:
781 StmtExprEvaluation(CodeGenFunction &CGF)
782 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700783 CGF.OutermostConditional = nullptr;
John McCall150b4622011-01-26 04:00:11 +0000784 }
785
786 ~StmtExprEvaluation() {
787 CGF.OutermostConditional = SavedOutermostConditional;
788 CGF.EnsureInsertPoint();
789 }
790 };
John McCalle996ffd2011-02-16 08:02:54 +0000791
John McCall56ca35d2011-02-17 10:25:35 +0000792 /// An object which temporarily prevents a value from being
793 /// destroyed by aggressive peephole optimizations that assume that
794 /// all uses of a value have been realized in the IR.
795 class PeepholeProtection {
796 llvm::Instruction *Inst;
797 friend class CodeGenFunction;
798
799 public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700800 PeepholeProtection() : Inst(nullptr) {}
John McCall4b9c2d22011-11-06 09:01:30 +0000801 };
John McCall56ca35d2011-02-17 10:25:35 +0000802
John McCall4b9c2d22011-11-06 09:01:30 +0000803 /// A non-RAII class containing all the information about a bound
804 /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for
805 /// this which makes individual mappings very simple; using this
806 /// class directly is useful when you have a variable number of
807 /// opaque values or don't want the RAII functionality for some
808 /// reason.
809 class OpaqueValueMappingData {
John McCalle996ffd2011-02-16 08:02:54 +0000810 const OpaqueValueExpr *OpaqueValue;
John McCall56ca35d2011-02-17 10:25:35 +0000811 bool BoundLValue;
812 CodeGenFunction::PeepholeProtection Protection;
John McCalle996ffd2011-02-16 08:02:54 +0000813
John McCall4b9c2d22011-11-06 09:01:30 +0000814 OpaqueValueMappingData(const OpaqueValueExpr *ov,
815 bool boundLValue)
816 : OpaqueValue(ov), BoundLValue(boundLValue) {}
John McCalle996ffd2011-02-16 08:02:54 +0000817 public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700818 OpaqueValueMappingData() : OpaqueValue(nullptr) {}
John McCall4b9c2d22011-11-06 09:01:30 +0000819
John McCall56ca35d2011-02-17 10:25:35 +0000820 static bool shouldBindAsLValue(const Expr *expr) {
John McCalla5493f82011-11-08 22:54:08 +0000821 // gl-values should be bound as l-values for obvious reasons.
822 // Records should be bound as l-values because IR generation
823 // always keeps them in memory. Expressions of function type
824 // act exactly like l-values but are formally required to be
825 // r-values in C.
826 return expr->isGLValue() ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700827 expr->getType()->isFunctionType() ||
828 hasAggregateEvaluationKind(expr->getType());
John McCall56ca35d2011-02-17 10:25:35 +0000829 }
830
John McCall4b9c2d22011-11-06 09:01:30 +0000831 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
832 const OpaqueValueExpr *ov,
833 const Expr *e) {
834 if (shouldBindAsLValue(ov))
835 return bind(CGF, ov, CGF.EmitLValue(e));
836 return bind(CGF, ov, CGF.EmitAnyExpr(e));
837 }
838
839 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
840 const OpaqueValueExpr *ov,
841 const LValue &lv) {
842 assert(shouldBindAsLValue(ov));
843 CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
844 return OpaqueValueMappingData(ov, true);
845 }
846
847 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
848 const OpaqueValueExpr *ov,
849 const RValue &rv) {
850 assert(!shouldBindAsLValue(ov));
851 CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
852
853 OpaqueValueMappingData data(ov, false);
854
855 // Work around an extremely aggressive peephole optimization in
856 // EmitScalarConversion which assumes that all other uses of a
857 // value are extant.
858 data.Protection = CGF.protectFromPeepholes(rv);
859
860 return data;
861 }
862
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700863 bool isValid() const { return OpaqueValue != nullptr; }
864 void clear() { OpaqueValue = nullptr; }
John McCall4b9c2d22011-11-06 09:01:30 +0000865
866 void unbind(CodeGenFunction &CGF) {
867 assert(OpaqueValue && "no data to unbind!");
868
869 if (BoundLValue) {
870 CGF.OpaqueLValues.erase(OpaqueValue);
871 } else {
872 CGF.OpaqueRValues.erase(OpaqueValue);
873 CGF.unprotectFromPeepholes(Protection);
874 }
875 }
876 };
877
878 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
879 class OpaqueValueMapping {
880 CodeGenFunction &CGF;
881 OpaqueValueMappingData Data;
882
883 public:
884 static bool shouldBindAsLValue(const Expr *expr) {
885 return OpaqueValueMappingData::shouldBindAsLValue(expr);
886 }
887
John McCall56ca35d2011-02-17 10:25:35 +0000888 /// Build the opaque value mapping for the given conditional
889 /// operator if it's the GNU ?: extension. This is a common
890 /// enough pattern that the convenience operator is really
891 /// helpful.
892 ///
893 OpaqueValueMapping(CodeGenFunction &CGF,
894 const AbstractConditionalOperator *op) : CGF(CGF) {
John McCall4b9c2d22011-11-06 09:01:30 +0000895 if (isa<ConditionalOperator>(op))
896 // Leave Data empty.
John McCall56ca35d2011-02-17 10:25:35 +0000897 return;
John McCall56ca35d2011-02-17 10:25:35 +0000898
899 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
John McCall4b9c2d22011-11-06 09:01:30 +0000900 Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
901 e->getCommon());
John McCall56ca35d2011-02-17 10:25:35 +0000902 }
903
John McCalle996ffd2011-02-16 08:02:54 +0000904 OpaqueValueMapping(CodeGenFunction &CGF,
905 const OpaqueValueExpr *opaqueValue,
John McCall56ca35d2011-02-17 10:25:35 +0000906 LValue lvalue)
John McCall4b9c2d22011-11-06 09:01:30 +0000907 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
John McCall56ca35d2011-02-17 10:25:35 +0000908 }
909
910 OpaqueValueMapping(CodeGenFunction &CGF,
911 const OpaqueValueExpr *opaqueValue,
912 RValue rvalue)
John McCall4b9c2d22011-11-06 09:01:30 +0000913 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
John McCalle996ffd2011-02-16 08:02:54 +0000914 }
915
916 void pop() {
John McCall4b9c2d22011-11-06 09:01:30 +0000917 Data.unbind(CGF);
918 Data.clear();
John McCalle996ffd2011-02-16 08:02:54 +0000919 }
920
921 ~OpaqueValueMapping() {
John McCall4b9c2d22011-11-06 09:01:30 +0000922 if (Data.isValid()) Data.unbind(CGF);
John McCalle996ffd2011-02-16 08:02:54 +0000923 }
924 };
Fariborz Jahaniane2204552010-11-16 19:29:39 +0000925
Chris Lattner7f02f722007-08-24 05:35:26 +0000926private:
Chris Lattnerd9becd12009-10-28 23:59:40 +0000927 CGDebugInfo *DebugInfo;
Devang Patelaa112892011-03-07 18:45:56 +0000928 bool DisableDebugInfo;
Mike Stump09429b92009-02-17 17:00:02 +0000929
John McCall93c332a2011-05-28 21:13:02 +0000930 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
931 /// calling llvm.stacksave for multiple VLAs in the same scope.
932 bool DidCallStackSave;
933
Mike Stumpf71d2322009-11-30 20:08:49 +0000934 /// IndirectBranch - The first time an indirect goto is seen we create a block
935 /// with an indirect branch. Every time we see the address of a label taken,
936 /// we add the label to the indirect goto. Every subsequent indirect goto is
937 /// codegen'd as a jump to the IndirectBranch's basic block.
Chris Lattnerd9becd12009-10-28 23:59:40 +0000938 llvm::IndirectBrInst *IndirectBranch;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000939
Mike Stump0dd9e882009-02-08 23:14:22 +0000940 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
941 /// decls.
John McCall6b5a61b2011-02-07 10:33:21 +0000942 DeclMapTy LocalDeclMap;
Reid Spencer5f016e22007-07-11 17:01:13 +0000943
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800944 /// SizeArguments - If a ParmVarDecl had the pass_object_size attribute, this
945 /// will contain a mapping from said ParmVarDecl to its implicit "object_size"
946 /// parameter.
947 llvm::SmallDenseMap<const ParmVarDecl *, const ImplicitParamDecl *, 2>
948 SizeArguments;
949
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700950 /// Track escaped local variables with auto storage. Used during SEH
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800951 /// outlining to produce a call to llvm.localescape.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700952 llvm::DenseMap<llvm::AllocaInst *, int> EscapedLocals;
953
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 /// LabelMap - This keeps track of the LLVM basic block for each C label.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000955 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
Mike Stump0dd9e882009-02-08 23:14:22 +0000956
Mike Stump0dd9e882009-02-08 23:14:22 +0000957 // BreakContinueStack - This keeps track of where break and continue
Anders Carlssone4b6d342009-02-10 05:52:02 +0000958 // statements should jump to.
Chris Lattnerda138702007-07-16 21:28:45 +0000959 struct BreakContinue {
John McCallf1549f62010-07-06 01:34:17 +0000960 BreakContinue(JumpDest Break, JumpDest Continue)
961 : BreakBlock(Break), ContinueBlock(Continue) {}
Mike Stump0dd9e882009-02-08 23:14:22 +0000962
John McCallf1549f62010-07-06 01:34:17 +0000963 JumpDest BreakBlock;
964 JumpDest ContinueBlock;
Mike Stump0dd9e882009-02-08 23:14:22 +0000965 };
Chris Lattner686775d2011-07-20 06:58:45 +0000966 SmallVector<BreakContinue, 8> BreakContinueStack;
Daniel Dunbar18ccc772008-09-28 01:03:14 +0000967
Stephen Hines651f13c2014-04-23 16:59:28 -0700968 CodeGenPGO PGO;
969
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700970 /// Calculate branch weights appropriate for PGO data
971 llvm::MDNode *createProfileWeights(uint64_t TrueCount, uint64_t FalseCount);
972 llvm::MDNode *createProfileWeights(ArrayRef<uint64_t> Weights);
973 llvm::MDNode *createProfileWeightsForLoop(const Stmt *Cond,
974 uint64_t LoopCount);
975
Stephen Hines651f13c2014-04-23 16:59:28 -0700976public:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700977 /// Increment the profiler's counter for the given statement.
978 void incrementProfileCounter(const Stmt *S) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700979 if (CGM.getCodeGenOpts().hasProfileClangInstr())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700980 PGO.emitCounterIncrement(Builder, S);
981 PGO.setCurrentStmt(S);
Stephen Hines651f13c2014-04-23 16:59:28 -0700982 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700983
984 /// Get the profiler's count for the given statement.
985 uint64_t getProfileCount(const Stmt *S) {
986 Optional<uint64_t> Count = PGO.getStmtCount(S);
987 if (!Count.hasValue())
988 return 0;
989 return *Count;
990 }
991
992 /// Set the profiler's current count.
993 void setCurrentProfileCount(uint64_t Count) {
994 PGO.setCurrentRegionCount(Count);
995 }
996
997 /// Get the profiler's current count. This is generally the count for the most
998 /// recently incremented counter.
999 uint64_t getCurrentProfileCount() {
1000 return PGO.getCurrentRegionCount();
1001 }
1002
Stephen Hines651f13c2014-04-23 16:59:28 -07001003private:
1004
Zhongxing Xu170fd492012-01-14 09:08:15 +00001005 /// SwitchInsn - This is nearest current switch instruction. It is null if
Mike Stump0dd9e882009-02-08 23:14:22 +00001006 /// current context is not in a switch.
Devang Patel51b09f22007-10-04 23:45:31 +00001007 llvm::SwitchInst *SwitchInsn;
Stephen Hines651f13c2014-04-23 16:59:28 -07001008 /// The branch weights of SwitchInsn when doing instrumentation based PGO.
1009 SmallVector<uint64_t, 16> *SwitchWeights;
Devang Patel51b09f22007-10-04 23:45:31 +00001010
Mike Stump0dd9e882009-02-08 23:14:22 +00001011 /// CaseRangeBlock - This block holds if condition check for last case
Devang Patel80fd5f92007-10-09 17:08:50 +00001012 /// statement range in current switch instruction.
Devang Patelc049e4f2007-10-08 20:57:48 +00001013 llvm::BasicBlock *CaseRangeBlock;
1014
John McCall56ca35d2011-02-17 10:25:35 +00001015 /// OpaqueLValues - Keeps track of the current set of opaque value
John McCalle996ffd2011-02-16 08:02:54 +00001016 /// expressions.
John McCall56ca35d2011-02-17 10:25:35 +00001017 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
1018 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
John McCalle996ffd2011-02-16 08:02:54 +00001019
Mike Stump0dd9e882009-02-08 23:14:22 +00001020 // VLASizeMap - This keeps track of the associated size for each VLA type.
Eli Friedmanbbed6b92009-08-15 02:50:32 +00001021 // We track this by the size expression rather than the type itself because
1022 // in certain situations, like a const qualifier applied to an VLA typedef,
1023 // multiple VLA types can share the same size expression.
Mike Stump0dd9e882009-02-08 23:14:22 +00001024 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
1025 // enter/leave scopes.
Eli Friedmanbbed6b92009-08-15 02:50:32 +00001026 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
Mike Stump0dd9e882009-02-08 23:14:22 +00001027
John McCallf1549f62010-07-06 01:34:17 +00001028 /// A block containing a single 'unreachable' instruction. Created
1029 /// lazily by getUnreachableBlock().
1030 llvm::BasicBlock *UnreachableBlock;
Mike Stump0dd9e882009-02-08 23:14:22 +00001031
Adrian Prantld072e592013-05-03 20:11:48 +00001032 /// Counts of the number return expressions in the function.
1033 unsigned NumReturnExprs;
Adrian Prantlfa6b0792013-05-02 17:30:20 +00001034
1035 /// Count the number of simple (constant) return expressions in the function.
1036 unsigned NumSimpleReturnExprs;
1037
Adrian Prantld072e592013-05-03 20:11:48 +00001038 /// The last regular (non-return) debug location (breakpoint) in the function.
1039 SourceLocation LastStopPoint;
Adrian Prantlfa6b0792013-05-02 17:30:20 +00001040
Richard Smithc3bf52c2013-04-20 22:23:05 +00001041public:
1042 /// A scope within which we are constructing the fields of an object which
1043 /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
1044 /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
1045 class FieldConstructionScope {
1046 public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001047 FieldConstructionScope(CodeGenFunction &CGF, Address This)
Richard Smithc3bf52c2013-04-20 22:23:05 +00001048 : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
1049 CGF.CXXDefaultInitExprThis = This;
1050 }
1051 ~FieldConstructionScope() {
1052 CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
1053 }
1054
1055 private:
1056 CodeGenFunction &CGF;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001057 Address OldCXXDefaultInitExprThis;
Richard Smithc3bf52c2013-04-20 22:23:05 +00001058 };
1059
1060 /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
1061 /// is overridden to be the object under construction.
1062 class CXXDefaultInitExprScope {
1063 public:
1064 CXXDefaultInitExprScope(CodeGenFunction &CGF)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001065 : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue),
1066 OldCXXThisAlignment(CGF.CXXThisAlignment) {
1067 CGF.CXXThisValue = CGF.CXXDefaultInitExprThis.getPointer();
1068 CGF.CXXThisAlignment = CGF.CXXDefaultInitExprThis.getAlignment();
Richard Smithc3bf52c2013-04-20 22:23:05 +00001069 }
1070 ~CXXDefaultInitExprScope() {
1071 CGF.CXXThisValue = OldCXXThisValue;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001072 CGF.CXXThisAlignment = OldCXXThisAlignment;
Richard Smithc3bf52c2013-04-20 22:23:05 +00001073 }
1074
1075 public:
1076 CodeGenFunction &CGF;
1077 llvm::Value *OldCXXThisValue;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001078 CharUnits OldCXXThisAlignment;
Richard Smithc3bf52c2013-04-20 22:23:05 +00001079 };
1080
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001081 class InlinedInheritingConstructorScope {
1082 public:
1083 InlinedInheritingConstructorScope(CodeGenFunction &CGF, GlobalDecl GD)
1084 : CGF(CGF), OldCurGD(CGF.CurGD), OldCurFuncDecl(CGF.CurFuncDecl),
1085 OldCurCodeDecl(CGF.CurCodeDecl),
1086 OldCXXABIThisDecl(CGF.CXXABIThisDecl),
1087 OldCXXABIThisValue(CGF.CXXABIThisValue),
1088 OldCXXThisValue(CGF.CXXThisValue),
1089 OldCXXABIThisAlignment(CGF.CXXABIThisAlignment),
1090 OldCXXThisAlignment(CGF.CXXThisAlignment),
1091 OldReturnValue(CGF.ReturnValue), OldFnRetTy(CGF.FnRetTy),
1092 OldCXXInheritedCtorInitExprArgs(
1093 std::move(CGF.CXXInheritedCtorInitExprArgs)) {
1094 CGF.CurGD = GD;
1095 CGF.CurFuncDecl = CGF.CurCodeDecl =
1096 cast<CXXConstructorDecl>(GD.getDecl());
1097 CGF.CXXABIThisDecl = nullptr;
1098 CGF.CXXABIThisValue = nullptr;
1099 CGF.CXXThisValue = nullptr;
1100 CGF.CXXABIThisAlignment = CharUnits();
1101 CGF.CXXThisAlignment = CharUnits();
1102 CGF.ReturnValue = Address::invalid();
1103 CGF.FnRetTy = QualType();
1104 CGF.CXXInheritedCtorInitExprArgs.clear();
1105 }
1106 ~InlinedInheritingConstructorScope() {
1107 CGF.CurGD = OldCurGD;
1108 CGF.CurFuncDecl = OldCurFuncDecl;
1109 CGF.CurCodeDecl = OldCurCodeDecl;
1110 CGF.CXXABIThisDecl = OldCXXABIThisDecl;
1111 CGF.CXXABIThisValue = OldCXXABIThisValue;
1112 CGF.CXXThisValue = OldCXXThisValue;
1113 CGF.CXXABIThisAlignment = OldCXXABIThisAlignment;
1114 CGF.CXXThisAlignment = OldCXXThisAlignment;
1115 CGF.ReturnValue = OldReturnValue;
1116 CGF.FnRetTy = OldFnRetTy;
1117 CGF.CXXInheritedCtorInitExprArgs =
1118 std::move(OldCXXInheritedCtorInitExprArgs);
1119 }
1120
1121 private:
1122 CodeGenFunction &CGF;
1123 GlobalDecl OldCurGD;
1124 const Decl *OldCurFuncDecl;
1125 const Decl *OldCurCodeDecl;
1126 ImplicitParamDecl *OldCXXABIThisDecl;
1127 llvm::Value *OldCXXABIThisValue;
1128 llvm::Value *OldCXXThisValue;
1129 CharUnits OldCXXABIThisAlignment;
1130 CharUnits OldCXXThisAlignment;
1131 Address OldReturnValue;
1132 QualType OldFnRetTy;
1133 CallArgList OldCXXInheritedCtorInitExprArgs;
1134 };
1135
Richard Smithc3bf52c2013-04-20 22:23:05 +00001136private:
Anders Carlssonf6c56e22009-11-25 03:15:49 +00001137 /// CXXThisDecl - When generating code for a C++ member function,
1138 /// this will hold the implicit 'this' declaration.
Eli Friedmancec5ebd2012-02-11 02:57:39 +00001139 ImplicitParamDecl *CXXABIThisDecl;
1140 llvm::Value *CXXABIThisValue;
John McCall25049412010-02-16 22:04:33 +00001141 llvm::Value *CXXThisValue;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001142 CharUnits CXXABIThisAlignment;
1143 CharUnits CXXThisAlignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Richard Smithc3bf52c2013-04-20 22:23:05 +00001145 /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1146 /// this expression.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001147 Address CXXDefaultInitExprThis = Address::invalid();
Richard Smithc3bf52c2013-04-20 22:23:05 +00001148
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001149 /// The values of function arguments to use when evaluating
1150 /// CXXInheritedCtorInitExprs within this context.
1151 CallArgList CXXInheritedCtorInitExprArgs;
1152
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001153 /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1154 /// destructor, this will hold the implicit argument (e.g. VTT).
1155 ImplicitParamDecl *CXXStructorImplicitParamDecl;
1156 llvm::Value *CXXStructorImplicitParamValue;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001157
John McCall150b4622011-01-26 04:00:11 +00001158 /// OutermostConditional - Points to the outermost active
1159 /// conditional control. This is used so that we know if a
1160 /// temporary should be destroyed conditionally.
1161 ConditionalEvaluation *OutermostConditional;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Nadav Rotem495cfa42013-03-23 06:43:35 +00001163 /// The current lexical scope.
1164 LexicalScope *CurLexicalScope;
Anders Carlsson7dfa4072009-09-12 02:14:24 +00001165
Adrian Prantl1c3db762013-05-16 00:41:26 +00001166 /// The current source location that should be used for exception
1167 /// handling code.
1168 SourceLocation CurEHLocation;
1169
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001170 /// BlockByrefInfos - For each __block variable, contains
1171 /// information about the layout of the variable.
1172 llvm::DenseMap<const ValueDecl *, BlockByrefInfo> BlockByrefInfos;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001173
John McCallf1549f62010-07-06 01:34:17 +00001174 llvm::BasicBlock *TerminateLandingPad;
Mike Stump182f3832009-12-10 00:02:42 +00001175 llvm::BasicBlock *TerminateHandler;
Chris Lattner83252dc2010-07-20 21:07:09 +00001176 llvm::BasicBlock *TrapBB;
Eli Friedman94067052009-12-10 02:21:21 +00001177
Tanya Lattner0df579e2012-07-09 22:06:01 +00001178 /// Add a kernel metadata node to the named metadata node 'opencl.kernels'.
1179 /// In the kernel metadata node, reference the kernel function and metadata
1180 /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2):
Joey Gouly37453b92013-03-08 09:42:32 +00001181 /// - A node for the vec_type_hint(<type>) qualifier contains string
1182 /// "vec_type_hint", an undefined value of the <type> data type,
1183 /// and a Boolean that is true if the <type> is integer and signed.
Tanya Lattner0df579e2012-07-09 22:06:01 +00001184 /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string
1185 /// "work_group_size_hint", and three 32-bit integers X, Y and Z.
1186 /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string
1187 /// "reqd_work_group_size", and three 32-bit integers X, Y and Z.
1188 void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1189 llvm::Function *Fn);
1190
Reid Spencer5f016e22007-07-11 17:01:13 +00001191public:
Fariborz Jahanian4904bf42012-06-26 16:06:38 +00001192 CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
John McCall1a343eb2011-11-10 08:15:53 +00001193 ~CodeGenFunction();
Mike Stump0dd9e882009-02-08 23:14:22 +00001194
John McCall1e7fe752010-09-02 09:58:18 +00001195 CodeGenTypes &getTypes() const { return CGM.getTypes(); }
John McCallf2aac842011-05-15 02:34:36 +00001196 ASTContext &getContext() const { return CGM.getContext(); }
Devang Patelaa112892011-03-07 18:45:56 +00001197 CGDebugInfo *getDebugInfo() {
1198 if (DisableDebugInfo)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001199 return nullptr;
Devang Patelaa112892011-03-07 18:45:56 +00001200 return DebugInfo;
1201 }
1202 void disableDebugInfo() { DisableDebugInfo = true; }
1203 void enableDebugInfo() { DisableDebugInfo = false; }
1204
John McCallf85e1932011-06-15 23:02:42 +00001205 bool shouldUseFusedARCCalls() {
1206 return CGM.getCodeGenOpts().OptimizationLevel == 0;
1207 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001208
David Blaikie4e4d0842012-03-11 07:00:24 +00001209 const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
John McCalld16c2cf2011-02-08 08:22:06 +00001210
Bill Wendling285cfd82011-09-19 20:31:14 +00001211 /// Returns a pointer to the function's exception object and selector slot,
1212 /// which is assigned in every landing pad.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001213 Address getExceptionSlot();
1214 Address getEHSelectorSlot();
John McCallf1549f62010-07-06 01:34:17 +00001215
Bill Wendlingae270592011-09-15 18:57:19 +00001216 /// Returns the contents of the function's exception object and selector
1217 /// slots.
1218 llvm::Value *getExceptionFromSlot();
1219 llvm::Value *getSelectorFromSlot();
1220
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001221 Address getNormalCleanupDestSlot();
John McCallff8e1152010-07-23 21:56:41 +00001222
John McCallf1549f62010-07-06 01:34:17 +00001223 llvm::BasicBlock *getUnreachableBlock() {
1224 if (!UnreachableBlock) {
1225 UnreachableBlock = createBasicBlock("unreachable");
1226 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1227 }
1228 return UnreachableBlock;
1229 }
1230
1231 llvm::BasicBlock *getInvokeDest() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001232 if (!EHStack.requiresLandingPad()) return nullptr;
John McCallf1549f62010-07-06 01:34:17 +00001233 return getInvokeDestImpl();
1234 }
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001235
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001236 bool currentFunctionUsesSEHTry() const { return CurSEHParent != nullptr; }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001237
John McCall64aa4b32013-04-16 22:48:15 +00001238 const TargetInfo &getTarget() const { return Target; }
John McCalld16c2cf2011-02-08 08:22:06 +00001239 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
Owen Anderson69243822009-07-13 04:10:07 +00001240
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001241 //===--------------------------------------------------------------------===//
John McCallbdc4d802011-07-09 01:37:26 +00001242 // Cleanups
1243 //===--------------------------------------------------------------------===//
1244
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001245 typedef void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty);
John McCallbdc4d802011-07-09 01:37:26 +00001246
John McCall2673c682011-07-11 08:38:19 +00001247 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001248 Address arrayEndPointer,
John McCall2673c682011-07-11 08:38:19 +00001249 QualType elementType,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001250 CharUnits elementAlignment,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001251 Destroyer *destroyer);
John McCall2673c682011-07-11 08:38:19 +00001252 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1253 llvm::Value *arrayEnd,
1254 QualType elementType,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001255 CharUnits elementAlignment,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001256 Destroyer *destroyer);
John McCallbdc4d802011-07-09 01:37:26 +00001257
John McCall9928c482011-07-12 16:41:08 +00001258 void pushDestroy(QualType::DestructionKind dtorKind,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001259 Address addr, QualType type);
John McCall074cae02013-02-01 05:11:40 +00001260 void pushEHDestroy(QualType::DestructionKind dtorKind,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001261 Address addr, QualType type);
1262 void pushDestroy(CleanupKind kind, Address addr, QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001263 Destroyer *destroyer, bool useEHCleanupForArray);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001264 void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr,
Richard Smith8a07cd32013-06-12 20:42:33 +00001265 QualType type, Destroyer *destroyer,
1266 bool useEHCleanupForArray);
Stephen Hines176edba2014-12-01 14:53:08 -08001267 void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1268 llvm::Value *CompletePtr,
1269 QualType ElementType);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001270 void pushStackRestore(CleanupKind kind, Address SPMem);
1271 void emitDestroy(Address addr, QualType type, Destroyer *destroyer,
John McCall2673c682011-07-11 08:38:19 +00001272 bool useEHCleanupForArray);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001273 llvm::Function *generateDestroyHelper(Address addr, QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001274 Destroyer *destroyer,
David Blaikiec7971a92013-08-27 23:57:18 +00001275 bool useEHCleanupForArray,
1276 const VarDecl *VD);
John McCallbdc4d802011-07-09 01:37:26 +00001277 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001278 QualType elementType, CharUnits elementAlign,
1279 Destroyer *destroyer,
John McCallfbf780a2011-07-13 08:09:46 +00001280 bool checkZeroLength, bool useEHCleanup);
John McCallbdc4d802011-07-09 01:37:26 +00001281
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001282 Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
John McCall9928c482011-07-12 16:41:08 +00001283
John McCallbdc4d802011-07-09 01:37:26 +00001284 /// Determines whether an EH cleanup is required to destroy a type
1285 /// with the given destruction kind.
1286 bool needsEHCleanup(QualType::DestructionKind kind) {
1287 switch (kind) {
1288 case QualType::DK_none:
1289 return false;
1290 case QualType::DK_cxx_destructor:
1291 case QualType::DK_objc_weak_lifetime:
David Blaikie4e4d0842012-03-11 07:00:24 +00001292 return getLangOpts().Exceptions;
John McCallbdc4d802011-07-09 01:37:26 +00001293 case QualType::DK_objc_strong_lifetime:
David Blaikie4e4d0842012-03-11 07:00:24 +00001294 return getLangOpts().Exceptions &&
John McCallbdc4d802011-07-09 01:37:26 +00001295 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1296 }
1297 llvm_unreachable("bad destruction kind");
1298 }
1299
John McCall9928c482011-07-12 16:41:08 +00001300 CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1301 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1302 }
1303
John McCallbdc4d802011-07-09 01:37:26 +00001304 //===--------------------------------------------------------------------===//
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001305 // Objective-C
1306 //===--------------------------------------------------------------------===//
1307
Chris Lattner391d77a2008-03-30 23:03:07 +00001308 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001309
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001310 void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001311
Mike Stump0dd9e882009-02-08 23:14:22 +00001312 /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +00001313 void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1314 const ObjCPropertyImplDecl *PID);
John McCall1e1f4872011-09-13 03:34:09 +00001315 void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
Fariborz Jahanianb6e5fe32012-01-07 18:56:22 +00001316 const ObjCPropertyImplDecl *propImpl,
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001317 const ObjCMethodDecl *GetterMothodDecl,
Fariborz Jahanianb6e5fe32012-01-07 18:56:22 +00001318 llvm::Constant *AtomicHelperFn);
Fariborz Jahanian2846b972011-02-18 19:15:13 +00001319
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00001320 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1321 ObjCMethodDecl *MD, bool ctor);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001322
Mike Stump0dd9e882009-02-08 23:14:22 +00001323 /// GenerateObjCSetter - Synthesize an Objective-C property setter function
1324 /// for the given property.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +00001325 void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1326 const ObjCPropertyImplDecl *PID);
John McCall71c758d2011-09-10 09:17:20 +00001327 void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
Fariborz Jahaniancd93b962012-01-06 22:33:54 +00001328 const ObjCPropertyImplDecl *propImpl,
1329 llvm::Constant *AtomicHelperFn);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001330
Mike Stump4e7a1f72009-02-21 20:00:35 +00001331 //===--------------------------------------------------------------------===//
1332 // Block Bits
1333 //===--------------------------------------------------------------------===//
1334
John McCall6b5a61b2011-02-07 10:33:21 +00001335 llvm::Value *EmitBlockLiteral(const BlockExpr *);
John McCall1a343eb2011-11-10 08:15:53 +00001336 llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
1337 static void destroyBlockInfos(CGBlockInfo *info);
Mike Stump4e7a1f72009-02-21 20:00:35 +00001338
Fariborz Jahanian564360b2010-06-24 00:08:06 +00001339 llvm::Function *GenerateBlockFunction(GlobalDecl GD,
John McCall6b5a61b2011-02-07 10:33:21 +00001340 const CGBlockInfo &Info,
Eli Friedman64bee652012-02-25 02:48:22 +00001341 const DeclMapTy &ldm,
1342 bool IsLambdaConversionToBlock);
Mike Stump4e7a1f72009-02-21 20:00:35 +00001343
John McCalld16c2cf2011-02-08 08:22:06 +00001344 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1345 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001346 llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
1347 const ObjCPropertyImplDecl *PID);
1348 llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
1349 const ObjCPropertyImplDecl *PID);
Eli Friedmancae40c42012-02-28 01:08:45 +00001350 llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
John McCalld16c2cf2011-02-08 08:22:06 +00001351
John McCalld16c2cf2011-02-08 08:22:06 +00001352 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1353
John McCall5af02db2011-03-31 01:59:53 +00001354 class AutoVarEmission;
1355
1356 void emitByrefStructureInit(const AutoVarEmission &emission);
1357 void enterByrefCleanup(const AutoVarEmission &emission);
1358
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001359 void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum,
1360 llvm::Value *ptr);
Mike Stump4e7a1f72009-02-21 20:00:35 +00001361
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001362 Address LoadBlockStruct();
1363 Address GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
1364
1365 /// BuildBlockByrefAddress - Computes the location of the
1366 /// data in a variable which is declared as __block.
1367 Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V,
1368 bool followForward = true);
1369 Address emitBlockByrefAddress(Address baseAddr,
1370 const BlockByrefInfo &info,
1371 bool followForward,
1372 const llvm::Twine &name);
1373
1374 const BlockByrefInfo &getBlockByrefInfo(const VarDecl *var);
Mike Stumpdab514f2009-03-04 03:23:46 +00001375
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001376 QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args);
1377
John McCalld26bc762011-03-09 04:27:21 +00001378 void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1379 const CGFunctionInfo &FnInfo);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001380 /// \brief Emit code for the start of a function.
1381 /// \param Loc The location to be associated with the function.
1382 /// \param StartLoc The location of the function body.
John McCallf5ebf9b2013-05-03 07:33:41 +00001383 void StartFunction(GlobalDecl GD,
1384 QualType RetTy,
Daniel Dunbar7c086512008-09-09 23:14:03 +00001385 llvm::Function *Fn,
John McCalld26bc762011-03-09 04:27:21 +00001386 const CGFunctionInfo &FnInfo,
Daniel Dunbar2284ac92008-10-18 18:22:23 +00001387 const FunctionArgList &Args,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001388 SourceLocation Loc = SourceLocation(),
1389 SourceLocation StartLoc = SourceLocation());
Daniel Dunbara448fb22008-11-11 23:11:34 +00001390
John McCall9fc6a772010-02-19 09:25:03 +00001391 void EmitConstructorBody(FunctionArgList &Args);
1392 void EmitDestructorBody(FunctionArgList &Args);
Lang Hames56c00c42013-02-17 07:22:09 +00001393 void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
Richard Smith3cebc732013-11-05 09:12:18 +00001394 void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001395 void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S);
John McCalla355e072010-02-18 03:17:58 +00001396
Faisal Valid6992ab2013-09-29 08:45:24 +00001397 void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
Eli Friedman64bee652012-02-25 02:48:22 +00001398 CallArgList &CallArgs);
Eli Friedmanbd89f8c2012-02-16 01:37:33 +00001399 void EmitLambdaToBlockPointerBody(FunctionArgList &Args);
Eli Friedman64bee652012-02-25 02:48:22 +00001400 void EmitLambdaBlockInvokeBody();
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001401 void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
1402 void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD);
Stephen Hines176edba2014-12-01 14:53:08 -08001403 void EmitAsanPrologueOrEpilogue(bool Prologue);
Eli Friedmanbd89f8c2012-02-16 01:37:33 +00001404
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001405 /// \brief Emit the unified return block, trying to avoid its emission when
1406 /// possible.
1407 /// \return The debug location of the user written return statement if the
1408 /// return block is is avoided.
1409 llvm::DebugLoc EmitReturnBlock();
Daniel Dunbar1c1d6072009-01-26 23:27:52 +00001410
Mike Stump0dd9e882009-02-08 23:14:22 +00001411 /// FinishFunction - Complete IR generation of the current function. It is
1412 /// legal to call this function even if there is no current insertion point.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001413 void FinishFunction(SourceLocation EndLoc=SourceLocation());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001414
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001415 void StartThunk(llvm::Function *Fn, GlobalDecl GD,
1416 const CGFunctionInfo &FnInfo);
Hans Wennborg93b717a2013-11-15 17:24:45 +00001417
Stephen Hines176edba2014-12-01 14:53:08 -08001418 void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk);
1419
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001420 void FinishThunk();
1421
Stephen Hines176edba2014-12-01 14:53:08 -08001422 /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
1423 void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr,
1424 llvm::Value *Callee);
Hans Wennborg93b717a2013-11-15 17:24:45 +00001425
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001426 /// Generate a thunk for the given method.
1427 void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
John McCalld26bc762011-03-09 04:27:21 +00001428 GlobalDecl GD, const ThunkInfo &Thunk);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001429
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001430 llvm::Function *GenerateVarArgsThunk(llvm::Function *Fn,
1431 const CGFunctionInfo &FnInfo,
1432 GlobalDecl GD, const ThunkInfo &Thunk);
Eli Friedman7dcdf5b2011-05-06 17:27:27 +00001433
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001434 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1435 FunctionArgList &Args);
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Eli Friedmanb74ed082012-02-14 02:31:03 +00001437 void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init,
1438 ArrayRef<VarDecl *> ArrayIndexes);
1439
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001440 /// Struct with all informations about dynamic [sub]class needed to set vptr.
1441 struct VPtr {
1442 BaseSubobject Base;
1443 const CXXRecordDecl *NearestVBase;
1444 CharUnits OffsetFromNearestVBase;
1445 const CXXRecordDecl *VTableClass;
1446 };
1447
1448 /// Initialize the vtable pointer of the given subobject.
1449 void InitializeVTablePointer(const VPtr &vptr);
1450
1451 typedef llvm::SmallVector<VPtr, 4> VPtrsVector;
Anders Carlssond103f9f2010-03-28 19:40:00 +00001452
1453 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001454 VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass);
1455
1456 void getVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase,
1457 CharUnits OffsetFromNearestVBase,
1458 bool BaseIsNonVirtualPrimaryBase,
1459 const CXXRecordDecl *VTableClass,
1460 VisitedVirtualBasesSetTy &VBases, VPtrsVector &vptrs);
Eli Friedman77a259c2009-12-08 06:46:18 +00001461
Anders Carlsson603d6d12010-03-28 21:07:49 +00001462 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
Anders Carlssond103f9f2010-03-28 19:40:00 +00001463
Dan Gohman043fb9a2010-10-26 18:44:08 +00001464 /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1465 /// to by This.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001466 llvm::Value *GetVTablePtr(Address This, llvm::Type *VTableTy,
1467 const CXXRecordDecl *VTableClass);
1468
1469 enum CFITypeCheckKind {
1470 CFITCK_VCall,
1471 CFITCK_NVCall,
1472 CFITCK_DerivedCast,
1473 CFITCK_UnrelatedCast,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001474 CFITCK_ICall,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001475 };
Anders Carlssond103f9f2010-03-28 19:40:00 +00001476
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001477 /// \brief Derived is the presumed address of an object of type T after a
1478 /// cast. If T is a polymorphic class type, emit a check that the virtual
1479 /// table for Derived belongs to a class derived from T.
1480 void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001481 bool MayBeNull, CFITypeCheckKind TCK,
1482 SourceLocation Loc);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001483
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001484 /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
1485 /// If vptr CFI is enabled, emit a check that VTable is valid.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001486 void EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001487 CFITypeCheckKind TCK, SourceLocation Loc);
Benjamin Kramer9581ed02013-08-25 22:46:27 +00001488
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001489 /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001490 /// RD using llvm.type.test.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001491 void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable,
1492 CFITypeCheckKind TCK, SourceLocation Loc);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001493
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001494 /// If whole-program virtual table optimization is enabled, emit an assumption
1495 /// that VTable is a member of RD's type identifier. Or, if vptr CFI is
1496 /// enabled, emit a check that VTable is a member of RD's type identifier.
1497 void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD,
1498 llvm::Value *VTable, SourceLocation Loc);
1499
1500 /// Returns whether we should perform a type checked load when loading a
1501 /// virtual function for virtual calls to members of RD. This is generally
1502 /// true when both vcall CFI and whole-program-vtables are enabled.
1503 bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD);
1504
1505 /// Emit a type checked load from the given vtable.
1506 llvm::Value *EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable,
1507 uint64_t VTableByteOffset);
1508
Benjamin Kramer9581ed02013-08-25 22:46:27 +00001509 /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
1510 /// expr can be devirtualized.
1511 bool CanDevirtualizeMemberFunctionCall(const Expr *Base,
1512 const CXXMethodDecl *MD);
1513
John McCall50da2ca2010-07-21 05:30:47 +00001514 /// EnterDtorCleanups - Enter the cleanups necessary to complete the
1515 /// given phase of destruction for a destructor. The end result
1516 /// should call destructors on members and base classes in reverse
1517 /// order of their construction.
1518 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Chris Lattner7255a2d2010-06-22 00:03:40 +00001520 /// ShouldInstrumentFunction - Return true if the current function should be
1521 /// instrumented with __cyg_profile_func_* calls
1522 bool ShouldInstrumentFunction();
1523
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001524 /// ShouldXRayInstrument - Return true if the current function should be
1525 /// instrumented with XRay nop sleds.
1526 bool ShouldXRayInstrumentFunction() const;
1527
Chris Lattner7255a2d2010-06-22 00:03:40 +00001528 /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1529 /// instrumentation function with the current function and the call site, if
1530 /// function instrumentation is enabled.
1531 void EmitFunctionInstrumentation(const char *Fn);
1532
Roman Divackybe4c8702011-02-10 16:52:03 +00001533 /// EmitMCountInstrumentation - Emit call to .mcount.
1534 void EmitMCountInstrumentation();
1535
Mike Stump0dd9e882009-02-08 23:14:22 +00001536 /// EmitFunctionProlog - Emit the target specific LLVM code to load the
1537 /// arguments for the given function. This is also responsible for naming the
1538 /// LLVM function arguments.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001539 void EmitFunctionProlog(const CGFunctionInfo &FI,
1540 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001541 const FunctionArgList &Args);
1542
Mike Stump0dd9e882009-02-08 23:14:22 +00001543 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
1544 /// given temporary.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001545 void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
1546 SourceLocation EndLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001547
Mike Stumpcce3d4f2009-12-07 23:38:24 +00001548 /// EmitStartEHSpec - Emit the start of the exception spec.
1549 void EmitStartEHSpec(const Decl *D);
1550
1551 /// EmitEndEHSpec - Emit the end of the exception spec.
1552 void EmitEndEHSpec(const Decl *D);
1553
John McCallf1549f62010-07-06 01:34:17 +00001554 /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1555 llvm::BasicBlock *getTerminateLandingPad();
1556
1557 /// getTerminateHandler - Return a handler (not a landing pad, just
1558 /// a catch handler) that just calls terminate. This is used when
1559 /// a terminate scope encloses a try.
Mike Stump9b39c512009-12-09 22:59:31 +00001560 llvm::BasicBlock *getTerminateHandler();
1561
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001562 llvm::Type *ConvertTypeForMem(QualType T);
1563 llvm::Type *ConvertType(QualType T);
1564 llvm::Type *ConvertType(const TypeDecl *T) {
John McCallbff225e2010-02-16 04:15:37 +00001565 return ConvertType(getContext().getTypeDeclType(T));
1566 }
Chris Lattnerc8aa5f12008-04-04 04:07:35 +00001567
Mike Stump0dd9e882009-02-08 23:14:22 +00001568 /// LoadObjCSelf - Load the value of self. This function is only valid while
1569 /// generating code for an Objective-C method.
Chris Lattnerc8aa5f12008-04-04 04:07:35 +00001570 llvm::Value *LoadObjCSelf();
Mike Stump0dd9e882009-02-08 23:14:22 +00001571
1572 /// TypeOfSelfObject - Return type of object that this self represents.
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001573 QualType TypeOfSelfObject();
Chris Lattner41110242008-06-17 18:05:57 +00001574
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 /// hasAggregateLLVMType - Return true if the specified AST type will map into
1576 /// an aggregate LLVM type or is void.
John McCall9d232c82013-03-07 21:37:08 +00001577 static TypeEvaluationKind getEvaluationKind(QualType T);
1578
1579 static bool hasScalarEvaluationKind(QualType T) {
1580 return getEvaluationKind(T) == TEK_Scalar;
1581 }
1582
1583 static bool hasAggregateEvaluationKind(QualType T) {
1584 return getEvaluationKind(T) == TEK_Aggregate;
1585 }
Daniel Dunbar55e87422008-11-11 02:29:29 +00001586
1587 /// createBasicBlock - Create an LLVM basic block.
Richard Smith4def70d2012-10-09 19:52:38 +00001588 llvm::BasicBlock *createBasicBlock(const Twine &name = "",
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001589 llvm::Function *parent = nullptr,
1590 llvm::BasicBlock *before = nullptr) {
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001591#ifdef NDEBUG
John McCalld16c2cf2011-02-08 08:22:06 +00001592 return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001593#else
John McCalld16c2cf2011-02-08 08:22:06 +00001594 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001595#endif
Daniel Dunbar55e87422008-11-11 02:29:29 +00001596 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001597
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
1599 /// label maps to.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001600 JumpDest getJumpDestForLabel(const LabelDecl *S);
Mike Stump0dd9e882009-02-08 23:14:22 +00001601
Mike Stumpf71d2322009-11-30 20:08:49 +00001602 /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1603 /// another basic block, simplify it. This assumes that no other code could
1604 /// potentially reference the basic block.
Daniel Dunbaraa5bd872009-04-01 04:37:47 +00001605 void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1606
Mike Stump0dd9e882009-02-08 23:14:22 +00001607 /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
1608 /// adding a fall-through branch from the current insert block if
1609 /// necessary. It is legal to call this function even if there is no current
1610 /// insertion point.
Daniel Dunbara0c21a82008-11-13 01:24:05 +00001611 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00001612 /// IsFinished - If true, indicates that the caller has finished emitting
1613 /// branches to the given block and does not expect to emit code into it. This
1614 /// means the block can be ignored if it is unreachable.
Daniel Dunbara0c21a82008-11-13 01:24:05 +00001615 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
Daniel Dunbar824e3bd2008-11-11 04:34:23 +00001616
John McCall777d6e52011-08-11 02:22:43 +00001617 /// EmitBlockAfterUses - Emit the given block somewhere hopefully
1618 /// near its uses, and leave the insertion point in it.
1619 void EmitBlockAfterUses(llvm::BasicBlock *BB);
1620
Mike Stump0dd9e882009-02-08 23:14:22 +00001621 /// EmitBranch - Emit a branch to the specified basic block from the current
1622 /// insert block, taking care to avoid creation of branches from dummy
1623 /// blocks. It is legal to call this function even if there is no current
1624 /// insertion point.
Daniel Dunbar5e08ad32008-11-11 22:06:59 +00001625 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00001626 /// This function clears the current insertion point. The caller should follow
1627 /// calls to this function with calls to Emit*Block prior to generation new
1628 /// code.
Daniel Dunbard57a8712008-11-11 09:41:28 +00001629 void EmitBranch(llvm::BasicBlock *Block);
1630
Mike Stump0dd9e882009-02-08 23:14:22 +00001631 /// HaveInsertPoint - True if an insertion point is defined. If not, this
1632 /// indicates that the current code being emitted is unreachable.
1633 bool HaveInsertPoint() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001634 return Builder.GetInsertBlock() != nullptr;
Daniel Dunbara448fb22008-11-11 23:11:34 +00001635 }
1636
Mike Stump0dd9e882009-02-08 23:14:22 +00001637 /// EnsureInsertPoint - Ensure that an insertion point is defined so that
1638 /// emitted IR has a place to go. Note that by definition, if this function
1639 /// creates a block then that block is unreachable; callers may do better to
1640 /// detect when no insertion point is defined and simply skip IR generation.
Daniel Dunbara448fb22008-11-11 23:11:34 +00001641 void EnsureInsertPoint() {
1642 if (!HaveInsertPoint())
1643 EmitBlock(createBasicBlock());
1644 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001645
Daniel Dunbar488e9932008-08-16 00:56:44 +00001646 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerdc5e8262007-12-02 01:43:38 +00001647 /// specified stmt yet.
David Blaikie0a1c8622013-08-19 21:02:26 +00001648 void ErrorUnsupported(const Stmt *S, const char *Type);
Reid Spencer5f016e22007-07-11 17:01:13 +00001649
1650 //===--------------------------------------------------------------------===//
1651 // Helpers
1652 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00001653
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001654 LValue MakeAddrLValue(Address Addr, QualType T,
1655 AlignmentSource AlignSource = AlignmentSource::Type) {
1656 return LValue::MakeAddr(Addr, T, getContext(), AlignSource,
Dan Gohman3d5aff52010-10-14 23:06:10 +00001657 CGM.getTBAAInfo(T));
Daniel Dunbar5cf8bfe2010-08-21 02:53:44 +00001658 }
John McCalle0c11682012-07-02 23:58:38 +00001659
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001660 LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment,
1661 AlignmentSource AlignSource = AlignmentSource::Type) {
1662 return LValue::MakeAddr(Address(V, Alignment), T, getContext(),
1663 AlignSource, CGM.getTBAAInfo(T));
1664 }
1665
1666 LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T);
Stephen Hines176edba2014-12-01 14:53:08 -08001667 LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001668 CharUnits getNaturalTypeAlignment(QualType T,
1669 AlignmentSource *Source = nullptr,
1670 bool forPointeeType = false);
1671 CharUnits getNaturalPointeeTypeAlignment(QualType T,
1672 AlignmentSource *Source = nullptr);
1673
1674 Address EmitLoadOfReference(Address Ref, const ReferenceType *RefTy,
1675 AlignmentSource *Source = nullptr);
1676 LValue EmitLoadOfReferenceLValue(Address Ref, const ReferenceType *RefTy);
Daniel Dunbar5cf8bfe2010-08-21 02:53:44 +00001677
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001678 Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy,
1679 AlignmentSource *Source = nullptr);
1680 LValue EmitLoadOfPointerLValue(Address Ptr, const PointerType *PtrTy);
1681
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
Daniel Dunbar195337d2010-02-09 02:48:28 +00001683 /// block. The caller is responsible for setting an appropriate alignment on
1684 /// the alloca.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001685 llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
Chris Lattner8cc488f2011-07-20 07:06:53 +00001686 const Twine &Name = "tmp");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001687 Address CreateTempAlloca(llvm::Type *Ty, CharUnits align,
1688 const Twine &Name = "tmp");
Mike Stump0dd9e882009-02-08 23:14:22 +00001689
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001690 /// CreateDefaultAlignedTempAlloca - This creates an alloca with the
1691 /// default ABI alignment of the given LLVM type.
1692 ///
1693 /// IMPORTANT NOTE: This is *not* generally the right alignment for
1694 /// any given AST type that happens to have been lowered to the
1695 /// given IR type. This should only ever be used for function-local,
1696 /// IR-driven manipulations like saving and restoring a value. Do
1697 /// not hand this address off to arbitrary IRGen routines, and especially
1698 /// do not pass it as an argument to a function that might expect a
1699 /// properly ABI-aligned value.
1700 Address CreateDefaultAlignTempAlloca(llvm::Type *Ty,
1701 const Twine &Name = "tmp");
1702
1703 /// InitTempAlloca - Provide an initial value for the given alloca which
1704 /// will be observable at all locations in the function.
1705 ///
1706 /// The address should be something that was returned from one of
1707 /// the CreateTempAlloca or CreateMemTemp routines, and the
1708 /// initializer must be valid in the entry block (i.e. it must
1709 /// either be a constant or an argument value).
1710 void InitTempAlloca(Address Alloca, llvm::Value *Value);
John McCallac418162010-04-22 01:10:34 +00001711
Daniel Dunbar9bd4da22010-02-16 19:44:13 +00001712 /// CreateIRTemp - Create a temporary IR object of the given type, with
1713 /// appropriate alignment. This routine should only be used when an temporary
1714 /// value needs to be stored into an alloca (for example, to avoid explicit
1715 /// PHI construction), but the type is the IR type, not the type appropriate
1716 /// for storing in memory.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001717 ///
1718 /// That is, this is exactly equivalent to CreateMemTemp, but calling
1719 /// ConvertType instead of ConvertTypeForMem.
1720 Address CreateIRTemp(QualType T, const Twine &Name = "tmp");
Daniel Dunbar9bd4da22010-02-16 19:44:13 +00001721
Daniel Dunbar195337d2010-02-09 02:48:28 +00001722 /// CreateMemTemp - Create a temporary memory object of the given type, with
1723 /// appropriate alignment.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001724 Address CreateMemTemp(QualType T, const Twine &Name = "tmp");
1725 Address CreateMemTemp(QualType T, CharUnits Align, const Twine &Name = "tmp");
Daniel Dunbar195337d2010-02-09 02:48:28 +00001726
John McCall558d2ab2010-09-15 10:14:12 +00001727 /// CreateAggTemp - Create a temporary memory object for the given
1728 /// aggregate type.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001729 AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001730 return AggValueSlot::forAddr(CreateMemTemp(T, Name),
Eli Friedmanf3940782011-12-03 00:54:26 +00001731 T.getQualifiers(),
John McCall7c2349b2011-08-25 20:40:09 +00001732 AggValueSlot::IsNotDestructed,
John McCall410ffb22011-08-25 23:04:34 +00001733 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier649b4a12012-03-29 17:37:10 +00001734 AggValueSlot::IsNotAliased);
John McCall558d2ab2010-09-15 10:14:12 +00001735 }
1736
John McCalld16c2cf2011-02-08 08:22:06 +00001737 /// Emit a cast to void* in the appropriate address space.
1738 llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1739
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
1741 /// expression and compare the result against zero, returning an Int1Ty value.
1742 llvm::Value *EvaluateExprAsBool(const Expr *E);
1743
John McCall2a416372010-12-05 02:00:02 +00001744 /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
1745 void EmitIgnoredExpr(const Expr *E);
1746
Chris Lattner9b655512007-08-31 22:49:20 +00001747 /// EmitAnyExpr - Emit code to compute the specified expression which can have
1748 /// any type. The result is returned as an RValue struct. If this is an
1749 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
1750 /// the result should be returned.
Mike Stump49d1cd52009-05-26 22:03:21 +00001751 ///
Dmitri Gribenko70517ca2012-08-23 17:58:28 +00001752 /// \param ignoreResult True if the resulting value isn't used.
John McCall558d2ab2010-09-15 10:14:12 +00001753 RValue EmitAnyExpr(const Expr *E,
John McCalle0c11682012-07-02 23:58:38 +00001754 AggValueSlot aggSlot = AggValueSlot::ignored(),
1755 bool ignoreResult = false);
Devang Pateld9363c32007-09-28 21:49:18 +00001756
Mike Stump0dd9e882009-02-08 23:14:22 +00001757 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
1758 // or the value of the expression, depending on how va_list is defined.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001759 Address EmitVAListRef(const Expr *E);
1760
1761 /// Emit a "reference" to a __builtin_ms_va_list; this is
1762 /// always the value of the expression, because a __builtin_ms_va_list is a
1763 /// pointer to a char.
1764 Address EmitMSVAListRef(const Expr *E);
Eli Friedman4fd0aa52009-01-20 17:46:04 +00001765
Mike Stump0dd9e882009-02-08 23:14:22 +00001766 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
1767 /// always be accessible even if no aggregate location is provided.
John McCall558d2ab2010-09-15 10:14:12 +00001768 RValue EmitAnyExprToTemp(const Expr *E);
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001769
John McCall60d33652011-03-08 09:11:50 +00001770 /// EmitAnyExprToMem - Emits the code necessary to evaluate an
Chad Rosier649b4a12012-03-29 17:37:10 +00001771 /// arbitrary expression into the given memory location.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001772 void EmitAnyExprToMem(const Expr *E, Address Location,
Chad Rosier649b4a12012-03-29 17:37:10 +00001773 Qualifiers Quals, bool IsInitializer);
John McCall3d3ec1c2010-04-21 10:05:39 +00001774
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001775 void EmitAnyExprToExn(const Expr *E, Address Addr);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001776
John McCall60d33652011-03-08 09:11:50 +00001777 /// EmitExprAsInit - Emits the code necessary to initialize a
1778 /// location in memory with the given initializer.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001779 void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1780 bool capturedByInit);
John McCall60d33652011-03-08 09:11:50 +00001781
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +00001782 /// hasVolatileMember - returns true if aggregate type has a volatile
1783 /// member.
1784 bool hasVolatileMember(QualType T) {
1785 if (const RecordType *RT = T->getAs<RecordType>()) {
1786 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
1787 return RD->hasVolatileMember();
1788 }
1789 return false;
1790 }
Arnaud A. de Grandmaison8fcbb8d2013-02-05 09:06:17 +00001791 /// EmitAggregateCopy - Emit an aggregate assignment.
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001792 ///
1793 /// The difference to EmitAggregateCopy is that tail padding is not copied.
1794 /// This is required for correctness when assigning non-POD structures in C++.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001795 void EmitAggregateAssign(Address DestPtr, Address SrcPtr,
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +00001796 QualType EltTy) {
1797 bool IsVolatile = hasVolatileMember(EltTy);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001798 EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, true);
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001799 }
1800
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001801 void EmitAggregateCopyCtor(Address DestPtr, Address SrcPtr,
1802 QualType DestTy, QualType SrcTy) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001803 EmitAggregateCopy(DestPtr, SrcPtr, SrcTy, /*IsVolatile=*/false,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001804 /*IsAssignment=*/false);
1805 }
1806
Arnaud A. de Grandmaison8fcbb8d2013-02-05 09:06:17 +00001807 /// EmitAggregateCopy - Emit an aggregate copy.
Mike Stump27fe2e62009-05-23 22:29:41 +00001808 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001809 /// \param isVolatile - True iff either the source or the destination is
Mike Stump27fe2e62009-05-23 22:29:41 +00001810 /// volatile.
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001811 /// \param isAssignment - If false, allow padding to be copied. This often
1812 /// yields more efficient.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001813 void EmitAggregateCopy(Address DestPtr, Address SrcPtr,
Eli Friedmanbd7d8282011-12-05 22:23:28 +00001814 QualType EltTy, bool isVolatile=false,
Benjamin Kramer6cacae82012-09-30 12:43:37 +00001815 bool isAssignment = false);
Daniel Dunbar7482d122008-09-09 20:49:46 +00001816
Anders Carlssondde0a942008-09-11 09:15:33 +00001817 /// GetAddrOfLocalVar - Return the address of a local variable.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001818 Address GetAddrOfLocalVar(const VarDecl *VD) {
1819 auto it = LocalDeclMap.find(VD);
1820 assert(it != LocalDeclMap.end() &&
1821 "Invalid argument to GetAddrOfLocalVar(), no decl!");
1822 return it->second;
John McCall4c40d982010-08-31 07:33:07 +00001823 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001824
John McCall56ca35d2011-02-17 10:25:35 +00001825 /// getOpaqueLValueMapping - Given an opaque value expression (which
1826 /// must be mapped to an l-value), return its mapping.
1827 const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
1828 assert(OpaqueValueMapping::shouldBindAsLValue(e));
1829
1830 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
1831 it = OpaqueLValues.find(e);
1832 assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
1833 return it->second;
1834 }
1835
1836 /// getOpaqueRValueMapping - Given an opaque value expression (which
1837 /// must be mapped to an r-value), return its mapping.
1838 const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
1839 assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1840
1841 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
1842 it = OpaqueRValues.find(e);
1843 assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
John McCalle996ffd2011-02-16 08:02:54 +00001844 return it->second;
1845 }
1846
Dan Gohman4f8d1232008-05-22 00:50:06 +00001847 /// getAccessedFieldNo - Given an encoded value and a result number, return
1848 /// the input field number being accessed.
1849 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
1850
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001851 llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
Chris Lattner3d00fdc2009-10-13 06:55:33 +00001852 llvm::BasicBlock *GetIndirectGotoBlock();
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00001853
Anders Carlsson1884eb02010-05-22 17:35:42 +00001854 /// EmitNullInitialization - Generate code to set a value of the given type to
1855 /// null, If the type contains data member pointers, they will be initialized
1856 /// to -1 in accordance with the Itanium C++ ABI.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001857 void EmitNullInitialization(Address DestPtr, QualType Ty);
Anders Carlssonddf7cac2008-11-04 05:30:00 +00001858
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001859 /// Emits a call to an LLVM variable-argument intrinsic, either
1860 /// \c llvm.va_start or \c llvm.va_end.
1861 /// \param ArgValue A reference to the \c va_list as emitted by either
1862 /// \c EmitVAListRef or \c EmitMSVAListRef.
1863 /// \param IsStart If \c true, emits a call to \c llvm.va_start; otherwise,
1864 /// calls \c llvm.va_end.
1865 llvm::Value *EmitVAStartEnd(llvm::Value *ArgValue, bool IsStart);
1866
1867 /// Generate code to get an argument from the passed in pointer
1868 /// and update it accordingly.
1869 /// \param VE The \c VAArgExpr for which to generate code.
1870 /// \param VAListAddr Receives a reference to the \c va_list as emitted by
1871 /// either \c EmitVAListRef or \c EmitMSVAListRef.
1872 /// \returns A pointer to the argument.
Anders Carlssonddf7cac2008-11-04 05:30:00 +00001873 // FIXME: We should be able to get rid of this method and use the va_arg
Mike Stump0dd9e882009-02-08 23:14:22 +00001874 // instruction in LLVM instead once it works well enough.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001875 Address EmitVAArg(VAArgExpr *VE, Address &VAListAddr);
Anders Carlssonf666b772008-12-20 20:27:15 +00001876
John McCallbdc4d802011-07-09 01:37:26 +00001877 /// emitArrayLength - Compute the length of an array, even if it's a
1878 /// VLA, and drill down to the base element type.
1879 llvm::Value *emitArrayLength(const ArrayType *arrayType,
1880 QualType &baseType,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001881 Address &addr);
John McCallbdc4d802011-07-09 01:37:26 +00001882
John McCallbc8d40d2011-06-24 21:55:10 +00001883 /// EmitVLASize - Capture all the sizes for the VLA expressions in
1884 /// the given variably-modified type and store them in the VLASizeMap.
Daniel Dunbard286f052009-07-19 06:58:07 +00001885 ///
1886 /// This function can be called with a null (unreachable) insert point.
John McCallbc8d40d2011-06-24 21:55:10 +00001887 void EmitVariablyModifiedType(QualType Ty);
Mike Stump0dd9e882009-02-08 23:14:22 +00001888
John McCallbc8d40d2011-06-24 21:55:10 +00001889 /// getVLASize - Returns an LLVM value that corresponds to the size,
1890 /// in non-variably-sized elements, of a variable length array type,
1891 /// plus that largest non-variably-sized element type. Assumes that
1892 /// the type has already been emitted with EmitVariablyModifiedType.
1893 std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1894 std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
Anders Carlssondcc90d82008-12-12 07:19:02 +00001895
Anders Carlsson5f4307b2009-04-14 16:58:56 +00001896 /// LoadCXXThis - Load the value of 'this'. This function is only valid while
1897 /// generating code for an C++ member function.
John McCall25049412010-02-16 22:04:33 +00001898 llvm::Value *LoadCXXThis() {
1899 assert(CXXThisValue && "no 'this' value for this function");
1900 return CXXThisValue;
1901 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001902 Address LoadCXXThisAddress();
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Anders Carlssonc997d422010-01-02 01:01:18 +00001904 /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1905 /// virtual bases.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001906 // FIXME: Every place that calls LoadCXXVTT is something
1907 // that needs to be abstracted properly.
John McCall25049412010-02-16 22:04:33 +00001908 llvm::Value *LoadCXXVTT() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001909 assert(CXXStructorImplicitParamValue && "no VTT value for this function");
1910 return CXXStructorImplicitParamValue;
1911 }
1912
John McCallbff225e2010-02-16 04:15:37 +00001913 /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
Anders Carlsson8561a862010-04-24 23:01:49 +00001914 /// complete class to the given direct base.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001915 Address
1916 GetAddressOfDirectBaseInCompleteClass(Address Value,
Anders Carlsson8561a862010-04-24 23:01:49 +00001917 const CXXRecordDecl *Derived,
1918 const CXXRecordDecl *Base,
1919 bool BaseIsVirtual);
Anders Carlssona88ad562010-04-24 21:51:08 +00001920
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001921 static bool ShouldNullCheckClassCastValue(const CastExpr *Cast);
1922
Mike Stumpf71d2322009-11-30 20:08:49 +00001923 /// GetAddressOfBaseClass - This function will add the necessary delta to the
1924 /// load of 'this' and returns address of the base class.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001925 Address GetAddressOfBaseClass(Address Value,
1926 const CXXRecordDecl *Derived,
1927 CastExpr::path_const_iterator PathBegin,
1928 CastExpr::path_const_iterator PathEnd,
1929 bool NullCheckValue, SourceLocation Loc);
Anders Carlsson34a2d382010-04-24 21:06:20 +00001930
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001931 Address GetAddressOfDerivedClass(Address Value,
1932 const CXXRecordDecl *Derived,
1933 CastExpr::path_const_iterator PathBegin,
1934 CastExpr::path_const_iterator PathEnd,
1935 bool NullCheckValue);
Anders Carlssona3697c92009-11-23 17:57:54 +00001936
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001937 /// GetVTTParameter - Return the VTT parameter that should be passed to a
1938 /// base constructor/destructor with virtual bases.
1939 /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
1940 /// to ItaniumCXXABI.cpp together with all the references to VTT.
1941 llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
1942 bool Delegating);
1943
John McCallc0bf4622010-02-23 00:48:20 +00001944 void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1945 CXXCtorType CtorType,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001946 const FunctionArgList &Args,
1947 SourceLocation Loc);
Sean Hunt059ce0d2011-05-01 07:04:31 +00001948 // It's important not to confuse this and the previous function. Delegating
1949 // constructors are the C++0x feature. The constructor delegate optimization
1950 // is used to reduce duplication in the base and complete consturctors where
1951 // they are substantially the same.
1952 void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1953 const FunctionArgList &Args);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001954
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001955 /// Emit a call to an inheriting constructor (that is, one that invokes a
1956 /// constructor inherited from a base class) by inlining its definition. This
1957 /// is necessary if the ABI does not support forwarding the arguments to the
1958 /// base class constructor (because they're variadic or similar).
1959 void EmitInlinedInheritingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1960 CXXCtorType CtorType,
1961 bool ForVirtualBase,
1962 bool Delegating,
1963 CallArgList &Args);
1964
1965 /// Emit a call to a constructor inherited from a base class, passing the
1966 /// current constructor's arguments along unmodified (without even making
1967 /// a copy).
1968 void EmitInheritedCXXConstructorCall(const CXXConstructorDecl *D,
1969 bool ForVirtualBase, Address This,
1970 bool InheritedFromVBase,
1971 const CXXInheritedCtorInitExpr *E);
1972
Anders Carlsson155ed4a2010-05-02 23:20:53 +00001973 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
Douglas Gregor378e1e72013-01-31 05:50:40 +00001974 bool ForVirtualBase, bool Delegating,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001975 Address This, const CXXConstructExpr *E);
1976
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001977 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1978 bool ForVirtualBase, bool Delegating,
1979 Address This, CallArgList &Args);
1980
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001981 /// Emit assumption load for all bases. Requires to be be called only on
1982 /// most-derived class and not under construction of the object.
1983 void EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, Address This);
1984
1985 /// Emit assumption that vptr load == global vtable.
1986 void EmitVTableAssumptionLoad(const VPtr &vptr, Address This);
Stephen Hines176edba2014-12-01 14:53:08 -08001987
Fariborz Jahanian34999872010-11-13 21:53:34 +00001988 void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001989 Address This, Address Src,
1990 const CXXConstructExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001992 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001993 const ArrayType *ArrayTy,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001994 Address ArrayPtr,
Stephen Hines176edba2014-12-01 14:53:08 -08001995 const CXXConstructExpr *E,
Douglas Gregor59174c02010-07-21 01:10:17 +00001996 bool ZeroInitialization = false);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001997
Anders Carlsson569c1f42009-09-23 02:45:36 +00001998 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1999 llvm::Value *NumElements,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002000 Address ArrayPtr,
Stephen Hines176edba2014-12-01 14:53:08 -08002001 const CXXConstructExpr *E,
Douglas Gregor59174c02010-07-21 01:10:17 +00002002 bool ZeroInitialization = false);
Anders Carlssonb14095a2009-04-17 00:06:03 +00002003
John McCallbdc4d802011-07-09 01:37:26 +00002004 static Destroyer destroyCXXObject;
2005
Anders Carlsson7267c162009-05-29 21:03:38 +00002006 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
Douglas Gregor378e1e72013-01-31 05:50:40 +00002007 bool ForVirtualBase, bool Delegating,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002008 Address This);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002009
John McCall19705672011-09-15 06:49:18 +00002010 void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002011 llvm::Type *ElementTy, Address NewPtr,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002012 llvm::Value *NumElements,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002013 llvm::Value *AllocSizeWithoutCookie);
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Peter Collingbourne86811602011-11-27 22:09:22 +00002015 void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002016 Address Ptr);
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002018 llvm::Value *EmitLifetimeStart(uint64_t Size, llvm::Value *Addr);
2019 void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr);
2020
Anders Carlssona00703d2009-05-31 01:40:14 +00002021 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
Anders Carlsson60e282c2009-08-16 21:13:42 +00002022 void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Eli Friedman4bf81522009-11-18 00:57:03 +00002024 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
2025 QualType DeleteTy);
2026
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002027 RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
2028 const Expr *Arg, bool IsDelete);
2029
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002030 llvm::Value *EmitCXXTypeidExpr(const CXXTypeidExpr *E);
2031 llvm::Value *EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE);
2032 Address EmitCXXUuidofExpr(const CXXUuidofExpr *E);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00002033
Richard Smith2c9f87c2012-08-24 00:54:33 +00002034 /// \brief Situations in which we might emit a check for the suitability of a
2035 /// pointer or glvalue.
Richard Smith7ac9ef12012-09-08 02:08:36 +00002036 enum TypeCheckKind {
Richard Smith2c9f87c2012-08-24 00:54:33 +00002037 /// Checking the operand of a load. Must be suitably sized and aligned.
Richard Smith7ac9ef12012-09-08 02:08:36 +00002038 TCK_Load,
Richard Smith2c9f87c2012-08-24 00:54:33 +00002039 /// Checking the destination of a store. Must be suitably sized and aligned.
Richard Smith7ac9ef12012-09-08 02:08:36 +00002040 TCK_Store,
Richard Smith2c9f87c2012-08-24 00:54:33 +00002041 /// Checking the bound value in a reference binding. Must be suitably sized
2042 /// and aligned, but is not required to refer to an object (until the
2043 /// reference is used), per core issue 453.
Richard Smith7ac9ef12012-09-08 02:08:36 +00002044 TCK_ReferenceBinding,
Richard Smith2c9f87c2012-08-24 00:54:33 +00002045 /// Checking the object expression in a non-static data member access. Must
2046 /// be an object within its lifetime.
Richard Smith7ac9ef12012-09-08 02:08:36 +00002047 TCK_MemberAccess,
Richard Smith2c9f87c2012-08-24 00:54:33 +00002048 /// Checking the 'this' pointer for a call to a non-static member function.
2049 /// Must be an object within its lifetime.
Richard Smith8e1cee62012-10-25 02:14:12 +00002050 TCK_MemberCall,
2051 /// Checking the 'this' pointer for a constructor call.
Richard Smithc7648302013-02-13 21:18:23 +00002052 TCK_ConstructorCall,
2053 /// Checking the operand of a static_cast to a derived pointer type. Must be
2054 /// null or an object within its lifetime.
2055 TCK_DowncastPointer,
2056 /// Checking the operand of a static_cast to a derived reference type. Must
2057 /// be an object within its lifetime.
Stephen Hines176edba2014-12-01 14:53:08 -08002058 TCK_DowncastReference,
2059 /// Checking the operand of a cast to a base object. Must be suitably sized
2060 /// and aligned.
2061 TCK_Upcast,
2062 /// Checking the operand of a cast to a virtual base object. Must be an
2063 /// object within its lifetime.
2064 TCK_UpcastToVirtualBase
Richard Smith2c9f87c2012-08-24 00:54:33 +00002065 };
2066
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002067 /// \brief Whether any type-checking sanitizers are enabled. If \c false,
2068 /// calls to EmitTypeCheck can be skipped.
2069 bool sanitizePerformTypeCheck() const;
2070
Richard Smith7ac9ef12012-09-08 02:08:36 +00002071 /// \brief Emit a check that \p V is the address of storage of the
Richard Smith2c9f87c2012-08-24 00:54:33 +00002072 /// appropriate size and alignment for an object of type \p Type.
Richard Smith4def70d2012-10-09 19:52:38 +00002073 void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
Stephen Hines176edba2014-12-01 14:53:08 -08002074 QualType Type, CharUnits Alignment = CharUnits::Zero(),
2075 bool SkipNullCheck = false);
Mike Stumpb14e62d2009-12-16 02:57:00 +00002076
Richard Smitha0a628f2013-02-23 02:53:19 +00002077 /// \brief Emit a check that \p Base points into an array object, which
2078 /// we can access at index \p Index. \p Accessed should be \c false if we
2079 /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
2080 void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
2081 QualType IndexType, bool Accessed);
2082
Chris Lattnerdd36d322010-01-09 21:40:03 +00002083 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
2084 bool isInc, bool isPre);
2085 ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
2086 bool isInc, bool isPre);
Stephen Hines176edba2014-12-01 14:53:08 -08002087
2088 void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment,
2089 llvm::Value *OffsetValue = nullptr) {
2090 Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
2091 OffsetValue);
2092 }
2093
Reid Spencer5f016e22007-07-11 17:01:13 +00002094 //===--------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002095 // Declaration Emission
2096 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00002097
Daniel Dunbard286f052009-07-19 06:58:07 +00002098 /// EmitDecl - Emit a declaration.
2099 ///
2100 /// This function can be called with a null (unreachable) insert point.
Reid Spencer5f016e22007-07-11 17:01:13 +00002101 void EmitDecl(const Decl &D);
Daniel Dunbard286f052009-07-19 06:58:07 +00002102
John McCallb6bbcc92010-10-15 04:57:14 +00002103 /// EmitVarDecl - Emit a local variable declaration.
Daniel Dunbard286f052009-07-19 06:58:07 +00002104 ///
2105 /// This function can be called with a null (unreachable) insert point.
John McCallb6bbcc92010-10-15 04:57:14 +00002106 void EmitVarDecl(const VarDecl &D);
Daniel Dunbard286f052009-07-19 06:58:07 +00002107
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002108 void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
2109 bool capturedByInit);
John McCall7acddac2011-06-17 06:42:21 +00002110 void EmitScalarInit(llvm::Value *init, LValue lvalue);
John McCallf85e1932011-06-15 23:02:42 +00002111
John McCallf1549f62010-07-06 01:34:17 +00002112 typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
2113 llvm::Value *Address);
2114
Stephen Hines176edba2014-12-01 14:53:08 -08002115 /// \brief Determine whether the given initializer is trivial in the sense
2116 /// that it requires no code to be generated.
2117 bool isTrivialInitializer(const Expr *Init);
2118
John McCallb6bbcc92010-10-15 04:57:14 +00002119 /// EmitAutoVarDecl - Emit an auto variable declaration.
Daniel Dunbard286f052009-07-19 06:58:07 +00002120 ///
2121 /// This function can be called with a null (unreachable) insert point.
John McCall34695852011-02-22 06:44:22 +00002122 void EmitAutoVarDecl(const VarDecl &D);
2123
2124 class AutoVarEmission {
2125 friend class CodeGenFunction;
2126
John McCall57b3b6a2011-02-22 07:16:58 +00002127 const VarDecl *Variable;
John McCall34695852011-02-22 06:44:22 +00002128
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002129 /// The address of the alloca. Invalid if the variable was emitted
John McCall34695852011-02-22 06:44:22 +00002130 /// as a global constant.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002131 Address Addr;
John McCall34695852011-02-22 06:44:22 +00002132
2133 llvm::Value *NRVOFlag;
2134
2135 /// True if the variable is a __block variable.
2136 bool IsByRef;
2137
2138 /// True if the variable is of aggregate type and has a constant
2139 /// initializer.
2140 bool IsConstantAggregate;
2141
Nadav Rotem495cfa42013-03-23 06:43:35 +00002142 /// Non-null if we should use lifetime annotations.
2143 llvm::Value *SizeForLifetimeMarkers;
2144
John McCall57b3b6a2011-02-22 07:16:58 +00002145 struct Invalid {};
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002146 AutoVarEmission(Invalid) : Variable(nullptr), Addr(Address::invalid()) {}
John McCall57b3b6a2011-02-22 07:16:58 +00002147
John McCall34695852011-02-22 06:44:22 +00002148 AutoVarEmission(const VarDecl &variable)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002149 : Variable(&variable), Addr(Address::invalid()), NRVOFlag(nullptr),
Nadav Rotem495cfa42013-03-23 06:43:35 +00002150 IsByRef(false), IsConstantAggregate(false),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002151 SizeForLifetimeMarkers(nullptr) {}
John McCall34695852011-02-22 06:44:22 +00002152
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002153 bool wasEmittedAsGlobal() const { return !Addr.isValid(); }
John McCall34695852011-02-22 06:44:22 +00002154
2155 public:
John McCall57b3b6a2011-02-22 07:16:58 +00002156 static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
2157
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002158 bool useLifetimeMarkers() const {
2159 return SizeForLifetimeMarkers != nullptr;
2160 }
Nadav Rotem495cfa42013-03-23 06:43:35 +00002161 llvm::Value *getSizeForLifetimeMarkers() const {
2162 assert(useLifetimeMarkers());
2163 return SizeForLifetimeMarkers;
2164 }
2165
2166 /// Returns the raw, allocated address, which is not necessarily
2167 /// the address of the object itself.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002168 Address getAllocatedAddress() const {
2169 return Addr;
Nadav Rotem495cfa42013-03-23 06:43:35 +00002170 }
2171
John McCall34695852011-02-22 06:44:22 +00002172 /// Returns the address of the object within this declaration.
2173 /// Note that this does not chase the forwarding pointer for
2174 /// __block decls.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002175 Address getObjectAddress(CodeGenFunction &CGF) const {
2176 if (!IsByRef) return Addr;
John McCall34695852011-02-22 06:44:22 +00002177
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002178 return CGF.emitBlockByrefAddress(Addr, Variable, /*forward*/ false);
John McCall34695852011-02-22 06:44:22 +00002179 }
2180 };
2181 AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
2182 void EmitAutoVarInit(const AutoVarEmission &emission);
2183 void EmitAutoVarCleanups(const AutoVarEmission &emission);
John McCallbdc4d802011-07-09 01:37:26 +00002184 void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
2185 QualType::DestructionKind dtorKind);
Daniel Dunbard286f052009-07-19 06:58:07 +00002186
John McCallb6bbcc92010-10-15 04:57:14 +00002187 void EmitStaticVarDecl(const VarDecl &D,
2188 llvm::GlobalValue::LinkageTypes Linkage);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002189
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002190 class ParamValue {
2191 llvm::Value *Value;
2192 unsigned Alignment;
2193 ParamValue(llvm::Value *V, unsigned A) : Value(V), Alignment(A) {}
2194 public:
2195 static ParamValue forDirect(llvm::Value *value) {
2196 return ParamValue(value, 0);
2197 }
2198 static ParamValue forIndirect(Address addr) {
2199 assert(!addr.getAlignment().isZero());
2200 return ParamValue(addr.getPointer(), addr.getAlignment().getQuantity());
2201 }
2202
2203 bool isIndirect() const { return Alignment != 0; }
2204 llvm::Value *getAnyValue() const { return Value; }
2205
2206 llvm::Value *getDirectValue() const {
2207 assert(!isIndirect());
2208 return Value;
2209 }
2210
2211 Address getIndirectAddress() const {
2212 assert(isIndirect());
2213 return Address(Value, CharUnits::fromQuantity(Alignment));
2214 }
2215 };
2216
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002217 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002218 void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo);
Mike Stump0dd9e882009-02-08 23:14:22 +00002219
John McCall56ca35d2011-02-17 10:25:35 +00002220 /// protectFromPeepholes - Protect a value that we're intending to
2221 /// store to the side, but which will probably be used later, from
2222 /// aggressive peepholing optimizations that might delete it.
2223 ///
2224 /// Pass the result to unprotectFromPeepholes to declare that
2225 /// protection is no longer required.
2226 ///
2227 /// There's no particular reason why this shouldn't apply to
2228 /// l-values, it's just that no existing peepholes work on pointers.
2229 PeepholeProtection protectFromPeepholes(RValue rvalue);
2230 void unprotectFromPeepholes(PeepholeProtection protection);
2231
Reid Spencer5f016e22007-07-11 17:01:13 +00002232 //===--------------------------------------------------------------------===//
2233 // Statement Emission
2234 //===--------------------------------------------------------------------===//
2235
Mike Stump0dd9e882009-02-08 23:14:22 +00002236 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
Daniel Dunbar09124252008-11-12 08:21:33 +00002237 void EmitStopPoint(const Stmt *S);
2238
Mike Stump0dd9e882009-02-08 23:14:22 +00002239 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
2240 /// this function even if there is no current insertion point.
2241 ///
2242 /// This function may clear the current insertion point; callers should use
2243 /// EnsureInsertPoint if they wish to subsequently generate code without first
2244 /// calling EmitBlock, EmitBranch, or EmitStmt.
Reid Spencer5f016e22007-07-11 17:01:13 +00002245 void EmitStmt(const Stmt *S);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002246
Daniel Dunbar09124252008-11-12 08:21:33 +00002247 /// EmitSimpleStmt - Try to emit a "simple" statement which does not
Mike Stump0dd9e882009-02-08 23:14:22 +00002248 /// necessarily require an insertion point or debug information; typically
2249 /// because the statement amounts to a jump or a container of other
2250 /// statements.
Daniel Dunbar09124252008-11-12 08:21:33 +00002251 ///
2252 /// \return True if the statement was handled.
2253 bool EmitSimpleStmt(const Stmt *S);
2254
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002255 Address EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
2256 AggValueSlot AVS = AggValueSlot::ignored());
2257 Address EmitCompoundStmtWithoutScope(const CompoundStmt &S,
2258 bool GetLast = false,
2259 AggValueSlot AVS =
Eli Friedman2ac2fa72013-06-10 22:04:49 +00002260 AggValueSlot::ignored());
Daniel Dunbara448fb22008-11-11 23:11:34 +00002261
Mike Stump0dd9e882009-02-08 23:14:22 +00002262 /// EmitLabel - Emit the block for the given label. It is legal to call this
2263 /// function even if there is no current insertion point.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002264 void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
Daniel Dunbara448fb22008-11-11 23:11:34 +00002265
Reid Spencer5f016e22007-07-11 17:01:13 +00002266 void EmitLabelStmt(const LabelStmt &S);
Richard Smith534986f2012-04-14 00:33:13 +00002267 void EmitAttributedStmt(const AttributedStmt &S);
Reid Spencer5f016e22007-07-11 17:01:13 +00002268 void EmitGotoStmt(const GotoStmt &S);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00002269 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
Reid Spencer5f016e22007-07-11 17:01:13 +00002270 void EmitIfStmt(const IfStmt &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002271
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002272 void EmitWhileStmt(const WhileStmt &S,
Stephen Hines176edba2014-12-01 14:53:08 -08002273 ArrayRef<const Attr *> Attrs = None);
2274 void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002275 void EmitForStmt(const ForStmt &S,
Stephen Hines176edba2014-12-01 14:53:08 -08002276 ArrayRef<const Attr *> Attrs = None);
Reid Spencer5f016e22007-07-11 17:01:13 +00002277 void EmitReturnStmt(const ReturnStmt &S);
2278 void EmitDeclStmt(const DeclStmt &S);
Daniel Dunbar09124252008-11-12 08:21:33 +00002279 void EmitBreakStmt(const BreakStmt &S);
2280 void EmitContinueStmt(const ContinueStmt &S);
Devang Patel51b09f22007-10-04 23:45:31 +00002281 void EmitSwitchStmt(const SwitchStmt &S);
2282 void EmitDefaultStmt(const DefaultStmt &S);
2283 void EmitCaseStmt(const CaseStmt &S);
Devang Patelc049e4f2007-10-08 20:57:48 +00002284 void EmitCaseStmtRange(const CaseStmt &S);
Chad Rosiera23b91d2012-08-28 18:54:39 +00002285 void EmitAsmStmt(const AsmStmt &S);
Mike Stump0dd9e882009-02-08 23:14:22 +00002286
Anders Carlsson3d8400d2008-08-30 19:51:14 +00002287 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002288 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
2289 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
Chris Lattner10cac6f2008-11-15 21:26:17 +00002290 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
John McCallf85e1932011-06-15 23:02:42 +00002291 void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
Mike Stump0dd9e882009-02-08 23:14:22 +00002292
John McCall59a70002010-07-07 06:56:46 +00002293 void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
2294 void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
John McCall9fc6a772010-02-19 09:25:03 +00002295
Anders Carlsson6815e942009-09-27 18:58:34 +00002296 void EmitCXXTryStmt(const CXXTryStmt &S);
Reid Kleckner98592d92013-09-16 21:46:30 +00002297 void EmitSEHTryStmt(const SEHTryStmt &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002298 void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002299 void EnterSEHTryStmt(const SEHTryStmt &S);
2300 void ExitSEHTryStmt(const SEHTryStmt &S);
2301
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002302 void startOutlinedSEHHelper(CodeGenFunction &ParentCGF, bool IsFilter,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002303 const Stmt *OutlinedStmt);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002304
2305 llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
2306 const SEHExceptStmt &Except);
2307
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002308 llvm::Function *GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF,
2309 const SEHFinallyStmt &Finally);
2310
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002311 void EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF,
2312 llvm::Value *ParentFP,
2313 llvm::Value *EntryEBP);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002314 llvm::Value *EmitSEHExceptionCode();
2315 llvm::Value *EmitSEHExceptionInfo();
2316 llvm::Value *EmitSEHAbnormalTermination();
2317
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002318 /// Scan the outlined statement for captures from the parent function. For
2319 /// each capture, mark the capture as escaped and emit a call to
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002320 /// llvm.localrecover. Insert the localrecover result into the LocalDeclMap.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002321 void EmitCapturedLocals(CodeGenFunction &ParentCGF, const Stmt *OutlinedStmt,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002322 bool IsFilter);
2323
2324 /// Recovers the address of a local in a parent function. ParentVar is the
2325 /// address of the variable used in the immediate parent function. It can
2326 /// either be an alloca or a call to llvm.localrecover if there are nested
2327 /// outlined functions. ParentFP is the frame pointer of the outermost parent
2328 /// frame.
2329 Address recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF,
2330 Address ParentVar,
2331 llvm::Value *ParentFP);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002332
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002333 void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
Stephen Hines176edba2014-12-01 14:53:08 -08002334 ArrayRef<const Attr *> Attrs = None);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002335
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002336 /// Returns calculated size of the specified type.
2337 llvm::Value *getTypeSize(QualType Ty);
Stephen Hines176edba2014-12-01 14:53:08 -08002338 LValue InitCapturedStruct(const CapturedStmt &S);
Ben Langmuir524387a2013-05-09 19:17:11 +00002339 llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002340 llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002341 Address GenerateCapturedStmtArgument(const CapturedStmt &S);
2342 llvm::Function *GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S);
2343 void GenerateOpenMPCapturedVars(const CapturedStmt &S,
2344 SmallVectorImpl<llvm::Value *> &CapturedVars);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002345 void emitOMPSimpleStore(LValue LVal, RValue RVal, QualType RValTy,
2346 SourceLocation Loc);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002347 /// \brief Perform element by element copying of arrays with type \a
2348 /// OriginalType from \a SrcAddr to \a DestAddr using copying procedure
2349 /// generated by \a CopyGen.
2350 ///
2351 /// \param DestAddr Address of the destination array.
2352 /// \param SrcAddr Address of the source array.
2353 /// \param OriginalType Type of destination and source arrays.
2354 /// \param CopyGen Copying procedure that copies value of single array element
2355 /// to another single array element.
2356 void EmitOMPAggregateAssign(
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002357 Address DestAddr, Address SrcAddr, QualType OriginalType,
2358 const llvm::function_ref<void(Address, Address)> &CopyGen);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002359 /// \brief Emit proper copying of data from one variable to another.
2360 ///
2361 /// \param OriginalType Original type of the copied variables.
2362 /// \param DestAddr Destination address.
2363 /// \param SrcAddr Source address.
2364 /// \param DestVD Destination variable used in \a CopyExpr (for arrays, has
2365 /// type of the base array element).
2366 /// \param SrcVD Source variable used in \a CopyExpr (for arrays, has type of
2367 /// the base array element).
2368 /// \param Copy Actual copygin expression for copying data from \a SrcVD to \a
2369 /// DestVD.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002370 void EmitOMPCopy(QualType OriginalType,
2371 Address DestAddr, Address SrcAddr,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002372 const VarDecl *DestVD, const VarDecl *SrcVD,
2373 const Expr *Copy);
2374 /// \brief Emit atomic update code for constructs: \a X = \a X \a BO \a E or
2375 /// \a X = \a E \a BO \a E.
2376 ///
2377 /// \param X Value to be updated.
2378 /// \param E Update value.
2379 /// \param BO Binary operation for update operation.
2380 /// \param IsXLHSInRHSPart true if \a X is LHS in RHS part of the update
2381 /// expression, false otherwise.
2382 /// \param AO Atomic ordering of the generated atomic instructions.
2383 /// \param CommonGen Code generator for complex expressions that cannot be
2384 /// expressed through atomicrmw instruction.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002385 /// \returns <true, OldAtomicValue> if simple 'atomicrmw' instruction was
2386 /// generated, <false, RValue::get(nullptr)> otherwise.
2387 std::pair<bool, RValue> EmitOMPAtomicSimpleUpdateExpr(
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002388 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
2389 llvm::AtomicOrdering AO, SourceLocation Loc,
2390 const llvm::function_ref<RValue(RValue)> &CommonGen);
2391 bool EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
Stephen Hines176edba2014-12-01 14:53:08 -08002392 OMPPrivateScope &PrivateScope);
2393 void EmitOMPPrivateClause(const OMPExecutableDirective &D,
2394 OMPPrivateScope &PrivateScope);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002395 /// \brief Emit code for copyin clause in \a D directive. The next code is
2396 /// generated at the start of outlined functions for directives:
2397 /// \code
2398 /// threadprivate_var1 = master_threadprivate_var1;
2399 /// operator=(threadprivate_var2, master_threadprivate_var2);
2400 /// ...
2401 /// __kmpc_barrier(&loc, global_tid);
2402 /// \endcode
2403 ///
2404 /// \param D OpenMP directive possibly with 'copyin' clause(s).
2405 /// \returns true if at least one copyin variable is found, false otherwise.
2406 bool EmitOMPCopyinClause(const OMPExecutableDirective &D);
2407 /// \brief Emit initial code for lastprivate variables. If some variable is
2408 /// not also firstprivate, then the default initialization is used. Otherwise
2409 /// initialization of this variable is performed by EmitOMPFirstprivateClause
2410 /// method.
2411 ///
2412 /// \param D Directive that may have 'lastprivate' directives.
2413 /// \param PrivateScope Private scope for capturing lastprivate variables for
2414 /// proper codegen in internal captured statement.
2415 ///
2416 /// \returns true if there is at least one lastprivate variable, false
2417 /// otherwise.
2418 bool EmitOMPLastprivateClauseInit(const OMPExecutableDirective &D,
2419 OMPPrivateScope &PrivateScope);
2420 /// \brief Emit final copying of lastprivate values to original variables at
2421 /// the end of the worksharing or simd directive.
2422 ///
2423 /// \param D Directive that has at least one 'lastprivate' directives.
2424 /// \param IsLastIterCond Boolean condition that must be set to 'i1 true' if
2425 /// it is the last iteration of the loop code in associated directive, or to
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002426 /// 'i1 false' otherwise. If this item is nullptr, no final check is required.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002427 void EmitOMPLastprivateClauseFinal(const OMPExecutableDirective &D,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002428 bool NoFinals,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002429 llvm::Value *IsLastIterCond = nullptr);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002430 /// Emit initial code for linear clauses.
2431 void EmitOMPLinearClause(const OMPLoopDirective &D,
2432 CodeGenFunction::OMPPrivateScope &PrivateScope);
2433 /// Emit final code for linear clauses.
2434 /// \param CondGen Optional conditional code for final part of codegen for
2435 /// linear clause.
2436 void EmitOMPLinearClauseFinal(
2437 const OMPLoopDirective &D,
2438 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002439 /// \brief Emit initial code for reduction variables. Creates reduction copies
2440 /// and initializes them with the values according to OpenMP standard.
2441 ///
2442 /// \param D Directive (possibly) with the 'reduction' clause.
2443 /// \param PrivateScope Private scope for capturing reduction variables for
2444 /// proper codegen in internal captured statement.
2445 ///
2446 void EmitOMPReductionClauseInit(const OMPExecutableDirective &D,
2447 OMPPrivateScope &PrivateScope);
2448 /// \brief Emit final update of reduction values to original variables at
2449 /// the end of the directive.
2450 ///
2451 /// \param D Directive that has at least one 'reduction' directives.
2452 void EmitOMPReductionClauseFinal(const OMPExecutableDirective &D);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002453 /// \brief Emit initial code for linear variables. Creates private copies
2454 /// and initializes them with the values according to OpenMP standard.
2455 ///
2456 /// \param D Directive (possibly) with the 'linear' clause.
2457 void EmitOMPLinearClauseInit(const OMPLoopDirective &D);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002458
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002459 typedef const llvm::function_ref<void(CodeGenFunction & /*CGF*/,
2460 llvm::Value * /*OutlinedFn*/,
2461 const OMPTaskDataTy & /*Data*/)>
2462 TaskGenTy;
2463 void EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
2464 const RegionCodeGenTy &BodyGen,
2465 const TaskGenTy &TaskGen, OMPTaskDataTy &Data);
2466
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002467 void EmitOMPParallelDirective(const OMPParallelDirective &S);
2468 void EmitOMPSimdDirective(const OMPSimdDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002469 void EmitOMPForDirective(const OMPForDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002470 void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002471 void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
2472 void EmitOMPSectionDirective(const OMPSectionDirective &S);
2473 void EmitOMPSingleDirective(const OMPSingleDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002474 void EmitOMPMasterDirective(const OMPMasterDirective &S);
2475 void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002476 void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002477 void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002478 void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002479 void EmitOMPTaskDirective(const OMPTaskDirective &S);
2480 void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
2481 void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
2482 void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002483 void EmitOMPTaskgroupDirective(const OMPTaskgroupDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002484 void EmitOMPFlushDirective(const OMPFlushDirective &S);
2485 void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
2486 void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
2487 void EmitOMPTargetDirective(const OMPTargetDirective &S);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002488 void EmitOMPTargetDataDirective(const OMPTargetDataDirective &S);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002489 void EmitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective &S);
2490 void EmitOMPTargetExitDataDirective(const OMPTargetExitDataDirective &S);
2491 void EmitOMPTargetUpdateDirective(const OMPTargetUpdateDirective &S);
2492 void EmitOMPTargetParallelDirective(const OMPTargetParallelDirective &S);
2493 void
2494 EmitOMPTargetParallelForDirective(const OMPTargetParallelForDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002495 void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002496 void
2497 EmitOMPCancellationPointDirective(const OMPCancellationPointDirective &S);
2498 void EmitOMPCancelDirective(const OMPCancelDirective &S);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002499 void EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002500 void EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S);
2501 void EmitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective &S);
2502 void EmitOMPDistributeDirective(const OMPDistributeDirective &S);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002503 void EmitOMPDistributeLoop(const OMPDistributeDirective &S);
2504 void EmitOMPDistributeParallelForDirective(
2505 const OMPDistributeParallelForDirective &S);
2506 void EmitOMPDistributeParallelForSimdDirective(
2507 const OMPDistributeParallelForSimdDirective &S);
2508 void EmitOMPDistributeSimdDirective(const OMPDistributeSimdDirective &S);
2509 void EmitOMPTargetParallelForSimdDirective(
2510 const OMPTargetParallelForSimdDirective &S);
Stephen Hines176edba2014-12-01 14:53:08 -08002511
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002512 /// Emit outlined function for the target directive.
2513 static std::pair<llvm::Function * /*OutlinedFn*/,
2514 llvm::Constant * /*OutlinedFnID*/>
2515 EmitOMPTargetDirectiveOutlinedFunction(CodeGenModule &CGM,
2516 const OMPTargetDirective &S,
2517 StringRef ParentName,
2518 bool IsOffloadEntry);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002519 /// \brief Emit inner loop of the worksharing/simd construct.
2520 ///
2521 /// \param S Directive, for which the inner loop must be emitted.
2522 /// \param RequiresCleanup true, if directive has some associated private
2523 /// variables.
2524 /// \param LoopCond Bollean condition for loop continuation.
2525 /// \param IncExpr Increment expression for loop control variable.
2526 /// \param BodyGen Generator for the inner body of the inner loop.
2527 /// \param PostIncGen Genrator for post-increment code (required for ordered
2528 /// loop directvies).
2529 void EmitOMPInnerLoop(
2530 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
2531 const Expr *IncExpr,
2532 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
2533 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002534
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002535 JumpDest getOMPCancelDestination(OpenMPDirectiveKind Kind);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002536 /// Emit initial code for loop counters of loop-based directives.
2537 void EmitOMPPrivateLoopCounters(const OMPLoopDirective &S,
2538 OMPPrivateScope &LoopScope);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002539
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002540private:
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002541 /// Helpers for the OpenMP loop directives.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002542 void EmitOMPLoopBody(const OMPLoopDirective &D, JumpDest LoopExit);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002543 void EmitOMPSimdInit(const OMPLoopDirective &D, bool IsMonotonic = false);
2544 void EmitOMPSimdFinal(
2545 const OMPLoopDirective &D,
2546 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002547 /// \brief Emit code for the worksharing loop-based directive.
2548 /// \return true, if this construct has any lastprivate clause, false -
2549 /// otherwise.
2550 bool EmitOMPWorksharingLoop(const OMPLoopDirective &S);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002551 void EmitOMPOuterLoop(bool IsMonotonic, bool DynamicOrOrdered,
2552 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
2553 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk);
2554 void EmitOMPForOuterLoop(const OpenMPScheduleTy &ScheduleKind,
2555 bool IsMonotonic, const OMPLoopDirective &S,
2556 OMPPrivateScope &LoopScope, bool Ordered, Address LB,
2557 Address UB, Address ST, Address IL,
2558 llvm::Value *Chunk);
2559 void EmitOMPDistributeOuterLoop(
2560 OpenMPDistScheduleClauseKind ScheduleKind,
2561 const OMPDistributeDirective &S, OMPPrivateScope &LoopScope,
2562 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002563 /// \brief Emit code for sections directive.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002564 void EmitSections(const OMPExecutableDirective &S);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002565
2566public:
Ben Langmuir524387a2013-05-09 19:17:11 +00002567
Reid Spencer5f016e22007-07-11 17:01:13 +00002568 //===--------------------------------------------------------------------===//
2569 // LValue Expression Emission
2570 //===--------------------------------------------------------------------===//
2571
Daniel Dunbar13e81732009-02-05 07:09:07 +00002572 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
2573 RValue GetUndefRValue(QualType Ty);
2574
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00002575 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
2576 /// and issue an ErrorUnsupported style diagnostic (using the
2577 /// provided Name).
2578 RValue EmitUnsupportedRValue(const Expr *E,
2579 const char *Name);
2580
Mike Stump0dd9e882009-02-08 23:14:22 +00002581 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
2582 /// an ErrorUnsupported style diagnostic (using the provided Name).
Daniel Dunbar6ba82a42008-08-25 20:45:57 +00002583 LValue EmitUnsupportedLValue(const Expr *E,
2584 const char *Name);
2585
Reid Spencer5f016e22007-07-11 17:01:13 +00002586 /// EmitLValue - Emit code to compute a designator that specifies the location
2587 /// of the expression.
2588 ///
2589 /// This can return one of two things: a simple address or a bitfield
2590 /// reference. In either case, the LLVM Value* in the LValue structure is
2591 /// guaranteed to be an LLVM pointer type.
2592 ///
2593 /// If this returns a bitfield reference, nothing about the pointee type of
2594 /// the LLVM value is known: For example, it may not be a pointer to an
2595 /// integer.
2596 ///
2597 /// If this returns a normal address, and if the lvalue's C type is fixed
2598 /// size, this method guarantees that the returned pointer type will point to
2599 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
2600 /// variable length type, this is not possible.
2601 ///
2602 LValue EmitLValue(const Expr *E);
Mike Stump0dd9e882009-02-08 23:14:22 +00002603
Richard Smith7ac9ef12012-09-08 02:08:36 +00002604 /// \brief Same as EmitLValue but additionally we generate checking code to
2605 /// guard against undefined behavior. This is only suitable when we know
2606 /// that the address will be used to access the object.
2607 LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
Mike Stumpb14e62d2009-12-16 02:57:00 +00002608
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002609 RValue convertTempToRValue(Address addr, QualType type,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002610 SourceLocation Loc);
John McCall9d232c82013-03-07 21:37:08 +00002611
John McCall9eda3ab2013-03-07 21:37:17 +00002612 void EmitAtomicInit(Expr *E, LValue lvalue);
2613
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002614 bool LValueIsSuitableForInlineAtomic(LValue Src);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002615
2616 RValue EmitAtomicLoad(LValue LV, SourceLocation SL,
2617 AggValueSlot Slot = AggValueSlot::ignored());
2618
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002619 RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002620 llvm::AtomicOrdering AO, bool IsVolatile = false,
John McCall9eda3ab2013-03-07 21:37:17 +00002621 AggValueSlot slot = AggValueSlot::ignored());
2622
2623 void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
2624
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002625 void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO,
2626 bool IsVolatile, bool isInit);
2627
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002628 std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange(
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002629 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002630 llvm::AtomicOrdering Success =
2631 llvm::AtomicOrdering::SequentiallyConsistent,
2632 llvm::AtomicOrdering Failure =
2633 llvm::AtomicOrdering::SequentiallyConsistent,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002634 bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored());
2635
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002636 void EmitAtomicUpdate(LValue LVal, llvm::AtomicOrdering AO,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002637 const llvm::function_ref<RValue(RValue)> &UpdateOp,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002638 bool IsVolatile);
2639
John McCall26815d92010-10-27 20:58:56 +00002640 /// EmitToMemory - Change a scalar value from its value
2641 /// representation to its in-memory representation.
2642 llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
2643
2644 /// EmitFromMemory - Change a scalar value from its memory
2645 /// representation to its value representation.
2646 llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
2647
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00002648 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2649 /// care to appropriately convert from the memory representation to
2650 /// the LLVM value representation.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002651 llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002652 SourceLocation Loc,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002653 AlignmentSource AlignSource =
2654 AlignmentSource::Type,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002655 llvm::MDNode *TBAAInfo = nullptr,
Manman Renb37a73d2013-04-04 21:53:22 +00002656 QualType TBAABaseTy = QualType(),
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002657 uint64_t TBAAOffset = 0,
2658 bool isNontemporal = false);
John McCall545d9962011-06-25 02:11:03 +00002659
2660 /// EmitLoadOfScalar - Load a scalar value from an address, taking
2661 /// care to appropriately convert from the memory representation to
2662 /// the LLVM value representation. The l-value must be a simple
2663 /// l-value.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002664 llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00002665
2666 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2667 /// care to appropriately convert from the memory representation to
2668 /// the LLVM value representation.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002669 void EmitStoreOfScalar(llvm::Value *Value, Address Addr,
2670 bool Volatile, QualType Ty,
2671 AlignmentSource AlignSource = AlignmentSource::Type,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002672 llvm::MDNode *TBAAInfo = nullptr, bool isInit = false,
Manman Renb37a73d2013-04-04 21:53:22 +00002673 QualType TBAABaseTy = QualType(),
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002674 uint64_t TBAAOffset = 0, bool isNontemporal = false);
John McCall545d9962011-06-25 02:11:03 +00002675
2676 /// EmitStoreOfScalar - Store a scalar value to an address, taking
2677 /// care to appropriately convert from the memory representation to
2678 /// the LLVM value representation. The l-value must be a simple
David Chisnall7a7ee302012-01-16 17:27:18 +00002679 /// l-value. The isInit flag indicates whether this is an initialization.
2680 /// If so, atomic qualifiers are ignored and the store is always non-atomic.
2681 void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00002682
Reid Spencer5f016e22007-07-11 17:01:13 +00002683 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
2684 /// this method emits the address of the lvalue, then loads the result as an
2685 /// rvalue, returning the rvalue.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002686 RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
John McCall545d9962011-06-25 02:11:03 +00002687 RValue EmitLoadOfExtVectorElementLValue(LValue V);
2688 RValue EmitLoadOfBitfieldLValue(LValue LV);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002689 RValue EmitLoadOfGlobalRegLValue(LValue LV);
Mike Stump0dd9e882009-02-08 23:14:22 +00002690
Reid Spencer5f016e22007-07-11 17:01:13 +00002691 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
2692 /// lvalue, where both are guaranteed to the have the same type, and that type
2693 /// is 'Ty'.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002694 void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false);
John McCall545d9962011-06-25 02:11:03 +00002695 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002696 void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
Daniel Dunbared3849b2008-11-19 09:36:46 +00002697
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002698 /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
2699 /// as EmitStoreThroughLValue.
Daniel Dunbared3849b2008-11-19 09:36:46 +00002700 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00002701 /// \param Result [out] - If non-null, this will be set to a Value* for the
2702 /// bit-field contents after the store, appropriate for use as the result of
2703 /// an assignment to the bit-field.
John McCall545d9962011-06-25 02:11:03 +00002704 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002705 llvm::Value **Result=nullptr);
Mike Stump0dd9e882009-02-08 23:14:22 +00002706
John McCall83ce9d42010-11-16 23:07:28 +00002707 /// Emit an l-value for an assignment (simple or compound) of complex type.
2708 LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
John McCall2a416372010-12-05 02:00:02 +00002709 LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002710 LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
2711 llvm::Value *&Result);
John McCall83ce9d42010-11-16 23:07:28 +00002712
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002713 // Note: only available for agg return types
Daniel Dunbar80e62c22008-09-04 03:20:13 +00002714 LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
John McCall2a416372010-12-05 02:00:02 +00002715 LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00002716 // Note: only available for agg return types
Christopher Lamb22c940e2007-12-29 05:02:41 +00002717 LValue EmitCallExprLValue(const CallExpr *E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00002718 // Note: only available for agg return types
2719 LValue EmitVAArgExprLValue(const VAArgExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00002720 LValue EmitDeclRefLValue(const DeclRefExpr *E);
2721 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002722 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
Chris Lattnerd9f69102008-08-10 01:53:14 +00002723 LValue EmitPredefinedLValue(const PredefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00002724 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Richard Smitha0a628f2013-02-23 02:53:19 +00002725 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2726 bool Accessed = false);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002727 LValue EmitOMPArraySectionExpr(const OMPArraySectionExpr *E,
2728 bool IsLowerBound = true);
Nate Begeman213541a2008-04-18 23:10:10 +00002729 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
Devang Patelb84a06e2007-10-23 02:10:49 +00002730 LValue EmitMemberExpr(const MemberExpr *E);
Fariborz Jahanian820bca42009-12-09 23:35:29 +00002731 LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
Eli Friedman06e863f2008-05-13 23:18:27 +00002732 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
Richard Smith13ec9102012-05-14 21:57:21 +00002733 LValue EmitInitListLValue(const InitListExpr *E);
John McCall56ca35d2011-02-17 10:25:35 +00002734 LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
Chris Lattner75dfeda2009-03-18 18:28:57 +00002735 LValue EmitCastLValue(const CastExpr *E);
Douglas Gregor03e80032011-06-21 17:03:29 +00002736 LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
John McCalle996ffd2011-02-16 08:02:54 +00002737 LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
Stephen Hines176edba2014-12-01 14:53:08 -08002738
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002739 Address EmitExtVectorElementLValue(LValue V);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002740
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00002741 RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002742
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002743 Address EmitArrayToPointerDecay(const Expr *Array,
2744 AlignmentSource *AlignSource = nullptr);
2745
John McCalldd2ecee2012-03-10 03:05:10 +00002746 class ConstantEmission {
2747 llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
2748 ConstantEmission(llvm::Constant *C, bool isReference)
2749 : ValueAndIsReference(C, isReference) {}
2750 public:
2751 ConstantEmission() {}
2752 static ConstantEmission forReference(llvm::Constant *C) {
2753 return ConstantEmission(C, true);
2754 }
2755 static ConstantEmission forValue(llvm::Constant *C) {
2756 return ConstantEmission(C, false);
2757 }
2758
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002759 explicit operator bool() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002760 return ValueAndIsReference.getOpaqueValue() != nullptr;
2761 }
John McCalldd2ecee2012-03-10 03:05:10 +00002762
2763 bool isReference() const { return ValueAndIsReference.getInt(); }
2764 LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
2765 assert(isReference());
2766 return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
2767 refExpr->getType());
2768 }
2769
2770 llvm::Constant *getValue() const {
2771 assert(!isReference());
2772 return ValueAndIsReference.getPointer();
2773 }
2774 };
2775
John McCallf4b88a42012-03-10 09:33:50 +00002776 ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
John McCalldd2ecee2012-03-10 03:05:10 +00002777
John McCall4b9c2d22011-11-06 09:01:30 +00002778 RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
2779 AggValueSlot slot = AggValueSlot::ignored());
2780 LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
2781
Daniel Dunbar2a031922009-04-22 05:08:15 +00002782 llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002783 const ObjCIvarDecl *Ivar);
Eli Friedman377ecc72012-04-16 03:54:45 +00002784 LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
John McCallf5ebf9b2013-05-03 07:33:41 +00002785 LValue EmitLValueForLambdaField(const FieldDecl *Field);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002786
Anders Carlsson06a29702010-01-29 05:24:29 +00002787 /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
2788 /// if the Field is a reference, this will return the address of the reference
2789 /// and not the address of the value stored in the reference.
Eli Friedman377ecc72012-04-16 03:54:45 +00002790 LValue EmitLValueForFieldInitialization(LValue Base,
2791 const FieldDecl* Field);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002792
Fariborz Jahanian45012a72009-02-03 00:09:52 +00002793 LValue EmitLValueForIvar(QualType ObjectTy,
2794 llvm::Value* Base, const ObjCIvarDecl *Ivar,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002795 unsigned CVRQualifiers);
2796
Anders Carlssonb58d0172009-05-30 23:23:33 +00002797 LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
Anders Carlssone61c9e82009-05-30 23:30:54 +00002798 LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
Eli Friedman31a37022012-02-08 05:34:55 +00002799 LValue EmitLambdaLValue(const LambdaExpr *E);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00002800 LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
Nico Weberc5f80462012-10-11 10:13:44 +00002801 LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002802
Daniel Dunbar0a04d772008-08-23 10:51:21 +00002803 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
Chris Lattner391d77a2008-03-30 23:03:07 +00002804 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
Chris Lattner65459942009-04-25 19:35:26 +00002805 LValue EmitStmtExprLValue(const StmtExpr *E);
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00002806 LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
Fariborz Jahanian03b29602010-06-17 19:56:20 +00002807 LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
Yunzhong Gao3b8e0b72013-08-30 08:53:09 +00002808 void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
John McCall56ca35d2011-02-17 10:25:35 +00002809
Reid Spencer5f016e22007-07-11 17:01:13 +00002810 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +00002811 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +00002812 //===--------------------------------------------------------------------===//
2813
Mike Stump0dd9e882009-02-08 23:14:22 +00002814 /// EmitCall - Generate a call of the given function, expecting the given
2815 /// result type, and using the given argument list which specifies both the
2816 /// LLVM arguments and the types they were derived from.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002817 RValue EmitCall(const CGFunctionInfo &FnInfo, llvm::Value *Callee,
2818 ReturnValueSlot ReturnValue, const CallArgList &Args,
2819 CGCalleeInfo CalleeInfo = CGCalleeInfo(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002820 llvm::Instruction **callOrInvoke = nullptr);
Mike Stump1eb44332009-09-09 15:08:12 +00002821
Stephen Hines176edba2014-12-01 14:53:08 -08002822 RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E,
Anders Carlssond2490a92009-12-24 20:40:36 +00002823 ReturnValueSlot ReturnValue,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002824 CGCalleeInfo CalleeInfo = CGCalleeInfo(),
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002825 llvm::Value *Chain = nullptr);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002826 RValue EmitCallExpr(const CallExpr *E,
Anders Carlssond2490a92009-12-24 20:40:36 +00002827 ReturnValueSlot ReturnValue = ReturnValueSlot());
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002829 void checkTargetFeatures(const CallExpr *E, const FunctionDecl *TargetDecl);
2830
John McCallbd7370a2013-02-28 19:01:20 +00002831 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2832 const Twine &name = "");
2833 llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2834 ArrayRef<llvm::Value*> args,
2835 const Twine &name = "");
2836 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2837 const Twine &name = "");
2838 llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2839 ArrayRef<llvm::Value*> args,
2840 const Twine &name = "");
2841
John McCallf1549f62010-07-06 01:34:17 +00002842 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +00002843 ArrayRef<llvm::Value *> Args,
Chris Lattner8cc488f2011-07-20 07:06:53 +00002844 const Twine &Name = "");
John McCallbd7370a2013-02-28 19:01:20 +00002845 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2846 ArrayRef<llvm::Value*> args,
2847 const Twine &name = "");
2848 llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2849 const Twine &name = "");
2850 void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2851 ArrayRef<llvm::Value*> args);
John McCallf1549f62010-07-06 01:34:17 +00002852
Fariborz Jahanian27262672011-01-20 17:19:02 +00002853 llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
2854 NestedNameSpecifier *Qual,
Chris Lattner2acc6e32011-07-18 04:24:23 +00002855 llvm::Type *Ty);
Fariborz Jahanianccd52592011-02-01 23:22:34 +00002856
2857 llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2858 CXXDtorType Type,
Fariborz Jahanian771c6782011-02-03 19:27:17 +00002859 const CXXRecordDecl *RD);
Anders Carlsson566abee2009-11-13 04:45:41 +00002860
Stephen Hines176edba2014-12-01 14:53:08 -08002861 RValue
2862 EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2863 ReturnValueSlot ReturnValue, llvm::Value *This,
2864 llvm::Value *ImplicitParam,
2865 QualType ImplicitParamTy, const CallExpr *E);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002866 RValue EmitCXXDestructorCall(const CXXDestructorDecl *DD, llvm::Value *Callee,
2867 llvm::Value *This, llvm::Value *ImplicitParam,
2868 QualType ImplicitParamTy, const CallExpr *E,
2869 StructorType Type);
Anders Carlssona1736c02009-12-24 21:13:40 +00002870 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2871 ReturnValueSlot ReturnValue);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002872 RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE,
2873 const CXXMethodDecl *MD,
2874 ReturnValueSlot ReturnValue,
2875 bool HasQualifier,
2876 NestedNameSpecifier *Qualifier,
2877 bool IsArrow, const Expr *Base);
2878 // Compute the object pointer.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002879 Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base,
2880 llvm::Value *memberPtr,
2881 const MemberPointerType *memberPtrType,
2882 AlignmentSource *AlignSource = nullptr);
Anders Carlssona1736c02009-12-24 21:13:40 +00002883 RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2884 ReturnValueSlot ReturnValue);
Ted Kremenek55499762008-06-17 02:43:46 +00002885
Anders Carlsson0f294632009-05-27 04:18:27 +00002886 RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
Anders Carlssona1736c02009-12-24 21:13:40 +00002887 const CXXMethodDecl *MD,
2888 ReturnValueSlot ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00002889
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +00002890 RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
2891 ReturnValueSlot ReturnValue);
2892
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002893 RValue EmitCUDADevicePrintfCallExpr(const CallExpr *E,
2894 ReturnValueSlot ReturnValue);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002895
Mike Stump1eb44332009-09-09 15:08:12 +00002896 RValue EmitBuiltinExpr(const FunctionDecl *FD,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002897 unsigned BuiltinID, const CallExpr *E,
2898 ReturnValueSlot ReturnValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00002899
Anders Carlssona1736c02009-12-24 21:13:40 +00002900 RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
Mike Stump09429b92009-02-17 17:00:02 +00002901
Mike Stump0dd9e882009-02-08 23:14:22 +00002902 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
2903 /// is unhandled by the current target.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00002904 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2905
Kevin Qin8137a602013-11-14 02:45:18 +00002906 llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
2907 const llvm::CmpInst::Predicate Fp,
2908 const llvm::CmpInst::Predicate Ip,
2909 const llvm::Twine &Name = "");
Chris Lattner2752c012010-03-03 19:03:45 +00002910 llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Stephen Hines651f13c2014-04-23 16:59:28 -07002911
2912 llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
2913 unsigned LLVMIntrinsic,
2914 unsigned AltLLVMIntrinsic,
2915 const char *NameHint,
2916 unsigned Modifier,
2917 const CallExpr *E,
2918 SmallVectorImpl<llvm::Value *> &Ops,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002919 Address PtrOp0, Address PtrOp1);
Stephen Hines651f13c2014-04-23 16:59:28 -07002920 llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2921 unsigned Modifier, llvm::Type *ArgTy,
2922 const CallExpr *E);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002923 llvm::Value *EmitNeonCall(llvm::Function *F,
Chris Lattner686775d2011-07-20 06:58:45 +00002924 SmallVectorImpl<llvm::Value*> &O,
Bob Wilsondb3d4d02010-12-08 22:37:56 +00002925 const char *name,
Nate Begeman61eecf52010-06-14 05:21:25 +00002926 unsigned shift = 0, bool rightshift = false);
Bob Wilsoncf556522010-12-07 22:40:02 +00002927 llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002928 llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
Nate Begeman61eecf52010-06-14 05:21:25 +00002929 bool negateForRightShift);
Amaury de la Vieuville7f0ff702013-10-04 13:13:15 +00002930 llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
2931 llvm::Type *Ty, bool usgn, const char *name);
Stephen Hines651f13c2014-04-23 16:59:28 -07002932 llvm::Value *vectorWrapScalar16(llvm::Value *Op);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002933 llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002934
Bill Wendling795b1002012-02-22 09:30:11 +00002935 llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
Anders Carlsson564f1de2007-12-09 23:17:02 +00002936 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2937 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002938 llvm::Value *EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002939 llvm::Value *EmitSystemZBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002940 llvm::Value *EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2941 llvm::Value *EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
2942 const CallExpr *E);
Mike Stump0dd9e882009-02-08 23:14:22 +00002943
Daniel Dunbared7c6182008-08-20 00:28:19 +00002944 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
Chris Lattner7f02f722007-08-24 05:35:26 +00002945 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
Patrick Beardeb382ec2012-04-19 00:25:12 +00002946 llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002947 llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
2948 llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2949 llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
2950 const ObjCMethodDecl *MethodWithObjects);
Chris Lattner8fdf3282008-06-24 17:04:18 +00002951 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
John McCallef072fd2010-05-22 01:48:05 +00002952 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2953 ReturnValueSlot Return = ReturnValueSlot());
Chris Lattner8fdf3282008-06-24 17:04:18 +00002954
John McCallf85e1932011-06-15 23:02:42 +00002955 /// Retrieves the default cleanup kind for an ARC cleanup.
2956 /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2957 CleanupKind getARCCleanupKind() {
2958 return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2959 ? NormalAndEHCleanup : NormalCleanup;
2960 }
2961
2962 // ARC primitives.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002963 void EmitARCInitWeak(Address addr, llvm::Value *value);
2964 void EmitARCDestroyWeak(Address addr);
2965 llvm::Value *EmitARCLoadWeak(Address addr);
2966 llvm::Value *EmitARCLoadWeakRetained(Address addr);
2967 llvm::Value *EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored);
2968 void EmitARCCopyWeak(Address dst, Address src);
2969 void EmitARCMoveWeak(Address dst, Address src);
John McCallf85e1932011-06-15 23:02:42 +00002970 llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2971 llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
John McCall545d9962011-06-25 02:11:03 +00002972 llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
John McCall5b07e802013-03-13 03:10:54 +00002973 bool resultIgnored);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002974 llvm::Value *EmitARCStoreStrongCall(Address addr, llvm::Value *value,
John McCall5b07e802013-03-13 03:10:54 +00002975 bool resultIgnored);
John McCallf85e1932011-06-15 23:02:42 +00002976 llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2977 llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
John McCall348f16f2011-10-04 06:23:45 +00002978 llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002979 void EmitARCDestroyStrong(Address addr, ARCPreciseLifetime_t precise);
John McCall5b07e802013-03-13 03:10:54 +00002980 void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
John McCallf85e1932011-06-15 23:02:42 +00002981 llvm::Value *EmitARCAutorelease(llvm::Value *value);
2982 llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2983 llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2984 llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002985 llvm::Value *EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value);
John McCallf85e1932011-06-15 23:02:42 +00002986
2987 std::pair<LValue,llvm::Value*>
2988 EmitARCStoreAutoreleasing(const BinaryOperator *e);
2989 std::pair<LValue,llvm::Value*>
2990 EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002991 std::pair<LValue,llvm::Value*>
2992 EmitARCStoreUnsafeUnretained(const BinaryOperator *e, bool ignored);
John McCallf85e1932011-06-15 23:02:42 +00002993
John McCall2b014d62011-10-01 10:32:24 +00002994 llvm::Value *EmitObjCThrowOperand(const Expr *expr);
John McCallf85e1932011-06-15 23:02:42 +00002995 llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2996 llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2997
John McCall348f16f2011-10-04 06:23:45 +00002998 llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002999 llvm::Value *EmitARCReclaimReturnedObject(const Expr *e,
3000 bool allowUnsafeClaim);
John McCallf85e1932011-06-15 23:02:42 +00003001 llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
3002 llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003003 llvm::Value *EmitARCUnsafeUnretainedScalarExpr(const Expr *expr);
John McCallf85e1932011-06-15 23:02:42 +00003004
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003005 void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
John McCallb6a60792013-03-23 02:35:54 +00003006
John McCallbdc4d802011-07-09 01:37:26 +00003007 static Destroyer destroyARCStrongImprecise;
3008 static Destroyer destroyARCStrongPrecise;
3009 static Destroyer destroyARCWeak;
3010
John McCallf85e1932011-06-15 23:02:42 +00003011 void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
3012 llvm::Value *EmitObjCAutoreleasePoolPush();
3013 llvm::Value *EmitObjCMRRAutoreleasePoolPush();
3014 void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
3015 void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
3016
Richard Smithd4ec5622013-06-12 23:38:09 +00003017 /// \brief Emits a reference binding to the passed in expression.
3018 RValue EmitReferenceBindingToExpr(const Expr *E);
Anders Carlsson3aba0932010-01-31 18:34:51 +00003019
Chris Lattner883f6a72007-08-11 00:04:45 +00003020 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +00003021 // Expression Emission
Chris Lattner883f6a72007-08-11 00:04:45 +00003022 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +00003023
3024 // Expressions are broken into three classes: scalar, complex, aggregate.
Mike Stump0dd9e882009-02-08 23:14:22 +00003025
3026 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
3027 /// scalar type, returning the result.
Anders Carlsson14c5cbf2009-08-16 07:36:22 +00003028 llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00003029
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003030 /// Emit a conversion from the specified type to the specified destination
3031 /// type, both of which are LLVM scalar types.
Chris Lattner3707b252007-08-26 06:48:56 +00003032 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003033 QualType DstTy, SourceLocation Loc);
Mike Stump0dd9e882009-02-08 23:14:22 +00003034
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003035 /// Emit a conversion from the specified complex type to the specified
3036 /// destination type, where the destination type is an LLVM scalar type.
Chris Lattner4f1a7b32007-08-26 16:34:22 +00003037 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003038 QualType DstTy,
3039 SourceLocation Loc);
Mike Stump0dd9e882009-02-08 23:14:22 +00003040
John McCall558d2ab2010-09-15 10:14:12 +00003041 /// EmitAggExpr - Emit the computation of the specified expression
3042 /// of aggregate type. The result is computed into the given slot,
3043 /// which may be null to indicate that the value is not needed.
John McCalle0c11682012-07-02 23:58:38 +00003044 void EmitAggExpr(const Expr *E, AggValueSlot AS);
Mike Stump0dd9e882009-02-08 23:14:22 +00003045
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00003046 /// EmitAggExprToLValue - Emit the computation of the specified expression of
3047 /// aggregate type into a temporary LValue.
3048 LValue EmitAggExprToLValue(const Expr *E);
3049
John McCall0c24c802011-06-24 23:21:27 +00003050 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
3051 /// make sure it survives garbage collection until this point.
3052 void EmitExtendGCLifetime(llvm::Value *object);
3053
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00003054 /// EmitComplexExpr - Emit the computation of the specified expression of
Chris Lattner23b1cdb2007-08-23 23:43:33 +00003055 /// complex type, returning the result.
John McCallb418d742010-11-16 10:08:07 +00003056 ComplexPairTy EmitComplexExpr(const Expr *E,
3057 bool IgnoreReal = false,
3058 bool IgnoreImag = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00003059
John McCall9d232c82013-03-07 21:37:08 +00003060 /// EmitComplexExprIntoLValue - Emit the given expression of complex
3061 /// type and place its result into the specified l-value.
3062 void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00003063
John McCall9d232c82013-03-07 21:37:08 +00003064 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
3065 void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
3066
3067 /// EmitLoadOfComplex - Load a complex number from the specified l-value.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00003068 ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
Chris Lattner2621fd12008-05-08 05:58:21 +00003069
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003070 Address emitAddrOfRealComponent(Address complex, QualType complexType);
3071 Address emitAddrOfImagComponent(Address complex, QualType complexType);
3072
Chandler Carruth0f30a122012-03-30 19:44:53 +00003073 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
3074 /// global variable that has already been created for it. If the initializer
3075 /// has a different type than GV does, this may free GV and return a different
3076 /// one. Otherwise it just returns GV.
3077 llvm::GlobalVariable *
3078 AddInitializerToStaticVarDecl(const VarDecl &D,
3079 llvm::GlobalVariable *GV);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003080
Daniel Dunbar0096acf2009-02-25 19:24:29 +00003081
Anders Carlsson3b2e16b2009-08-08 21:45:14 +00003082 /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
3083 /// variable with global storage.
Richard Smith7ca48502012-02-13 22:16:19 +00003084 void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr,
3085 bool PerformInit);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +00003086
Stephen Hines176edba2014-12-01 14:53:08 -08003087 llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor,
3088 llvm::Constant *Addr);
3089
John McCall20bb1752012-05-01 06:13:13 +00003090 /// Call atexit() with a function that passes the given argument to
3091 /// the given function.
David Blaikiec7971a92013-08-27 23:57:18 +00003092 void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn,
3093 llvm::Constant *addr);
Mike Stump1eb44332009-09-09 15:08:12 +00003094
John McCall3030eb82010-11-06 09:44:32 +00003095 /// Emit code in this function to perform a guarded variable
3096 /// initialization. Guarded initializations are used when it's not
3097 /// possible to prove that an initialization will be done exactly
3098 /// once, e.g. with a static local variable or a static data member
3099 /// of a class template.
Chandler Carruth0f30a122012-03-30 19:44:53 +00003100 void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
Richard Smith7ca48502012-02-13 22:16:19 +00003101 bool PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +00003102
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00003103 /// GenerateCXXGlobalInitFunc - Generates code for initializing global
3104 /// variables.
3105 void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
Stephen Hines176edba2014-12-01 14:53:08 -08003106 ArrayRef<llvm::Function *> CXXThreadLocals,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003107 Address Guard = Address::invalid());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00003108
John McCall3f88f682012-04-06 18:21:03 +00003109 /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00003110 /// variables.
John McCall3f88f682012-04-06 18:21:03 +00003111 void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
3112 const std::vector<std::pair<llvm::WeakVH,
3113 llvm::Constant*> > &DtorsAndObjects);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00003114
John McCalld26bc762011-03-09 04:27:21 +00003115 void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
3116 const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +00003117 llvm::GlobalVariable *Addr,
3118 bool PerformInit);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00003119
John McCall558d2ab2010-09-15 10:14:12 +00003120 void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
Fariborz Jahanian34999872010-11-13 21:53:34 +00003121
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003122 void EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, const Expr *Exp);
Mike Stump1eb44332009-09-09 15:08:12 +00003123
John McCall1a343eb2011-11-10 08:15:53 +00003124 void enterFullExpression(const ExprWithCleanups *E) {
3125 if (E->getNumObjects() == 0) return;
3126 enterNonTrivialFullExpression(E);
3127 }
3128 void enterNonTrivialFullExpression(const ExprWithCleanups *E);
Mike Stump1eb44332009-09-09 15:08:12 +00003129
Richard Smith4c71b8c2013-05-07 21:53:22 +00003130 void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
Douglas Gregor1eb2e592010-05-16 00:44:00 +00003131
Eli Friedman4c5d8af2012-02-09 03:32:31 +00003132 void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
3133
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003134 RValue EmitAtomicExpr(AtomicExpr *E);
Eli Friedman276b0612011-10-11 02:20:01 +00003135
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00003136 //===--------------------------------------------------------------------===//
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003137 // Annotations Emission
3138 //===--------------------------------------------------------------------===//
3139
3140 /// Emit an annotation call (intrinsic or builtin).
3141 llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn,
3142 llvm::Value *AnnotatedVal,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003143 StringRef AnnotationStr,
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003144 SourceLocation Location);
3145
3146 /// Emit local annotations for the local variable V, declared by D.
3147 void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
3148
3149 /// Emit field annotations for the given field & value. Returns the
3150 /// annotation result.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003151 Address EmitFieldAnnotations(const FieldDecl *D, Address V);
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003152
3153 //===--------------------------------------------------------------------===//
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00003154 // Internal Helpers
3155 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00003156
Chris Lattner0946ccd2008-11-11 07:41:27 +00003157 /// ContainsLabel - Return true if the statement contains a label in it. If
3158 /// this statement is not executed normally, it not containing a label means
3159 /// that we can just remove the code.
3160 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00003161
Chris Lattneref425a62011-02-28 00:18:40 +00003162 /// containsBreak - Return true if the statement contains a break out of it.
3163 /// If the statement (recursively) contains a switch or loop with a break
3164 /// inside of it, this is fine.
3165 static bool containsBreak(const Stmt *S);
3166
Daniel Dunbar4bc04552008-11-12 10:12:14 +00003167 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
Chris Lattnerc2c90012011-02-27 23:02:32 +00003168 /// to a constant, or if it does but contains a label, return false. If it
3169 /// constant folds return true and set the boolean result in Result.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003170 bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result,
3171 bool AllowLabels = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00003172
Chris Lattneref425a62011-02-28 00:18:40 +00003173 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
3174 /// to a constant, or if it does but contains a label, return false. If it
3175 /// constant folds return true and set the folded value.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003176 bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result,
3177 bool AllowLabels = false);
3178
Chris Lattner31a09842008-11-12 08:04:58 +00003179 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
3180 /// if statement) to the specified blocks. Based on the condition, this might
3181 /// try to simplify the codegen of the conditional based on the branch.
Stephen Hines651f13c2014-04-23 16:59:28 -07003182 /// TrueCount should be the number of times we expect the condition to
3183 /// evaluate to true based on PGO data.
Chris Lattner9bc47e22008-11-12 07:46:33 +00003184 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
Stephen Hines651f13c2014-04-23 16:59:28 -07003185 llvm::BasicBlock *FalseBlock, uint64_t TrueCount);
Mike Stumpbe07f602009-12-14 21:58:14 +00003186
Richard Smith4def70d2012-10-09 19:52:38 +00003187 /// \brief Emit a description of a type in a format suitable for passing to
3188 /// a runtime sanitizer handler.
3189 llvm::Constant *EmitCheckTypeDescriptor(QualType T);
3190
3191 /// \brief Convert a value into a format suitable for passing to a runtime
3192 /// sanitizer handler.
3193 llvm::Value *EmitCheckValue(llvm::Value *V);
3194
3195 /// \brief Emit a description of a source location in a format suitable for
3196 /// passing to a runtime sanitizer handler.
3197 llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
3198
Richard Smithcc305612012-11-01 22:15:34 +00003199 /// \brief Create a basic block that will call a handler function in a
3200 /// sanitizer runtime with the provided arguments, and create a conditional
3201 /// branch to it.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003202 void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked,
Stephen Hines176edba2014-12-01 14:53:08 -08003203 StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
3204 ArrayRef<llvm::Value *> DynamicArgs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003205
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003206 /// \brief Emit a slow path cross-DSO CFI check which calls __cfi_slowpath
3207 /// if Cond if false.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003208 void EmitCfiSlowPathCheck(SanitizerMask Kind, llvm::Value *Cond,
3209 llvm::ConstantInt *TypeId, llvm::Value *Ptr,
3210 ArrayRef<llvm::Constant *> StaticArgs);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003211
Richard Smithcc305612012-11-01 22:15:34 +00003212 /// \brief Create a basic block that will call the trap intrinsic, and emit a
3213 /// conditional branch to it, for the -ftrapv checks.
Chad Rosier78d85b12013-01-29 23:31:22 +00003214 void EmitTrapCheck(llvm::Value *Checked);
Richard Smithcc305612012-11-01 22:15:34 +00003215
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003216 /// \brief Emit a call to trap or debugtrap and attach function attribute
3217 /// "trap-func-name" if specified.
3218 llvm::CallInst *EmitTrapCall(llvm::Intrinsic::ID IntrID);
3219
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003220 /// \brief Emit a cross-DSO CFI failure handling function.
3221 void EmitCfiCheckFail();
3222
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003223 /// \brief Create a check for a function parameter that may potentially be
3224 /// declared as non-null.
3225 void EmitNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc,
3226 const FunctionDecl *FD, unsigned ParmNum);
3227
Anders Carlsson21c9ad92010-03-30 03:27:09 +00003228 /// EmitCallArg - Emit a single call argument.
John McCall413ebdb2011-03-11 20:59:21 +00003229 void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
Anders Carlsson21c9ad92010-03-30 03:27:09 +00003230
John McCall27360712010-05-26 22:34:26 +00003231 /// EmitDelegateCallArg - We are performing a delegate call; that
3232 /// is, the current function is delegating to another one. Produce
3233 /// a r-value suitable for passing the given parameter.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00003234 void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
3235 SourceLocation loc);
John McCall27360712010-05-26 22:34:26 +00003236
Peter Collingbournec5096cb2011-10-27 19:19:51 +00003237 /// SetFPAccuracy - Set the minimum required accuracy of the given floating
3238 /// point operation, expressed as the maximum relative error in ulp.
Duncan Sands82500162012-04-10 08:23:07 +00003239 void SetFPAccuracy(llvm::Value *Val, float Accuracy);
Peter Collingbournec5096cb2011-10-27 19:19:51 +00003240
Chris Lattner31a09842008-11-12 08:04:58 +00003241private:
Rafael Espindolac3f89552012-03-24 16:50:34 +00003242 llvm::MDNode *getRangeForLoadFromType(QualType Ty);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00003243 void EmitReturnOfRValue(RValue RV, QualType Ty);
3244
Stephen Hines651f13c2014-04-23 16:59:28 -07003245 void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
3246
3247 llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4>
3248 DeferredReplacements;
3249
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003250 /// Set the address of a local variable.
3251 void setAddrOfLocalVar(const VarDecl *VD, Address Addr) {
3252 assert(!LocalDeclMap.count(VD) && "Decl already exists in LocalDeclMap!");
3253 LocalDeclMap.insert({VD, Addr});
3254 }
3255
Daniel Dunbar56273772008-09-17 00:51:38 +00003256 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
3257 /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
3258 ///
3259 /// \param AI - The first function argument of the expansion.
Stephen Hines176edba2014-12-01 14:53:08 -08003260 void ExpandTypeFromArgs(QualType Ty, LValue Dst,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003261 SmallVectorImpl<llvm::Value *>::iterator &AI);
Daniel Dunbar56273772008-09-17 00:51:38 +00003262
Stephen Hines176edba2014-12-01 14:53:08 -08003263 /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg
3264 /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
3265 /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
3266 void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
3267 SmallVectorImpl<llvm::Value *> &IRCallArgs,
3268 unsigned &IRCallArgPos);
Anders Carlssonc8c7b182009-01-11 19:40:10 +00003269
Chad Rosier42b60552012-08-23 20:00:18 +00003270 llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
Anders Carlssonc8c7b182009-01-11 19:40:10 +00003271 const Expr *InputExpr, std::string &ConstraintStr);
Mike Stump0dd9e882009-02-08 23:14:22 +00003272
Chad Rosier42b60552012-08-23 20:00:18 +00003273 llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
Eli Friedman6d7cfd72010-07-16 00:55:21 +00003274 LValue InputValue, QualType InputType,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00003275 std::string &ConstraintStr,
3276 SourceLocation Loc);
Eli Friedman6d7cfd72010-07-16 00:55:21 +00003277
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003278 /// \brief Attempts to statically evaluate the object size of E. If that
3279 /// fails, emits code to figure the size of E out for us. This is
3280 /// pass_object_size aware.
3281 llvm::Value *evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type,
3282 llvm::IntegerType *ResType);
3283
3284 /// \brief Emits the size of E, as required by __builtin_object_size. This
3285 /// function is aware of pass_object_size parameters, and will act accordingly
3286 /// if E is a parameter with the pass_object_size attribute.
3287 llvm::Value *emitBuiltinObjectSize(const Expr *E, unsigned Type,
3288 llvm::IntegerType *ResType);
3289
Stephen Hines651f13c2014-04-23 16:59:28 -07003290public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003291#ifndef NDEBUG
3292 // Determine whether the given argument is an Objective-C method
3293 // that may have type parameters in its signature.
3294 static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method) {
3295 const DeclContext *dc = method->getDeclContext();
3296 if (const ObjCInterfaceDecl *classDecl= dyn_cast<ObjCInterfaceDecl>(dc)) {
3297 return classDecl->getTypeParamListAsWritten();
3298 }
3299
3300 if (const ObjCCategoryDecl *catDecl = dyn_cast<ObjCCategoryDecl>(dc)) {
3301 return catDecl->getTypeParamList();
3302 }
3303
3304 return false;
3305 }
3306
3307 template<typename T>
3308 static bool isObjCMethodWithTypeParams(const T *) { return false; }
3309#endif
3310
Anders Carlsson0139bb92009-04-08 20:47:54 +00003311 /// EmitCallArgs - Emit call arguments for a function.
Stephen Hines651f13c2014-04-23 16:59:28 -07003312 template <typename T>
3313 void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003314 llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
Stephen Hines176edba2014-12-01 14:53:08 -08003315 const FunctionDecl *CalleeDecl = nullptr,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003316 unsigned ParamsToSkip = 0) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003317 SmallVector<QualType, 16> ArgTypes;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003318 CallExpr::const_arg_iterator Arg = ArgRange.begin();
Stephen Hines651f13c2014-04-23 16:59:28 -07003319
Stephen Hines176edba2014-12-01 14:53:08 -08003320 assert((ParamsToSkip == 0 || CallArgTypeInfo) &&
3321 "Can't skip parameters if type info is not provided");
3322 if (CallArgTypeInfo) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003323#ifndef NDEBUG
3324 bool isGenericMethod = isObjCMethodWithTypeParams(CallArgTypeInfo);
3325#endif
3326
Stephen Hines176edba2014-12-01 14:53:08 -08003327 // First, use the argument types that the type info knows about
3328 for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip,
3329 E = CallArgTypeInfo->param_type_end();
3330 I != E; ++I, ++Arg) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003331 assert(Arg != ArgRange.end() && "Running over edge of argument list!");
3332 assert((isGenericMethod ||
3333 ((*I)->isVariablyModifiedType() ||
3334 (*I).getNonReferenceType()->isObjCRetainableType() ||
3335 getContext()
3336 .getCanonicalType((*I).getNonReferenceType())
3337 .getTypePtr() ==
3338 getContext()
3339 .getCanonicalType((*Arg)->getType())
3340 .getTypePtr())) &&
3341 "type mismatch in call argument!");
Stephen Hines176edba2014-12-01 14:53:08 -08003342 ArgTypes.push_back(*I);
3343 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003344 }
3345
3346 // Either we've emitted all the call args, or we have a call to variadic
Stephen Hines176edba2014-12-01 14:53:08 -08003347 // function.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003348 assert((Arg == ArgRange.end() || !CallArgTypeInfo ||
3349 CallArgTypeInfo->isVariadic()) &&
3350 "Extra arguments in non-variadic function!");
Stephen Hines651f13c2014-04-23 16:59:28 -07003351
3352 // If we still have any arguments, emit them using the type of the argument.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003353 for (auto *A : llvm::make_range(Arg, ArgRange.end()))
3354 ArgTypes.push_back(getVarArgType(A));
Stephen Hines651f13c2014-04-23 16:59:28 -07003355
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003356 EmitCallArgs(Args, ArgTypes, ArgRange, CalleeDecl, ParamsToSkip);
Stephen Hines651f13c2014-04-23 16:59:28 -07003357 }
3358
3359 void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003360 llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
Stephen Hines176edba2014-12-01 14:53:08 -08003361 const FunctionDecl *CalleeDecl = nullptr,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003362 unsigned ParamsToSkip = 0);
Stephen Hines651f13c2014-04-23 16:59:28 -07003363
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003364 /// EmitPointerWithAlignment - Given an expression with a pointer
3365 /// type, emit the value and compute our best estimate of the
3366 /// alignment of the pointee.
3367 ///
3368 /// Note that this function will conservatively fall back on the type
3369 /// when it doesn't
3370 ///
3371 /// \param Source - If non-null, this will be initialized with
3372 /// information about the source of the alignment. Note that this
3373 /// function will conservatively fall back on the type when it
3374 /// doesn't recognize the expression, which means that sometimes
3375 ///
3376 /// a worst-case One
3377 /// reasonable way to use this information is when there's a
3378 /// language guarantee that the pointer must be aligned to some
3379 /// stricter value, and we're simply trying to ensure that
3380 /// sufficiently obvious uses of under-aligned objects don't get
3381 /// miscompiled; for example, a placement new into the address of
3382 /// a local variable. In such a case, it's quite reasonable to
3383 /// just ignore the returned alignment when it isn't from an
3384 /// explicit source.
3385 Address EmitPointerWithAlignment(const Expr *Addr,
3386 AlignmentSource *Source = nullptr);
3387
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003388 void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK);
3389
Stephen Hines651f13c2014-04-23 16:59:28 -07003390private:
Stephen Hines176edba2014-12-01 14:53:08 -08003391 QualType getVarArgType(const Expr *Arg);
3392
John McCall492c4f92010-03-03 04:15:11 +00003393 const TargetCodeGenInfo &getTargetHooks() const {
3394 return CGM.getTargetCodeGenInfo();
3395 }
John McCall744016d2010-07-06 23:57:41 +00003396
3397 void EmitDeclMetadata();
John McCallf0c11f72011-03-31 08:03:29 +00003398
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003399 BlockByrefHelpers *buildByrefHelpers(llvm::StructType &byrefType,
3400 const AutoVarEmission &emission);
Dan Gohmanb49bd272012-02-16 00:57:37 +00003401
3402 void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
Jay Foadf4c3db12012-03-02 18:34:30 +00003403
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003404 llvm::Value *GetValueForARMHint(unsigned BuiltinID);
Reid Spencer5f016e22007-07-11 17:01:13 +00003405};
Mike Stump1eb44332009-09-09 15:08:12 +00003406
John McCall150b4622011-01-26 04:00:11 +00003407/// Helper class with most of the code for saving a value for a
3408/// conditional expression cleanup.
John McCall804b8072011-01-28 10:53:53 +00003409struct DominatingLLVMValue {
John McCall150b4622011-01-26 04:00:11 +00003410 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
3411
3412 /// Answer whether the given value needs extra work to be saved.
3413 static bool needsSaving(llvm::Value *value) {
3414 // If it's not an instruction, we don't need to save.
3415 if (!isa<llvm::Instruction>(value)) return false;
3416
3417 // If it's an instruction in the entry block, we don't need to save.
3418 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
3419 return (block != &block->getParent()->getEntryBlock());
3420 }
3421
3422 /// Try to save the given value.
3423 static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
3424 if (!needsSaving(value)) return saved_type(value, false);
3425
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003426 // Otherwise, we need an alloca.
3427 auto align = CharUnits::fromQuantity(
3428 CGF.CGM.getDataLayout().getPrefTypeAlignment(value->getType()));
3429 Address alloca =
3430 CGF.CreateTempAlloca(value->getType(), align, "cond-cleanup.save");
John McCall150b4622011-01-26 04:00:11 +00003431 CGF.Builder.CreateStore(value, alloca);
3432
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003433 return saved_type(alloca.getPointer(), true);
John McCall150b4622011-01-26 04:00:11 +00003434 }
3435
3436 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003437 // If the value says it wasn't saved, trust that it's still dominating.
John McCall150b4622011-01-26 04:00:11 +00003438 if (!value.getInt()) return value.getPointer();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003439
3440 // Otherwise, it should be an alloca instruction, as set up in save().
3441 auto alloca = cast<llvm::AllocaInst>(value.getPointer());
3442 return CGF.Builder.CreateAlignedLoad(alloca, alloca->getAlignment());
John McCall150b4622011-01-26 04:00:11 +00003443 }
3444};
3445
John McCall804b8072011-01-28 10:53:53 +00003446/// A partial specialization of DominatingValue for llvm::Values that
3447/// might be llvm::Instructions.
3448template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
3449 typedef T *type;
John McCall150b4622011-01-26 04:00:11 +00003450 static type restore(CodeGenFunction &CGF, saved_type value) {
John McCall804b8072011-01-28 10:53:53 +00003451 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
3452 }
3453};
3454
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003455/// A specialization of DominatingValue for Address.
3456template <> struct DominatingValue<Address> {
3457 typedef Address type;
3458
3459 struct saved_type {
3460 DominatingLLVMValue::saved_type SavedValue;
3461 CharUnits Alignment;
3462 };
3463
3464 static bool needsSaving(type value) {
3465 return DominatingLLVMValue::needsSaving(value.getPointer());
3466 }
3467 static saved_type save(CodeGenFunction &CGF, type value) {
3468 return { DominatingLLVMValue::save(CGF, value.getPointer()),
3469 value.getAlignment() };
3470 }
3471 static type restore(CodeGenFunction &CGF, saved_type value) {
3472 return Address(DominatingLLVMValue::restore(CGF, value.SavedValue),
3473 value.Alignment);
3474 }
3475};
3476
John McCall804b8072011-01-28 10:53:53 +00003477/// A specialization of DominatingValue for RValue.
3478template <> struct DominatingValue<RValue> {
3479 typedef RValue type;
3480 class saved_type {
3481 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
3482 AggregateAddress, ComplexAddress };
3483
3484 llvm::Value *Value;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003485 unsigned K : 3;
3486 unsigned Align : 29;
3487 saved_type(llvm::Value *v, Kind k, unsigned a = 0)
3488 : Value(v), K(k), Align(a) {}
John McCall804b8072011-01-28 10:53:53 +00003489
3490 public:
3491 static bool needsSaving(RValue value);
3492 static saved_type save(CodeGenFunction &CGF, RValue value);
3493 RValue restore(CodeGenFunction &CGF);
3494
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003495 // implementations in CGCleanup.cpp
John McCall804b8072011-01-28 10:53:53 +00003496 };
3497
3498 static bool needsSaving(type value) {
3499 return saved_type::needsSaving(value);
3500 }
3501 static saved_type save(CodeGenFunction &CGF, type value) {
3502 return saved_type::save(CGF, value);
3503 }
3504 static type restore(CodeGenFunction &CGF, saved_type value) {
3505 return value.restore(CGF);
John McCall150b4622011-01-26 04:00:11 +00003506 }
3507};
3508
Reid Spencer5f016e22007-07-11 17:01:13 +00003509} // end namespace CodeGen
3510} // end namespace clang
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00003511
Reid Spencer5f016e22007-07-11 17:01:13 +00003512#endif