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