Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1 | //===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 10 | // This is the internal per-function state used for llvm translation. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H |
| 15 | #define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 17 | #include "CGBuilder.h" |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 18 | #include "CGDebugInfo.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 19 | #include "CGLoopInfo.h" |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 20 | #include "CGValue.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "CodeGenModule.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 22 | #include "CodeGenPGO.h" |
| 23 | #include "EHScopeStack.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/AST/CharUnits.h" |
| 25 | #include "clang/AST/ExprCXX.h" |
| 26 | #include "clang/AST/ExprObjC.h" |
| 27 | #include "clang/AST/Type.h" |
| 28 | #include "clang/Basic/ABI.h" |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 29 | #include "clang/Basic/CapturedStmt.h" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 30 | #include "clang/Basic/OpenMPKinds.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
| 32 | #include "clang/Frontend/CodeGenOptions.h" |
| 33 | #include "llvm/ADT/ArrayRef.h" |
| 34 | #include "llvm/ADT/DenseMap.h" |
| 35 | #include "llvm/ADT/SmallVector.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 36 | #include "llvm/IR/ValueHandle.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 38 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | namespace llvm { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 40 | class BasicBlock; |
| 41 | class LLVMContext; |
| 42 | class MDNode; |
| 43 | class Module; |
| 44 | class SwitchInst; |
| 45 | class Twine; |
| 46 | class Value; |
| 47 | class CallSite; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | namespace clang { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 51 | class ASTContext; |
| 52 | class BlockDecl; |
| 53 | class CXXDestructorDecl; |
| 54 | class CXXForRangeStmt; |
| 55 | class CXXTryStmt; |
| 56 | class Decl; |
| 57 | class LabelDecl; |
| 58 | class EnumConstantDecl; |
| 59 | class FunctionDecl; |
| 60 | class FunctionProtoType; |
| 61 | class LabelStmt; |
| 62 | class ObjCContainerDecl; |
| 63 | class ObjCInterfaceDecl; |
| 64 | class ObjCIvarDecl; |
| 65 | class ObjCMethodDecl; |
| 66 | class ObjCImplementationDecl; |
| 67 | class ObjCPropertyImplDecl; |
| 68 | class TargetInfo; |
| 69 | class TargetCodeGenInfo; |
| 70 | class VarDecl; |
| 71 | class ObjCForCollectionStmt; |
| 72 | class ObjCAtTryStmt; |
| 73 | class ObjCAtThrowStmt; |
| 74 | class ObjCAtSynchronizedStmt; |
| 75 | class ObjCAutoreleasePoolStmt; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 76 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | namespace CodeGen { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 78 | class CodeGenTypes; |
| 79 | class CGFunctionInfo; |
| 80 | class CGRecordLayout; |
| 81 | class CGBlockInfo; |
| 82 | class CGCXXABI; |
| 83 | class BlockFlags; |
| 84 | class BlockFieldFlags; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 85 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 86 | /// The kind of evaluation to perform on values of a particular |
| 87 | /// type. Basically, is the code in CGExprScalar, CGExprComplex, or |
| 88 | /// CGExprAgg? |
| 89 | /// |
| 90 | /// TODO: should vectors maybe be split out into their own thing? |
| 91 | enum TypeEvaluationKind { |
| 92 | TEK_Scalar, |
| 93 | TEK_Complex, |
| 94 | TEK_Aggregate |
| 95 | }; |
| 96 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | /// CodeGenFunction - This class organizes the per-function state that is used |
| 98 | /// while generating LLVM code. |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 99 | class CodeGenFunction : public CodeGenTypeCache { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 100 | CodeGenFunction(const CodeGenFunction &) = delete; |
| 101 | void operator=(const CodeGenFunction &) = delete; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 102 | |
| 103 | friend class CGCXXABI; |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 104 | public: |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 105 | /// A jump destination is an abstract label, branching to which may |
| 106 | /// require a jump out through normal cleanups. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 107 | struct JumpDest { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 108 | JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {} |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 109 | JumpDest(llvm::BasicBlock *Block, |
| 110 | EHScopeStack::stable_iterator Depth, |
| 111 | unsigned Index) |
| 112 | : Block(Block), ScopeDepth(Depth), Index(Index) {} |
| 113 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 114 | bool isValid() const { return Block != nullptr; } |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 115 | llvm::BasicBlock *getBlock() const { return Block; } |
| 116 | EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; } |
| 117 | unsigned getDestIndex() const { return Index; } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 118 | |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 119 | // This should be used cautiously. |
| 120 | void setScopeDepth(EHScopeStack::stable_iterator depth) { |
| 121 | ScopeDepth = depth; |
| 122 | } |
| 123 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 124 | private: |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 125 | llvm::BasicBlock *Block; |
| 126 | EHScopeStack::stable_iterator ScopeDepth; |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 127 | unsigned Index; |
| 128 | }; |
| 129 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | CodeGenModule &CGM; // Per-module state. |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 131 | const TargetInfo &Target; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 133 | typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 134 | LoopInfoStack LoopStack; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 135 | CGBuilderTy Builder; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 136 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 137 | /// \brief CGBuilder insert helper. This function is called after an |
| 138 | /// instruction is created using Builder. |
| 139 | void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, |
| 140 | llvm::BasicBlock *BB, |
| 141 | llvm::BasicBlock::iterator InsertPt) const; |
| 142 | |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 143 | /// CurFuncDecl - Holds the Decl for the current outermost |
| 144 | /// non-closure context. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 145 | const Decl *CurFuncDecl; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 146 | /// CurCodeDecl - This is the inner-most code context, which includes blocks. |
| 147 | const Decl *CurCodeDecl; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 148 | const CGFunctionInfo *CurFnInfo; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 149 | QualType FnRetTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | llvm::Function *CurFn; |
| 151 | |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 152 | /// CurGD - The GlobalDecl for the current function being compiled. |
| 153 | GlobalDecl CurGD; |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 154 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 155 | /// PrologueCleanupDepth - The cleanup depth enclosing all the |
| 156 | /// cleanups associated with the parameters. |
| 157 | EHScopeStack::stable_iterator PrologueCleanupDepth; |
| 158 | |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 159 | /// ReturnBlock - Unified return block. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 160 | JumpDest ReturnBlock; |
| 161 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 162 | /// ReturnValue - The temporary alloca to hold the return value. This is null |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 163 | /// iff the function has no return value. |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 164 | llvm::Value *ReturnValue; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 165 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | /// AllocaInsertPoint - This is an instruction in the entry block before which |
| 167 | /// we prefer to insert allocas. |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 168 | llvm::AssertingVH<llvm::Instruction> AllocaInsertPt; |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 169 | |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 170 | /// \brief API for captured statement code generation. |
| 171 | class CGCapturedStmtInfo { |
| 172 | public: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 173 | explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default) |
| 174 | : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {} |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 175 | explicit CGCapturedStmtInfo(const CapturedStmt &S, |
| 176 | CapturedRegionKind K = CR_Default) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 177 | : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) { |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 178 | |
| 179 | RecordDecl::field_iterator Field = |
| 180 | S.getCapturedRecordDecl()->field_begin(); |
| 181 | for (CapturedStmt::const_capture_iterator I = S.capture_begin(), |
| 182 | E = S.capture_end(); |
| 183 | I != E; ++I, ++Field) { |
| 184 | if (I->capturesThis()) |
| 185 | CXXThisFieldDecl = *Field; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 186 | else if (I->capturesVariable()) |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 187 | CaptureFields[I->getCapturedVar()] = *Field; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | virtual ~CGCapturedStmtInfo(); |
| 192 | |
| 193 | CapturedRegionKind getKind() const { return Kind; } |
| 194 | |
| 195 | void setContextValue(llvm::Value *V) { ThisValue = V; } |
| 196 | // \brief Retrieve the value of the context parameter. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 197 | virtual llvm::Value *getContextValue() const { return ThisValue; } |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 198 | |
| 199 | /// \brief Lookup the captured field decl for a variable. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 200 | virtual const FieldDecl *lookup(const VarDecl *VD) const { |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 201 | return CaptureFields.lookup(VD); |
| 202 | } |
| 203 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 204 | bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; } |
| 205 | virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; } |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 206 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 207 | static bool classof(const CGCapturedStmtInfo *) { |
| 208 | return true; |
| 209 | } |
| 210 | |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 211 | /// \brief Emit the captured statement body. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 212 | virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 213 | RegionCounter Cnt = CGF.getPGORegionCounter(S); |
| 214 | Cnt.beginRegion(CGF.Builder); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 215 | CGF.EmitStmt(S); |
| 216 | } |
| 217 | |
| 218 | /// \brief Get the name of the capture helper. |
| 219 | virtual StringRef getHelperName() const { return "__captured_stmt"; } |
| 220 | |
| 221 | private: |
| 222 | /// \brief The kind of captured statement being generated. |
| 223 | CapturedRegionKind Kind; |
| 224 | |
| 225 | /// \brief Keep the map between VarDecl and FieldDecl. |
| 226 | llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields; |
| 227 | |
| 228 | /// \brief The base address of the captured record, passed in as the first |
| 229 | /// argument of the parallel region function. |
| 230 | llvm::Value *ThisValue; |
| 231 | |
| 232 | /// \brief Captured 'this' type. |
| 233 | FieldDecl *CXXThisFieldDecl; |
| 234 | }; |
| 235 | CGCapturedStmtInfo *CapturedStmtInfo; |
| 236 | |
Nuno Lopes | b3198a8 | 2012-05-08 22:10:46 +0000 | [diff] [blame] | 237 | /// BoundsChecking - Emit run-time bounds checks. Higher values mean |
| 238 | /// potentially higher performance penalties. |
| 239 | unsigned char BoundsChecking; |
| 240 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 241 | /// \brief Sanitizers enabled for this function. |
| 242 | SanitizerSet SanOpts; |
| 243 | |
| 244 | /// \brief True if CodeGen currently emits code implementing sanitizer checks. |
| 245 | bool IsSanitizerScope; |
| 246 | |
| 247 | /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope. |
| 248 | class SanitizerScope { |
| 249 | CodeGenFunction *CGF; |
| 250 | public: |
| 251 | SanitizerScope(CodeGenFunction *CGF); |
| 252 | ~SanitizerScope(); |
| 253 | }; |
| 254 | |
| 255 | /// In C++, whether we are code generating a thunk. This controls whether we |
| 256 | /// should emit cleanups. |
| 257 | bool CurFuncIsThunk; |
Will Dietz | 4f45bc0 | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 258 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 259 | /// In ARC, whether we should autorelease the return value. |
| 260 | bool AutoreleaseResult; |
| 261 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 262 | /// Whether we processed a Microsoft-style asm block during CodeGen. These can |
| 263 | /// potentially set the return value. |
| 264 | bool SawAsmBlock; |
| 265 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 266 | const CodeGen::CGBlockInfo *BlockInfo; |
| 267 | llvm::Value *BlockPointer; |
| 268 | |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 269 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
| 270 | FieldDecl *LambdaThisCaptureField; |
| 271 | |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 272 | /// \brief A mapping from NRVO variables to the flags used to indicate |
| 273 | /// when the NRVO has been applied to this variable. |
| 274 | llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 275 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 276 | EHScopeStack EHStack; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 277 | llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 278 | llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 279 | |
| 280 | /// Header for data within LifetimeExtendedCleanupStack. |
| 281 | struct LifetimeExtendedCleanupHeader { |
| 282 | /// The size of the following cleanup object. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 283 | unsigned Size : 29; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 284 | /// The kind of cleanup to push: a value from the CleanupKind enumeration. |
| 285 | unsigned Kind : 3; |
| 286 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 287 | size_t getSize() const { return size_t(Size); } |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 288 | CleanupKind getKind() const { return static_cast<CleanupKind>(Kind); } |
| 289 | }; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 290 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 291 | /// i32s containing the indexes of the cleanup destinations. |
| 292 | llvm::AllocaInst *NormalCleanupDest; |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 293 | |
| 294 | unsigned NextCleanupDestIndex; |
| 295 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 296 | /// FirstBlockInfo - The head of a singly-linked-list of block layouts. |
| 297 | CGBlockInfo *FirstBlockInfo; |
| 298 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 299 | /// EHResumeBlock - Unified block containing a call to llvm.eh.resume. |
| 300 | llvm::BasicBlock *EHResumeBlock; |
| 301 | |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 302 | /// The exception slot. All landing pads write the current exception pointer |
| 303 | /// into this alloca. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 304 | llvm::Value *ExceptionSlot; |
| 305 | |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 306 | /// The selector slot. Under the MandatoryCleanup model, all landing pads |
| 307 | /// write the current selector value into this alloca. |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 308 | llvm::AllocaInst *EHSelectorSlot; |
| 309 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 310 | llvm::AllocaInst *AbnormalTerminationSlot; |
| 311 | |
| 312 | /// The implicit parameter to SEH filter functions of type |
| 313 | /// 'EXCEPTION_POINTERS*'. |
| 314 | ImplicitParamDecl *SEHPointersDecl; |
| 315 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 316 | /// Emits a landing pad for the current EH stack. |
| 317 | llvm::BasicBlock *EmitLandingPad(); |
| 318 | |
| 319 | llvm::BasicBlock *getInvokeDestImpl(); |
| 320 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 321 | template <class T> |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 322 | typename DominatingValue<T>::saved_type saveValueInCond(T value) { |
| 323 | return DominatingValue<T>::save(*this, value); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 326 | public: |
Anders Carlsson | fa1f756 | 2009-02-10 06:07:49 +0000 | [diff] [blame] | 327 | /// ObjCEHValueStack - Stack of Objective-C exception values, used for |
| 328 | /// rethrows. |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 329 | SmallVector<llvm::Value*, 8> ObjCEHValueStack; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 330 | |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 331 | /// A class controlling the emission of a finally block. |
| 332 | class FinallyInfo { |
| 333 | /// Where the catchall's edge through the cleanup should go. |
| 334 | JumpDest RethrowDest; |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 335 | |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 336 | /// A function to call to enter the catch. |
| 337 | llvm::Constant *BeginCatchFn; |
| 338 | |
| 339 | /// An i1 variable indicating whether or not the @finally is |
| 340 | /// running for an exception. |
| 341 | llvm::AllocaInst *ForEHVar; |
| 342 | |
| 343 | /// An i8* variable into which the exception pointer to rethrow |
| 344 | /// has been saved. |
| 345 | llvm::AllocaInst *SavedExnVar; |
| 346 | |
| 347 | public: |
| 348 | void enter(CodeGenFunction &CGF, const Stmt *Finally, |
| 349 | llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn, |
| 350 | llvm::Constant *rethrowFn); |
| 351 | void exit(CodeGenFunction &CGF); |
| 352 | }; |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 353 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 354 | /// Cleanups can be emitted for two reasons: normal control leaving a region |
| 355 | /// exceptional control flow leaving a region. |
| 356 | struct SEHFinallyInfo { |
| 357 | SEHFinallyInfo() |
| 358 | : FinallyBB(nullptr), ContBB(nullptr), ResumeBB(nullptr) {} |
| 359 | |
| 360 | llvm::BasicBlock *FinallyBB; |
| 361 | llvm::BasicBlock *ContBB; |
| 362 | llvm::BasicBlock *ResumeBB; |
| 363 | }; |
| 364 | |
| 365 | /// Returns true inside SEH __try blocks. |
| 366 | bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); } |
| 367 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 368 | /// pushFullExprCleanup - Push a cleanup to be run at the end of the |
| 369 | /// current full-expression. Safe against the possibility that |
| 370 | /// we're currently inside a conditionally-evaluated expression. |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 371 | template <class T, class A0> |
| 372 | void pushFullExprCleanup(CleanupKind kind, A0 a0) { |
| 373 | // If we're not in a conditional branch, or if none of the |
| 374 | // arguments requires saving, then use the unconditional cleanup. |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 375 | if (!isInConditionalBranch()) |
| 376 | return EHStack.pushCleanup<T>(kind, a0); |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 377 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 378 | typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0); |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 379 | |
| 380 | typedef EHScopeStack::ConditionalCleanup1<T, A0> CleanupType; |
| 381 | EHStack.pushCleanup<CleanupType>(kind, a0_saved); |
| 382 | initFullExprCleanup(); |
| 383 | } |
| 384 | |
| 385 | /// pushFullExprCleanup - Push a cleanup to be run at the end of the |
| 386 | /// current full-expression. Safe against the possibility that |
| 387 | /// we're currently inside a conditionally-evaluated expression. |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 388 | template <class T, class A0, class A1> |
| 389 | void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) { |
| 390 | // If we're not in a conditional branch, or if none of the |
| 391 | // arguments requires saving, then use the unconditional cleanup. |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 392 | if (!isInConditionalBranch()) |
| 393 | return EHStack.pushCleanup<T>(kind, a0, a1); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 394 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 395 | typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0); |
| 396 | typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 397 | |
| 398 | typedef EHScopeStack::ConditionalCleanup2<T, A0, A1> CleanupType; |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 399 | EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved); |
| 400 | initFullExprCleanup(); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 403 | /// pushFullExprCleanup - Push a cleanup to be run at the end of the |
| 404 | /// current full-expression. Safe against the possibility that |
| 405 | /// we're currently inside a conditionally-evaluated expression. |
| 406 | template <class T, class A0, class A1, class A2> |
| 407 | void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2) { |
| 408 | // If we're not in a conditional branch, or if none of the |
| 409 | // arguments requires saving, then use the unconditional cleanup. |
| 410 | if (!isInConditionalBranch()) { |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 411 | return EHStack.pushCleanup<T>(kind, a0, a1, a2); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0); |
| 415 | typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1); |
| 416 | typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2); |
| 417 | |
| 418 | typedef EHScopeStack::ConditionalCleanup3<T, A0, A1, A2> CleanupType; |
| 419 | EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, a2_saved); |
| 420 | initFullExprCleanup(); |
| 421 | } |
| 422 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 423 | /// pushFullExprCleanup - Push a cleanup to be run at the end of the |
| 424 | /// current full-expression. Safe against the possibility that |
| 425 | /// we're currently inside a conditionally-evaluated expression. |
| 426 | template <class T, class A0, class A1, class A2, class A3> |
| 427 | void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2, A3 a3) { |
| 428 | // If we're not in a conditional branch, or if none of the |
| 429 | // arguments requires saving, then use the unconditional cleanup. |
| 430 | if (!isInConditionalBranch()) { |
| 431 | return EHStack.pushCleanup<T>(kind, a0, a1, a2, a3); |
| 432 | } |
| 433 | |
| 434 | typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0); |
| 435 | typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1); |
| 436 | typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2); |
| 437 | typename DominatingValue<A3>::saved_type a3_saved = saveValueInCond(a3); |
| 438 | |
| 439 | typedef EHScopeStack::ConditionalCleanup4<T, A0, A1, A2, A3> CleanupType; |
| 440 | EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, |
| 441 | a2_saved, a3_saved); |
| 442 | initFullExprCleanup(); |
| 443 | } |
| 444 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 445 | /// \brief Queue a cleanup to be pushed after finishing the current |
| 446 | /// full-expression. |
| 447 | template <class T, class A0, class A1, class A2, class A3> |
| 448 | void pushCleanupAfterFullExpr(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3) { |
| 449 | assert(!isInConditionalBranch() && "can't defer conditional cleanup"); |
| 450 | |
| 451 | LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind }; |
| 452 | |
| 453 | size_t OldSize = LifetimeExtendedCleanupStack.size(); |
| 454 | LifetimeExtendedCleanupStack.resize( |
| 455 | LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size); |
| 456 | |
| 457 | char *Buffer = &LifetimeExtendedCleanupStack[OldSize]; |
| 458 | new (Buffer) LifetimeExtendedCleanupHeader(Header); |
| 459 | new (Buffer + sizeof(Header)) T(a0, a1, a2, a3); |
| 460 | } |
| 461 | |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 462 | /// Set up the last cleaup that was pushed as a conditional |
| 463 | /// full-expression cleanup. |
| 464 | void initFullExprCleanup(); |
| 465 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 466 | /// PushDestructorCleanup - Push a cleanup to call the |
| 467 | /// complete-object destructor of an object of the given type at the |
| 468 | /// given address. Does nothing if T is not a C++ class type with a |
| 469 | /// non-trivial destructor. |
| 470 | void PushDestructorCleanup(QualType T, llvm::Value *Addr); |
| 471 | |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 472 | /// PushDestructorCleanup - Push a cleanup to call the |
| 473 | /// complete-object variant of the given destructor on the object at |
| 474 | /// the given address. |
| 475 | void PushDestructorCleanup(const CXXDestructorDecl *Dtor, |
| 476 | llvm::Value *Addr); |
| 477 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 478 | /// PopCleanupBlock - Will pop the cleanup entry on the stack and |
| 479 | /// process all branch fixups. |
Adrian Prantl | 1c3db76 | 2013-05-16 00:41:26 +0000 | [diff] [blame] | 480 | void PopCleanupBlock(bool FallThroughIsBranchThrough = false); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 481 | |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 482 | /// DeactivateCleanupBlock - Deactivates the given cleanup block. |
| 483 | /// The block cannot be reactivated. Pops it if it's the top of the |
| 484 | /// stack. |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 485 | /// |
| 486 | /// \param DominatingIP - An instruction which is known to |
| 487 | /// dominate the current IP (if set) and which lies along |
| 488 | /// all paths of execution between the current IP and the |
| 489 | /// the point at which the cleanup comes into scope. |
| 490 | void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, |
| 491 | llvm::Instruction *DominatingIP); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 492 | |
| 493 | /// ActivateCleanupBlock - Activates an initially-inactive cleanup. |
| 494 | /// Cannot be used to resurrect a deactivated cleanup. |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 495 | /// |
| 496 | /// \param DominatingIP - An instruction which is known to |
| 497 | /// dominate the current IP (if set) and which lies along |
| 498 | /// all paths of execution between the current IP and the |
| 499 | /// the point at which the cleanup comes into scope. |
| 500 | void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, |
| 501 | llvm::Instruction *DominatingIP); |
John McCall | cd2d2b7 | 2010-08-13 21:20:51 +0000 | [diff] [blame] | 502 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 503 | /// \brief Enters a new scope for capturing cleanups, all of which |
| 504 | /// will be executed once the scope is exited. |
| 505 | class RunCleanupsScope { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 506 | EHScopeStack::stable_iterator CleanupStackDepth; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 507 | size_t LifetimeExtendedCleanupStackSize; |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 508 | bool OldDidCallStackSave; |
John McCall | ed38313 | 2013-03-01 01:24:35 +0000 | [diff] [blame] | 509 | protected: |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 510 | bool PerformCleanup; |
John McCall | ed38313 | 2013-03-01 01:24:35 +0000 | [diff] [blame] | 511 | private: |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 512 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 513 | RunCleanupsScope(const RunCleanupsScope &) = delete; |
| 514 | void operator=(const RunCleanupsScope &) = delete; |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 515 | |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 516 | protected: |
| 517 | CodeGenFunction& CGF; |
Will Dietz | 4f45bc0 | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 518 | |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 519 | public: |
| 520 | /// \brief Enter a new cleanup scope. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 521 | explicit RunCleanupsScope(CodeGenFunction &CGF) |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 522 | : PerformCleanup(true), CGF(CGF) |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 523 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 524 | CleanupStackDepth = CGF.EHStack.stable_begin(); |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 525 | LifetimeExtendedCleanupStackSize = |
| 526 | CGF.LifetimeExtendedCleanupStack.size(); |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 527 | OldDidCallStackSave = CGF.DidCallStackSave; |
Argyrios Kyrtzidis | 4ada2ca | 2010-09-14 00:42:34 +0000 | [diff] [blame] | 528 | CGF.DidCallStackSave = false; |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /// \brief Exit this cleanup scope, emitting any accumulated |
| 532 | /// cleanups. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 533 | ~RunCleanupsScope() { |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 534 | if (PerformCleanup) { |
| 535 | CGF.DidCallStackSave = OldDidCallStackSave; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 536 | CGF.PopCleanupBlocks(CleanupStackDepth, |
| 537 | LifetimeExtendedCleanupStackSize); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | /// \brief Determine whether this scope requires any cleanups. |
| 542 | bool requiresCleanups() const { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 543 | return CGF.EHStack.stable_begin() != CleanupStackDepth; |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /// \brief Force the emission of cleanups now, instead of waiting |
| 547 | /// until this object is destroyed. |
| 548 | void ForceCleanup() { |
| 549 | assert(PerformCleanup && "Already forced cleanup"); |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 550 | CGF.DidCallStackSave = OldDidCallStackSave; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 551 | CGF.PopCleanupBlocks(CleanupStackDepth, |
| 552 | LifetimeExtendedCleanupStackSize); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 553 | PerformCleanup = false; |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 554 | } |
| 555 | }; |
| 556 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 557 | class LexicalScope : public RunCleanupsScope { |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 558 | SourceRange Range; |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 559 | SmallVector<const LabelDecl*, 4> Labels; |
| 560 | LexicalScope *ParentScope; |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 561 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 562 | LexicalScope(const LexicalScope &) = delete; |
| 563 | void operator=(const LexicalScope &) = delete; |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 564 | |
| 565 | public: |
| 566 | /// \brief Enter a new cleanup scope. |
| 567 | explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range) |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 568 | : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) { |
| 569 | CGF.CurLexicalScope = this; |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 570 | if (CGDebugInfo *DI = CGF.getDebugInfo()) |
| 571 | DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin()); |
| 572 | } |
| 573 | |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 574 | void addLabel(const LabelDecl *label) { |
| 575 | assert(PerformCleanup && "adding label to dead scope?"); |
| 576 | Labels.push_back(label); |
| 577 | } |
| 578 | |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 579 | /// \brief Exit this cleanup scope, emitting any accumulated |
| 580 | /// cleanups. |
| 581 | ~LexicalScope() { |
Adrian Prantl | efb72ad | 2013-04-01 19:02:06 +0000 | [diff] [blame] | 582 | if (CGDebugInfo *DI = CGF.getDebugInfo()) |
| 583 | DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd()); |
| 584 | |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 585 | // If we should perform a cleanup, force them now. Note that |
| 586 | // this ends the cleanup scope before rescoping any labels. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 587 | if (PerformCleanup) { |
| 588 | ApplyDebugLocation DL(CGF, Range.getEnd()); |
| 589 | ForceCleanup(); |
| 590 | } |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /// \brief Force the emission of cleanups now, instead of waiting |
| 594 | /// until this object is destroyed. |
| 595 | void ForceCleanup() { |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 596 | CGF.CurLexicalScope = ParentScope; |
Adrian Prantl | efb72ad | 2013-04-01 19:02:06 +0000 | [diff] [blame] | 597 | RunCleanupsScope::ForceCleanup(); |
| 598 | |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 599 | if (!Labels.empty()) |
| 600 | rescopeLabels(); |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 601 | } |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 602 | |
| 603 | void rescopeLabels(); |
Eric Christopher | c328779 | 2011-10-19 00:43:52 +0000 | [diff] [blame] | 604 | }; |
| 605 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 606 | /// \brief The scope used to remap some variables as private in the OpenMP |
| 607 | /// loop body (or other captured region emitted without outlining), and to |
| 608 | /// restore old vars back on exit. |
| 609 | class OMPPrivateScope : public RunCleanupsScope { |
| 610 | typedef llvm::DenseMap<const VarDecl *, llvm::Value *> VarDeclMapTy; |
| 611 | VarDeclMapTy SavedLocals; |
| 612 | VarDeclMapTy SavedPrivates; |
| 613 | |
| 614 | private: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 615 | OMPPrivateScope(const OMPPrivateScope &) = delete; |
| 616 | void operator=(const OMPPrivateScope &) = delete; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 617 | |
| 618 | public: |
| 619 | /// \brief Enter a new OpenMP private scope. |
| 620 | explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {} |
| 621 | |
| 622 | /// \brief Registers \a LocalVD variable as a private and apply \a |
| 623 | /// PrivateGen function for it to generate corresponding private variable. |
| 624 | /// \a PrivateGen returns an address of the generated private variable. |
| 625 | /// \return true if the variable is registered as private, false if it has |
| 626 | /// been privatized already. |
| 627 | bool |
| 628 | addPrivate(const VarDecl *LocalVD, |
| 629 | const std::function<llvm::Value *()> &PrivateGen) { |
| 630 | assert(PerformCleanup && "adding private to dead scope"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 631 | if (SavedLocals.count(LocalVD) > 0) return false; |
| 632 | SavedLocals[LocalVD] = CGF.LocalDeclMap.lookup(LocalVD); |
| 633 | CGF.LocalDeclMap.erase(LocalVD); |
| 634 | SavedPrivates[LocalVD] = PrivateGen(); |
| 635 | CGF.LocalDeclMap[LocalVD] = SavedLocals[LocalVD]; |
| 636 | return true; |
| 637 | } |
| 638 | |
| 639 | /// \brief Privatizes local variables previously registered as private. |
| 640 | /// Registration is separate from the actual privatization to allow |
| 641 | /// initializers use values of the original variables, not the private one. |
| 642 | /// This is important, for example, if the private variable is a class |
| 643 | /// variable initialized by a constructor that references other private |
| 644 | /// variables. But at initialization original variables must be used, not |
| 645 | /// private copies. |
| 646 | /// \return true if at least one variable was privatized, false otherwise. |
| 647 | bool Privatize() { |
| 648 | for (auto VDPair : SavedPrivates) { |
| 649 | CGF.LocalDeclMap[VDPair.first] = VDPair.second; |
| 650 | } |
| 651 | SavedPrivates.clear(); |
| 652 | return !SavedLocals.empty(); |
| 653 | } |
| 654 | |
| 655 | void ForceCleanup() { |
| 656 | RunCleanupsScope::ForceCleanup(); |
| 657 | // Remap vars back to the original values. |
| 658 | for (auto I : SavedLocals) { |
| 659 | CGF.LocalDeclMap[I.first] = I.second; |
| 660 | } |
| 661 | SavedLocals.clear(); |
| 662 | } |
| 663 | |
| 664 | /// \brief Exit scope - all the mapped variables are restored. |
| 665 | ~OMPPrivateScope() { ForceCleanup(); } |
| 666 | }; |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 667 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 668 | /// \brief Takes the old cleanup stack size and emits the cleanup blocks |
| 669 | /// that have been added. |
Adrian Prantl | 1c3db76 | 2013-05-16 00:41:26 +0000 | [diff] [blame] | 670 | void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize); |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 671 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 672 | /// \brief Takes the old cleanup stack size and emits the cleanup blocks |
| 673 | /// that have been added, then adds all lifetime-extended cleanups from |
| 674 | /// the given position to the stack. |
| 675 | void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize, |
| 676 | size_t OldLifetimeExtendedStackSize); |
| 677 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 678 | void ResolveBranchFixups(llvm::BasicBlock *Target); |
| 679 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 680 | /// The given basic block lies in the current EH scope, but may be a |
| 681 | /// target of a potentially scope-crossing jump; get a stable handle |
| 682 | /// to which we can perform this jump later. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 683 | JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) { |
John McCall | 413e677 | 2010-07-28 01:07:35 +0000 | [diff] [blame] | 684 | return JumpDest(Target, |
| 685 | EHStack.getInnermostNormalCleanup(), |
| 686 | NextCleanupDestIndex++); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 687 | } |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 688 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 689 | /// The given basic block lies in the current EH scope, but may be a |
| 690 | /// target of a potentially scope-crossing jump; get a stable handle |
| 691 | /// to which we can perform this jump later. |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 692 | JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) { |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 693 | return getJumpDestInCurrentScope(createBasicBlock(Name)); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /// EmitBranchThroughCleanup - Emit a branch from the current insert |
| 697 | /// block through the normal cleanup handling code (if any) and then |
| 698 | /// on to \arg Dest. |
| 699 | void EmitBranchThroughCleanup(JumpDest Dest); |
Chris Lattner | b11f919 | 2011-04-17 00:54:30 +0000 | [diff] [blame] | 700 | |
| 701 | /// isObviouslyBranchWithoutCleanups - Return true if a branch to the |
| 702 | /// specified destination obviously has no cleanups to run. 'false' is always |
| 703 | /// a conservatively correct answer for this method. |
| 704 | bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 705 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 706 | /// popCatchScope - Pops the catch scope at the top of the EHScope |
| 707 | /// stack, emitting any required code (other than the catch handlers |
| 708 | /// themselves). |
| 709 | void popCatchScope(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 710 | |
David Chisnall | c686004 | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 711 | llvm::BasicBlock *getEHResumeBlock(bool isCleanup); |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 712 | llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 713 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 714 | /// An object to manage conditionally-evaluated expressions. |
| 715 | class ConditionalEvaluation { |
| 716 | llvm::BasicBlock *StartBB; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 718 | public: |
| 719 | ConditionalEvaluation(CodeGenFunction &CGF) |
| 720 | : StartBB(CGF.Builder.GetInsertBlock()) {} |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 721 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 722 | void begin(CodeGenFunction &CGF) { |
| 723 | assert(CGF.OutermostConditional != this); |
| 724 | if (!CGF.OutermostConditional) |
| 725 | CGF.OutermostConditional = this; |
| 726 | } |
| 727 | |
| 728 | void end(CodeGenFunction &CGF) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 729 | assert(CGF.OutermostConditional != nullptr); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 730 | if (CGF.OutermostConditional == this) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 731 | CGF.OutermostConditional = nullptr; |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /// Returns a block which will be executed prior to each |
| 735 | /// evaluation of the conditional code. |
| 736 | llvm::BasicBlock *getStartingBlock() const { |
| 737 | return StartBB; |
| 738 | } |
| 739 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 741 | /// isInConditionalBranch - Return true if we're currently emitting |
| 742 | /// one branch or the other of a conditional expression. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 743 | bool isInConditionalBranch() const { return OutermostConditional != nullptr; } |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 744 | |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 745 | void setBeforeOutermostConditional(llvm::Value *value, llvm::Value *addr) { |
| 746 | assert(isInConditionalBranch()); |
| 747 | llvm::BasicBlock *block = OutermostConditional->getStartingBlock(); |
| 748 | new llvm::StoreInst(value, addr, &block->back()); |
| 749 | } |
| 750 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 751 | /// An RAII object to record that we're evaluating a statement |
| 752 | /// expression. |
| 753 | class StmtExprEvaluation { |
| 754 | CodeGenFunction &CGF; |
| 755 | |
| 756 | /// We have to save the outermost conditional: cleanups in a |
| 757 | /// statement expression aren't conditional just because the |
| 758 | /// StmtExpr is. |
| 759 | ConditionalEvaluation *SavedOutermostConditional; |
| 760 | |
| 761 | public: |
| 762 | StmtExprEvaluation(CodeGenFunction &CGF) |
| 763 | : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 764 | CGF.OutermostConditional = nullptr; |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | ~StmtExprEvaluation() { |
| 768 | CGF.OutermostConditional = SavedOutermostConditional; |
| 769 | CGF.EnsureInsertPoint(); |
| 770 | } |
| 771 | }; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 772 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 773 | /// An object which temporarily prevents a value from being |
| 774 | /// destroyed by aggressive peephole optimizations that assume that |
| 775 | /// all uses of a value have been realized in the IR. |
| 776 | class PeepholeProtection { |
| 777 | llvm::Instruction *Inst; |
| 778 | friend class CodeGenFunction; |
| 779 | |
| 780 | public: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 781 | PeepholeProtection() : Inst(nullptr) {} |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 782 | }; |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 783 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 784 | /// A non-RAII class containing all the information about a bound |
| 785 | /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for |
| 786 | /// this which makes individual mappings very simple; using this |
| 787 | /// class directly is useful when you have a variable number of |
| 788 | /// opaque values or don't want the RAII functionality for some |
| 789 | /// reason. |
| 790 | class OpaqueValueMappingData { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 791 | const OpaqueValueExpr *OpaqueValue; |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 792 | bool BoundLValue; |
| 793 | CodeGenFunction::PeepholeProtection Protection; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 794 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 795 | OpaqueValueMappingData(const OpaqueValueExpr *ov, |
| 796 | bool boundLValue) |
| 797 | : OpaqueValue(ov), BoundLValue(boundLValue) {} |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 798 | public: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 799 | OpaqueValueMappingData() : OpaqueValue(nullptr) {} |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 800 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 801 | static bool shouldBindAsLValue(const Expr *expr) { |
John McCall | a5493f8 | 2011-11-08 22:54:08 +0000 | [diff] [blame] | 802 | // gl-values should be bound as l-values for obvious reasons. |
| 803 | // Records should be bound as l-values because IR generation |
| 804 | // always keeps them in memory. Expressions of function type |
| 805 | // act exactly like l-values but are formally required to be |
| 806 | // r-values in C. |
| 807 | return expr->isGLValue() || |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 808 | expr->getType()->isFunctionType() || |
| 809 | hasAggregateEvaluationKind(expr->getType()); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 810 | } |
| 811 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 812 | static OpaqueValueMappingData bind(CodeGenFunction &CGF, |
| 813 | const OpaqueValueExpr *ov, |
| 814 | const Expr *e) { |
| 815 | if (shouldBindAsLValue(ov)) |
| 816 | return bind(CGF, ov, CGF.EmitLValue(e)); |
| 817 | return bind(CGF, ov, CGF.EmitAnyExpr(e)); |
| 818 | } |
| 819 | |
| 820 | static OpaqueValueMappingData bind(CodeGenFunction &CGF, |
| 821 | const OpaqueValueExpr *ov, |
| 822 | const LValue &lv) { |
| 823 | assert(shouldBindAsLValue(ov)); |
| 824 | CGF.OpaqueLValues.insert(std::make_pair(ov, lv)); |
| 825 | return OpaqueValueMappingData(ov, true); |
| 826 | } |
| 827 | |
| 828 | static OpaqueValueMappingData bind(CodeGenFunction &CGF, |
| 829 | const OpaqueValueExpr *ov, |
| 830 | const RValue &rv) { |
| 831 | assert(!shouldBindAsLValue(ov)); |
| 832 | CGF.OpaqueRValues.insert(std::make_pair(ov, rv)); |
| 833 | |
| 834 | OpaqueValueMappingData data(ov, false); |
| 835 | |
| 836 | // Work around an extremely aggressive peephole optimization in |
| 837 | // EmitScalarConversion which assumes that all other uses of a |
| 838 | // value are extant. |
| 839 | data.Protection = CGF.protectFromPeepholes(rv); |
| 840 | |
| 841 | return data; |
| 842 | } |
| 843 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 844 | bool isValid() const { return OpaqueValue != nullptr; } |
| 845 | void clear() { OpaqueValue = nullptr; } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 846 | |
| 847 | void unbind(CodeGenFunction &CGF) { |
| 848 | assert(OpaqueValue && "no data to unbind!"); |
| 849 | |
| 850 | if (BoundLValue) { |
| 851 | CGF.OpaqueLValues.erase(OpaqueValue); |
| 852 | } else { |
| 853 | CGF.OpaqueRValues.erase(OpaqueValue); |
| 854 | CGF.unprotectFromPeepholes(Protection); |
| 855 | } |
| 856 | } |
| 857 | }; |
| 858 | |
| 859 | /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr. |
| 860 | class OpaqueValueMapping { |
| 861 | CodeGenFunction &CGF; |
| 862 | OpaqueValueMappingData Data; |
| 863 | |
| 864 | public: |
| 865 | static bool shouldBindAsLValue(const Expr *expr) { |
| 866 | return OpaqueValueMappingData::shouldBindAsLValue(expr); |
| 867 | } |
| 868 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 869 | /// Build the opaque value mapping for the given conditional |
| 870 | /// operator if it's the GNU ?: extension. This is a common |
| 871 | /// enough pattern that the convenience operator is really |
| 872 | /// helpful. |
| 873 | /// |
| 874 | OpaqueValueMapping(CodeGenFunction &CGF, |
| 875 | const AbstractConditionalOperator *op) : CGF(CGF) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 876 | if (isa<ConditionalOperator>(op)) |
| 877 | // Leave Data empty. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 878 | return; |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 879 | |
| 880 | const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 881 | Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(), |
| 882 | e->getCommon()); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 883 | } |
| 884 | |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 885 | OpaqueValueMapping(CodeGenFunction &CGF, |
| 886 | const OpaqueValueExpr *opaqueValue, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 887 | LValue lvalue) |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 888 | : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | OpaqueValueMapping(CodeGenFunction &CGF, |
| 892 | const OpaqueValueExpr *opaqueValue, |
| 893 | RValue rvalue) |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 894 | : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | void pop() { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 898 | Data.unbind(CGF); |
| 899 | Data.clear(); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | ~OpaqueValueMapping() { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 903 | if (Data.isValid()) Data.unbind(CGF); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 904 | } |
| 905 | }; |
Fariborz Jahanian | e220455 | 2010-11-16 19:29:39 +0000 | [diff] [blame] | 906 | |
| 907 | /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field |
| 908 | /// number that holds the value. |
| 909 | unsigned getByRefValueLLVMField(const ValueDecl *VD) const; |
Fariborz Jahanian | 52a80e1 | 2011-01-26 23:08:27 +0000 | [diff] [blame] | 910 | |
| 911 | /// BuildBlockByrefAddress - Computes address location of the |
| 912 | /// variable which is declared as __block. |
| 913 | llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr, |
| 914 | const VarDecl *V); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 915 | private: |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 916 | CGDebugInfo *DebugInfo; |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 917 | bool DisableDebugInfo; |
Mike Stump | 09429b9 | 2009-02-17 17:00:02 +0000 | [diff] [blame] | 918 | |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 919 | /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid |
| 920 | /// calling llvm.stacksave for multiple VLAs in the same scope. |
| 921 | bool DidCallStackSave; |
| 922 | |
Mike Stump | f71d232 | 2009-11-30 20:08:49 +0000 | [diff] [blame] | 923 | /// IndirectBranch - The first time an indirect goto is seen we create a block |
| 924 | /// with an indirect branch. Every time we see the address of a label taken, |
| 925 | /// we add the label to the indirect goto. Every subsequent indirect goto is |
| 926 | /// codegen'd as a jump to the IndirectBranch's basic block. |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 927 | llvm::IndirectBrInst *IndirectBranch; |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 928 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 929 | /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C |
| 930 | /// decls. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 931 | typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy; |
| 932 | DeclMapTy LocalDeclMap; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 933 | |
| 934 | /// LabelMap - This keeps track of the LLVM basic block for each C label. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 935 | llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 936 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 937 | // BreakContinueStack - This keeps track of where break and continue |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 938 | // statements should jump to. |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 939 | struct BreakContinue { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 940 | BreakContinue(JumpDest Break, JumpDest Continue) |
| 941 | : BreakBlock(Break), ContinueBlock(Continue) {} |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 942 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 943 | JumpDest BreakBlock; |
| 944 | JumpDest ContinueBlock; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 945 | }; |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 946 | SmallVector<BreakContinue, 8> BreakContinueStack; |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 947 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 948 | CodeGenPGO PGO; |
| 949 | |
| 950 | public: |
| 951 | /// Get a counter for instrumentation of the region associated with the given |
| 952 | /// statement. |
| 953 | RegionCounter getPGORegionCounter(const Stmt *S) { |
| 954 | return RegionCounter(PGO, S); |
| 955 | } |
| 956 | private: |
| 957 | |
Zhongxing Xu | 170fd49 | 2012-01-14 09:08:15 +0000 | [diff] [blame] | 958 | /// SwitchInsn - This is nearest current switch instruction. It is null if |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 959 | /// current context is not in a switch. |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 960 | llvm::SwitchInst *SwitchInsn; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 961 | /// The branch weights of SwitchInsn when doing instrumentation based PGO. |
| 962 | SmallVector<uint64_t, 16> *SwitchWeights; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 963 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 964 | /// CaseRangeBlock - This block holds if condition check for last case |
Devang Patel | 80fd5f9 | 2007-10-09 17:08:50 +0000 | [diff] [blame] | 965 | /// statement range in current switch instruction. |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 966 | llvm::BasicBlock *CaseRangeBlock; |
| 967 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 968 | /// OpaqueLValues - Keeps track of the current set of opaque value |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 969 | /// expressions. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 970 | llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues; |
| 971 | llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 972 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 973 | // VLASizeMap - This keeps track of the associated size for each VLA type. |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 974 | // We track this by the size expression rather than the type itself because |
| 975 | // in certain situations, like a const qualifier applied to an VLA typedef, |
| 976 | // multiple VLA types can share the same size expression. |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 977 | // FIXME: Maybe this could be a stack of maps that is pushed/popped as we |
| 978 | // enter/leave scopes. |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 979 | llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 980 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 981 | /// A block containing a single 'unreachable' instruction. Created |
| 982 | /// lazily by getUnreachableBlock(). |
| 983 | llvm::BasicBlock *UnreachableBlock; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 984 | |
Adrian Prantl | d072e59 | 2013-05-03 20:11:48 +0000 | [diff] [blame] | 985 | /// Counts of the number return expressions in the function. |
| 986 | unsigned NumReturnExprs; |
Adrian Prantl | fa6b079 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 987 | |
| 988 | /// Count the number of simple (constant) return expressions in the function. |
| 989 | unsigned NumSimpleReturnExprs; |
| 990 | |
Adrian Prantl | d072e59 | 2013-05-03 20:11:48 +0000 | [diff] [blame] | 991 | /// The last regular (non-return) debug location (breakpoint) in the function. |
| 992 | SourceLocation LastStopPoint; |
Adrian Prantl | fa6b079 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 993 | |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 994 | public: |
| 995 | /// A scope within which we are constructing the fields of an object which |
| 996 | /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use |
| 997 | /// if we need to evaluate a CXXDefaultInitExpr within the evaluation. |
| 998 | class FieldConstructionScope { |
| 999 | public: |
| 1000 | FieldConstructionScope(CodeGenFunction &CGF, llvm::Value *This) |
| 1001 | : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) { |
| 1002 | CGF.CXXDefaultInitExprThis = This; |
| 1003 | } |
| 1004 | ~FieldConstructionScope() { |
| 1005 | CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis; |
| 1006 | } |
| 1007 | |
| 1008 | private: |
| 1009 | CodeGenFunction &CGF; |
| 1010 | llvm::Value *OldCXXDefaultInitExprThis; |
| 1011 | }; |
| 1012 | |
| 1013 | /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this' |
| 1014 | /// is overridden to be the object under construction. |
| 1015 | class CXXDefaultInitExprScope { |
| 1016 | public: |
| 1017 | CXXDefaultInitExprScope(CodeGenFunction &CGF) |
| 1018 | : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue) { |
| 1019 | CGF.CXXThisValue = CGF.CXXDefaultInitExprThis; |
| 1020 | } |
| 1021 | ~CXXDefaultInitExprScope() { |
| 1022 | CGF.CXXThisValue = OldCXXThisValue; |
| 1023 | } |
| 1024 | |
| 1025 | public: |
| 1026 | CodeGenFunction &CGF; |
| 1027 | llvm::Value *OldCXXThisValue; |
| 1028 | }; |
| 1029 | |
| 1030 | private: |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 1031 | /// CXXThisDecl - When generating code for a C++ member function, |
| 1032 | /// this will hold the implicit 'this' declaration. |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 1033 | ImplicitParamDecl *CXXABIThisDecl; |
| 1034 | llvm::Value *CXXABIThisValue; |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 1035 | llvm::Value *CXXThisValue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1037 | /// The value of 'this' to use when evaluating CXXDefaultInitExprs within |
| 1038 | /// this expression. |
| 1039 | llvm::Value *CXXDefaultInitExprThis; |
| 1040 | |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1041 | /// CXXStructorImplicitParamDecl - When generating code for a constructor or |
| 1042 | /// destructor, this will hold the implicit argument (e.g. VTT). |
| 1043 | ImplicitParamDecl *CXXStructorImplicitParamDecl; |
| 1044 | llvm::Value *CXXStructorImplicitParamValue; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1045 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 1046 | /// OutermostConditional - Points to the outermost active |
| 1047 | /// conditional control. This is used so that we know if a |
| 1048 | /// temporary should be destroyed conditionally. |
| 1049 | ConditionalEvaluation *OutermostConditional; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 1051 | /// The current lexical scope. |
| 1052 | LexicalScope *CurLexicalScope; |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 1053 | |
Adrian Prantl | 1c3db76 | 2013-05-16 00:41:26 +0000 | [diff] [blame] | 1054 | /// The current source location that should be used for exception |
| 1055 | /// handling code. |
| 1056 | SourceLocation CurEHLocation; |
| 1057 | |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 1058 | /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM |
| 1059 | /// type as well as the field number that contains the actual data. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1060 | llvm::DenseMap<const ValueDecl *, std::pair<llvm::Type *, |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 1061 | unsigned> > ByRefValueInfo; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1062 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1063 | llvm::BasicBlock *TerminateLandingPad; |
Mike Stump | 182f383 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 1064 | llvm::BasicBlock *TerminateHandler; |
Chris Lattner | 83252dc | 2010-07-20 21:07:09 +0000 | [diff] [blame] | 1065 | llvm::BasicBlock *TrapBB; |
Eli Friedman | 9406705 | 2009-12-10 02:21:21 +0000 | [diff] [blame] | 1066 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 1067 | /// Add a kernel metadata node to the named metadata node 'opencl.kernels'. |
| 1068 | /// In the kernel metadata node, reference the kernel function and metadata |
| 1069 | /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2): |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 1070 | /// - A node for the vec_type_hint(<type>) qualifier contains string |
| 1071 | /// "vec_type_hint", an undefined value of the <type> data type, |
| 1072 | /// and a Boolean that is true if the <type> is integer and signed. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 1073 | /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string |
| 1074 | /// "work_group_size_hint", and three 32-bit integers X, Y and Z. |
| 1075 | /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string |
| 1076 | /// "reqd_work_group_size", and three 32-bit integers X, Y and Z. |
| 1077 | void EmitOpenCLKernelMetadata(const FunctionDecl *FD, |
| 1078 | llvm::Function *Fn); |
| 1079 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1080 | public: |
Fariborz Jahanian | 4904bf4 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 1081 | CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false); |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 1082 | ~CodeGenFunction(); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1083 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1084 | CodeGenTypes &getTypes() const { return CGM.getTypes(); } |
John McCall | f2aac84 | 2011-05-15 02:34:36 +0000 | [diff] [blame] | 1085 | ASTContext &getContext() const { return CGM.getContext(); } |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 1086 | CGDebugInfo *getDebugInfo() { |
| 1087 | if (DisableDebugInfo) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1088 | return nullptr; |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 1089 | return DebugInfo; |
| 1090 | } |
| 1091 | void disableDebugInfo() { DisableDebugInfo = true; } |
| 1092 | void enableDebugInfo() { DisableDebugInfo = false; } |
| 1093 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1094 | bool shouldUseFusedARCCalls() { |
| 1095 | return CGM.getCodeGenOpts().OptimizationLevel == 0; |
| 1096 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1097 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1098 | const LangOptions &getLangOpts() const { return CGM.getLangOpts(); } |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1099 | |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 1100 | /// Returns a pointer to the function's exception object and selector slot, |
| 1101 | /// which is assigned in every landing pad. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1102 | llvm::Value *getExceptionSlot(); |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 1103 | llvm::Value *getEHSelectorSlot(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1104 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1105 | /// Stack slot that contains whether a __finally block is being executed as an |
| 1106 | /// EH cleanup or as a normal cleanup. |
| 1107 | llvm::Value *getAbnormalTerminationSlot(); |
| 1108 | |
Bill Wendling | ae27059 | 2011-09-15 18:57:19 +0000 | [diff] [blame] | 1109 | /// Returns the contents of the function's exception object and selector |
| 1110 | /// slots. |
| 1111 | llvm::Value *getExceptionFromSlot(); |
| 1112 | llvm::Value *getSelectorFromSlot(); |
| 1113 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1114 | llvm::Value *getNormalCleanupDestSlot(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1115 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1116 | llvm::BasicBlock *getUnreachableBlock() { |
| 1117 | if (!UnreachableBlock) { |
| 1118 | UnreachableBlock = createBasicBlock("unreachable"); |
| 1119 | new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock); |
| 1120 | } |
| 1121 | return UnreachableBlock; |
| 1122 | } |
| 1123 | |
| 1124 | llvm::BasicBlock *getInvokeDest() { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1125 | if (!EHStack.requiresLandingPad()) return nullptr; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1126 | return getInvokeDestImpl(); |
| 1127 | } |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1128 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1129 | bool currentFunctionUsesSEHTry() const { |
| 1130 | const auto *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl); |
| 1131 | return FD && FD->usesSEHTry(); |
| 1132 | } |
| 1133 | |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1134 | const TargetInfo &getTarget() const { return Target; } |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1135 | llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); } |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1136 | |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1137 | //===--------------------------------------------------------------------===// |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1138 | // Cleanups |
| 1139 | //===--------------------------------------------------------------------===// |
| 1140 | |
| 1141 | typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty); |
| 1142 | |
John McCall | 2673c68 | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 1143 | void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, |
| 1144 | llvm::Value *arrayEndPointer, |
| 1145 | QualType elementType, |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1146 | Destroyer *destroyer); |
John McCall | 2673c68 | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 1147 | void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, |
| 1148 | llvm::Value *arrayEnd, |
| 1149 | QualType elementType, |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1150 | Destroyer *destroyer); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1151 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1152 | void pushDestroy(QualType::DestructionKind dtorKind, |
| 1153 | llvm::Value *addr, QualType type); |
John McCall | 074cae0 | 2013-02-01 05:11:40 +0000 | [diff] [blame] | 1154 | void pushEHDestroy(QualType::DestructionKind dtorKind, |
| 1155 | llvm::Value *addr, QualType type); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1156 | void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type, |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1157 | Destroyer *destroyer, bool useEHCleanupForArray); |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 1158 | void pushLifetimeExtendedDestroy(CleanupKind kind, llvm::Value *addr, |
| 1159 | QualType type, Destroyer *destroyer, |
| 1160 | bool useEHCleanupForArray); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1161 | void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, |
| 1162 | llvm::Value *CompletePtr, |
| 1163 | QualType ElementType); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1164 | void pushStackRestore(CleanupKind kind, llvm::Value *SPMem); |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1165 | void emitDestroy(llvm::Value *addr, QualType type, Destroyer *destroyer, |
John McCall | 2673c68 | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 1166 | bool useEHCleanupForArray); |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 1167 | llvm::Function *generateDestroyHelper(llvm::Constant *addr, QualType type, |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1168 | Destroyer *destroyer, |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 1169 | bool useEHCleanupForArray, |
| 1170 | const VarDecl *VD); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1171 | void emitArrayDestroy(llvm::Value *begin, llvm::Value *end, |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1172 | QualType type, Destroyer *destroyer, |
John McCall | fbf780a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 1173 | bool checkZeroLength, bool useEHCleanup); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1174 | |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1175 | Destroyer *getDestroyer(QualType::DestructionKind destructionKind); |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1176 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1177 | /// Determines whether an EH cleanup is required to destroy a type |
| 1178 | /// with the given destruction kind. |
| 1179 | bool needsEHCleanup(QualType::DestructionKind kind) { |
| 1180 | switch (kind) { |
| 1181 | case QualType::DK_none: |
| 1182 | return false; |
| 1183 | case QualType::DK_cxx_destructor: |
| 1184 | case QualType::DK_objc_weak_lifetime: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1185 | return getLangOpts().Exceptions; |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1186 | case QualType::DK_objc_strong_lifetime: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1187 | return getLangOpts().Exceptions && |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1188 | CGM.getCodeGenOpts().ObjCAutoRefCountExceptions; |
| 1189 | } |
| 1190 | llvm_unreachable("bad destruction kind"); |
| 1191 | } |
| 1192 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1193 | CleanupKind getCleanupKind(QualType::DestructionKind kind) { |
| 1194 | return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup); |
| 1195 | } |
| 1196 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1197 | //===--------------------------------------------------------------------===// |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1198 | // Objective-C |
| 1199 | //===--------------------------------------------------------------------===// |
| 1200 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1201 | void GenerateObjCMethod(const ObjCMethodDecl *OMD); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1202 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1203 | void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1204 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1205 | /// GenerateObjCGetter - Synthesize an Objective-C property getter function. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1206 | void GenerateObjCGetter(ObjCImplementationDecl *IMP, |
| 1207 | const ObjCPropertyImplDecl *PID); |
John McCall | 1e1f487 | 2011-09-13 03:34:09 +0000 | [diff] [blame] | 1208 | void generateObjCGetterBody(const ObjCImplementationDecl *classImpl, |
Fariborz Jahanian | b6e5fe3 | 2012-01-07 18:56:22 +0000 | [diff] [blame] | 1209 | const ObjCPropertyImplDecl *propImpl, |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 1210 | const ObjCMethodDecl *GetterMothodDecl, |
Fariborz Jahanian | b6e5fe3 | 2012-01-07 18:56:22 +0000 | [diff] [blame] | 1211 | llvm::Constant *AtomicHelperFn); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 1212 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1213 | void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, |
| 1214 | ObjCMethodDecl *MD, bool ctor); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1215 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1216 | /// GenerateObjCSetter - Synthesize an Objective-C property setter function |
| 1217 | /// for the given property. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1218 | void GenerateObjCSetter(ObjCImplementationDecl *IMP, |
| 1219 | const ObjCPropertyImplDecl *PID); |
John McCall | 71c758d | 2011-09-10 09:17:20 +0000 | [diff] [blame] | 1220 | void generateObjCSetterBody(const ObjCImplementationDecl *classImpl, |
Fariborz Jahanian | cd93b96 | 2012-01-06 22:33:54 +0000 | [diff] [blame] | 1221 | const ObjCPropertyImplDecl *propImpl, |
| 1222 | llvm::Constant *AtomicHelperFn); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 1223 | bool IndirectObjCSetterArg(const CGFunctionInfo &FI); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 1224 | bool IvarTypeWithAggrGCObjects(QualType Ty); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1225 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1226 | //===--------------------------------------------------------------------===// |
| 1227 | // Block Bits |
| 1228 | //===--------------------------------------------------------------------===// |
| 1229 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1230 | llvm::Value *EmitBlockLiteral(const BlockExpr *); |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 1231 | llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info); |
| 1232 | static void destroyBlockInfos(CGBlockInfo *info); |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 1233 | llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *, |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1234 | const CGBlockInfo &Info, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1235 | llvm::StructType *, |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1236 | llvm::Constant *BlockVarLayout); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1237 | |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 1238 | llvm::Function *GenerateBlockFunction(GlobalDecl GD, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1239 | const CGBlockInfo &Info, |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 1240 | const DeclMapTy &ldm, |
| 1241 | bool IsLambdaConversionToBlock); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1242 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1243 | llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo); |
| 1244 | llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1245 | llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction( |
| 1246 | const ObjCPropertyImplDecl *PID); |
| 1247 | llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction( |
| 1248 | const ObjCPropertyImplDecl *PID); |
Eli Friedman | cae40c4 | 2012-02-28 01:08:45 +0000 | [diff] [blame] | 1249 | llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1250 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1251 | void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags); |
| 1252 | |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1253 | class AutoVarEmission; |
| 1254 | |
| 1255 | void emitByrefStructureInit(const AutoVarEmission &emission); |
| 1256 | void enterByrefCleanup(const AutoVarEmission &emission); |
| 1257 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1258 | llvm::Value *LoadBlockStruct() { |
| 1259 | assert(BlockPointer && "no block pointer set!"); |
| 1260 | return BlockPointer; |
| 1261 | } |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1262 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1263 | void AllocateBlockCXXThisPointer(const CXXThisExpr *E); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1264 | void AllocateBlockDecl(const DeclRefExpr *E); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1265 | llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1266 | llvm::Type *BuildByRefType(const VarDecl *var); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 1267 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1268 | void GenerateCode(GlobalDecl GD, llvm::Function *Fn, |
| 1269 | const CGFunctionInfo &FnInfo); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1270 | /// \brief Emit code for the start of a function. |
| 1271 | /// \param Loc The location to be associated with the function. |
| 1272 | /// \param StartLoc The location of the function body. |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 1273 | void StartFunction(GlobalDecl GD, |
| 1274 | QualType RetTy, |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 1275 | llvm::Function *Fn, |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1276 | const CGFunctionInfo &FnInfo, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 1277 | const FunctionArgList &Args, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1278 | SourceLocation Loc = SourceLocation(), |
| 1279 | SourceLocation StartLoc = SourceLocation()); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1280 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1281 | void EmitConstructorBody(FunctionArgList &Args); |
| 1282 | void EmitDestructorBody(FunctionArgList &Args); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1283 | void emitImplicitAssignmentOperatorBody(FunctionArgList &Args); |
Richard Smith | 3cebc73 | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 1284 | void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1285 | void EmitBlockWithFallThrough(llvm::BasicBlock *BB, RegionCounter &Cnt); |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 1286 | |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 1287 | void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator, |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 1288 | CallArgList &CallArgs); |
Eli Friedman | bd89f8c | 2012-02-16 01:37:33 +0000 | [diff] [blame] | 1289 | void EmitLambdaToBlockPointerBody(FunctionArgList &Args); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 1290 | void EmitLambdaBlockInvokeBody(); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 1291 | void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD); |
| 1292 | void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1293 | void EmitAsanPrologueOrEpilogue(bool Prologue); |
Eli Friedman | bd89f8c | 2012-02-16 01:37:33 +0000 | [diff] [blame] | 1294 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1295 | /// \brief Emit the unified return block, trying to avoid its emission when |
| 1296 | /// possible. |
| 1297 | /// \return The debug location of the user written return statement if the |
| 1298 | /// return block is is avoided. |
| 1299 | llvm::DebugLoc EmitReturnBlock(); |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 1300 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1301 | /// FinishFunction - Complete IR generation of the current function. It is |
| 1302 | /// legal to call this function even if there is no current insertion point. |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1303 | void FinishFunction(SourceLocation EndLoc=SourceLocation()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1304 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1305 | void StartThunk(llvm::Function *Fn, GlobalDecl GD, |
| 1306 | const CGFunctionInfo &FnInfo); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1307 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1308 | void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk); |
| 1309 | |
| 1310 | /// Emit a musttail call for a thunk with a potentially adjusted this pointer. |
| 1311 | void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr, |
| 1312 | llvm::Value *Callee); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1313 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 1314 | /// GenerateThunk - Generate a thunk for the given method. |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1315 | void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo, |
| 1316 | GlobalDecl GD, const ThunkInfo &Thunk); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1317 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 1318 | void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo, |
| 1319 | GlobalDecl GD, const ThunkInfo &Thunk); |
| 1320 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1321 | void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type, |
| 1322 | FunctionArgList &Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 1324 | void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init, |
| 1325 | ArrayRef<VarDecl *> ArrayIndexes); |
| 1326 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1327 | /// InitializeVTablePointer - Initialize the vtable pointer of the given |
| 1328 | /// subobject. |
| 1329 | /// |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1330 | void InitializeVTablePointer(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1331 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1332 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1333 | const CXXRecordDecl *VTableClass); |
| 1334 | |
| 1335 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1336 | void InitializeVTablePointers(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1337 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1338 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1339 | bool BaseIsNonVirtualPrimaryBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1340 | const CXXRecordDecl *VTableClass, |
| 1341 | VisitedVirtualBasesSetTy& VBases); |
Eli Friedman | 77a259c | 2009-12-08 06:46:18 +0000 | [diff] [blame] | 1342 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1343 | void InitializeVTablePointers(const CXXRecordDecl *ClassDecl); |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1344 | |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 1345 | /// GetVTablePtr - Return the Value of the vtable pointer member pointed |
| 1346 | /// to by This. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1347 | llvm::Value *GetVTablePtr(llvm::Value *This, llvm::Type *Ty); |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1348 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1349 | /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable. |
| 1350 | /// If vptr CFI is enabled, emit a check that VTable is valid. |
| 1351 | void EmitVTablePtrCheckForCall(const CXXMethodDecl *MD, llvm::Value *VTable); |
Benjamin Kramer | 9581ed0 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 1352 | |
| 1353 | /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given |
| 1354 | /// expr can be devirtualized. |
| 1355 | bool CanDevirtualizeMemberFunctionCall(const Expr *Base, |
| 1356 | const CXXMethodDecl *MD); |
| 1357 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1358 | /// EnterDtorCleanups - Enter the cleanups necessary to complete the |
| 1359 | /// given phase of destruction for a destructor. The end result |
| 1360 | /// should call destructors on members and base classes in reverse |
| 1361 | /// order of their construction. |
| 1362 | void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 1364 | /// ShouldInstrumentFunction - Return true if the current function should be |
| 1365 | /// instrumented with __cyg_profile_func_* calls |
| 1366 | bool ShouldInstrumentFunction(); |
| 1367 | |
| 1368 | /// EmitFunctionInstrumentation - Emit LLVM code to call the specified |
| 1369 | /// instrumentation function with the current function and the call site, if |
| 1370 | /// function instrumentation is enabled. |
| 1371 | void EmitFunctionInstrumentation(const char *Fn); |
| 1372 | |
Roman Divacky | be4c870 | 2011-02-10 16:52:03 +0000 | [diff] [blame] | 1373 | /// EmitMCountInstrumentation - Emit call to .mcount. |
| 1374 | void EmitMCountInstrumentation(); |
| 1375 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1376 | /// EmitFunctionProlog - Emit the target specific LLVM code to load the |
| 1377 | /// arguments for the given function. This is also responsible for naming the |
| 1378 | /// LLVM function arguments. |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1379 | void EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1380 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1381 | const FunctionArgList &Args); |
| 1382 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1383 | /// EmitFunctionEpilog - Emit the target specific LLVM code to return the |
| 1384 | /// given temporary. |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1385 | void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc, |
| 1386 | SourceLocation EndLoc); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1387 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 1388 | /// EmitStartEHSpec - Emit the start of the exception spec. |
| 1389 | void EmitStartEHSpec(const Decl *D); |
| 1390 | |
| 1391 | /// EmitEndEHSpec - Emit the end of the exception spec. |
| 1392 | void EmitEndEHSpec(const Decl *D); |
| 1393 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1394 | /// getTerminateLandingPad - Return a landing pad that just calls terminate. |
| 1395 | llvm::BasicBlock *getTerminateLandingPad(); |
| 1396 | |
| 1397 | /// getTerminateHandler - Return a handler (not a landing pad, just |
| 1398 | /// a catch handler) that just calls terminate. This is used when |
| 1399 | /// a terminate scope encloses a try. |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1400 | llvm::BasicBlock *getTerminateHandler(); |
| 1401 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1402 | llvm::Type *ConvertTypeForMem(QualType T); |
| 1403 | llvm::Type *ConvertType(QualType T); |
| 1404 | llvm::Type *ConvertType(const TypeDecl *T) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 1405 | return ConvertType(getContext().getTypeDeclType(T)); |
| 1406 | } |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 1407 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1408 | /// LoadObjCSelf - Load the value of self. This function is only valid while |
| 1409 | /// generating code for an Objective-C method. |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 1410 | llvm::Value *LoadObjCSelf(); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1411 | |
| 1412 | /// TypeOfSelfObject - Return type of object that this self represents. |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1413 | QualType TypeOfSelfObject(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1414 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1415 | /// hasAggregateLLVMType - Return true if the specified AST type will map into |
| 1416 | /// an aggregate LLVM type or is void. |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1417 | static TypeEvaluationKind getEvaluationKind(QualType T); |
| 1418 | |
| 1419 | static bool hasScalarEvaluationKind(QualType T) { |
| 1420 | return getEvaluationKind(T) == TEK_Scalar; |
| 1421 | } |
| 1422 | |
| 1423 | static bool hasAggregateEvaluationKind(QualType T) { |
| 1424 | return getEvaluationKind(T) == TEK_Aggregate; |
| 1425 | } |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1426 | |
| 1427 | /// createBasicBlock - Create an LLVM basic block. |
Richard Smith | 4def70d | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1428 | llvm::BasicBlock *createBasicBlock(const Twine &name = "", |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1429 | llvm::Function *parent = nullptr, |
| 1430 | llvm::BasicBlock *before = nullptr) { |
Daniel Dunbar | 29ea672 | 2008-11-12 00:01:12 +0000 | [diff] [blame] | 1431 | #ifdef NDEBUG |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1432 | return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before); |
Daniel Dunbar | 29ea672 | 2008-11-12 00:01:12 +0000 | [diff] [blame] | 1433 | #else |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1434 | return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before); |
Daniel Dunbar | 29ea672 | 2008-11-12 00:01:12 +0000 | [diff] [blame] | 1435 | #endif |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1436 | } |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1437 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1438 | /// getBasicBlockForLabel - Return the LLVM basicblock that the specified |
| 1439 | /// label maps to. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1440 | JumpDest getJumpDestForLabel(const LabelDecl *S); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1441 | |
Mike Stump | f71d232 | 2009-11-30 20:08:49 +0000 | [diff] [blame] | 1442 | /// SimplifyForwardingBlocks - If the given basic block is only a branch to |
| 1443 | /// another basic block, simplify it. This assumes that no other code could |
| 1444 | /// potentially reference the basic block. |
Daniel Dunbar | aa5bd87 | 2009-04-01 04:37:47 +0000 | [diff] [blame] | 1445 | void SimplifyForwardingBlocks(llvm::BasicBlock *BB); |
| 1446 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1447 | /// EmitBlock - Emit the given block \arg BB and set it as the insert point, |
| 1448 | /// adding a fall-through branch from the current insert block if |
| 1449 | /// necessary. It is legal to call this function even if there is no current |
| 1450 | /// insertion point. |
Daniel Dunbar | a0c21a8 | 2008-11-13 01:24:05 +0000 | [diff] [blame] | 1451 | /// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1452 | /// IsFinished - If true, indicates that the caller has finished emitting |
| 1453 | /// branches to the given block and does not expect to emit code into it. This |
| 1454 | /// means the block can be ignored if it is unreachable. |
Daniel Dunbar | a0c21a8 | 2008-11-13 01:24:05 +0000 | [diff] [blame] | 1455 | void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false); |
Daniel Dunbar | 824e3bd | 2008-11-11 04:34:23 +0000 | [diff] [blame] | 1456 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 1457 | /// EmitBlockAfterUses - Emit the given block somewhere hopefully |
| 1458 | /// near its uses, and leave the insertion point in it. |
| 1459 | void EmitBlockAfterUses(llvm::BasicBlock *BB); |
| 1460 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1461 | /// EmitBranch - Emit a branch to the specified basic block from the current |
| 1462 | /// insert block, taking care to avoid creation of branches from dummy |
| 1463 | /// blocks. It is legal to call this function even if there is no current |
| 1464 | /// insertion point. |
Daniel Dunbar | 5e08ad3 | 2008-11-11 22:06:59 +0000 | [diff] [blame] | 1465 | /// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1466 | /// This function clears the current insertion point. The caller should follow |
| 1467 | /// calls to this function with calls to Emit*Block prior to generation new |
| 1468 | /// code. |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1469 | void EmitBranch(llvm::BasicBlock *Block); |
| 1470 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1471 | /// HaveInsertPoint - True if an insertion point is defined. If not, this |
| 1472 | /// indicates that the current code being emitted is unreachable. |
| 1473 | bool HaveInsertPoint() const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1474 | return Builder.GetInsertBlock() != nullptr; |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1477 | /// EnsureInsertPoint - Ensure that an insertion point is defined so that |
| 1478 | /// emitted IR has a place to go. Note that by definition, if this function |
| 1479 | /// creates a block then that block is unreachable; callers may do better to |
| 1480 | /// detect when no insertion point is defined and simply skip IR generation. |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1481 | void EnsureInsertPoint() { |
| 1482 | if (!HaveInsertPoint()) |
| 1483 | EmitBlock(createBasicBlock()); |
| 1484 | } |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1485 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 1486 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 1487 | /// specified stmt yet. |
David Blaikie | 0a1c862 | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 1488 | void ErrorUnsupported(const Stmt *S, const char *Type); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1489 | |
| 1490 | //===--------------------------------------------------------------------===// |
| 1491 | // Helpers |
| 1492 | //===--------------------------------------------------------------------===// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1493 | |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1494 | LValue MakeAddrLValue(llvm::Value *V, QualType T, |
| 1495 | CharUnits Alignment = CharUnits()) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1496 | return LValue::MakeAddr(V, T, Alignment, getContext(), |
| 1497 | CGM.getTBAAInfo(T)); |
Daniel Dunbar | 5cf8bfe | 2010-08-21 02:53:44 +0000 | [diff] [blame] | 1498 | } |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 1499 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1500 | LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T); |
Daniel Dunbar | 5cf8bfe | 2010-08-21 02:53:44 +0000 | [diff] [blame] | 1501 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1502 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1503 | /// block. The caller is responsible for setting an appropriate alignment on |
| 1504 | /// the alloca. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1505 | llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty, |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 1506 | const Twine &Name = "tmp"); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1507 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 1508 | /// InitTempAlloca - Provide an initial value for the given alloca. |
| 1509 | void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value); |
| 1510 | |
Daniel Dunbar | 9bd4da2 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 1511 | /// CreateIRTemp - Create a temporary IR object of the given type, with |
| 1512 | /// appropriate alignment. This routine should only be used when an temporary |
| 1513 | /// value needs to be stored into an alloca (for example, to avoid explicit |
| 1514 | /// PHI construction), but the type is the IR type, not the type appropriate |
| 1515 | /// for storing in memory. |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 1516 | llvm::AllocaInst *CreateIRTemp(QualType T, const Twine &Name = "tmp"); |
Daniel Dunbar | 9bd4da2 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 1517 | |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1518 | /// CreateMemTemp - Create a temporary memory object of the given type, with |
| 1519 | /// appropriate alignment. |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 1520 | llvm::AllocaInst *CreateMemTemp(QualType T, const Twine &Name = "tmp"); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1521 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1522 | /// CreateAggTemp - Create a temporary memory object for the given |
| 1523 | /// aggregate type. |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 1524 | AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") { |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 1525 | CharUnits Alignment = getContext().getTypeAlignInChars(T); |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 1526 | return AggValueSlot::forAddr(CreateMemTemp(T, Name), Alignment, |
| 1527 | T.getQualifiers(), |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 1528 | AggValueSlot::IsNotDestructed, |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 1529 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1530 | AggValueSlot::IsNotAliased); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1533 | /// CreateInAllocaTmp - Create a temporary memory object for the given |
| 1534 | /// aggregate type. |
| 1535 | AggValueSlot CreateInAllocaTmp(QualType T, const Twine &Name = "inalloca"); |
| 1536 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1537 | /// Emit a cast to void* in the appropriate address space. |
| 1538 | llvm::Value *EmitCastToVoidPtr(llvm::Value *value); |
| 1539 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1540 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 1541 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 1542 | llvm::Value *EvaluateExprAsBool(const Expr *E); |
| 1543 | |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1544 | /// EmitIgnoredExpr - Emit an expression in a context which ignores the result. |
| 1545 | void EmitIgnoredExpr(const Expr *E); |
| 1546 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1547 | /// EmitAnyExpr - Emit code to compute the specified expression which can have |
| 1548 | /// any type. The result is returned as an RValue struct. If this is an |
| 1549 | /// aggregate expression, the aggloc/agglocvolatile arguments indicate where |
| 1550 | /// the result should be returned. |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 1551 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1552 | /// \param ignoreResult True if the resulting value isn't used. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1553 | RValue EmitAnyExpr(const Expr *E, |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 1554 | AggValueSlot aggSlot = AggValueSlot::ignored(), |
| 1555 | bool ignoreResult = false); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 1556 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1557 | // EmitVAListRef - Emit a "reference" to a va_list; this is either the address |
| 1558 | // or the value of the expression, depending on how va_list is defined. |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 1559 | llvm::Value *EmitVAListRef(const Expr *E); |
| 1560 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1561 | /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will |
| 1562 | /// always be accessible even if no aggregate location is provided. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1563 | RValue EmitAnyExprToTemp(const Expr *E); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1564 | |
John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1565 | /// EmitAnyExprToMem - Emits the code necessary to evaluate an |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1566 | /// arbitrary expression into the given memory location. |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1567 | void EmitAnyExprToMem(const Expr *E, llvm::Value *Location, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1568 | Qualifiers Quals, bool IsInitializer); |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1569 | |
John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1570 | /// EmitExprAsInit - Emits the code necessary to initialize a |
| 1571 | /// location in memory with the given initializer. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1572 | void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, |
| 1573 | bool capturedByInit); |
John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1574 | |
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 1575 | /// hasVolatileMember - returns true if aggregate type has a volatile |
| 1576 | /// member. |
| 1577 | bool hasVolatileMember(QualType T) { |
| 1578 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1579 | const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); |
| 1580 | return RD->hasVolatileMember(); |
| 1581 | } |
| 1582 | return false; |
| 1583 | } |
Arnaud A. de Grandmaison | 8fcbb8d | 2013-02-05 09:06:17 +0000 | [diff] [blame] | 1584 | /// EmitAggregateCopy - Emit an aggregate assignment. |
Benjamin Kramer | 6cacae8 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 1585 | /// |
| 1586 | /// The difference to EmitAggregateCopy is that tail padding is not copied. |
| 1587 | /// This is required for correctness when assigning non-POD structures in C++. |
| 1588 | void EmitAggregateAssign(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 1589 | QualType EltTy) { |
| 1590 | bool IsVolatile = hasVolatileMember(EltTy); |
| 1591 | EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, CharUnits::Zero(), |
| 1592 | true); |
Benjamin Kramer | 6cacae8 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1595 | void EmitAggregateCopyCtor(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
| 1596 | QualType DestTy, QualType SrcTy) { |
| 1597 | CharUnits DestTypeAlign = getContext().getTypeAlignInChars(DestTy); |
| 1598 | CharUnits SrcTypeAlign = getContext().getTypeAlignInChars(SrcTy); |
| 1599 | EmitAggregateCopy(DestPtr, SrcPtr, SrcTy, /*IsVolatile=*/false, |
| 1600 | std::min(DestTypeAlign, SrcTypeAlign), |
| 1601 | /*IsAssignment=*/false); |
| 1602 | } |
| 1603 | |
Arnaud A. de Grandmaison | 8fcbb8d | 2013-02-05 09:06:17 +0000 | [diff] [blame] | 1604 | /// EmitAggregateCopy - Emit an aggregate copy. |
Mike Stump | 27fe2e6 | 2009-05-23 22:29:41 +0000 | [diff] [blame] | 1605 | /// |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1606 | /// \param isVolatile - True iff either the source or the destination is |
Mike Stump | 27fe2e6 | 2009-05-23 22:29:41 +0000 | [diff] [blame] | 1607 | /// volatile. |
Benjamin Kramer | 6cacae8 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 1608 | /// \param isAssignment - If false, allow padding to be copied. This often |
| 1609 | /// yields more efficient. |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 1610 | void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
Eli Friedman | bd7d828 | 2011-12-05 22:23:28 +0000 | [diff] [blame] | 1611 | QualType EltTy, bool isVolatile=false, |
Benjamin Kramer | 6cacae8 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 1612 | CharUnits Alignment = CharUnits::Zero(), |
| 1613 | bool isAssignment = false); |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 1614 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1615 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 1616 | /// then reuse it. |
| 1617 | void StartBlock(const char *N); |
| 1618 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1619 | /// GetAddrOfLocalVar - Return the address of a local variable. |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1620 | llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) { |
| 1621 | llvm::Value *Res = LocalDeclMap[VD]; |
| 1622 | assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); |
| 1623 | return Res; |
| 1624 | } |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1625 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1626 | /// getOpaqueLValueMapping - Given an opaque value expression (which |
| 1627 | /// must be mapped to an l-value), return its mapping. |
| 1628 | const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) { |
| 1629 | assert(OpaqueValueMapping::shouldBindAsLValue(e)); |
| 1630 | |
| 1631 | llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator |
| 1632 | it = OpaqueLValues.find(e); |
| 1633 | assert(it != OpaqueLValues.end() && "no mapping for opaque value!"); |
| 1634 | return it->second; |
| 1635 | } |
| 1636 | |
| 1637 | /// getOpaqueRValueMapping - Given an opaque value expression (which |
| 1638 | /// must be mapped to an r-value), return its mapping. |
| 1639 | const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) { |
| 1640 | assert(!OpaqueValueMapping::shouldBindAsLValue(e)); |
| 1641 | |
| 1642 | llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator |
| 1643 | it = OpaqueRValues.find(e); |
| 1644 | assert(it != OpaqueRValues.end() && "no mapping for opaque value!"); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 1645 | return it->second; |
| 1646 | } |
| 1647 | |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 1648 | /// getAccessedFieldNo - Given an encoded value and a result number, return |
| 1649 | /// the input field number being accessed. |
| 1650 | static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts); |
| 1651 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1652 | llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 1653 | llvm::BasicBlock *GetIndirectGotoBlock(); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 1654 | |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 1655 | /// EmitNullInitialization - Generate code to set a value of the given type to |
| 1656 | /// null, If the type contains data member pointers, they will be initialized |
| 1657 | /// to -1 in accordance with the Itanium C++ ABI. |
| 1658 | void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty); |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1659 | |
| 1660 | // EmitVAArg - Generate code to get an argument from the passed in pointer |
| 1661 | // and update it accordingly. The return value is a pointer to the argument. |
| 1662 | // FIXME: We should be able to get rid of this method and use the va_arg |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1663 | // instruction in LLVM instead once it works well enough. |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1664 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty); |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 1665 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1666 | /// emitArrayLength - Compute the length of an array, even if it's a |
| 1667 | /// VLA, and drill down to the base element type. |
| 1668 | llvm::Value *emitArrayLength(const ArrayType *arrayType, |
| 1669 | QualType &baseType, |
| 1670 | llvm::Value *&addr); |
| 1671 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1672 | /// EmitVLASize - Capture all the sizes for the VLA expressions in |
| 1673 | /// the given variably-modified type and store them in the VLASizeMap. |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1674 | /// |
| 1675 | /// This function can be called with a null (unreachable) insert point. |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1676 | void EmitVariablyModifiedType(QualType Ty); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1677 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1678 | /// getVLASize - Returns an LLVM value that corresponds to the size, |
| 1679 | /// in non-variably-sized elements, of a variable length array type, |
| 1680 | /// plus that largest non-variably-sized element type. Assumes that |
| 1681 | /// the type has already been emitted with EmitVariablyModifiedType. |
| 1682 | std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla); |
| 1683 | std::pair<llvm::Value*,QualType> getVLASize(QualType vla); |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 1684 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 1685 | /// LoadCXXThis - Load the value of 'this'. This function is only valid while |
| 1686 | /// generating code for an C++ member function. |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 1687 | llvm::Value *LoadCXXThis() { |
| 1688 | assert(CXXThisValue && "no 'this' value for this function"); |
| 1689 | return CXXThisValue; |
| 1690 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1692 | /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have |
| 1693 | /// virtual bases. |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1694 | // FIXME: Every place that calls LoadCXXVTT is something |
| 1695 | // that needs to be abstracted properly. |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 1696 | llvm::Value *LoadCXXVTT() { |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1697 | assert(CXXStructorImplicitParamValue && "no VTT value for this function"); |
| 1698 | return CXXStructorImplicitParamValue; |
| 1699 | } |
| 1700 | |
| 1701 | /// LoadCXXStructorImplicitParam - Load the implicit parameter |
| 1702 | /// for a constructor/destructor. |
| 1703 | llvm::Value *LoadCXXStructorImplicitParam() { |
| 1704 | assert(CXXStructorImplicitParamValue && |
| 1705 | "no implicit argument value for this function"); |
| 1706 | return CXXStructorImplicitParamValue; |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 1707 | } |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 1708 | |
| 1709 | /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 1710 | /// complete class to the given direct base. |
| 1711 | llvm::Value * |
| 1712 | GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value, |
| 1713 | const CXXRecordDecl *Derived, |
| 1714 | const CXXRecordDecl *Base, |
| 1715 | bool BaseIsVirtual); |
Anders Carlsson | a88ad56 | 2010-04-24 21:51:08 +0000 | [diff] [blame] | 1716 | |
Mike Stump | f71d232 | 2009-11-30 20:08:49 +0000 | [diff] [blame] | 1717 | /// GetAddressOfBaseClass - This function will add the necessary delta to the |
| 1718 | /// load of 'this' and returns address of the base class. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1719 | llvm::Value *GetAddressOfBaseClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 1720 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1721 | CastExpr::path_const_iterator PathBegin, |
| 1722 | CastExpr::path_const_iterator PathEnd, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1723 | bool NullCheckValue, SourceLocation Loc); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 1724 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1725 | llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 1726 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1727 | CastExpr::path_const_iterator PathBegin, |
| 1728 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1729 | bool NullCheckValue); |
| 1730 | |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1731 | /// GetVTTParameter - Return the VTT parameter that should be passed to a |
| 1732 | /// base constructor/destructor with virtual bases. |
| 1733 | /// FIXME: VTTs are Itanium ABI-specific, so the definition should move |
| 1734 | /// to ItaniumCXXABI.cpp together with all the references to VTT. |
| 1735 | llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase, |
| 1736 | bool Delegating); |
| 1737 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1738 | void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1739 | CXXCtorType CtorType, |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1740 | const FunctionArgList &Args, |
| 1741 | SourceLocation Loc); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1742 | // It's important not to confuse this and the previous function. Delegating |
| 1743 | // constructors are the C++0x feature. The constructor delegate optimization |
| 1744 | // is used to reduce duplication in the base and complete consturctors where |
| 1745 | // they are substantially the same. |
| 1746 | void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1747 | const FunctionArgList &Args); |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 1748 | void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1749 | bool ForVirtualBase, bool Delegating, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1750 | llvm::Value *This, const CXXConstructExpr *E); |
| 1751 | |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1752 | void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, |
| 1753 | llvm::Value *This, llvm::Value *Src, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1754 | const CXXConstructExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1756 | void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 1757 | const ConstantArrayType *ArrayTy, |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 1758 | llvm::Value *ArrayPtr, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1759 | const CXXConstructExpr *E, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1760 | bool ZeroInitialization = false); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1761 | |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 1762 | void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1763 | llvm::Value *NumElements, |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 1764 | llvm::Value *ArrayPtr, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1765 | const CXXConstructExpr *E, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1766 | bool ZeroInitialization = false); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 1767 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1768 | static Destroyer destroyCXXObject; |
| 1769 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 1770 | void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1771 | bool ForVirtualBase, bool Delegating, |
| 1772 | llvm::Value *This); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1773 | |
John McCall | 1970567 | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1774 | void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1775 | llvm::Value *NewPtr, llvm::Value *NumElements, |
| 1776 | llvm::Value *AllocSizeWithoutCookie); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Peter Collingbourne | 8681160 | 2011-11-27 22:09:22 +0000 | [diff] [blame] | 1778 | void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType, |
| 1779 | llvm::Value *Ptr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | |
Anders Carlsson | a00703d | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 1781 | llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E); |
Anders Carlsson | 60e282c | 2009-08-16 21:13:42 +0000 | [diff] [blame] | 1782 | void EmitCXXDeleteExpr(const CXXDeleteExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | |
Eli Friedman | 4bf8152 | 2009-11-18 00:57:03 +0000 | [diff] [blame] | 1784 | void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, |
| 1785 | QualType DeleteTy); |
| 1786 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1787 | RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, |
| 1788 | const Expr *Arg, bool IsDelete); |
| 1789 | |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1790 | llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1791 | llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE); |
Nico Weber | c5f8046 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1792 | llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1793 | |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 1794 | /// \brief Situations in which we might emit a check for the suitability of a |
| 1795 | /// pointer or glvalue. |
Richard Smith | 7ac9ef1 | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 1796 | enum TypeCheckKind { |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 1797 | /// Checking the operand of a load. Must be suitably sized and aligned. |
Richard Smith | 7ac9ef1 | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 1798 | TCK_Load, |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 1799 | /// Checking the destination of a store. Must be suitably sized and aligned. |
Richard Smith | 7ac9ef1 | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 1800 | TCK_Store, |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 1801 | /// Checking the bound value in a reference binding. Must be suitably sized |
| 1802 | /// and aligned, but is not required to refer to an object (until the |
| 1803 | /// reference is used), per core issue 453. |
Richard Smith | 7ac9ef1 | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 1804 | TCK_ReferenceBinding, |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 1805 | /// Checking the object expression in a non-static data member access. Must |
| 1806 | /// be an object within its lifetime. |
Richard Smith | 7ac9ef1 | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 1807 | TCK_MemberAccess, |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 1808 | /// Checking the 'this' pointer for a call to a non-static member function. |
| 1809 | /// Must be an object within its lifetime. |
Richard Smith | 8e1cee6 | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 1810 | TCK_MemberCall, |
| 1811 | /// Checking the 'this' pointer for a constructor call. |
Richard Smith | c764830 | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 1812 | TCK_ConstructorCall, |
| 1813 | /// Checking the operand of a static_cast to a derived pointer type. Must be |
| 1814 | /// null or an object within its lifetime. |
| 1815 | TCK_DowncastPointer, |
| 1816 | /// Checking the operand of a static_cast to a derived reference type. Must |
| 1817 | /// be an object within its lifetime. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1818 | TCK_DowncastReference, |
| 1819 | /// Checking the operand of a cast to a base object. Must be suitably sized |
| 1820 | /// and aligned. |
| 1821 | TCK_Upcast, |
| 1822 | /// Checking the operand of a cast to a virtual base object. Must be an |
| 1823 | /// object within its lifetime. |
| 1824 | TCK_UpcastToVirtualBase |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 1825 | }; |
| 1826 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1827 | /// \brief Whether any type-checking sanitizers are enabled. If \c false, |
| 1828 | /// calls to EmitTypeCheck can be skipped. |
| 1829 | bool sanitizePerformTypeCheck() const; |
| 1830 | |
Richard Smith | 7ac9ef1 | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 1831 | /// \brief Emit a check that \p V is the address of storage of the |
Richard Smith | 2c9f87c | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 1832 | /// appropriate size and alignment for an object of type \p Type. |
Richard Smith | 4def70d | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1833 | void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1834 | QualType Type, CharUnits Alignment = CharUnits::Zero(), |
| 1835 | bool SkipNullCheck = false); |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 1836 | |
Richard Smith | a0a628f | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 1837 | /// \brief Emit a check that \p Base points into an array object, which |
| 1838 | /// we can access at index \p Index. \p Accessed should be \c false if we |
| 1839 | /// this expression is used as an lvalue, for instance in "&Arr[Idx]". |
| 1840 | void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index, |
| 1841 | QualType IndexType, bool Accessed); |
| 1842 | |
Chris Lattner | dd36d32 | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 1843 | llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 1844 | bool isInc, bool isPre); |
| 1845 | ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 1846 | bool isInc, bool isPre); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1847 | |
| 1848 | void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment, |
| 1849 | llvm::Value *OffsetValue = nullptr) { |
| 1850 | Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment, |
| 1851 | OffsetValue); |
| 1852 | } |
| 1853 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1854 | //===--------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1855 | // Declaration Emission |
| 1856 | //===--------------------------------------------------------------------===// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1857 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1858 | /// EmitDecl - Emit a declaration. |
| 1859 | /// |
| 1860 | /// This function can be called with a null (unreachable) insert point. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1861 | void EmitDecl(const Decl &D); |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1862 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1863 | /// EmitVarDecl - Emit a local variable declaration. |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1864 | /// |
| 1865 | /// This function can be called with a null (unreachable) insert point. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1866 | void EmitVarDecl(const VarDecl &D); |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1867 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1868 | void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, |
| 1869 | bool capturedByInit); |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1870 | void EmitScalarInit(llvm::Value *init, LValue lvalue); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1871 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1872 | typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D, |
| 1873 | llvm::Value *Address); |
| 1874 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1875 | /// \brief Determine whether the given initializer is trivial in the sense |
| 1876 | /// that it requires no code to be generated. |
| 1877 | bool isTrivialInitializer(const Expr *Init); |
| 1878 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1879 | /// EmitAutoVarDecl - Emit an auto variable declaration. |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1880 | /// |
| 1881 | /// This function can be called with a null (unreachable) insert point. |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1882 | void EmitAutoVarDecl(const VarDecl &D); |
| 1883 | |
| 1884 | class AutoVarEmission { |
| 1885 | friend class CodeGenFunction; |
| 1886 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1887 | const VarDecl *Variable; |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1888 | |
| 1889 | /// The alignment of the variable. |
| 1890 | CharUnits Alignment; |
| 1891 | |
| 1892 | /// The address of the alloca. Null if the variable was emitted |
| 1893 | /// as a global constant. |
| 1894 | llvm::Value *Address; |
| 1895 | |
| 1896 | llvm::Value *NRVOFlag; |
| 1897 | |
| 1898 | /// True if the variable is a __block variable. |
| 1899 | bool IsByRef; |
| 1900 | |
| 1901 | /// True if the variable is of aggregate type and has a constant |
| 1902 | /// initializer. |
| 1903 | bool IsConstantAggregate; |
| 1904 | |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 1905 | /// Non-null if we should use lifetime annotations. |
| 1906 | llvm::Value *SizeForLifetimeMarkers; |
| 1907 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1908 | struct Invalid {}; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1909 | AutoVarEmission(Invalid) : Variable(nullptr) {} |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1910 | |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1911 | AutoVarEmission(const VarDecl &variable) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1912 | : Variable(&variable), Address(nullptr), NRVOFlag(nullptr), |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 1913 | IsByRef(false), IsConstantAggregate(false), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1914 | SizeForLifetimeMarkers(nullptr) {} |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1915 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1916 | bool wasEmittedAsGlobal() const { return Address == nullptr; } |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1917 | |
| 1918 | public: |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1919 | static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); } |
| 1920 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1921 | bool useLifetimeMarkers() const { |
| 1922 | return SizeForLifetimeMarkers != nullptr; |
| 1923 | } |
Nadav Rotem | 495cfa4 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 1924 | llvm::Value *getSizeForLifetimeMarkers() const { |
| 1925 | assert(useLifetimeMarkers()); |
| 1926 | return SizeForLifetimeMarkers; |
| 1927 | } |
| 1928 | |
| 1929 | /// Returns the raw, allocated address, which is not necessarily |
| 1930 | /// the address of the object itself. |
| 1931 | llvm::Value *getAllocatedAddress() const { |
| 1932 | return Address; |
| 1933 | } |
| 1934 | |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1935 | /// Returns the address of the object within this declaration. |
| 1936 | /// Note that this does not chase the forwarding pointer for |
| 1937 | /// __block decls. |
| 1938 | llvm::Value *getObjectAddress(CodeGenFunction &CGF) const { |
| 1939 | if (!IsByRef) return Address; |
| 1940 | |
| 1941 | return CGF.Builder.CreateStructGEP(Address, |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1942 | CGF.getByRefValueLLVMField(Variable), |
| 1943 | Variable->getNameAsString()); |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1944 | } |
| 1945 | }; |
| 1946 | AutoVarEmission EmitAutoVarAlloca(const VarDecl &var); |
| 1947 | void EmitAutoVarInit(const AutoVarEmission &emission); |
| 1948 | void EmitAutoVarCleanups(const AutoVarEmission &emission); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1949 | void emitAutoVarTypeCleanup(const AutoVarEmission &emission, |
| 1950 | QualType::DestructionKind dtorKind); |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1951 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1952 | void EmitStaticVarDecl(const VarDecl &D, |
| 1953 | llvm::GlobalValue::LinkageTypes Linkage); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1954 | |
| 1955 | /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1956 | void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, bool ArgIsPointer, |
| 1957 | unsigned ArgNo); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1958 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1959 | /// protectFromPeepholes - Protect a value that we're intending to |
| 1960 | /// store to the side, but which will probably be used later, from |
| 1961 | /// aggressive peepholing optimizations that might delete it. |
| 1962 | /// |
| 1963 | /// Pass the result to unprotectFromPeepholes to declare that |
| 1964 | /// protection is no longer required. |
| 1965 | /// |
| 1966 | /// There's no particular reason why this shouldn't apply to |
| 1967 | /// l-values, it's just that no existing peepholes work on pointers. |
| 1968 | PeepholeProtection protectFromPeepholes(RValue rvalue); |
| 1969 | void unprotectFromPeepholes(PeepholeProtection protection); |
| 1970 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1971 | //===--------------------------------------------------------------------===// |
| 1972 | // Statement Emission |
| 1973 | //===--------------------------------------------------------------------===// |
| 1974 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1975 | /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info. |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1976 | void EmitStopPoint(const Stmt *S); |
| 1977 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1978 | /// EmitStmt - Emit the code for the statement \arg S. It is legal to call |
| 1979 | /// this function even if there is no current insertion point. |
| 1980 | /// |
| 1981 | /// This function may clear the current insertion point; callers should use |
| 1982 | /// EnsureInsertPoint if they wish to subsequently generate code without first |
| 1983 | /// calling EmitBlock, EmitBranch, or EmitStmt. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1984 | void EmitStmt(const Stmt *S); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1985 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1986 | /// EmitSimpleStmt - Try to emit a "simple" statement which does not |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1987 | /// necessarily require an insertion point or debug information; typically |
| 1988 | /// because the statement amounts to a jump or a container of other |
| 1989 | /// statements. |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1990 | /// |
| 1991 | /// \return True if the statement was handled. |
| 1992 | bool EmitSimpleStmt(const Stmt *S); |
| 1993 | |
Eli Friedman | 2ac2fa7 | 2013-06-10 22:04:49 +0000 | [diff] [blame] | 1994 | llvm::Value *EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, |
| 1995 | AggValueSlot AVS = AggValueSlot::ignored()); |
| 1996 | llvm::Value *EmitCompoundStmtWithoutScope(const CompoundStmt &S, |
| 1997 | bool GetLast = false, |
| 1998 | AggValueSlot AVS = |
| 1999 | AggValueSlot::ignored()); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2000 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2001 | /// EmitLabel - Emit the block for the given label. It is legal to call this |
| 2002 | /// function even if there is no current insertion point. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2003 | void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt. |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2004 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2005 | void EmitLabelStmt(const LabelStmt &S); |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 2006 | void EmitAttributedStmt(const AttributedStmt &S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2007 | void EmitGotoStmt(const GotoStmt &S); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 2008 | void EmitIndirectGotoStmt(const IndirectGotoStmt &S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2009 | void EmitIfStmt(const IfStmt &S); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2010 | |
| 2011 | void EmitCondBrHints(llvm::LLVMContext &Context, llvm::BranchInst *CondBr, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2012 | ArrayRef<const Attr *> Attrs); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2013 | void EmitWhileStmt(const WhileStmt &S, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2014 | ArrayRef<const Attr *> Attrs = None); |
| 2015 | void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2016 | void EmitForStmt(const ForStmt &S, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2017 | ArrayRef<const Attr *> Attrs = None); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2018 | void EmitReturnStmt(const ReturnStmt &S); |
| 2019 | void EmitDeclStmt(const DeclStmt &S); |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 2020 | void EmitBreakStmt(const BreakStmt &S); |
| 2021 | void EmitContinueStmt(const ContinueStmt &S); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 2022 | void EmitSwitchStmt(const SwitchStmt &S); |
| 2023 | void EmitDefaultStmt(const DefaultStmt &S); |
| 2024 | void EmitCaseStmt(const CaseStmt &S); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 2025 | void EmitCaseStmtRange(const CaseStmt &S); |
Chad Rosier | a23b91d | 2012-08-28 18:54:39 +0000 | [diff] [blame] | 2026 | void EmitAsmStmt(const AsmStmt &S); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2027 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 2028 | void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2029 | void EmitObjCAtTryStmt(const ObjCAtTryStmt &S); |
| 2030 | void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 2031 | void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2032 | void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2033 | |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 2034 | void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); |
| 2035 | void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 2036 | |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 2037 | void EmitCXXTryStmt(const CXXTryStmt &S); |
Reid Kleckner | 98592d9 | 2013-09-16 21:46:30 +0000 | [diff] [blame] | 2038 | void EmitSEHTryStmt(const SEHTryStmt &S); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2039 | void EmitSEHLeaveStmt(const SEHLeaveStmt &S); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2040 | void EnterSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI); |
| 2041 | void ExitSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI); |
| 2042 | |
| 2043 | llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF, |
| 2044 | const SEHExceptStmt &Except); |
| 2045 | |
| 2046 | void EmitSEHExceptionCodeSave(); |
| 2047 | llvm::Value *EmitSEHExceptionCode(); |
| 2048 | llvm::Value *EmitSEHExceptionInfo(); |
| 2049 | llvm::Value *EmitSEHAbnormalTermination(); |
| 2050 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2051 | void EmitCXXForRangeStmt(const CXXForRangeStmt &S, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2052 | ArrayRef<const Attr *> Attrs = None); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2053 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2054 | LValue InitCapturedStruct(const CapturedStmt &S); |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2055 | llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2056 | void GenerateCapturedStmtFunctionProlog(const CapturedStmt &S); |
| 2057 | llvm::Function *GenerateCapturedStmtFunctionEpilog(const CapturedStmt &S); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2058 | llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2059 | llvm::Value *GenerateCapturedStmtArgument(const CapturedStmt &S); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2060 | void EmitOMPAggregateAssign(LValue OriginalAddr, llvm::Value *PrivateAddr, |
| 2061 | const Expr *AssignExpr, QualType Type, |
| 2062 | const VarDecl *VDInit); |
| 2063 | void EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
| 2064 | OMPPrivateScope &PrivateScope); |
| 2065 | void EmitOMPPrivateClause(const OMPExecutableDirective &D, |
| 2066 | OMPPrivateScope &PrivateScope); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2067 | |
| 2068 | void EmitOMPParallelDirective(const OMPParallelDirective &S); |
| 2069 | void EmitOMPSimdDirective(const OMPSimdDirective &S); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2070 | void EmitOMPForDirective(const OMPForDirective &S); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2071 | void EmitOMPForSimdDirective(const OMPForSimdDirective &S); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2072 | void EmitOMPSectionsDirective(const OMPSectionsDirective &S); |
| 2073 | void EmitOMPSectionDirective(const OMPSectionDirective &S); |
| 2074 | void EmitOMPSingleDirective(const OMPSingleDirective &S); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2075 | void EmitOMPMasterDirective(const OMPMasterDirective &S); |
| 2076 | void EmitOMPCriticalDirective(const OMPCriticalDirective &S); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2077 | void EmitOMPParallelForDirective(const OMPParallelForDirective &S); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2078 | void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2079 | void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2080 | void EmitOMPTaskDirective(const OMPTaskDirective &S); |
| 2081 | void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S); |
| 2082 | void EmitOMPBarrierDirective(const OMPBarrierDirective &S); |
| 2083 | void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S); |
| 2084 | void EmitOMPFlushDirective(const OMPFlushDirective &S); |
| 2085 | void EmitOMPOrderedDirective(const OMPOrderedDirective &S); |
| 2086 | void EmitOMPAtomicDirective(const OMPAtomicDirective &S); |
| 2087 | void EmitOMPTargetDirective(const OMPTargetDirective &S); |
| 2088 | void EmitOMPTeamsDirective(const OMPTeamsDirective &S); |
| 2089 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2090 | private: |
| 2091 | |
| 2092 | /// Helpers for the OpenMP loop directives. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2093 | void EmitOMPLoopBody(const OMPLoopDirective &Directive, |
| 2094 | bool SeparateIter = false); |
| 2095 | void EmitOMPInnerLoop(const OMPLoopDirective &S, OMPPrivateScope &LoopScope, |
| 2096 | bool SeparateIter = false); |
| 2097 | void EmitOMPSimdFinal(const OMPLoopDirective &S); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2098 | void EmitOMPWorksharingLoop(const OMPLoopDirective &S); |
| 2099 | void EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind, |
| 2100 | const OMPLoopDirective &S, |
| 2101 | OMPPrivateScope &LoopScope, llvm::Value *LB, |
| 2102 | llvm::Value *UB, llvm::Value *ST, llvm::Value *IL, |
| 2103 | llvm::Value *Chunk); |
| 2104 | |
| 2105 | public: |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2107 | //===--------------------------------------------------------------------===// |
| 2108 | // LValue Expression Emission |
| 2109 | //===--------------------------------------------------------------------===// |
| 2110 | |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 2111 | /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type. |
| 2112 | RValue GetUndefRValue(QualType Ty); |
| 2113 | |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 2114 | /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E |
| 2115 | /// and issue an ErrorUnsupported style diagnostic (using the |
| 2116 | /// provided Name). |
| 2117 | RValue EmitUnsupportedRValue(const Expr *E, |
| 2118 | const char *Name); |
| 2119 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2120 | /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue |
| 2121 | /// an ErrorUnsupported style diagnostic (using the provided Name). |
Daniel Dunbar | 6ba82a4 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 2122 | LValue EmitUnsupportedLValue(const Expr *E, |
| 2123 | const char *Name); |
| 2124 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2125 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 2126 | /// of the expression. |
| 2127 | /// |
| 2128 | /// This can return one of two things: a simple address or a bitfield |
| 2129 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 2130 | /// guaranteed to be an LLVM pointer type. |
| 2131 | /// |
| 2132 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 2133 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 2134 | /// integer. |
| 2135 | /// |
| 2136 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 2137 | /// size, this method guarantees that the returned pointer type will point to |
| 2138 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 2139 | /// variable length type, this is not possible. |
| 2140 | /// |
| 2141 | LValue EmitLValue(const Expr *E); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2142 | |
Richard Smith | 7ac9ef1 | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 2143 | /// \brief Same as EmitLValue but additionally we generate checking code to |
| 2144 | /// guard against undefined behavior. This is only suitable when we know |
| 2145 | /// that the address will be used to access the object. |
| 2146 | LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK); |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 2147 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2148 | RValue convertTempToRValue(llvm::Value *addr, QualType type, |
| 2149 | SourceLocation Loc); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2150 | |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2151 | void EmitAtomicInit(Expr *E, LValue lvalue); |
| 2152 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2153 | bool LValueIsSuitableForInlineAtomic(LValue Src); |
| 2154 | bool typeIsSuitableForInlineAtomic(QualType Ty, bool IsVolatile) const; |
| 2155 | |
| 2156 | RValue EmitAtomicLoad(LValue LV, SourceLocation SL, |
| 2157 | AggValueSlot Slot = AggValueSlot::ignored()); |
| 2158 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2159 | RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2160 | llvm::AtomicOrdering AO, bool IsVolatile = false, |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2161 | AggValueSlot slot = AggValueSlot::ignored()); |
| 2162 | |
| 2163 | void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit); |
| 2164 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2165 | void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO, |
| 2166 | bool IsVolatile, bool isInit); |
| 2167 | |
| 2168 | std::pair<RValue, RValue> EmitAtomicCompareExchange( |
| 2169 | LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc, |
| 2170 | llvm::AtomicOrdering Success = llvm::SequentiallyConsistent, |
| 2171 | llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent, |
| 2172 | bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored()); |
| 2173 | |
John McCall | 26815d9 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 2174 | /// EmitToMemory - Change a scalar value from its value |
| 2175 | /// representation to its in-memory representation. |
| 2176 | llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty); |
| 2177 | |
| 2178 | /// EmitFromMemory - Change a scalar value from its memory |
| 2179 | /// representation to its value representation. |
| 2180 | llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty); |
| 2181 | |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 2182 | /// EmitLoadOfScalar - Load a scalar value from an address, taking |
| 2183 | /// care to appropriately convert from the memory representation to |
| 2184 | /// the LLVM value representation. |
Mike Stump | 09429b9 | 2009-02-17 17:00:02 +0000 | [diff] [blame] | 2185 | llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 2186 | unsigned Alignment, QualType Ty, |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2187 | SourceLocation Loc, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2188 | llvm::MDNode *TBAAInfo = nullptr, |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 2189 | QualType TBAABaseTy = QualType(), |
| 2190 | uint64_t TBAAOffset = 0); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2191 | |
| 2192 | /// EmitLoadOfScalar - Load a scalar value from an address, taking |
| 2193 | /// care to appropriately convert from the memory representation to |
| 2194 | /// the LLVM value representation. The l-value must be a simple |
| 2195 | /// l-value. |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2196 | llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc); |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 2197 | |
| 2198 | /// EmitStoreOfScalar - Store a scalar value to an address, taking |
| 2199 | /// care to appropriately convert from the memory representation to |
| 2200 | /// the LLVM value representation. |
Mike Stump | 09429b9 | 2009-02-17 17:00:02 +0000 | [diff] [blame] | 2201 | void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 2202 | bool Volatile, unsigned Alignment, QualType Ty, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2203 | llvm::MDNode *TBAAInfo = nullptr, bool isInit = false, |
Manman Ren | b37a73d | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 2204 | QualType TBAABaseTy = QualType(), |
| 2205 | uint64_t TBAAOffset = 0); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2206 | |
| 2207 | /// EmitStoreOfScalar - Store a scalar value to an address, taking |
| 2208 | /// care to appropriately convert from the memory representation to |
| 2209 | /// the LLVM value representation. The l-value must be a simple |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 2210 | /// l-value. The isInit flag indicates whether this is an initialization. |
| 2211 | /// If so, atomic qualifiers are ignored and the store is always non-atomic. |
| 2212 | void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false); |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 2213 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2214 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 2215 | /// this method emits the address of the lvalue, then loads the result as an |
| 2216 | /// rvalue, returning the rvalue. |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2217 | RValue EmitLoadOfLValue(LValue V, SourceLocation Loc); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2218 | RValue EmitLoadOfExtVectorElementLValue(LValue V); |
| 2219 | RValue EmitLoadOfBitfieldLValue(LValue LV); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2220 | RValue EmitLoadOfGlobalRegLValue(LValue LV); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2221 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2222 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 2223 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 2224 | /// is 'Ty'. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2225 | void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2226 | void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2227 | void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst); |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 2228 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2229 | /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints |
| 2230 | /// as EmitStoreThroughLValue. |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 2231 | /// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2232 | /// \param Result [out] - If non-null, this will be set to a Value* for the |
| 2233 | /// bit-field contents after the store, appropriate for use as the result of |
| 2234 | /// an assignment to the bit-field. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2235 | void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2236 | llvm::Value **Result=nullptr); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2237 | |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 2238 | /// Emit an l-value for an assignment (simple or compound) of complex type. |
| 2239 | LValue EmitComplexAssignmentLValue(const BinaryOperator *E); |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2240 | LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2241 | LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E, |
| 2242 | llvm::Value *&Result); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 2243 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2244 | // Note: only available for agg return types |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 2245 | LValue EmitBinaryOperatorLValue(const BinaryOperator *E); |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2246 | LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E); |
Daniel Dunbar | 5b5c9ef | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 2247 | // Note: only available for agg return types |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 2248 | LValue EmitCallExprLValue(const CallExpr *E); |
Daniel Dunbar | 5b5c9ef | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 2249 | // Note: only available for agg return types |
| 2250 | LValue EmitVAArgExprLValue(const VAArgExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2251 | LValue EmitDeclRefLValue(const DeclRefExpr *E); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2252 | LValue EmitReadRegister(const VarDecl *VD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2253 | LValue EmitStringLiteralLValue(const StringLiteral *E); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2254 | LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2255 | LValue EmitPredefinedLValue(const PredefinedExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2256 | LValue EmitUnaryOpLValue(const UnaryOperator *E); |
Richard Smith | a0a628f | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 2257 | LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E, |
| 2258 | bool Accessed = false); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2259 | LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 2260 | LValue EmitMemberExpr(const MemberExpr *E); |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 2261 | LValue EmitObjCIsaExpr(const ObjCIsaExpr *E); |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 2262 | LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E); |
Richard Smith | 13ec910 | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 2263 | LValue EmitInitListLValue(const InitListExpr *E); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2264 | LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E); |
Chris Lattner | 75dfeda | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 2265 | LValue EmitCastLValue(const CastExpr *E); |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2266 | LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 2267 | LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2268 | |
| 2269 | llvm::Value *EmitExtVectorElementLValue(LValue V); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2270 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2271 | RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2272 | |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 2273 | class ConstantEmission { |
| 2274 | llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference; |
| 2275 | ConstantEmission(llvm::Constant *C, bool isReference) |
| 2276 | : ValueAndIsReference(C, isReference) {} |
| 2277 | public: |
| 2278 | ConstantEmission() {} |
| 2279 | static ConstantEmission forReference(llvm::Constant *C) { |
| 2280 | return ConstantEmission(C, true); |
| 2281 | } |
| 2282 | static ConstantEmission forValue(llvm::Constant *C) { |
| 2283 | return ConstantEmission(C, false); |
| 2284 | } |
| 2285 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2286 | explicit operator bool() const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2287 | return ValueAndIsReference.getOpaqueValue() != nullptr; |
| 2288 | } |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 2289 | |
| 2290 | bool isReference() const { return ValueAndIsReference.getInt(); } |
| 2291 | LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const { |
| 2292 | assert(isReference()); |
| 2293 | return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(), |
| 2294 | refExpr->getType()); |
| 2295 | } |
| 2296 | |
| 2297 | llvm::Constant *getValue() const { |
| 2298 | assert(!isReference()); |
| 2299 | return ValueAndIsReference.getPointer(); |
| 2300 | } |
| 2301 | }; |
| 2302 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2303 | ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr); |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 2304 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2305 | RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e, |
| 2306 | AggValueSlot slot = AggValueSlot::ignored()); |
| 2307 | LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e); |
| 2308 | |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2309 | llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface, |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2310 | const ObjCIvarDecl *Ivar); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2311 | LValue EmitLValueForField(LValue Base, const FieldDecl* Field); |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 2312 | LValue EmitLValueForLambdaField(const FieldDecl *Field); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2313 | |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 2314 | /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that |
| 2315 | /// if the Field is a reference, this will return the address of the reference |
| 2316 | /// and not the address of the value stored in the reference. |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2317 | LValue EmitLValueForFieldInitialization(LValue Base, |
| 2318 | const FieldDecl* Field); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2319 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 2320 | LValue EmitLValueForIvar(QualType ObjectTy, |
| 2321 | llvm::Value* Base, const ObjCIvarDecl *Ivar, |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2322 | unsigned CVRQualifiers); |
| 2323 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 2324 | LValue EmitCXXConstructLValue(const CXXConstructExpr *E); |
Anders Carlsson | e61c9e8 | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 2325 | LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E); |
Eli Friedman | 31a3702 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 2326 | LValue EmitLambdaLValue(const LambdaExpr *E); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 2327 | LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E); |
Nico Weber | c5f8046 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 2328 | LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2329 | |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 2330 | LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2331 | LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); |
Chris Lattner | 6545994 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 2332 | LValue EmitStmtExprLValue(const StmtExpr *E); |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2333 | LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E); |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 2334 | LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E); |
Yunzhong Gao | 3b8e0b7 | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 2335 | void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2336 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2337 | //===--------------------------------------------------------------------===// |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 2338 | // Scalar Expression Emission |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2339 | //===--------------------------------------------------------------------===// |
| 2340 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2341 | /// EmitCall - Generate a call of the given function, expecting the given |
| 2342 | /// result type, and using the given argument list which specifies both the |
| 2343 | /// LLVM arguments and the types they were derived from. |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2344 | /// |
Mike Stump | f71d232 | 2009-11-30 20:08:49 +0000 | [diff] [blame] | 2345 | /// \param TargetDecl - If given, the decl of the function in a direct call; |
| 2346 | /// used to set attributes on the call (noreturn, etc.). |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 2347 | RValue EmitCall(const CGFunctionInfo &FnInfo, |
| 2348 | llvm::Value *Callee, |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 2349 | ReturnValueSlot ReturnValue, |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2350 | const CallArgList &Args, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2351 | const Decl *TargetDecl = nullptr, |
| 2352 | llvm::Instruction **callOrInvoke = nullptr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2354 | RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E, |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2355 | ReturnValueSlot ReturnValue, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2356 | const Decl *TargetDecl = nullptr, |
| 2357 | llvm::Value *Chain = nullptr); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2358 | RValue EmitCallExpr(const CallExpr *E, |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2359 | ReturnValueSlot ReturnValue = ReturnValueSlot()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2360 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2361 | llvm::CallInst *EmitRuntimeCall(llvm::Value *callee, |
| 2362 | const Twine &name = ""); |
| 2363 | llvm::CallInst *EmitRuntimeCall(llvm::Value *callee, |
| 2364 | ArrayRef<llvm::Value*> args, |
| 2365 | const Twine &name = ""); |
| 2366 | llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee, |
| 2367 | const Twine &name = ""); |
| 2368 | llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee, |
| 2369 | ArrayRef<llvm::Value*> args, |
| 2370 | const Twine &name = ""); |
| 2371 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2372 | llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee, |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 2373 | ArrayRef<llvm::Value *> Args, |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 2374 | const Twine &Name = ""); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2375 | llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee, |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 2376 | const Twine &Name = ""); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2377 | llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee, |
| 2378 | ArrayRef<llvm::Value*> args, |
| 2379 | const Twine &name = ""); |
| 2380 | llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee, |
| 2381 | const Twine &name = ""); |
| 2382 | void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee, |
| 2383 | ArrayRef<llvm::Value*> args); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2384 | |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 2385 | llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 2386 | NestedNameSpecifier *Qual, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2387 | llvm::Type *Ty); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 2388 | |
| 2389 | llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD, |
| 2390 | CXXDtorType Type, |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 2391 | const CXXRecordDecl *RD); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 2392 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2393 | RValue |
| 2394 | EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee, |
| 2395 | ReturnValueSlot ReturnValue, llvm::Value *This, |
| 2396 | llvm::Value *ImplicitParam, |
| 2397 | QualType ImplicitParamTy, const CallExpr *E); |
| 2398 | RValue EmitCXXStructorCall(const CXXMethodDecl *MD, llvm::Value *Callee, |
| 2399 | ReturnValueSlot ReturnValue, llvm::Value *This, |
| 2400 | llvm::Value *ImplicitParam, |
| 2401 | QualType ImplicitParamTy, const CallExpr *E, |
| 2402 | StructorType Type); |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2403 | RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E, |
| 2404 | ReturnValueSlot ReturnValue); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2405 | RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE, |
| 2406 | const CXXMethodDecl *MD, |
| 2407 | ReturnValueSlot ReturnValue, |
| 2408 | bool HasQualifier, |
| 2409 | NestedNameSpecifier *Qualifier, |
| 2410 | bool IsArrow, const Expr *Base); |
| 2411 | // Compute the object pointer. |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2412 | RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 2413 | ReturnValueSlot ReturnValue); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2414 | |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 2415 | RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2416 | const CXXMethodDecl *MD, |
| 2417 | ReturnValueSlot ReturnValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | |
Peter Collingbourne | 6c0aa5f | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 2419 | RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, |
| 2420 | ReturnValueSlot ReturnValue); |
| 2421 | |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2422 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2423 | RValue EmitBuiltinExpr(const FunctionDecl *FD, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2424 | unsigned BuiltinID, const CallExpr *E, |
| 2425 | ReturnValueSlot ReturnValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2426 | |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2427 | RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue); |
Mike Stump | 09429b9 | 2009-02-17 17:00:02 +0000 | [diff] [blame] | 2428 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2429 | /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call |
| 2430 | /// is unhandled by the current target. |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 2431 | llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
| 2432 | |
Kevin Qin | 8137a60 | 2013-11-14 02:45:18 +0000 | [diff] [blame] | 2433 | llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty, |
| 2434 | const llvm::CmpInst::Predicate Fp, |
| 2435 | const llvm::CmpInst::Predicate Ip, |
| 2436 | const llvm::Twine &Name = ""); |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 2437 | llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2438 | |
| 2439 | llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID, |
| 2440 | unsigned LLVMIntrinsic, |
| 2441 | unsigned AltLLVMIntrinsic, |
| 2442 | const char *NameHint, |
| 2443 | unsigned Modifier, |
| 2444 | const CallExpr *E, |
| 2445 | SmallVectorImpl<llvm::Value *> &Ops, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2446 | llvm::Value *Align = nullptr); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2447 | llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID, |
| 2448 | unsigned Modifier, llvm::Type *ArgTy, |
| 2449 | const CallExpr *E); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2450 | llvm::Value *EmitNeonCall(llvm::Function *F, |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 2451 | SmallVectorImpl<llvm::Value*> &O, |
Bob Wilson | db3d4d0 | 2010-12-08 22:37:56 +0000 | [diff] [blame] | 2452 | const char *name, |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 2453 | unsigned shift = 0, bool rightshift = false); |
Bob Wilson | cf55652 | 2010-12-07 22:40:02 +0000 | [diff] [blame] | 2454 | llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2455 | llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty, |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 2456 | bool negateForRightShift); |
Amaury de la Vieuville | 7f0ff70 | 2013-10-04 13:13:15 +0000 | [diff] [blame] | 2457 | llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt, |
| 2458 | llvm::Type *Ty, bool usgn, const char *name); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2459 | // Helper functions for EmitAArch64BuiltinExpr. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2460 | llvm::Value *vectorWrapScalar8(llvm::Value *Op); |
| 2461 | llvm::Value *vectorWrapScalar16(llvm::Value *Op); |
| 2462 | llvm::Value *emitVectorWrappedScalar8Intrinsic( |
| 2463 | unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name); |
| 2464 | llvm::Value *emitVectorWrappedScalar16Intrinsic( |
| 2465 | unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2466 | llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2467 | llvm::Value *EmitNeon64Call(llvm::Function *F, |
| 2468 | llvm::SmallVectorImpl<llvm::Value *> &O, |
| 2469 | const char *name); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2470 | |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2471 | llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops); |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 2472 | llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
| 2473 | llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2474 | llvm::Value *EmitR600BuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2475 | |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 2476 | llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2477 | llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2478 | llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2479 | llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E); |
| 2480 | llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E); |
| 2481 | llvm::Value *EmitObjCCollectionLiteral(const Expr *E, |
| 2482 | const ObjCMethodDecl *MethodWithObjects); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 2483 | llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E); |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 2484 | RValue EmitObjCMessageExpr(const ObjCMessageExpr *E, |
| 2485 | ReturnValueSlot Return = ReturnValueSlot()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 2486 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2487 | /// Retrieves the default cleanup kind for an ARC cleanup. |
| 2488 | /// Except under -fobjc-arc-eh, ARC cleanups are normal-only. |
| 2489 | CleanupKind getARCCleanupKind() { |
| 2490 | return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions |
| 2491 | ? NormalAndEHCleanup : NormalCleanup; |
| 2492 | } |
| 2493 | |
| 2494 | // ARC primitives. |
| 2495 | void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr); |
| 2496 | void EmitARCDestroyWeak(llvm::Value *addr); |
| 2497 | llvm::Value *EmitARCLoadWeak(llvm::Value *addr); |
| 2498 | llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr); |
| 2499 | llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr, |
| 2500 | bool ignored); |
| 2501 | void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src); |
| 2502 | void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src); |
| 2503 | llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value); |
| 2504 | llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2505 | llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value, |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2506 | bool resultIgnored); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2507 | llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value, |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2508 | bool resultIgnored); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2509 | llvm::Value *EmitARCRetain(QualType type, llvm::Value *value); |
| 2510 | llvm::Value *EmitARCRetainNonBlock(llvm::Value *value); |
John McCall | 348f16f | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 2511 | llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory); |
John McCall | 5b07e80 | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2512 | void EmitARCDestroyStrong(llvm::Value *addr, ARCPreciseLifetime_t precise); |
| 2513 | void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2514 | llvm::Value *EmitARCAutorelease(llvm::Value *value); |
| 2515 | llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value); |
| 2516 | llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value); |
| 2517 | llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value); |
| 2518 | |
| 2519 | std::pair<LValue,llvm::Value*> |
| 2520 | EmitARCStoreAutoreleasing(const BinaryOperator *e); |
| 2521 | std::pair<LValue,llvm::Value*> |
| 2522 | EmitARCStoreStrong(const BinaryOperator *e, bool ignored); |
| 2523 | |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2524 | llvm::Value *EmitObjCThrowOperand(const Expr *expr); |
| 2525 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2526 | llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr); |
| 2527 | llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr); |
| 2528 | llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr); |
| 2529 | |
John McCall | 348f16f | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 2530 | llvm::Value *EmitARCExtendBlockObject(const Expr *expr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2531 | llvm::Value *EmitARCRetainScalarExpr(const Expr *expr); |
| 2532 | llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr); |
| 2533 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2534 | void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values); |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2535 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2536 | static Destroyer destroyARCStrongImprecise; |
| 2537 | static Destroyer destroyARCStrongPrecise; |
| 2538 | static Destroyer destroyARCWeak; |
| 2539 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2540 | void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr); |
| 2541 | llvm::Value *EmitObjCAutoreleasePoolPush(); |
| 2542 | llvm::Value *EmitObjCMRRAutoreleasePoolPush(); |
| 2543 | void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr); |
| 2544 | void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr); |
| 2545 | |
Richard Smith | d4ec562 | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 2546 | /// \brief Emits a reference binding to the passed in expression. |
| 2547 | RValue EmitReferenceBindingToExpr(const Expr *E); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 2548 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 2549 | //===--------------------------------------------------------------------===// |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 2550 | // Expression Emission |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 2551 | //===--------------------------------------------------------------------===// |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 2552 | |
| 2553 | // Expressions are broken into three classes: scalar, complex, aggregate. |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2554 | |
| 2555 | /// EmitScalarExpr - Emit the computation of the specified expression of LLVM |
| 2556 | /// scalar type, returning the result. |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 2557 | llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2558 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 2559 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 2560 | /// specified destination type, both of which are LLVM scalar types. |
| 2561 | llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy, |
| 2562 | QualType DstTy); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2563 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2564 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2565 | /// complex type to the specified destination type, where the destination type |
| 2566 | /// is an LLVM scalar type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2567 | llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, |
| 2568 | QualType DstTy); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2569 | |
| 2570 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2571 | /// EmitAggExpr - Emit the computation of the specified expression |
| 2572 | /// of aggregate type. The result is computed into the given slot, |
| 2573 | /// which may be null to indicate that the value is not needed. |
John McCall | e0c1168 | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 2574 | void EmitAggExpr(const Expr *E, AggValueSlot AS); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2575 | |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 2576 | /// EmitAggExprToLValue - Emit the computation of the specified expression of |
| 2577 | /// aggregate type into a temporary LValue. |
| 2578 | LValue EmitAggExprToLValue(const Expr *E); |
| 2579 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2580 | /// EmitGCMemmoveCollectable - Emit special API for structs with object |
| 2581 | /// pointers. |
| 2582 | void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 2583 | QualType Ty); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2584 | |
John McCall | 0c24c80 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2585 | /// EmitExtendGCLifetime - Given a pointer to an Objective-C object, |
| 2586 | /// make sure it survives garbage collection until this point. |
| 2587 | void EmitExtendGCLifetime(llvm::Value *object); |
| 2588 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 2589 | /// EmitComplexExpr - Emit the computation of the specified expression of |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 2590 | /// complex type, returning the result. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 2591 | ComplexPairTy EmitComplexExpr(const Expr *E, |
| 2592 | bool IgnoreReal = false, |
| 2593 | bool IgnoreImag = false); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2594 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2595 | /// EmitComplexExprIntoLValue - Emit the given expression of complex |
| 2596 | /// type and place its result into the specified l-value. |
| 2597 | void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 2598 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2599 | /// EmitStoreOfComplex - Store a complex number into the specified l-value. |
| 2600 | void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit); |
| 2601 | |
| 2602 | /// EmitLoadOfComplex - Load a complex number from the specified l-value. |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2603 | ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc); |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 2604 | |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 2605 | /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the |
| 2606 | /// global variable that has already been created for it. If the initializer |
| 2607 | /// has a different type than GV does, this may free GV and return a different |
| 2608 | /// one. Otherwise it just returns GV. |
| 2609 | llvm::GlobalVariable * |
| 2610 | AddInitializerToStaticVarDecl(const VarDecl &D, |
| 2611 | llvm::GlobalVariable *GV); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2612 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 2613 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 2614 | /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++ |
| 2615 | /// variable with global storage. |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 2616 | void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr, |
| 2617 | bool PerformInit); |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 2618 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2619 | llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor, |
| 2620 | llvm::Constant *Addr); |
| 2621 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2622 | /// Call atexit() with a function that passes the given argument to |
| 2623 | /// the given function. |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 2624 | void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn, |
| 2625 | llvm::Constant *addr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2626 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 2627 | /// Emit code in this function to perform a guarded variable |
| 2628 | /// initialization. Guarded initializations are used when it's not |
| 2629 | /// possible to prove that an initialization will be done exactly |
| 2630 | /// once, e.g. with a static local variable or a static data member |
| 2631 | /// of a class template. |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 2632 | void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 2633 | bool PerformInit); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2634 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 2635 | /// GenerateCXXGlobalInitFunc - Generates code for initializing global |
| 2636 | /// variables. |
| 2637 | void GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2638 | ArrayRef<llvm::Function *> CXXThreadLocals, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2639 | llvm::GlobalVariable *Guard = nullptr); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 2640 | |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 2641 | /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 2642 | /// variables. |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 2643 | void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn, |
| 2644 | const std::vector<std::pair<llvm::WeakVH, |
| 2645 | llvm::Constant*> > &DtorsAndObjects); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 2646 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2647 | void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
| 2648 | const VarDecl *D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 2649 | llvm::GlobalVariable *Addr, |
| 2650 | bool PerformInit); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 2651 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2652 | void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 2653 | |
| 2654 | void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 2655 | const Expr *Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 2657 | void enterFullExpression(const ExprWithCleanups *E) { |
| 2658 | if (E->getNumObjects() == 0) return; |
| 2659 | enterNonTrivialFullExpression(E); |
| 2660 | } |
| 2661 | void enterNonTrivialFullExpression(const ExprWithCleanups *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2662 | |
Richard Smith | 4c71b8c | 2013-05-07 21:53:22 +0000 | [diff] [blame] | 2663 | void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true); |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 2664 | |
Eli Friedman | 4c5d8af | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 2665 | void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest); |
| 2666 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2667 | RValue EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest = nullptr); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2668 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 2669 | //===--------------------------------------------------------------------===// |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2670 | // Annotations Emission |
| 2671 | //===--------------------------------------------------------------------===// |
| 2672 | |
| 2673 | /// Emit an annotation call (intrinsic or builtin). |
| 2674 | llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn, |
| 2675 | llvm::Value *AnnotatedVal, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2676 | StringRef AnnotationStr, |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2677 | SourceLocation Location); |
| 2678 | |
| 2679 | /// Emit local annotations for the local variable V, declared by D. |
| 2680 | void EmitVarAnnotations(const VarDecl *D, llvm::Value *V); |
| 2681 | |
| 2682 | /// Emit field annotations for the given field & value. Returns the |
| 2683 | /// annotation result. |
| 2684 | llvm::Value *EmitFieldAnnotations(const FieldDecl *D, llvm::Value *V); |
| 2685 | |
| 2686 | //===--------------------------------------------------------------------===// |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 2687 | // Internal Helpers |
| 2688 | //===--------------------------------------------------------------------===// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2689 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2690 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 2691 | /// this statement is not executed normally, it not containing a label means |
| 2692 | /// that we can just remove the code. |
| 2693 | static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2694 | |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 2695 | /// containsBreak - Return true if the statement contains a break out of it. |
| 2696 | /// If the statement (recursively) contains a switch or loop with a break |
| 2697 | /// inside of it, this is fine. |
| 2698 | static bool containsBreak(const Stmt *S); |
| 2699 | |
Daniel Dunbar | 4bc0455 | 2008-11-12 10:12:14 +0000 | [diff] [blame] | 2700 | /// ConstantFoldsToSimpleInteger - If the specified expression does not fold |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 2701 | /// to a constant, or if it does but contains a label, return false. If it |
| 2702 | /// constant folds return true and set the boolean result in Result. |
| 2703 | bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2704 | |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 2705 | /// ConstantFoldsToSimpleInteger - If the specified expression does not fold |
| 2706 | /// to a constant, or if it does but contains a label, return false. If it |
| 2707 | /// constant folds return true and set the folded value. |
Richard Trieu | e1ecdc1 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 2708 | bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result); |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 2709 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 2710 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an |
| 2711 | /// if statement) to the specified blocks. Based on the condition, this might |
| 2712 | /// try to simplify the codegen of the conditional based on the branch. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2713 | /// TrueCount should be the number of times we expect the condition to |
| 2714 | /// evaluate to true based on PGO data. |
Chris Lattner | 9bc47e2 | 2008-11-12 07:46:33 +0000 | [diff] [blame] | 2715 | void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2716 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount); |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 2717 | |
Richard Smith | 4def70d | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2718 | /// \brief Emit a description of a type in a format suitable for passing to |
| 2719 | /// a runtime sanitizer handler. |
| 2720 | llvm::Constant *EmitCheckTypeDescriptor(QualType T); |
| 2721 | |
| 2722 | /// \brief Convert a value into a format suitable for passing to a runtime |
| 2723 | /// sanitizer handler. |
| 2724 | llvm::Value *EmitCheckValue(llvm::Value *V); |
| 2725 | |
| 2726 | /// \brief Emit a description of a source location in a format suitable for |
| 2727 | /// passing to a runtime sanitizer handler. |
| 2728 | llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc); |
| 2729 | |
Richard Smith | cc30561 | 2012-11-01 22:15:34 +0000 | [diff] [blame] | 2730 | /// \brief Create a basic block that will call a handler function in a |
| 2731 | /// sanitizer runtime with the provided arguments, and create a conditional |
| 2732 | /// branch to it. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2733 | void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerKind>> Checked, |
| 2734 | StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs, |
| 2735 | ArrayRef<llvm::Value *> DynamicArgs); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2736 | |
Richard Smith | cc30561 | 2012-11-01 22:15:34 +0000 | [diff] [blame] | 2737 | /// \brief Create a basic block that will call the trap intrinsic, and emit a |
| 2738 | /// conditional branch to it, for the -ftrapv checks. |
Chad Rosier | 78d85b1 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 2739 | void EmitTrapCheck(llvm::Value *Checked); |
Richard Smith | cc30561 | 2012-11-01 22:15:34 +0000 | [diff] [blame] | 2740 | |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 2741 | /// EmitCallArg - Emit a single call argument. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2742 | void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType); |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 2743 | |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2744 | /// EmitDelegateCallArg - We are performing a delegate call; that |
| 2745 | /// is, the current function is delegating to another one. Produce |
| 2746 | /// a r-value suitable for passing the given parameter. |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2747 | void EmitDelegateCallArg(CallArgList &args, const VarDecl *param, |
| 2748 | SourceLocation loc); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2749 | |
Peter Collingbourne | c5096cb | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 2750 | /// SetFPAccuracy - Set the minimum required accuracy of the given floating |
| 2751 | /// point operation, expressed as the maximum relative error in ulp. |
Duncan Sands | 8250016 | 2012-04-10 08:23:07 +0000 | [diff] [blame] | 2752 | void SetFPAccuracy(llvm::Value *Val, float Accuracy); |
Peter Collingbourne | c5096cb | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 2753 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 2754 | private: |
Rafael Espindola | c3f8955 | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 2755 | llvm::MDNode *getRangeForLoadFromType(QualType Ty); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2756 | void EmitReturnOfRValue(RValue RV, QualType Ty); |
| 2757 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2758 | void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New); |
| 2759 | |
| 2760 | llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4> |
| 2761 | DeferredReplacements; |
| 2762 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2763 | /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty |
| 2764 | /// from function arguments into \arg Dst. See ABIArgInfo::Expand. |
| 2765 | /// |
| 2766 | /// \param AI - The first function argument of the expansion. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2767 | void ExpandTypeFromArgs(QualType Ty, LValue Dst, |
| 2768 | SmallVectorImpl<llvm::Argument *>::iterator &AI); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2769 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2770 | /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg |
| 2771 | /// Ty, into individual arguments on the provided vector \arg IRCallArgs, |
| 2772 | /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand. |
| 2773 | void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy, |
| 2774 | SmallVectorImpl<llvm::Value *> &IRCallArgs, |
| 2775 | unsigned &IRCallArgPos); |
Anders Carlsson | c8c7b18 | 2009-01-11 19:40:10 +0000 | [diff] [blame] | 2776 | |
Chad Rosier | 42b6055 | 2012-08-23 20:00:18 +0000 | [diff] [blame] | 2777 | llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info, |
Anders Carlsson | c8c7b18 | 2009-01-11 19:40:10 +0000 | [diff] [blame] | 2778 | const Expr *InputExpr, std::string &ConstraintStr); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2779 | |
Chad Rosier | 42b6055 | 2012-08-23 20:00:18 +0000 | [diff] [blame] | 2780 | llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info, |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 2781 | LValue InputValue, QualType InputType, |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2782 | std::string &ConstraintStr, |
| 2783 | SourceLocation Loc); |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 2784 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2785 | public: |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 2786 | /// EmitCallArgs - Emit call arguments for a function. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2787 | template <typename T> |
| 2788 | void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo, |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 2789 | CallExpr::const_arg_iterator ArgBeg, |
Adrian Prantl | b0e603c | 2013-07-26 20:42:57 +0000 | [diff] [blame] | 2790 | CallExpr::const_arg_iterator ArgEnd, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2791 | const FunctionDecl *CalleeDecl = nullptr, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2792 | unsigned ParamsToSkip = 0) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2793 | SmallVector<QualType, 16> ArgTypes; |
| 2794 | CallExpr::const_arg_iterator Arg = ArgBeg; |
| 2795 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2796 | assert((ParamsToSkip == 0 || CallArgTypeInfo) && |
| 2797 | "Can't skip parameters if type info is not provided"); |
| 2798 | if (CallArgTypeInfo) { |
| 2799 | // First, use the argument types that the type info knows about |
| 2800 | for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip, |
| 2801 | E = CallArgTypeInfo->param_type_end(); |
| 2802 | I != E; ++I, ++Arg) { |
| 2803 | assert(Arg != ArgEnd && "Running over edge of argument list!"); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2804 | assert( |
| 2805 | ((*I)->isVariablyModifiedType() || |
| 2806 | getContext() |
| 2807 | .getCanonicalType((*I).getNonReferenceType()) |
| 2808 | .getTypePtr() == |
| 2809 | getContext().getCanonicalType(Arg->getType()).getTypePtr()) && |
| 2810 | "type mismatch in call argument!"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2811 | ArgTypes.push_back(*I); |
| 2812 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | // Either we've emitted all the call args, or we have a call to variadic |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2816 | // function. |
| 2817 | assert( |
| 2818 | (Arg == ArgEnd || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) && |
| 2819 | "Extra arguments in non-variadic function!"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2820 | |
| 2821 | // If we still have any arguments, emit them using the type of the argument. |
| 2822 | for (; Arg != ArgEnd; ++Arg) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2823 | ArgTypes.push_back(getVarArgType(*Arg)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2824 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2825 | EmitCallArgs(Args, ArgTypes, ArgBeg, ArgEnd, CalleeDecl, ParamsToSkip); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes, |
| 2829 | CallExpr::const_arg_iterator ArgBeg, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2830 | CallExpr::const_arg_iterator ArgEnd, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2831 | const FunctionDecl *CalleeDecl = nullptr, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2832 | unsigned ParamsToSkip = 0); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2833 | |
| 2834 | private: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2835 | QualType getVarArgType(const Expr *Arg); |
| 2836 | |
John McCall | 492c4f9 | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 2837 | const TargetCodeGenInfo &getTargetHooks() const { |
| 2838 | return CGM.getTargetCodeGenInfo(); |
| 2839 | } |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2840 | |
| 2841 | void EmitDeclMetadata(); |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 2842 | |
| 2843 | CodeGenModule::ByrefHelpers * |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2844 | buildByrefHelpers(llvm::StructType &byrefType, |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 2845 | const AutoVarEmission &emission); |
Dan Gohman | b49bd27 | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 2846 | |
| 2847 | void AddObjCARCExceptionMetadata(llvm::Instruction *Inst); |
Jay Foad | f4c3db1 | 2012-03-02 18:34:30 +0000 | [diff] [blame] | 2848 | |
Eli Friedman | ea93e40 | 2012-08-23 03:10:17 +0000 | [diff] [blame] | 2849 | /// GetPointeeAlignment - Given an expression with a pointer type, emit the |
| 2850 | /// value and compute our best estimate of the alignment of the pointee. |
| 2851 | std::pair<llvm::Value*, unsigned> EmitPointerWithAlignment(const Expr *Addr); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2852 | |
| 2853 | llvm::Value *GetValueForARMHint(unsigned BuiltinID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2854 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2856 | /// Helper class with most of the code for saving a value for a |
| 2857 | /// conditional expression cleanup. |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 2858 | struct DominatingLLVMValue { |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2859 | typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type; |
| 2860 | |
| 2861 | /// Answer whether the given value needs extra work to be saved. |
| 2862 | static bool needsSaving(llvm::Value *value) { |
| 2863 | // If it's not an instruction, we don't need to save. |
| 2864 | if (!isa<llvm::Instruction>(value)) return false; |
| 2865 | |
| 2866 | // If it's an instruction in the entry block, we don't need to save. |
| 2867 | llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent(); |
| 2868 | return (block != &block->getParent()->getEntryBlock()); |
| 2869 | } |
| 2870 | |
| 2871 | /// Try to save the given value. |
| 2872 | static saved_type save(CodeGenFunction &CGF, llvm::Value *value) { |
| 2873 | if (!needsSaving(value)) return saved_type(value, false); |
| 2874 | |
| 2875 | // Otherwise we need an alloca. |
| 2876 | llvm::Value *alloca = |
| 2877 | CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save"); |
| 2878 | CGF.Builder.CreateStore(value, alloca); |
| 2879 | |
| 2880 | return saved_type(alloca, true); |
| 2881 | } |
| 2882 | |
| 2883 | static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) { |
| 2884 | if (!value.getInt()) return value.getPointer(); |
| 2885 | return CGF.Builder.CreateLoad(value.getPointer()); |
| 2886 | } |
| 2887 | }; |
| 2888 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 2889 | /// A partial specialization of DominatingValue for llvm::Values that |
| 2890 | /// might be llvm::Instructions. |
| 2891 | template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue { |
| 2892 | typedef T *type; |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2893 | static type restore(CodeGenFunction &CGF, saved_type value) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 2894 | return static_cast<T*>(DominatingLLVMValue::restore(CGF, value)); |
| 2895 | } |
| 2896 | }; |
| 2897 | |
| 2898 | /// A specialization of DominatingValue for RValue. |
| 2899 | template <> struct DominatingValue<RValue> { |
| 2900 | typedef RValue type; |
| 2901 | class saved_type { |
| 2902 | enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral, |
| 2903 | AggregateAddress, ComplexAddress }; |
| 2904 | |
| 2905 | llvm::Value *Value; |
| 2906 | Kind K; |
| 2907 | saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {} |
| 2908 | |
| 2909 | public: |
| 2910 | static bool needsSaving(RValue value); |
| 2911 | static saved_type save(CodeGenFunction &CGF, RValue value); |
| 2912 | RValue restore(CodeGenFunction &CGF); |
| 2913 | |
| 2914 | // implementations in CGExprCXX.cpp |
| 2915 | }; |
| 2916 | |
| 2917 | static bool needsSaving(type value) { |
| 2918 | return saved_type::needsSaving(value); |
| 2919 | } |
| 2920 | static saved_type save(CodeGenFunction &CGF, type value) { |
| 2921 | return saved_type::save(CGF, value); |
| 2922 | } |
| 2923 | static type restore(CodeGenFunction &CGF, saved_type value) { |
| 2924 | return value.restore(CGF); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2925 | } |
| 2926 | }; |
| 2927 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2928 | } // end namespace CodeGen |
| 2929 | } // end namespace clang |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 2930 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2931 | #endif |