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