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 | |
Chris Lattner | ef52a2f | 2008-02-29 17:10:38 +0000 | [diff] [blame] | 14 | #ifndef CLANG_CODEGEN_CODEGENFUNCTION_H |
| 15 | #define CLANG_CODEGEN_CODEGENFUNCTION_H |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 17 | #include "clang/AST/Type.h" |
Argyrios Kyrtzidis | e3a09e6 | 2008-09-10 02:36:38 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprObjC.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 20 | #include "clang/AST/CharUnits.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CodeGenOptions.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 22 | #include "clang/Basic/ABI.h" |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
| 24 | #include "llvm/ADT/DenseMap.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
| 26 | #include "llvm/Support/ValueHandle.h" |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 27 | #include "CodeGenModule.h" |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 28 | #include "CGBuilder.h" |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 29 | #include "CGValue.h" |
| 30 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | namespace llvm { |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 32 | class BasicBlock; |
Benjamin Kramer | f21efe9 | 2009-08-11 17:46:57 +0000 | [diff] [blame] | 33 | class LLVMContext; |
David Chisnall | dd5c98f | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 34 | class MDNode; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | class Module; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 36 | class SwitchInst; |
Daniel Dunbar | 259e9cc | 2009-10-19 01:21:05 +0000 | [diff] [blame] | 37 | class Twine; |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 38 | class Value; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 39 | class CallSite; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | namespace clang { |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 43 | class APValue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | class ASTContext; |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 45 | class CXXDestructorDecl; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 46 | class CXXForRangeStmt; |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 47 | class CXXTryStmt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | class Decl; |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 49 | class LabelDecl; |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 50 | class EnumConstantDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | class FunctionDecl; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 52 | class FunctionProtoType; |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 53 | class LabelStmt; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 54 | class ObjCContainerDecl; |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 55 | class ObjCInterfaceDecl; |
| 56 | class ObjCIvarDecl; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 57 | class ObjCMethodDecl; |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 58 | class ObjCImplementationDecl; |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 59 | class ObjCPropertyImplDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | class TargetInfo; |
John McCall | 492c4f9 | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 61 | class TargetCodeGenInfo; |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 62 | class VarDecl; |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 63 | class ObjCForCollectionStmt; |
| 64 | class ObjCAtTryStmt; |
| 65 | class ObjCAtThrowStmt; |
| 66 | class ObjCAtSynchronizedStmt; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 67 | class ObjCAutoreleasePoolStmt; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 68 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | namespace CodeGen { |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 70 | class CodeGenTypes; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 71 | class CGDebugInfo; |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 72 | class CGFunctionInfo; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 73 | class CGRecordLayout; |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 74 | class CGBlockInfo; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 75 | class CGCXXABI; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 76 | class BlockFlags; |
| 77 | class BlockFieldFlags; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 78 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 79 | /// A branch fixup. These are required when emitting a goto to a |
| 80 | /// label which hasn't been emitted yet. The goto is optimistically |
| 81 | /// emitted as a branch to the basic block for the label, and (if it |
| 82 | /// occurs in a scope with non-trivial cleanups) a fixup is added to |
| 83 | /// the innermost cleanup. When a (normal) cleanup is popped, any |
| 84 | /// unresolved fixups in that scope are threaded through the cleanup. |
| 85 | struct BranchFixup { |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 86 | /// The block containing the terminator which needs to be modified |
| 87 | /// into a switch if this fixup is resolved into the current scope. |
| 88 | /// If null, LatestBranch points directly to the destination. |
| 89 | llvm::BasicBlock *OptimisticBranchBlock; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 90 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 91 | /// The ultimate destination of the branch. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 92 | /// |
| 93 | /// This can be set to null to indicate that this fixup was |
| 94 | /// successfully resolved. |
| 95 | llvm::BasicBlock *Destination; |
| 96 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 97 | /// The destination index value. |
| 98 | unsigned DestinationIndex; |
| 99 | |
| 100 | /// The initial branch of the fixup. |
| 101 | llvm::BranchInst *InitialBranch; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 104 | template <class T> struct InvariantValue { |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 105 | typedef T type; |
| 106 | typedef T saved_type; |
| 107 | static bool needsSaving(type value) { return false; } |
| 108 | static saved_type save(CodeGenFunction &CGF, type value) { return value; } |
| 109 | static type restore(CodeGenFunction &CGF, saved_type value) { return value; } |
| 110 | }; |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 111 | |
| 112 | /// A metaprogramming class for ensuring that a value will dominate an |
| 113 | /// arbitrary position in a function. |
| 114 | template <class T> struct DominatingValue : InvariantValue<T> {}; |
| 115 | |
| 116 | template <class T, bool mightBeInstruction = |
| 117 | llvm::is_base_of<llvm::Value, T>::value && |
| 118 | !llvm::is_base_of<llvm::Constant, T>::value && |
| 119 | !llvm::is_base_of<llvm::BasicBlock, T>::value> |
| 120 | struct DominatingPointer; |
| 121 | template <class T> struct DominatingPointer<T,false> : InvariantValue<T*> {}; |
| 122 | // template <class T> struct DominatingPointer<T,true> at end of file |
| 123 | |
| 124 | template <class T> struct DominatingValue<T*> : DominatingPointer<T> {}; |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 125 | |
John McCall | cd2d2b7 | 2010-08-13 21:20:51 +0000 | [diff] [blame] | 126 | enum CleanupKind { |
| 127 | EHCleanup = 0x1, |
| 128 | NormalCleanup = 0x2, |
| 129 | NormalAndEHCleanup = EHCleanup | NormalCleanup, |
| 130 | |
| 131 | InactiveCleanup = 0x4, |
| 132 | InactiveEHCleanup = EHCleanup | InactiveCleanup, |
| 133 | InactiveNormalCleanup = NormalCleanup | InactiveCleanup, |
| 134 | InactiveNormalAndEHCleanup = NormalAndEHCleanup | InactiveCleanup |
| 135 | }; |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 136 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 137 | /// A stack of scopes which respond to exceptions, including cleanups |
| 138 | /// and catch blocks. |
| 139 | class EHScopeStack { |
| 140 | public: |
| 141 | /// A saved depth on the scope stack. This is necessary because |
| 142 | /// pushing scopes onto the stack invalidates iterators. |
| 143 | class stable_iterator { |
| 144 | friend class EHScopeStack; |
| 145 | |
| 146 | /// Offset from StartOfData to EndOfBuffer. |
| 147 | ptrdiff_t Size; |
| 148 | |
| 149 | stable_iterator(ptrdiff_t Size) : Size(Size) {} |
| 150 | |
| 151 | public: |
| 152 | static stable_iterator invalid() { return stable_iterator(-1); } |
| 153 | stable_iterator() : Size(-1) {} |
| 154 | |
| 155 | bool isValid() const { return Size >= 0; } |
| 156 | |
John McCall | 838d796 | 2010-08-14 07:46:19 +0000 | [diff] [blame] | 157 | /// Returns true if this scope encloses I. |
| 158 | /// Returns false if I is invalid. |
| 159 | /// This scope must be valid. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 160 | bool encloses(stable_iterator I) const { return Size <= I.Size; } |
John McCall | 838d796 | 2010-08-14 07:46:19 +0000 | [diff] [blame] | 161 | |
| 162 | /// Returns true if this scope strictly encloses I: that is, |
| 163 | /// if it encloses I and is not I. |
| 164 | /// Returns false is I is invalid. |
| 165 | /// This scope must be valid. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 166 | bool strictlyEncloses(stable_iterator I) const { return Size < I.Size; } |
John McCall | 1bda662 | 2010-07-21 00:40:03 +0000 | [diff] [blame] | 167 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 168 | friend bool operator==(stable_iterator A, stable_iterator B) { |
| 169 | return A.Size == B.Size; |
| 170 | } |
| 171 | friend bool operator!=(stable_iterator A, stable_iterator B) { |
| 172 | return A.Size != B.Size; |
| 173 | } |
| 174 | }; |
| 175 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 176 | /// Information for lazily generating a cleanup. Subclasses must be |
| 177 | /// POD-like: cleanups will not be destructed, and they will be |
| 178 | /// allocated on the cleanup stack and freely copied and moved |
| 179 | /// around. |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 180 | /// |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 181 | /// Cleanup implementations should generally be declared in an |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 182 | /// anonymous namespace. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 183 | class Cleanup { |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 184 | // Anchor the construction vtable. |
| 185 | virtual void anchor(); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 186 | public: |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 187 | // Provide a virtual destructor to suppress a very common warning |
| 188 | // that unfortunately cannot be suppressed without this. Cleanups |
| 189 | // should not rely on this destructor ever being called. |
| 190 | virtual ~Cleanup() {} |
John McCall | 3e29f96 | 2010-07-13 23:19:49 +0000 | [diff] [blame] | 191 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 192 | /// Emit the cleanup. For normal cleanups, this is run in the |
| 193 | /// same EH context as when the cleanup was pushed, i.e. the |
| 194 | /// immediately-enclosing context of the cleanup scope. For |
| 195 | /// EH cleanups, this is run in a terminate context. |
| 196 | /// |
| 197 | // \param IsForEHCleanup true if this is for an EH cleanup, false |
| 198 | /// if for a normal cleanup. |
| 199 | virtual void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) = 0; |
| 200 | }; |
| 201 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 202 | /// ConditionalCleanupN stores the saved form of its N parameters, |
| 203 | /// then restores them and performs the cleanup. |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 204 | template <class T, class A0> |
| 205 | class ConditionalCleanup1 : public Cleanup { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 206 | typedef typename DominatingValue<A0>::saved_type A0_saved; |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 207 | A0_saved a0_saved; |
| 208 | |
| 209 | void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 210 | A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved); |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 211 | T(a0).Emit(CGF, IsForEHCleanup); |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | public: |
| 215 | ConditionalCleanup1(A0_saved a0) |
| 216 | : a0_saved(a0) {} |
| 217 | }; |
| 218 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 219 | template <class T, class A0, class A1> |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 220 | class ConditionalCleanup2 : public Cleanup { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 221 | typedef typename DominatingValue<A0>::saved_type A0_saved; |
| 222 | typedef typename DominatingValue<A1>::saved_type A1_saved; |
John McCall | 4bbcbda | 2011-01-26 19:15:39 +0000 | [diff] [blame] | 223 | A0_saved a0_saved; |
| 224 | A1_saved a1_saved; |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 225 | |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 226 | void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 227 | A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved); |
| 228 | A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved); |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 229 | T(a0, a1).Emit(CGF, IsForEHCleanup); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | public: |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 233 | ConditionalCleanup2(A0_saved a0, A1_saved a1) |
| 234 | : a0_saved(a0), a1_saved(a1) {} |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 237 | template <class T, class A0, class A1, class A2> |
| 238 | class ConditionalCleanup3 : public Cleanup { |
| 239 | typedef typename DominatingValue<A0>::saved_type A0_saved; |
| 240 | typedef typename DominatingValue<A1>::saved_type A1_saved; |
| 241 | typedef typename DominatingValue<A2>::saved_type A2_saved; |
| 242 | A0_saved a0_saved; |
| 243 | A1_saved a1_saved; |
| 244 | A2_saved a2_saved; |
| 245 | |
| 246 | void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) { |
| 247 | A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved); |
| 248 | A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved); |
| 249 | A2 a2 = DominatingValue<A2>::restore(CGF, a2_saved); |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 250 | T(a0, a1, a2).Emit(CGF, IsForEHCleanup); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | public: |
| 254 | ConditionalCleanup3(A0_saved a0, A1_saved a1, A2_saved a2) |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 255 | : a0_saved(a0), a1_saved(a1), a2_saved(a2) {} |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 258 | template <class T, class A0, class A1, class A2, class A3> |
| 259 | class ConditionalCleanup4 : public Cleanup { |
| 260 | typedef typename DominatingValue<A0>::saved_type A0_saved; |
| 261 | typedef typename DominatingValue<A1>::saved_type A1_saved; |
| 262 | typedef typename DominatingValue<A2>::saved_type A2_saved; |
| 263 | typedef typename DominatingValue<A3>::saved_type A3_saved; |
| 264 | A0_saved a0_saved; |
| 265 | A1_saved a1_saved; |
| 266 | A2_saved a2_saved; |
| 267 | A3_saved a3_saved; |
| 268 | |
| 269 | void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) { |
| 270 | A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved); |
| 271 | A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved); |
| 272 | A2 a2 = DominatingValue<A2>::restore(CGF, a2_saved); |
| 273 | A3 a3 = DominatingValue<A3>::restore(CGF, a3_saved); |
| 274 | T(a0, a1, a2, a3).Emit(CGF, IsForEHCleanup); |
| 275 | } |
| 276 | |
| 277 | public: |
| 278 | ConditionalCleanup4(A0_saved a0, A1_saved a1, A2_saved a2, A3_saved a3) |
| 279 | : a0_saved(a0), a1_saved(a1), a2_saved(a2), a3_saved(a3) {} |
| 280 | }; |
| 281 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 282 | private: |
| 283 | // The implementation for this class is in CGException.h and |
| 284 | // CGException.cpp; the definition is here because it's used as a |
| 285 | // member of CodeGenFunction. |
| 286 | |
| 287 | /// The start of the scope-stack buffer, i.e. the allocated pointer |
| 288 | /// for the buffer. All of these pointers are either simultaneously |
| 289 | /// null or simultaneously valid. |
| 290 | char *StartOfBuffer; |
| 291 | |
| 292 | /// The end of the buffer. |
| 293 | char *EndOfBuffer; |
| 294 | |
| 295 | /// The first valid entry in the buffer. |
| 296 | char *StartOfData; |
| 297 | |
| 298 | /// The innermost normal cleanup on the stack. |
| 299 | stable_iterator InnermostNormalCleanup; |
| 300 | |
| 301 | /// The innermost EH cleanup on the stack. |
| 302 | stable_iterator InnermostEHCleanup; |
| 303 | |
| 304 | /// The number of catches on the stack. |
| 305 | unsigned CatchDepth; |
| 306 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 307 | /// The current EH destination index. Reset to FirstCatchIndex |
| 308 | /// whenever the last EH cleanup is popped. |
| 309 | unsigned NextEHDestIndex; |
| 310 | enum { FirstEHDestIndex = 1 }; |
| 311 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 312 | /// The current set of branch fixups. A branch fixup is a jump to |
| 313 | /// an as-yet unemitted label, i.e. a label for which we don't yet |
| 314 | /// know the EH stack depth. Whenever we pop a cleanup, we have |
| 315 | /// to thread all the current branch fixups through it. |
| 316 | /// |
| 317 | /// Fixups are recorded as the Use of the respective branch or |
| 318 | /// switch statement. The use points to the final destination. |
| 319 | /// When popping out of a cleanup, these uses are threaded through |
| 320 | /// the cleanup and adjusted to point to the new cleanup. |
| 321 | /// |
| 322 | /// Note that branches are allowed to jump into protected scopes |
| 323 | /// in certain situations; e.g. the following code is legal: |
| 324 | /// struct A { ~A(); }; // trivial ctor, non-trivial dtor |
| 325 | /// goto foo; |
| 326 | /// A a; |
| 327 | /// foo: |
| 328 | /// bar(); |
| 329 | llvm::SmallVector<BranchFixup, 8> BranchFixups; |
| 330 | |
| 331 | char *allocate(size_t Size); |
| 332 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 333 | void *pushCleanup(CleanupKind K, size_t DataSize); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 334 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 335 | public: |
| 336 | EHScopeStack() : StartOfBuffer(0), EndOfBuffer(0), StartOfData(0), |
| 337 | InnermostNormalCleanup(stable_end()), |
| 338 | InnermostEHCleanup(stable_end()), |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 339 | CatchDepth(0), NextEHDestIndex(FirstEHDestIndex) {} |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 340 | ~EHScopeStack() { delete[] StartOfBuffer; } |
| 341 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 342 | // Variadic templates would make this not terrible. |
| 343 | |
| 344 | /// Push a lazily-created cleanup on the stack. |
John McCall | 8e3f861 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 345 | template <class T> |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 346 | void pushCleanup(CleanupKind Kind) { |
| 347 | void *Buffer = pushCleanup(Kind, sizeof(T)); |
| 348 | Cleanup *Obj = new(Buffer) T(); |
John McCall | 8e3f861 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 349 | (void) Obj; |
| 350 | } |
| 351 | |
| 352 | /// Push a lazily-created cleanup on the stack. |
| 353 | template <class T, class A0> |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 354 | void pushCleanup(CleanupKind Kind, A0 a0) { |
| 355 | void *Buffer = pushCleanup(Kind, sizeof(T)); |
| 356 | Cleanup *Obj = new(Buffer) T(a0); |
John McCall | 8e3f861 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 357 | (void) Obj; |
| 358 | } |
| 359 | |
| 360 | /// Push a lazily-created cleanup on the stack. |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 361 | template <class T, class A0, class A1> |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 362 | void pushCleanup(CleanupKind Kind, A0 a0, A1 a1) { |
| 363 | void *Buffer = pushCleanup(Kind, sizeof(T)); |
| 364 | Cleanup *Obj = new(Buffer) T(a0, a1); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 365 | (void) Obj; |
| 366 | } |
| 367 | |
| 368 | /// Push a lazily-created cleanup on the stack. |
| 369 | template <class T, class A0, class A1, class A2> |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 370 | void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2) { |
| 371 | void *Buffer = pushCleanup(Kind, sizeof(T)); |
| 372 | Cleanup *Obj = new(Buffer) T(a0, a1, a2); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 373 | (void) Obj; |
| 374 | } |
| 375 | |
| 376 | /// Push a lazily-created cleanup on the stack. |
| 377 | template <class T, class A0, class A1, class A2, class A3> |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 378 | void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3) { |
| 379 | void *Buffer = pushCleanup(Kind, sizeof(T)); |
| 380 | Cleanup *Obj = new(Buffer) T(a0, a1, a2, a3); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 381 | (void) Obj; |
| 382 | } |
| 383 | |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 384 | /// Push a lazily-created cleanup on the stack. |
| 385 | template <class T, class A0, class A1, class A2, class A3, class A4> |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 386 | void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { |
| 387 | void *Buffer = pushCleanup(Kind, sizeof(T)); |
| 388 | Cleanup *Obj = new(Buffer) T(a0, a1, a2, a3, a4); |
John McCall | 7719971 | 2010-07-21 05:47:49 +0000 | [diff] [blame] | 389 | (void) Obj; |
| 390 | } |
| 391 | |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 392 | // Feel free to add more variants of the following: |
| 393 | |
| 394 | /// Push a cleanup with non-constant storage requirements on the |
| 395 | /// stack. The cleanup type must provide an additional static method: |
| 396 | /// static size_t getExtraSize(size_t); |
| 397 | /// The argument to this method will be the value N, which will also |
| 398 | /// be passed as the first argument to the constructor. |
| 399 | /// |
| 400 | /// The data stored in the extra storage must obey the same |
| 401 | /// restrictions as normal cleanup member data. |
| 402 | /// |
| 403 | /// The pointer returned from this method is valid until the cleanup |
| 404 | /// stack is modified. |
| 405 | template <class T, class A0, class A1, class A2> |
| 406 | T *pushCleanupWithExtra(CleanupKind Kind, size_t N, A0 a0, A1 a1, A2 a2) { |
| 407 | void *Buffer = pushCleanup(Kind, sizeof(T) + T::getExtraSize(N)); |
| 408 | return new (Buffer) T(N, a0, a1, a2); |
| 409 | } |
| 410 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 411 | /// Pops a cleanup scope off the stack. This should only be called |
| 412 | /// by CodeGenFunction::PopCleanupBlock. |
| 413 | void popCleanup(); |
| 414 | |
| 415 | /// Push a set of catch handlers on the stack. The catch is |
| 416 | /// uninitialized and will need to have the given number of handlers |
| 417 | /// set on it. |
| 418 | class EHCatchScope *pushCatch(unsigned NumHandlers); |
| 419 | |
| 420 | /// Pops a catch scope off the stack. |
| 421 | void popCatch(); |
| 422 | |
| 423 | /// Push an exceptions filter on the stack. |
| 424 | class EHFilterScope *pushFilter(unsigned NumFilters); |
| 425 | |
| 426 | /// Pops an exceptions filter off the stack. |
| 427 | void popFilter(); |
| 428 | |
| 429 | /// Push a terminate handler on the stack. |
| 430 | void pushTerminate(); |
| 431 | |
| 432 | /// Pops a terminate handler off the stack. |
| 433 | void popTerminate(); |
| 434 | |
| 435 | /// Determines whether the exception-scopes stack is empty. |
| 436 | bool empty() const { return StartOfData == EndOfBuffer; } |
| 437 | |
| 438 | bool requiresLandingPad() const { |
| 439 | return (CatchDepth || hasEHCleanups()); |
| 440 | } |
| 441 | |
| 442 | /// Determines whether there are any normal cleanups on the stack. |
| 443 | bool hasNormalCleanups() const { |
| 444 | return InnermostNormalCleanup != stable_end(); |
| 445 | } |
| 446 | |
| 447 | /// Returns the innermost normal cleanup on the stack, or |
| 448 | /// stable_end() if there are no normal cleanups. |
| 449 | stable_iterator getInnermostNormalCleanup() const { |
| 450 | return InnermostNormalCleanup; |
| 451 | } |
John McCall | 838d796 | 2010-08-14 07:46:19 +0000 | [diff] [blame] | 452 | stable_iterator getInnermostActiveNormalCleanup() const; // CGException.h |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 453 | |
| 454 | /// Determines whether there are any EH cleanups on the stack. |
| 455 | bool hasEHCleanups() const { |
| 456 | return InnermostEHCleanup != stable_end(); |
| 457 | } |
| 458 | |
| 459 | /// Returns the innermost EH cleanup on the stack, or stable_end() |
| 460 | /// if there are no EH cleanups. |
| 461 | stable_iterator getInnermostEHCleanup() const { |
| 462 | return InnermostEHCleanup; |
| 463 | } |
John McCall | 838d796 | 2010-08-14 07:46:19 +0000 | [diff] [blame] | 464 | stable_iterator getInnermostActiveEHCleanup() const; // CGException.h |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 465 | |
| 466 | /// An unstable reference to a scope-stack depth. Invalidated by |
| 467 | /// pushes but not pops. |
| 468 | class iterator; |
| 469 | |
| 470 | /// Returns an iterator pointing to the innermost EH scope. |
| 471 | iterator begin() const; |
| 472 | |
| 473 | /// Returns an iterator pointing to the outermost EH scope. |
| 474 | iterator end() const; |
| 475 | |
| 476 | /// Create a stable reference to the top of the EH stack. The |
| 477 | /// returned reference is valid until that scope is popped off the |
| 478 | /// stack. |
| 479 | stable_iterator stable_begin() const { |
| 480 | return stable_iterator(EndOfBuffer - StartOfData); |
| 481 | } |
| 482 | |
| 483 | /// Create a stable reference to the bottom of the EH stack. |
| 484 | static stable_iterator stable_end() { |
| 485 | return stable_iterator(0); |
| 486 | } |
| 487 | |
| 488 | /// Translates an iterator into a stable_iterator. |
| 489 | stable_iterator stabilize(iterator it) const; |
| 490 | |
| 491 | /// Finds the nearest cleanup enclosing the given iterator. |
| 492 | /// Returns stable_iterator::invalid() if there are no such cleanups. |
| 493 | stable_iterator getEnclosingEHCleanup(iterator it) const; |
| 494 | |
| 495 | /// Turn a stable reference to a scope depth into a unstable pointer |
| 496 | /// to the EH stack. |
| 497 | iterator find(stable_iterator save) const; |
| 498 | |
| 499 | /// Removes the cleanup pointed to by the given stable_iterator. |
| 500 | void removeCleanup(stable_iterator save); |
| 501 | |
| 502 | /// Add a branch fixup to the current cleanup scope. |
| 503 | BranchFixup &addBranchFixup() { |
| 504 | assert(hasNormalCleanups() && "adding fixup in scope without cleanups"); |
| 505 | BranchFixups.push_back(BranchFixup()); |
| 506 | return BranchFixups.back(); |
| 507 | } |
| 508 | |
| 509 | unsigned getNumBranchFixups() const { return BranchFixups.size(); } |
| 510 | BranchFixup &getBranchFixup(unsigned I) { |
| 511 | assert(I < getNumBranchFixups()); |
| 512 | return BranchFixups[I]; |
| 513 | } |
| 514 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 515 | /// Pops lazily-removed fixups from the end of the list. This |
| 516 | /// should only be called by procedures which have just popped a |
| 517 | /// cleanup or resolved one or more fixups. |
| 518 | void popNullFixups(); |
| 519 | |
| 520 | /// Clears the branch-fixups list. This should only be called by |
John McCall | 8abdbd8 | 2010-09-18 02:24:39 +0000 | [diff] [blame] | 521 | /// ResolveAllBranchFixups. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 522 | void clearFixups() { BranchFixups.clear(); } |
| 523 | |
| 524 | /// Gets the next EH destination index. |
| 525 | unsigned getNextEHDestIndex() { return NextEHDestIndex++; } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 526 | }; |
| 527 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 528 | /// CodeGenFunction - This class organizes the per-function state that is used |
| 529 | /// while generating LLVM code. |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 530 | class CodeGenFunction : public CodeGenTypeCache { |
Anders Carlsson | 8a219ce | 2009-02-24 04:21:31 +0000 | [diff] [blame] | 531 | CodeGenFunction(const CodeGenFunction&); // DO NOT IMPLEMENT |
| 532 | void operator=(const CodeGenFunction&); // DO NOT IMPLEMENT |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 533 | |
| 534 | friend class CGCXXABI; |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 535 | public: |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 536 | /// A jump destination is an abstract label, branching to which may |
| 537 | /// require a jump out through normal cleanups. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 538 | struct JumpDest { |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 539 | JumpDest() : Block(0), ScopeDepth(), Index(0) {} |
| 540 | JumpDest(llvm::BasicBlock *Block, |
| 541 | EHScopeStack::stable_iterator Depth, |
| 542 | unsigned Index) |
| 543 | : Block(Block), ScopeDepth(Depth), Index(Index) {} |
| 544 | |
| 545 | bool isValid() const { return Block != 0; } |
| 546 | llvm::BasicBlock *getBlock() const { return Block; } |
| 547 | EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; } |
| 548 | unsigned getDestIndex() const { return Index; } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 549 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 550 | private: |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 551 | llvm::BasicBlock *Block; |
| 552 | EHScopeStack::stable_iterator ScopeDepth; |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 553 | unsigned Index; |
| 554 | }; |
| 555 | |
| 556 | /// An unwind destination is an abstract label, branching to which |
| 557 | /// may require a jump out through EH cleanups. |
| 558 | struct UnwindDest { |
| 559 | UnwindDest() : Block(0), ScopeDepth(), Index(0) {} |
| 560 | UnwindDest(llvm::BasicBlock *Block, |
| 561 | EHScopeStack::stable_iterator Depth, |
| 562 | unsigned Index) |
| 563 | : Block(Block), ScopeDepth(Depth), Index(Index) {} |
| 564 | |
| 565 | bool isValid() const { return Block != 0; } |
| 566 | llvm::BasicBlock *getBlock() const { return Block; } |
| 567 | EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; } |
| 568 | unsigned getDestIndex() const { return Index; } |
| 569 | |
| 570 | private: |
| 571 | llvm::BasicBlock *Block; |
| 572 | EHScopeStack::stable_iterator ScopeDepth; |
| 573 | unsigned Index; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 574 | }; |
| 575 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 576 | CodeGenModule &CGM; // Per-module state. |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 577 | const TargetInfo &Target; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 578 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 579 | typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 580 | CGBuilderTy Builder; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 581 | |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 582 | /// CurFuncDecl - Holds the Decl for the current function or ObjC method. |
| 583 | /// This excludes BlockDecls. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 584 | const Decl *CurFuncDecl; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 585 | /// CurCodeDecl - This is the inner-most code context, which includes blocks. |
| 586 | const Decl *CurCodeDecl; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 587 | const CGFunctionInfo *CurFnInfo; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 588 | QualType FnRetTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 589 | llvm::Function *CurFn; |
| 590 | |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 591 | /// CurGD - The GlobalDecl for the current function being compiled. |
| 592 | GlobalDecl CurGD; |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 593 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 594 | /// PrologueCleanupDepth - The cleanup depth enclosing all the |
| 595 | /// cleanups associated with the parameters. |
| 596 | EHScopeStack::stable_iterator PrologueCleanupDepth; |
| 597 | |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 598 | /// ReturnBlock - Unified return block. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 599 | JumpDest ReturnBlock; |
| 600 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 601 | /// ReturnValue - The temporary alloca to hold the return value. This is null |
| 602 | /// iff the function has no return value. |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 603 | llvm::Value *ReturnValue; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 604 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 605 | /// RethrowBlock - Unified rethrow block. |
| 606 | UnwindDest RethrowBlock; |
| 607 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 608 | /// AllocaInsertPoint - This is an instruction in the entry block before which |
| 609 | /// we prefer to insert allocas. |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 610 | llvm::AssertingVH<llvm::Instruction> AllocaInsertPt; |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 611 | |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 612 | bool CatchUndefined; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 613 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 614 | /// In ARC, whether we should autorelease the return value. |
| 615 | bool AutoreleaseResult; |
| 616 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 617 | const CodeGen::CGBlockInfo *BlockInfo; |
| 618 | llvm::Value *BlockPointer; |
| 619 | |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 620 | /// \brief A mapping from NRVO variables to the flags used to indicate |
| 621 | /// when the NRVO has been applied to this variable. |
| 622 | llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 623 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 624 | EHScopeStack EHStack; |
| 625 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 626 | /// i32s containing the indexes of the cleanup destinations. |
| 627 | llvm::AllocaInst *NormalCleanupDest; |
| 628 | llvm::AllocaInst *EHCleanupDest; |
| 629 | |
| 630 | unsigned NextCleanupDestIndex; |
| 631 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 632 | /// The exception slot. All landing pads write the current |
| 633 | /// exception pointer into this alloca. |
| 634 | llvm::Value *ExceptionSlot; |
| 635 | |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 636 | /// The selector slot. Under the MandatoryCleanup model, all |
| 637 | /// landing pads write the current selector value into this alloca. |
| 638 | llvm::AllocaInst *EHSelectorSlot; |
| 639 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 640 | /// Emits a landing pad for the current EH stack. |
| 641 | llvm::BasicBlock *EmitLandingPad(); |
| 642 | |
| 643 | llvm::BasicBlock *getInvokeDestImpl(); |
| 644 | |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 645 | /// Set up the last cleaup that was pushed as a conditional |
| 646 | /// full-expression cleanup. |
| 647 | void initFullExprCleanup(); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 648 | |
| 649 | template <class T> |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 650 | typename DominatingValue<T>::saved_type saveValueInCond(T value) { |
| 651 | return DominatingValue<T>::save(*this, value); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 654 | public: |
Anders Carlsson | fa1f756 | 2009-02-10 06:07:49 +0000 | [diff] [blame] | 655 | /// ObjCEHValueStack - Stack of Objective-C exception values, used for |
| 656 | /// rethrows. |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 657 | llvm::SmallVector<llvm::Value*, 8> ObjCEHValueStack; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 658 | |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 659 | /// A class controlling the emission of a finally block. |
| 660 | class FinallyInfo { |
| 661 | /// Where the catchall's edge through the cleanup should go. |
| 662 | JumpDest RethrowDest; |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 663 | |
John McCall | d768e9d | 2011-06-22 02:32:12 +0000 | [diff] [blame] | 664 | /// A function to call to enter the catch. |
| 665 | llvm::Constant *BeginCatchFn; |
| 666 | |
| 667 | /// An i1 variable indicating whether or not the @finally is |
| 668 | /// running for an exception. |
| 669 | llvm::AllocaInst *ForEHVar; |
| 670 | |
| 671 | /// An i8* variable into which the exception pointer to rethrow |
| 672 | /// has been saved. |
| 673 | llvm::AllocaInst *SavedExnVar; |
| 674 | |
| 675 | public: |
| 676 | void enter(CodeGenFunction &CGF, const Stmt *Finally, |
| 677 | llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn, |
| 678 | llvm::Constant *rethrowFn); |
| 679 | void exit(CodeGenFunction &CGF); |
| 680 | }; |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 681 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 682 | /// pushFullExprCleanup - Push a cleanup to be run at the end of the |
| 683 | /// current full-expression. Safe against the possibility that |
| 684 | /// we're currently inside a conditionally-evaluated expression. |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 685 | template <class T, class A0> |
| 686 | void pushFullExprCleanup(CleanupKind kind, A0 a0) { |
| 687 | // If we're not in a conditional branch, or if none of the |
| 688 | // arguments requires saving, then use the unconditional cleanup. |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 689 | if (!isInConditionalBranch()) |
| 690 | return EHStack.pushCleanup<T>(kind, a0); |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 691 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 692 | typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0); |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 693 | |
| 694 | typedef EHScopeStack::ConditionalCleanup1<T, A0> CleanupType; |
| 695 | EHStack.pushCleanup<CleanupType>(kind, a0_saved); |
| 696 | initFullExprCleanup(); |
| 697 | } |
| 698 | |
| 699 | /// pushFullExprCleanup - Push a cleanup to be run at the end of the |
| 700 | /// current full-expression. Safe against the possibility that |
| 701 | /// we're currently inside a conditionally-evaluated expression. |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 702 | template <class T, class A0, class A1> |
| 703 | void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) { |
| 704 | // If we're not in a conditional branch, or if none of the |
| 705 | // arguments requires saving, then use the unconditional cleanup. |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 706 | if (!isInConditionalBranch()) |
| 707 | return EHStack.pushCleanup<T>(kind, a0, a1); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 708 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 709 | typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0); |
| 710 | typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 711 | |
| 712 | typedef EHScopeStack::ConditionalCleanup2<T, A0, A1> CleanupType; |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 713 | EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved); |
| 714 | initFullExprCleanup(); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 717 | /// pushFullExprCleanup - Push a cleanup to be run at the end of the |
| 718 | /// current full-expression. Safe against the possibility that |
| 719 | /// we're currently inside a conditionally-evaluated expression. |
| 720 | template <class T, class A0, class A1, class A2> |
| 721 | void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2) { |
| 722 | // If we're not in a conditional branch, or if none of the |
| 723 | // arguments requires saving, then use the unconditional cleanup. |
| 724 | if (!isInConditionalBranch()) { |
John McCall | c4a1a84 | 2011-07-12 00:15:30 +0000 | [diff] [blame] | 725 | return EHStack.pushCleanup<T>(kind, a0, a1, a2); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0); |
| 729 | typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1); |
| 730 | typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2); |
| 731 | |
| 732 | typedef EHScopeStack::ConditionalCleanup3<T, A0, A1, A2> CleanupType; |
| 733 | EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, a2_saved); |
| 734 | initFullExprCleanup(); |
| 735 | } |
| 736 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 737 | /// pushFullExprCleanup - Push a cleanup to be run at the end of the |
| 738 | /// current full-expression. Safe against the possibility that |
| 739 | /// we're currently inside a conditionally-evaluated expression. |
| 740 | template <class T, class A0, class A1, class A2, class A3> |
| 741 | void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2, A3 a3) { |
| 742 | // If we're not in a conditional branch, or if none of the |
| 743 | // arguments requires saving, then use the unconditional cleanup. |
| 744 | if (!isInConditionalBranch()) { |
| 745 | return EHStack.pushCleanup<T>(kind, a0, a1, a2, a3); |
| 746 | } |
| 747 | |
| 748 | typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0); |
| 749 | typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1); |
| 750 | typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2); |
| 751 | typename DominatingValue<A3>::saved_type a3_saved = saveValueInCond(a3); |
| 752 | |
| 753 | typedef EHScopeStack::ConditionalCleanup4<T, A0, A1, A2, A3> CleanupType; |
| 754 | EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, |
| 755 | a2_saved, a3_saved); |
| 756 | initFullExprCleanup(); |
| 757 | } |
| 758 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 759 | /// PushDestructorCleanup - Push a cleanup to call the |
| 760 | /// complete-object destructor of an object of the given type at the |
| 761 | /// given address. Does nothing if T is not a C++ class type with a |
| 762 | /// non-trivial destructor. |
| 763 | void PushDestructorCleanup(QualType T, llvm::Value *Addr); |
| 764 | |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 765 | /// PushDestructorCleanup - Push a cleanup to call the |
| 766 | /// complete-object variant of the given destructor on the object at |
| 767 | /// the given address. |
| 768 | void PushDestructorCleanup(const CXXDestructorDecl *Dtor, |
| 769 | llvm::Value *Addr); |
| 770 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 771 | /// PopCleanupBlock - Will pop the cleanup entry on the stack and |
| 772 | /// process all branch fixups. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 773 | void PopCleanupBlock(bool FallThroughIsBranchThrough = false); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 774 | |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 775 | /// DeactivateCleanupBlock - Deactivates the given cleanup block. |
| 776 | /// The block cannot be reactivated. Pops it if it's the top of the |
| 777 | /// stack. |
| 778 | void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup); |
| 779 | |
| 780 | /// ActivateCleanupBlock - Activates an initially-inactive cleanup. |
| 781 | /// Cannot be used to resurrect a deactivated cleanup. |
| 782 | void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup); |
John McCall | cd2d2b7 | 2010-08-13 21:20:51 +0000 | [diff] [blame] | 783 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 784 | /// \brief Enters a new scope for capturing cleanups, all of which |
| 785 | /// will be executed once the scope is exited. |
| 786 | class RunCleanupsScope { |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 787 | CodeGenFunction& CGF; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 788 | EHScopeStack::stable_iterator CleanupStackDepth; |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 789 | bool OldDidCallStackSave; |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 790 | bool PerformCleanup; |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 791 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 792 | RunCleanupsScope(const RunCleanupsScope &); // DO NOT IMPLEMENT |
| 793 | RunCleanupsScope &operator=(const RunCleanupsScope &); // DO NOT IMPLEMENT |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 794 | |
| 795 | public: |
| 796 | /// \brief Enter a new cleanup scope. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 797 | explicit RunCleanupsScope(CodeGenFunction &CGF) |
| 798 | : CGF(CGF), PerformCleanup(true) |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 799 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 800 | CleanupStackDepth = CGF.EHStack.stable_begin(); |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 801 | OldDidCallStackSave = CGF.DidCallStackSave; |
Argyrios Kyrtzidis | 4ada2ca | 2010-09-14 00:42:34 +0000 | [diff] [blame] | 802 | CGF.DidCallStackSave = false; |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /// \brief Exit this cleanup scope, emitting any accumulated |
| 806 | /// cleanups. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 807 | ~RunCleanupsScope() { |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 808 | if (PerformCleanup) { |
| 809 | CGF.DidCallStackSave = OldDidCallStackSave; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 810 | CGF.PopCleanupBlocks(CleanupStackDepth); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
| 814 | /// \brief Determine whether this scope requires any cleanups. |
| 815 | bool requiresCleanups() const { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 816 | return CGF.EHStack.stable_begin() != CleanupStackDepth; |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /// \brief Force the emission of cleanups now, instead of waiting |
| 820 | /// until this object is destroyed. |
| 821 | void ForceCleanup() { |
| 822 | assert(PerformCleanup && "Already forced cleanup"); |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 823 | CGF.DidCallStackSave = OldDidCallStackSave; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 824 | CGF.PopCleanupBlocks(CleanupStackDepth); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 825 | PerformCleanup = false; |
Douglas Gregor | 01234bb | 2009-11-24 16:43:22 +0000 | [diff] [blame] | 826 | } |
| 827 | }; |
| 828 | |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 829 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 830 | /// PopCleanupBlocks - Takes the old cleanup stack size and emits |
| 831 | /// the cleanup blocks that have been added. |
| 832 | void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize); |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 833 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 834 | void ResolveBranchFixups(llvm::BasicBlock *Target); |
| 835 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 836 | /// The given basic block lies in the current EH scope, but may be a |
| 837 | /// target of a potentially scope-crossing jump; get a stable handle |
| 838 | /// to which we can perform this jump later. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 839 | JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) { |
John McCall | 413e677 | 2010-07-28 01:07:35 +0000 | [diff] [blame] | 840 | return JumpDest(Target, |
| 841 | EHStack.getInnermostNormalCleanup(), |
| 842 | NextCleanupDestIndex++); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 843 | } |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 844 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 845 | /// The given basic block lies in the current EH scope, but may be a |
| 846 | /// target of a potentially scope-crossing jump; get a stable handle |
| 847 | /// to which we can perform this jump later. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 848 | JumpDest getJumpDestInCurrentScope(llvm::StringRef Name = llvm::StringRef()) { |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 849 | return getJumpDestInCurrentScope(createBasicBlock(Name)); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /// EmitBranchThroughCleanup - Emit a branch from the current insert |
| 853 | /// block through the normal cleanup handling code (if any) and then |
| 854 | /// on to \arg Dest. |
| 855 | void EmitBranchThroughCleanup(JumpDest Dest); |
Chris Lattner | b11f919 | 2011-04-17 00:54:30 +0000 | [diff] [blame] | 856 | |
| 857 | /// isObviouslyBranchWithoutCleanups - Return true if a branch to the |
| 858 | /// specified destination obviously has no cleanups to run. 'false' is always |
| 859 | /// a conservatively correct answer for this method. |
| 860 | bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 861 | |
| 862 | /// EmitBranchThroughEHCleanup - Emit a branch from the current |
| 863 | /// insert block through the EH cleanup handling code (if any) and |
| 864 | /// then on to \arg Dest. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 865 | void EmitBranchThroughEHCleanup(UnwindDest Dest); |
| 866 | |
| 867 | /// getRethrowDest - Returns the unified outermost-scope rethrow |
| 868 | /// destination. |
| 869 | UnwindDest getRethrowDest(); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 870 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 871 | /// An object to manage conditionally-evaluated expressions. |
| 872 | class ConditionalEvaluation { |
| 873 | llvm::BasicBlock *StartBB; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 874 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 875 | public: |
| 876 | ConditionalEvaluation(CodeGenFunction &CGF) |
| 877 | : StartBB(CGF.Builder.GetInsertBlock()) {} |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 878 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 879 | void begin(CodeGenFunction &CGF) { |
| 880 | assert(CGF.OutermostConditional != this); |
| 881 | if (!CGF.OutermostConditional) |
| 882 | CGF.OutermostConditional = this; |
| 883 | } |
| 884 | |
| 885 | void end(CodeGenFunction &CGF) { |
| 886 | assert(CGF.OutermostConditional != 0); |
| 887 | if (CGF.OutermostConditional == this) |
| 888 | CGF.OutermostConditional = 0; |
| 889 | } |
| 890 | |
| 891 | /// Returns a block which will be executed prior to each |
| 892 | /// evaluation of the conditional code. |
| 893 | llvm::BasicBlock *getStartingBlock() const { |
| 894 | return StartBB; |
| 895 | } |
| 896 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 898 | /// isInConditionalBranch - Return true if we're currently emitting |
| 899 | /// one branch or the other of a conditional expression. |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 900 | bool isInConditionalBranch() const { return OutermostConditional != 0; } |
| 901 | |
| 902 | /// An RAII object to record that we're evaluating a statement |
| 903 | /// expression. |
| 904 | class StmtExprEvaluation { |
| 905 | CodeGenFunction &CGF; |
| 906 | |
| 907 | /// We have to save the outermost conditional: cleanups in a |
| 908 | /// statement expression aren't conditional just because the |
| 909 | /// StmtExpr is. |
| 910 | ConditionalEvaluation *SavedOutermostConditional; |
| 911 | |
| 912 | public: |
| 913 | StmtExprEvaluation(CodeGenFunction &CGF) |
| 914 | : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) { |
| 915 | CGF.OutermostConditional = 0; |
| 916 | } |
| 917 | |
| 918 | ~StmtExprEvaluation() { |
| 919 | CGF.OutermostConditional = SavedOutermostConditional; |
| 920 | CGF.EnsureInsertPoint(); |
| 921 | } |
| 922 | }; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 923 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 924 | /// An object which temporarily prevents a value from being |
| 925 | /// destroyed by aggressive peephole optimizations that assume that |
| 926 | /// all uses of a value have been realized in the IR. |
| 927 | class PeepholeProtection { |
| 928 | llvm::Instruction *Inst; |
| 929 | friend class CodeGenFunction; |
| 930 | |
| 931 | public: |
| 932 | PeepholeProtection() : Inst(0) {} |
| 933 | }; |
| 934 | |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 935 | /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr. |
| 936 | class OpaqueValueMapping { |
| 937 | CodeGenFunction &CGF; |
| 938 | const OpaqueValueExpr *OpaqueValue; |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 939 | bool BoundLValue; |
| 940 | CodeGenFunction::PeepholeProtection Protection; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 941 | |
| 942 | public: |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 943 | static bool shouldBindAsLValue(const Expr *expr) { |
| 944 | return expr->isGLValue() || expr->getType()->isRecordType(); |
| 945 | } |
| 946 | |
| 947 | /// Build the opaque value mapping for the given conditional |
| 948 | /// operator if it's the GNU ?: extension. This is a common |
| 949 | /// enough pattern that the convenience operator is really |
| 950 | /// helpful. |
| 951 | /// |
| 952 | OpaqueValueMapping(CodeGenFunction &CGF, |
| 953 | const AbstractConditionalOperator *op) : CGF(CGF) { |
| 954 | if (isa<ConditionalOperator>(op)) { |
| 955 | OpaqueValue = 0; |
| 956 | BoundLValue = false; |
| 957 | return; |
| 958 | } |
| 959 | |
| 960 | const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op); |
| 961 | init(e->getOpaqueValue(), e->getCommon()); |
| 962 | } |
| 963 | |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 964 | OpaqueValueMapping(CodeGenFunction &CGF, |
| 965 | const OpaqueValueExpr *opaqueValue, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 966 | LValue lvalue) |
| 967 | : CGF(CGF), OpaqueValue(opaqueValue), BoundLValue(true) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 968 | assert(opaqueValue && "no opaque value expression!"); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 969 | assert(shouldBindAsLValue(opaqueValue)); |
| 970 | initLValue(lvalue); |
| 971 | } |
| 972 | |
| 973 | OpaqueValueMapping(CodeGenFunction &CGF, |
| 974 | const OpaqueValueExpr *opaqueValue, |
| 975 | RValue rvalue) |
| 976 | : CGF(CGF), OpaqueValue(opaqueValue), BoundLValue(false) { |
| 977 | assert(opaqueValue && "no opaque value expression!"); |
| 978 | assert(!shouldBindAsLValue(opaqueValue)); |
| 979 | initRValue(rvalue); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | void pop() { |
| 983 | assert(OpaqueValue && "mapping already popped!"); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 984 | popImpl(); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 985 | OpaqueValue = 0; |
| 986 | } |
| 987 | |
| 988 | ~OpaqueValueMapping() { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 989 | if (OpaqueValue) popImpl(); |
| 990 | } |
| 991 | |
| 992 | private: |
| 993 | void popImpl() { |
| 994 | if (BoundLValue) |
| 995 | CGF.OpaqueLValues.erase(OpaqueValue); |
| 996 | else { |
| 997 | CGF.OpaqueRValues.erase(OpaqueValue); |
| 998 | CGF.unprotectFromPeepholes(Protection); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | void init(const OpaqueValueExpr *ov, const Expr *e) { |
| 1003 | OpaqueValue = ov; |
| 1004 | BoundLValue = shouldBindAsLValue(ov); |
| 1005 | assert(BoundLValue == shouldBindAsLValue(e) |
| 1006 | && "inconsistent expression value kinds!"); |
| 1007 | if (BoundLValue) |
| 1008 | initLValue(CGF.EmitLValue(e)); |
| 1009 | else |
| 1010 | initRValue(CGF.EmitAnyExpr(e)); |
| 1011 | } |
| 1012 | |
| 1013 | void initLValue(const LValue &lv) { |
| 1014 | CGF.OpaqueLValues.insert(std::make_pair(OpaqueValue, lv)); |
| 1015 | } |
| 1016 | |
| 1017 | void initRValue(const RValue &rv) { |
| 1018 | // Work around an extremely aggressive peephole optimization in |
| 1019 | // EmitScalarConversion which assumes that all other uses of a |
| 1020 | // value are extant. |
| 1021 | Protection = CGF.protectFromPeepholes(rv); |
| 1022 | CGF.OpaqueRValues.insert(std::make_pair(OpaqueValue, rv)); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 1023 | } |
| 1024 | }; |
Fariborz Jahanian | e220455 | 2010-11-16 19:29:39 +0000 | [diff] [blame] | 1025 | |
| 1026 | /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field |
| 1027 | /// number that holds the value. |
| 1028 | unsigned getByRefValueLLVMField(const ValueDecl *VD) const; |
Fariborz Jahanian | 52a80e1 | 2011-01-26 23:08:27 +0000 | [diff] [blame] | 1029 | |
| 1030 | /// BuildBlockByrefAddress - Computes address location of the |
| 1031 | /// variable which is declared as __block. |
| 1032 | llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr, |
| 1033 | const VarDecl *V); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1034 | private: |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 1035 | CGDebugInfo *DebugInfo; |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 1036 | bool DisableDebugInfo; |
Mike Stump | 09429b9 | 2009-02-17 17:00:02 +0000 | [diff] [blame] | 1037 | |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 1038 | /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid |
| 1039 | /// calling llvm.stacksave for multiple VLAs in the same scope. |
| 1040 | bool DidCallStackSave; |
| 1041 | |
Mike Stump | f71d232 | 2009-11-30 20:08:49 +0000 | [diff] [blame] | 1042 | /// IndirectBranch - The first time an indirect goto is seen we create a block |
| 1043 | /// with an indirect branch. Every time we see the address of a label taken, |
| 1044 | /// we add the label to the indirect goto. Every subsequent indirect goto is |
| 1045 | /// codegen'd as a jump to the IndirectBranch's basic block. |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 1046 | llvm::IndirectBrInst *IndirectBranch; |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 1047 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1048 | /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C |
| 1049 | /// decls. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1050 | typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy; |
| 1051 | DeclMapTy LocalDeclMap; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1052 | |
| 1053 | /// 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] | 1054 | llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1055 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1056 | // BreakContinueStack - This keeps track of where break and continue |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 1057 | // statements should jump to. |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 1058 | struct BreakContinue { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1059 | BreakContinue(JumpDest Break, JumpDest Continue) |
| 1060 | : BreakBlock(Break), ContinueBlock(Continue) {} |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1061 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1062 | JumpDest BreakBlock; |
| 1063 | JumpDest ContinueBlock; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1064 | }; |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 1065 | llvm::SmallVector<BreakContinue, 8> BreakContinueStack; |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1066 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1067 | /// SwitchInsn - This is nearest current switch instruction. It is null if if |
| 1068 | /// current context is not in a switch. |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1069 | llvm::SwitchInst *SwitchInsn; |
| 1070 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1071 | /// CaseRangeBlock - This block holds if condition check for last case |
Devang Patel | 80fd5f9 | 2007-10-09 17:08:50 +0000 | [diff] [blame] | 1072 | /// statement range in current switch instruction. |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1073 | llvm::BasicBlock *CaseRangeBlock; |
| 1074 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1075 | /// OpaqueLValues - Keeps track of the current set of opaque value |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 1076 | /// expressions. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1077 | llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues; |
| 1078 | llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 1079 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1080 | // VLASizeMap - This keeps track of the associated size for each VLA type. |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 1081 | // We track this by the size expression rather than the type itself because |
| 1082 | // in certain situations, like a const qualifier applied to an VLA typedef, |
| 1083 | // multiple VLA types can share the same size expression. |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1084 | // FIXME: Maybe this could be a stack of maps that is pushed/popped as we |
| 1085 | // enter/leave scopes. |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 1086 | llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1087 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1088 | /// A block containing a single 'unreachable' instruction. Created |
| 1089 | /// lazily by getUnreachableBlock(). |
| 1090 | llvm::BasicBlock *UnreachableBlock; |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1091 | |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 1092 | /// CXXThisDecl - When generating code for a C++ member function, |
| 1093 | /// this will hold the implicit 'this' declaration. |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 1094 | ImplicitParamDecl *CXXThisDecl; |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 1095 | llvm::Value *CXXThisValue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 1097 | /// CXXVTTDecl - When generating code for a base object constructor or |
| 1098 | /// base object destructor with virtual bases, this will hold the implicit |
| 1099 | /// VTT parameter. |
| 1100 | ImplicitParamDecl *CXXVTTDecl; |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 1101 | llvm::Value *CXXVTTValue; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1102 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 1103 | /// OutermostConditional - Points to the outermost active |
| 1104 | /// conditional control. This is used so that we know if a |
| 1105 | /// temporary should be destroyed conditionally. |
| 1106 | ConditionalEvaluation *OutermostConditional; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 1108 | |
| 1109 | /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM |
| 1110 | /// type as well as the field number that contains the actual data. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1111 | llvm::DenseMap<const ValueDecl *, std::pair<const llvm::Type *, |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 1112 | unsigned> > ByRefValueInfo; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1113 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1114 | llvm::BasicBlock *TerminateLandingPad; |
Mike Stump | 182f383 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 1115 | llvm::BasicBlock *TerminateHandler; |
Chris Lattner | 83252dc | 2010-07-20 21:07:09 +0000 | [diff] [blame] | 1116 | llvm::BasicBlock *TrapBB; |
Eli Friedman | 9406705 | 2009-12-10 02:21:21 +0000 | [diff] [blame] | 1117 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1118 | public: |
| 1119 | CodeGenFunction(CodeGenModule &cgm); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1120 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1121 | CodeGenTypes &getTypes() const { return CGM.getTypes(); } |
John McCall | f2aac84 | 2011-05-15 02:34:36 +0000 | [diff] [blame] | 1122 | ASTContext &getContext() const { return CGM.getContext(); } |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 1123 | CGDebugInfo *getDebugInfo() { |
| 1124 | if (DisableDebugInfo) |
| 1125 | return NULL; |
| 1126 | return DebugInfo; |
| 1127 | } |
| 1128 | void disableDebugInfo() { DisableDebugInfo = true; } |
| 1129 | void enableDebugInfo() { DisableDebugInfo = false; } |
| 1130 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1131 | bool shouldUseFusedARCCalls() { |
| 1132 | return CGM.getCodeGenOpts().OptimizationLevel == 0; |
| 1133 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1134 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1135 | const LangOptions &getLangOptions() const { return CGM.getLangOptions(); } |
| 1136 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1137 | /// Returns a pointer to the function's exception object slot, which |
| 1138 | /// is assigned in every landing pad. |
| 1139 | llvm::Value *getExceptionSlot(); |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 1140 | llvm::Value *getEHSelectorSlot(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1141 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1142 | llvm::Value *getNormalCleanupDestSlot(); |
| 1143 | llvm::Value *getEHCleanupDestSlot(); |
| 1144 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1145 | llvm::BasicBlock *getUnreachableBlock() { |
| 1146 | if (!UnreachableBlock) { |
| 1147 | UnreachableBlock = createBasicBlock("unreachable"); |
| 1148 | new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock); |
| 1149 | } |
| 1150 | return UnreachableBlock; |
| 1151 | } |
| 1152 | |
| 1153 | llvm::BasicBlock *getInvokeDest() { |
| 1154 | if (!EHStack.requiresLandingPad()) return 0; |
| 1155 | return getInvokeDestImpl(); |
| 1156 | } |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1157 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1158 | llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); } |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1159 | |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1160 | //===--------------------------------------------------------------------===// |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1161 | // Cleanups |
| 1162 | //===--------------------------------------------------------------------===// |
| 1163 | |
| 1164 | typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty); |
| 1165 | |
John McCall | 2673c68 | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 1166 | void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, |
| 1167 | llvm::Value *arrayEndPointer, |
| 1168 | QualType elementType, |
| 1169 | Destroyer &destroyer); |
| 1170 | void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, |
| 1171 | llvm::Value *arrayEnd, |
| 1172 | QualType elementType, |
| 1173 | Destroyer &destroyer); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1174 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1175 | void pushDestroy(QualType::DestructionKind dtorKind, |
| 1176 | llvm::Value *addr, QualType type); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1177 | void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type, |
John McCall | 2673c68 | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 1178 | Destroyer &destroyer, bool useEHCleanupForArray); |
| 1179 | void emitDestroy(llvm::Value *addr, QualType type, Destroyer &destroyer, |
| 1180 | bool useEHCleanupForArray); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1181 | void emitArrayDestroy(llvm::Value *begin, llvm::Value *end, |
John McCall | 2673c68 | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 1182 | QualType type, Destroyer &destroyer, |
| 1183 | bool useEHCleanup); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1184 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1185 | Destroyer &getDestroyer(QualType::DestructionKind destructionKind); |
| 1186 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1187 | /// Determines whether an EH cleanup is required to destroy a type |
| 1188 | /// with the given destruction kind. |
| 1189 | bool needsEHCleanup(QualType::DestructionKind kind) { |
| 1190 | switch (kind) { |
| 1191 | case QualType::DK_none: |
| 1192 | return false; |
| 1193 | case QualType::DK_cxx_destructor: |
| 1194 | case QualType::DK_objc_weak_lifetime: |
| 1195 | return getLangOptions().Exceptions; |
| 1196 | case QualType::DK_objc_strong_lifetime: |
| 1197 | return getLangOptions().Exceptions && |
| 1198 | CGM.getCodeGenOpts().ObjCAutoRefCountExceptions; |
| 1199 | } |
| 1200 | llvm_unreachable("bad destruction kind"); |
| 1201 | } |
| 1202 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1203 | CleanupKind getCleanupKind(QualType::DestructionKind kind) { |
| 1204 | return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup); |
| 1205 | } |
| 1206 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1207 | //===--------------------------------------------------------------------===// |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1208 | // Objective-C |
| 1209 | //===--------------------------------------------------------------------===// |
| 1210 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1211 | void GenerateObjCMethod(const ObjCMethodDecl *OMD); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1212 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1213 | void StartObjCMethod(const ObjCMethodDecl *MD, |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 1214 | const ObjCContainerDecl *CD, |
| 1215 | SourceLocation StartLoc); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1216 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1217 | /// GenerateObjCGetter - Synthesize an Objective-C property getter function. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1218 | void GenerateObjCGetter(ObjCImplementationDecl *IMP, |
| 1219 | const ObjCPropertyImplDecl *PID); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 1220 | void GenerateObjCGetterBody(ObjCIvarDecl *Ivar, bool IsAtomic, bool IsStrong); |
| 1221 | void GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD, |
| 1222 | ObjCIvarDecl *Ivar); |
| 1223 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 1224 | void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, |
| 1225 | ObjCMethodDecl *MD, bool ctor); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1226 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1227 | /// GenerateObjCSetter - Synthesize an Objective-C property setter function |
| 1228 | /// for the given property. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1229 | void GenerateObjCSetter(ObjCImplementationDecl *IMP, |
| 1230 | const ObjCPropertyImplDecl *PID); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 1231 | bool IndirectObjCSetterArg(const CGFunctionInfo &FI); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 1232 | bool IvarTypeWithAggrGCObjects(QualType Ty); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1233 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1234 | //===--------------------------------------------------------------------===// |
| 1235 | // Block Bits |
| 1236 | //===--------------------------------------------------------------------===// |
| 1237 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1238 | llvm::Value *EmitBlockLiteral(const BlockExpr *); |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 1239 | llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *, |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1240 | const CGBlockInfo &Info, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1241 | const llvm::StructType *, |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1242 | llvm::Constant *BlockVarLayout); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1243 | |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 1244 | llvm::Function *GenerateBlockFunction(GlobalDecl GD, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1245 | const CGBlockInfo &Info, |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 1246 | const Decl *OuterFuncDecl, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1247 | const DeclMapTy &ldm); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1248 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1249 | llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo); |
| 1250 | llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo); |
| 1251 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1252 | void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags); |
| 1253 | |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1254 | class AutoVarEmission; |
| 1255 | |
| 1256 | void emitByrefStructureInit(const AutoVarEmission &emission); |
| 1257 | void enterByrefCleanup(const AutoVarEmission &emission); |
| 1258 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1259 | llvm::Value *LoadBlockStruct() { |
| 1260 | assert(BlockPointer && "no block pointer set!"); |
| 1261 | return BlockPointer; |
| 1262 | } |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1263 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1264 | void AllocateBlockCXXThisPointer(const CXXThisExpr *E); |
| 1265 | void AllocateBlockDecl(const BlockDeclRefExpr *E); |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 1266 | llvm::Value *GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { |
| 1267 | return GetAddrOfBlockDecl(E->getDecl(), E->isByRef()); |
| 1268 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1269 | llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef); |
| 1270 | const llvm::Type *BuildByRefType(const VarDecl *var); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 1271 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1272 | void GenerateCode(GlobalDecl GD, llvm::Function *Fn, |
| 1273 | const CGFunctionInfo &FnInfo); |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 1274 | void StartFunction(GlobalDecl GD, QualType RetTy, |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 1275 | llvm::Function *Fn, |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1276 | const CGFunctionInfo &FnInfo, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 1277 | const FunctionArgList &Args, |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 1278 | SourceLocation StartLoc); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1279 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1280 | void EmitConstructorBody(FunctionArgList &Args); |
| 1281 | void EmitDestructorBody(FunctionArgList &Args); |
| 1282 | void EmitFunctionBody(FunctionArgList &Args); |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 1283 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1284 | /// EmitReturnBlock - Emit the unified return block, trying to avoid its |
| 1285 | /// emission when possible. |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 1286 | void EmitReturnBlock(); |
| 1287 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1288 | /// FinishFunction - Complete IR generation of the current function. It is |
| 1289 | /// 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] | 1290 | void FinishFunction(SourceLocation EndLoc=SourceLocation()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1291 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 1292 | /// GenerateThunk - Generate a thunk for the given method. |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1293 | void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo, |
| 1294 | GlobalDecl GD, const ThunkInfo &Thunk); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1295 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 1296 | void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo, |
| 1297 | GlobalDecl GD, const ThunkInfo &Thunk); |
| 1298 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1299 | void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type, |
| 1300 | FunctionArgList &Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1302 | /// InitializeVTablePointer - Initialize the vtable pointer of the given |
| 1303 | /// subobject. |
| 1304 | /// |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1305 | void InitializeVTablePointer(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1306 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1307 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1308 | llvm::Constant *VTable, |
| 1309 | const CXXRecordDecl *VTableClass); |
| 1310 | |
| 1311 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1312 | void InitializeVTablePointers(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1313 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1314 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1315 | bool BaseIsNonVirtualPrimaryBase, |
| 1316 | llvm::Constant *VTable, |
| 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. |
| 1324 | llvm::Value *GetVTablePtr(llvm::Value *This, const llvm::Type *Ty); |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1325 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1326 | /// EnterDtorCleanups - Enter the cleanups necessary to complete the |
| 1327 | /// given phase of destruction for a destructor. The end result |
| 1328 | /// should call destructors on members and base classes in reverse |
| 1329 | /// order of their construction. |
| 1330 | void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 1332 | /// ShouldInstrumentFunction - Return true if the current function should be |
| 1333 | /// instrumented with __cyg_profile_func_* calls |
| 1334 | bool ShouldInstrumentFunction(); |
| 1335 | |
| 1336 | /// EmitFunctionInstrumentation - Emit LLVM code to call the specified |
| 1337 | /// instrumentation function with the current function and the call site, if |
| 1338 | /// function instrumentation is enabled. |
| 1339 | void EmitFunctionInstrumentation(const char *Fn); |
| 1340 | |
Roman Divacky | be4c870 | 2011-02-10 16:52:03 +0000 | [diff] [blame] | 1341 | /// EmitMCountInstrumentation - Emit call to .mcount. |
| 1342 | void EmitMCountInstrumentation(); |
| 1343 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1344 | /// EmitFunctionProlog - Emit the target specific LLVM code to load the |
| 1345 | /// arguments for the given function. This is also responsible for naming the |
| 1346 | /// LLVM function arguments. |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1347 | void EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1348 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1349 | const FunctionArgList &Args); |
| 1350 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1351 | /// EmitFunctionEpilog - Emit the target specific LLVM code to return the |
| 1352 | /// given temporary. |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 1353 | void EmitFunctionEpilog(const CGFunctionInfo &FI); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1354 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 1355 | /// EmitStartEHSpec - Emit the start of the exception spec. |
| 1356 | void EmitStartEHSpec(const Decl *D); |
| 1357 | |
| 1358 | /// EmitEndEHSpec - Emit the end of the exception spec. |
| 1359 | void EmitEndEHSpec(const Decl *D); |
| 1360 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1361 | /// getTerminateLandingPad - Return a landing pad that just calls terminate. |
| 1362 | llvm::BasicBlock *getTerminateLandingPad(); |
| 1363 | |
| 1364 | /// getTerminateHandler - Return a handler (not a landing pad, just |
| 1365 | /// a catch handler) that just calls terminate. This is used when |
| 1366 | /// a terminate scope encloses a try. |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 1367 | llvm::BasicBlock *getTerminateHandler(); |
| 1368 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1369 | llvm::Type *ConvertTypeForMem(QualType T); |
| 1370 | llvm::Type *ConvertType(QualType T); |
| 1371 | llvm::Type *ConvertType(const TypeDecl *T) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 1372 | return ConvertType(getContext().getTypeDeclType(T)); |
| 1373 | } |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 1374 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1375 | /// LoadObjCSelf - Load the value of self. This function is only valid while |
| 1376 | /// generating code for an Objective-C method. |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 1377 | llvm::Value *LoadObjCSelf(); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1378 | |
| 1379 | /// TypeOfSelfObject - Return type of object that this self represents. |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 1380 | QualType TypeOfSelfObject(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1381 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1382 | /// hasAggregateLLVMType - Return true if the specified AST type will map into |
| 1383 | /// an aggregate LLVM type or is void. |
| 1384 | static bool hasAggregateLLVMType(QualType T); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1385 | |
| 1386 | /// createBasicBlock - Create an LLVM basic block. |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1387 | llvm::BasicBlock *createBasicBlock(llvm::StringRef name = "", |
| 1388 | llvm::Function *parent = 0, |
| 1389 | llvm::BasicBlock *before = 0) { |
Daniel Dunbar | 29ea672 | 2008-11-12 00:01:12 +0000 | [diff] [blame] | 1390 | #ifdef NDEBUG |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1391 | return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before); |
Daniel Dunbar | 29ea672 | 2008-11-12 00:01:12 +0000 | [diff] [blame] | 1392 | #else |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1393 | return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before); |
Daniel Dunbar | 29ea672 | 2008-11-12 00:01:12 +0000 | [diff] [blame] | 1394 | #endif |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1395 | } |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1396 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1397 | /// getBasicBlockForLabel - Return the LLVM basicblock that the specified |
| 1398 | /// label maps to. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1399 | JumpDest getJumpDestForLabel(const LabelDecl *S); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1400 | |
Mike Stump | f71d232 | 2009-11-30 20:08:49 +0000 | [diff] [blame] | 1401 | /// SimplifyForwardingBlocks - If the given basic block is only a branch to |
| 1402 | /// another basic block, simplify it. This assumes that no other code could |
| 1403 | /// potentially reference the basic block. |
Daniel Dunbar | aa5bd87 | 2009-04-01 04:37:47 +0000 | [diff] [blame] | 1404 | void SimplifyForwardingBlocks(llvm::BasicBlock *BB); |
| 1405 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1406 | /// EmitBlock - Emit the given block \arg BB and set it as the insert point, |
| 1407 | /// adding a fall-through branch from the current insert block if |
| 1408 | /// necessary. It is legal to call this function even if there is no current |
| 1409 | /// insertion point. |
Daniel Dunbar | a0c21a8 | 2008-11-13 01:24:05 +0000 | [diff] [blame] | 1410 | /// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1411 | /// IsFinished - If true, indicates that the caller has finished emitting |
| 1412 | /// branches to the given block and does not expect to emit code into it. This |
| 1413 | /// means the block can be ignored if it is unreachable. |
Daniel Dunbar | a0c21a8 | 2008-11-13 01:24:05 +0000 | [diff] [blame] | 1414 | void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false); |
Daniel Dunbar | 824e3bd | 2008-11-11 04:34:23 +0000 | [diff] [blame] | 1415 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1416 | /// EmitBranch - Emit a branch to the specified basic block from the current |
| 1417 | /// insert block, taking care to avoid creation of branches from dummy |
| 1418 | /// blocks. It is legal to call this function even if there is no current |
| 1419 | /// insertion point. |
Daniel Dunbar | 5e08ad3 | 2008-11-11 22:06:59 +0000 | [diff] [blame] | 1420 | /// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1421 | /// This function clears the current insertion point. The caller should follow |
| 1422 | /// calls to this function with calls to Emit*Block prior to generation new |
| 1423 | /// code. |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1424 | void EmitBranch(llvm::BasicBlock *Block); |
| 1425 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1426 | /// HaveInsertPoint - True if an insertion point is defined. If not, this |
| 1427 | /// indicates that the current code being emitted is unreachable. |
| 1428 | bool HaveInsertPoint() const { |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1429 | return Builder.GetInsertBlock() != 0; |
| 1430 | } |
| 1431 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1432 | /// EnsureInsertPoint - Ensure that an insertion point is defined so that |
| 1433 | /// emitted IR has a place to go. Note that by definition, if this function |
| 1434 | /// creates a block then that block is unreachable; callers may do better to |
| 1435 | /// detect when no insertion point is defined and simply skip IR generation. |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1436 | void EnsureInsertPoint() { |
| 1437 | if (!HaveInsertPoint()) |
| 1438 | EmitBlock(createBasicBlock()); |
| 1439 | } |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1440 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 1441 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 1442 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 1443 | void ErrorUnsupported(const Stmt *S, const char *Type, |
| 1444 | bool OmitOnError=false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1445 | |
| 1446 | //===--------------------------------------------------------------------===// |
| 1447 | // Helpers |
| 1448 | //===--------------------------------------------------------------------===// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1449 | |
Daniel Dunbar | 983e3d7 | 2010-08-21 04:20:22 +0000 | [diff] [blame] | 1450 | LValue MakeAddrLValue(llvm::Value *V, QualType T, unsigned Alignment = 0) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1451 | return LValue::MakeAddr(V, T, Alignment, getContext(), |
| 1452 | CGM.getTBAAInfo(T)); |
Daniel Dunbar | 5cf8bfe | 2010-08-21 02:53:44 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1455 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1456 | /// block. The caller is responsible for setting an appropriate alignment on |
| 1457 | /// the alloca. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1458 | llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty, |
Daniel Dunbar | 259e9cc | 2009-10-19 01:21:05 +0000 | [diff] [blame] | 1459 | const llvm::Twine &Name = "tmp"); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1460 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 1461 | /// InitTempAlloca - Provide an initial value for the given alloca. |
| 1462 | void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value); |
| 1463 | |
Daniel Dunbar | 9bd4da2 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 1464 | /// CreateIRTemp - Create a temporary IR object of the given type, with |
| 1465 | /// appropriate alignment. This routine should only be used when an temporary |
| 1466 | /// value needs to be stored into an alloca (for example, to avoid explicit |
| 1467 | /// PHI construction), but the type is the IR type, not the type appropriate |
| 1468 | /// for storing in memory. |
Chris Lattner | 121b3fa | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 1469 | llvm::AllocaInst *CreateIRTemp(QualType T, const llvm::Twine &Name = "tmp"); |
Daniel Dunbar | 9bd4da2 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 1470 | |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1471 | /// CreateMemTemp - Create a temporary memory object of the given type, with |
| 1472 | /// appropriate alignment. |
Chris Lattner | 121b3fa | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 1473 | llvm::AllocaInst *CreateMemTemp(QualType T, const llvm::Twine &Name = "tmp"); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1474 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1475 | /// CreateAggTemp - Create a temporary memory object for the given |
| 1476 | /// aggregate type. |
| 1477 | AggValueSlot CreateAggTemp(QualType T, const llvm::Twine &Name = "tmp") { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1478 | return AggValueSlot::forAddr(CreateMemTemp(T, Name), T.getQualifiers(), |
| 1479 | false); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1482 | /// Emit a cast to void* in the appropriate address space. |
| 1483 | llvm::Value *EmitCastToVoidPtr(llvm::Value *value); |
| 1484 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1485 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 1486 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 1487 | llvm::Value *EvaluateExprAsBool(const Expr *E); |
| 1488 | |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1489 | /// EmitIgnoredExpr - Emit an expression in a context which ignores the result. |
| 1490 | void EmitIgnoredExpr(const Expr *E); |
| 1491 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1492 | /// EmitAnyExpr - Emit code to compute the specified expression which can have |
| 1493 | /// any type. The result is returned as an RValue struct. If this is an |
| 1494 | /// aggregate expression, the aggloc/agglocvolatile arguments indicate where |
| 1495 | /// the result should be returned. |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 1496 | /// |
| 1497 | /// \param IgnoreResult - True if the resulting value isn't used. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1498 | RValue EmitAnyExpr(const Expr *E, |
| 1499 | AggValueSlot AggSlot = AggValueSlot::ignored(), |
| 1500 | bool IgnoreResult = false); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 1501 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1502 | // EmitVAListRef - Emit a "reference" to a va_list; this is either the address |
| 1503 | // 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] | 1504 | llvm::Value *EmitVAListRef(const Expr *E); |
| 1505 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1506 | /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will |
| 1507 | /// always be accessible even if no aggregate location is provided. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1508 | RValue EmitAnyExprToTemp(const Expr *E); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1509 | |
John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1510 | /// EmitAnyExprToMem - Emits the code necessary to evaluate an |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1511 | /// arbitrary expression into the given memory location. |
| 1512 | void EmitAnyExprToMem(const Expr *E, llvm::Value *Location, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1513 | Qualifiers Quals, bool IsInitializer); |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1514 | |
John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1515 | /// EmitExprAsInit - Emits the code necessary to initialize a |
| 1516 | /// location in memory with the given initializer. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1517 | void EmitExprAsInit(const Expr *init, const ValueDecl *D, |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1518 | LValue lvalue, bool capturedByInit); |
John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1519 | |
Mike Stump | 27fe2e6 | 2009-05-23 22:29:41 +0000 | [diff] [blame] | 1520 | /// EmitAggregateCopy - Emit an aggrate copy. |
| 1521 | /// |
| 1522 | /// \param isVolatile - True iff either the source or the destination is |
| 1523 | /// volatile. |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 1524 | void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
Mike Stump | 27fe2e6 | 2009-05-23 22:29:41 +0000 | [diff] [blame] | 1525 | QualType EltTy, bool isVolatile=false); |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 1526 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1527 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 1528 | /// then reuse it. |
| 1529 | void StartBlock(const char *N); |
| 1530 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 1531 | /// GetAddrOfStaticLocalVar - Return the address of a static local variable. |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1532 | llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
| 1533 | return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); |
| 1534 | } |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 1535 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1536 | /// GetAddrOfLocalVar - Return the address of a local variable. |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1537 | llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) { |
| 1538 | llvm::Value *Res = LocalDeclMap[VD]; |
| 1539 | assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); |
| 1540 | return Res; |
| 1541 | } |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1542 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1543 | /// getOpaqueLValueMapping - Given an opaque value expression (which |
| 1544 | /// must be mapped to an l-value), return its mapping. |
| 1545 | const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) { |
| 1546 | assert(OpaqueValueMapping::shouldBindAsLValue(e)); |
| 1547 | |
| 1548 | llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator |
| 1549 | it = OpaqueLValues.find(e); |
| 1550 | assert(it != OpaqueLValues.end() && "no mapping for opaque value!"); |
| 1551 | return it->second; |
| 1552 | } |
| 1553 | |
| 1554 | /// getOpaqueRValueMapping - Given an opaque value expression (which |
| 1555 | /// must be mapped to an r-value), return its mapping. |
| 1556 | const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) { |
| 1557 | assert(!OpaqueValueMapping::shouldBindAsLValue(e)); |
| 1558 | |
| 1559 | llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator |
| 1560 | it = OpaqueRValues.find(e); |
| 1561 | assert(it != OpaqueRValues.end() && "no mapping for opaque value!"); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 1562 | return it->second; |
| 1563 | } |
| 1564 | |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 1565 | /// getAccessedFieldNo - Given an encoded value and a result number, return |
| 1566 | /// the input field number being accessed. |
| 1567 | static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts); |
| 1568 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1569 | llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 1570 | llvm::BasicBlock *GetIndirectGotoBlock(); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 1571 | |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 1572 | /// EmitNullInitialization - Generate code to set a value of the given type to |
| 1573 | /// null, If the type contains data member pointers, they will be initialized |
| 1574 | /// to -1 in accordance with the Itanium C++ ABI. |
| 1575 | void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty); |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1576 | |
| 1577 | // EmitVAArg - Generate code to get an argument from the passed in pointer |
| 1578 | // and update it accordingly. The return value is a pointer to the argument. |
| 1579 | // 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] | 1580 | // instruction in LLVM instead once it works well enough. |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1581 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty); |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 1582 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1583 | /// emitArrayLength - Compute the length of an array, even if it's a |
| 1584 | /// VLA, and drill down to the base element type. |
| 1585 | llvm::Value *emitArrayLength(const ArrayType *arrayType, |
| 1586 | QualType &baseType, |
| 1587 | llvm::Value *&addr); |
| 1588 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1589 | /// EmitVLASize - Capture all the sizes for the VLA expressions in |
| 1590 | /// the given variably-modified type and store them in the VLASizeMap. |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1591 | /// |
| 1592 | /// This function can be called with a null (unreachable) insert point. |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1593 | void EmitVariablyModifiedType(QualType Ty); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1594 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1595 | /// getVLASize - Returns an LLVM value that corresponds to the size, |
| 1596 | /// in non-variably-sized elements, of a variable length array type, |
| 1597 | /// plus that largest non-variably-sized element type. Assumes that |
| 1598 | /// the type has already been emitted with EmitVariablyModifiedType. |
| 1599 | std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla); |
| 1600 | std::pair<llvm::Value*,QualType> getVLASize(QualType vla); |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 1601 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 1602 | /// LoadCXXThis - Load the value of 'this'. This function is only valid while |
| 1603 | /// generating code for an C++ member function. |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 1604 | llvm::Value *LoadCXXThis() { |
| 1605 | assert(CXXThisValue && "no 'this' value for this function"); |
| 1606 | return CXXThisValue; |
| 1607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1609 | /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have |
| 1610 | /// virtual bases. |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 1611 | llvm::Value *LoadCXXVTT() { |
| 1612 | assert(CXXVTTValue && "no VTT value for this function"); |
| 1613 | return CXXVTTValue; |
| 1614 | } |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 1615 | |
| 1616 | /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 1617 | /// complete class to the given direct base. |
| 1618 | llvm::Value * |
| 1619 | GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value, |
| 1620 | const CXXRecordDecl *Derived, |
| 1621 | const CXXRecordDecl *Base, |
| 1622 | bool BaseIsVirtual); |
Anders Carlsson | a88ad56 | 2010-04-24 21:51:08 +0000 | [diff] [blame] | 1623 | |
Mike Stump | f71d232 | 2009-11-30 20:08:49 +0000 | [diff] [blame] | 1624 | /// GetAddressOfBaseClass - This function will add the necessary delta to the |
| 1625 | /// load of 'this' and returns address of the base class. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1626 | llvm::Value *GetAddressOfBaseClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 1627 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1628 | CastExpr::path_const_iterator PathBegin, |
| 1629 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 1630 | bool NullCheckValue); |
| 1631 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1632 | llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 1633 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1634 | CastExpr::path_const_iterator PathBegin, |
| 1635 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1636 | bool NullCheckValue); |
| 1637 | |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 1638 | llvm::Value *GetVirtualBaseClassOffset(llvm::Value *This, |
| 1639 | const CXXRecordDecl *ClassDecl, |
| 1640 | const CXXRecordDecl *BaseClassDecl); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1641 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1642 | void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1643 | CXXCtorType CtorType, |
| 1644 | const FunctionArgList &Args); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1645 | // It's important not to confuse this and the previous function. Delegating |
| 1646 | // constructors are the C++0x feature. The constructor delegate optimization |
| 1647 | // is used to reduce duplication in the base and complete consturctors where |
| 1648 | // they are substantially the same. |
| 1649 | void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1650 | const FunctionArgList &Args); |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 1651 | void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, |
| 1652 | bool ForVirtualBase, llvm::Value *This, |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 1653 | CallExpr::const_arg_iterator ArgBeg, |
| 1654 | CallExpr::const_arg_iterator ArgEnd); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1655 | |
| 1656 | void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, |
| 1657 | llvm::Value *This, llvm::Value *Src, |
| 1658 | CallExpr::const_arg_iterator ArgBeg, |
| 1659 | CallExpr::const_arg_iterator ArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1661 | void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 1662 | const ConstantArrayType *ArrayTy, |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 1663 | llvm::Value *ArrayPtr, |
| 1664 | CallExpr::const_arg_iterator ArgBeg, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1665 | CallExpr::const_arg_iterator ArgEnd, |
| 1666 | bool ZeroInitialization = false); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1667 | |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 1668 | void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1669 | llvm::Value *NumElements, |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 1670 | llvm::Value *ArrayPtr, |
| 1671 | CallExpr::const_arg_iterator ArgBeg, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1672 | CallExpr::const_arg_iterator ArgEnd, |
| 1673 | bool ZeroInitialization = false); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 1674 | |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1675 | void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1676 | const ArrayType *Array, |
| 1677 | llvm::Value *This); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1679 | static Destroyer destroyCXXObject; |
| 1680 | |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 1681 | void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1682 | llvm::Value *NumElements, |
| 1683 | llvm::Value *This); |
| 1684 | |
Anders Carlsson | 02e370a | 2010-06-08 22:14:59 +0000 | [diff] [blame] | 1685 | llvm::Function *GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, |
| 1686 | const ArrayType *Array, |
| 1687 | llvm::Value *This); |
Fariborz Jahanian | 88f4280 | 2009-11-10 19:24:06 +0000 | [diff] [blame] | 1688 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 1689 | void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 1690 | bool ForVirtualBase, llvm::Value *This); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1691 | |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 1692 | void EmitNewArrayInitializer(const CXXNewExpr *E, llvm::Value *NewPtr, |
| 1693 | llvm::Value *NumElements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1695 | void EmitCXXTemporary(const CXXTemporary *Temporary, llvm::Value *Ptr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Anders Carlsson | a00703d | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 1697 | llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E); |
Anders Carlsson | 60e282c | 2009-08-16 21:13:42 +0000 | [diff] [blame] | 1698 | void EmitCXXDeleteExpr(const CXXDeleteExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Eli Friedman | 4bf8152 | 2009-11-18 00:57:03 +0000 | [diff] [blame] | 1700 | void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, |
| 1701 | QualType DeleteTy); |
| 1702 | |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1703 | llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1704 | llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1705 | |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 1706 | void EmitCheck(llvm::Value *, unsigned Size); |
| 1707 | |
Chris Lattner | dd36d32 | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 1708 | llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 1709 | bool isInc, bool isPre); |
| 1710 | ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 1711 | bool isInc, bool isPre); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1712 | //===--------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1713 | // Declaration Emission |
| 1714 | //===--------------------------------------------------------------------===// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1715 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1716 | /// EmitDecl - Emit a declaration. |
| 1717 | /// |
| 1718 | /// This function can be called with a null (unreachable) insert point. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1719 | void EmitDecl(const Decl &D); |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1720 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1721 | /// EmitVarDecl - Emit a local variable declaration. |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1722 | /// |
| 1723 | /// This function can be called with a null (unreachable) insert point. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1724 | void EmitVarDecl(const VarDecl &D); |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1725 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1726 | void EmitScalarInit(const Expr *init, const ValueDecl *D, |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1727 | LValue lvalue, bool capturedByInit); |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1728 | void EmitScalarInit(llvm::Value *init, LValue lvalue); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1729 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1730 | typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D, |
| 1731 | llvm::Value *Address); |
| 1732 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1733 | /// EmitAutoVarDecl - Emit an auto variable declaration. |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1734 | /// |
| 1735 | /// This function can be called with a null (unreachable) insert point. |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1736 | void EmitAutoVarDecl(const VarDecl &D); |
| 1737 | |
| 1738 | class AutoVarEmission { |
| 1739 | friend class CodeGenFunction; |
| 1740 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1741 | const VarDecl *Variable; |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1742 | |
| 1743 | /// The alignment of the variable. |
| 1744 | CharUnits Alignment; |
| 1745 | |
| 1746 | /// The address of the alloca. Null if the variable was emitted |
| 1747 | /// as a global constant. |
| 1748 | llvm::Value *Address; |
| 1749 | |
| 1750 | llvm::Value *NRVOFlag; |
| 1751 | |
| 1752 | /// True if the variable is a __block variable. |
| 1753 | bool IsByRef; |
| 1754 | |
| 1755 | /// True if the variable is of aggregate type and has a constant |
| 1756 | /// initializer. |
| 1757 | bool IsConstantAggregate; |
| 1758 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1759 | struct Invalid {}; |
| 1760 | AutoVarEmission(Invalid) : Variable(0) {} |
| 1761 | |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1762 | AutoVarEmission(const VarDecl &variable) |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1763 | : Variable(&variable), Address(0), NRVOFlag(0), |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1764 | IsByRef(false), IsConstantAggregate(false) {} |
| 1765 | |
| 1766 | bool wasEmittedAsGlobal() const { return Address == 0; } |
| 1767 | |
| 1768 | public: |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1769 | static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); } |
| 1770 | |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1771 | /// Returns the address of the object within this declaration. |
| 1772 | /// Note that this does not chase the forwarding pointer for |
| 1773 | /// __block decls. |
| 1774 | llvm::Value *getObjectAddress(CodeGenFunction &CGF) const { |
| 1775 | if (!IsByRef) return Address; |
| 1776 | |
| 1777 | return CGF.Builder.CreateStructGEP(Address, |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1778 | CGF.getByRefValueLLVMField(Variable), |
| 1779 | Variable->getNameAsString()); |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1780 | } |
| 1781 | }; |
| 1782 | AutoVarEmission EmitAutoVarAlloca(const VarDecl &var); |
| 1783 | void EmitAutoVarInit(const AutoVarEmission &emission); |
| 1784 | void EmitAutoVarCleanups(const AutoVarEmission &emission); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1785 | void emitAutoVarTypeCleanup(const AutoVarEmission &emission, |
| 1786 | QualType::DestructionKind dtorKind); |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1787 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1788 | void EmitStaticVarDecl(const VarDecl &D, |
| 1789 | llvm::GlobalValue::LinkageTypes Linkage); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1790 | |
| 1791 | /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl. |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1792 | void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, unsigned ArgNo); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1793 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1794 | /// protectFromPeepholes - Protect a value that we're intending to |
| 1795 | /// store to the side, but which will probably be used later, from |
| 1796 | /// aggressive peepholing optimizations that might delete it. |
| 1797 | /// |
| 1798 | /// Pass the result to unprotectFromPeepholes to declare that |
| 1799 | /// protection is no longer required. |
| 1800 | /// |
| 1801 | /// There's no particular reason why this shouldn't apply to |
| 1802 | /// l-values, it's just that no existing peepholes work on pointers. |
| 1803 | PeepholeProtection protectFromPeepholes(RValue rvalue); |
| 1804 | void unprotectFromPeepholes(PeepholeProtection protection); |
| 1805 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1806 | //===--------------------------------------------------------------------===// |
| 1807 | // Statement Emission |
| 1808 | //===--------------------------------------------------------------------===// |
| 1809 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1810 | /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info. |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1811 | void EmitStopPoint(const Stmt *S); |
| 1812 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1813 | /// EmitStmt - Emit the code for the statement \arg S. It is legal to call |
| 1814 | /// this function even if there is no current insertion point. |
| 1815 | /// |
| 1816 | /// This function may clear the current insertion point; callers should use |
| 1817 | /// EnsureInsertPoint if they wish to subsequently generate code without first |
| 1818 | /// calling EmitBlock, EmitBranch, or EmitStmt. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1819 | void EmitStmt(const Stmt *S); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1820 | |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1821 | /// EmitSimpleStmt - Try to emit a "simple" statement which does not |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1822 | /// necessarily require an insertion point or debug information; typically |
| 1823 | /// because the statement amounts to a jump or a container of other |
| 1824 | /// statements. |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1825 | /// |
| 1826 | /// \return True if the statement was handled. |
| 1827 | bool EmitSimpleStmt(const Stmt *S); |
| 1828 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1829 | RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1830 | AggValueSlot AVS = AggValueSlot::ignored()); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1831 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1832 | /// EmitLabel - Emit the block for the given label. It is legal to call this |
| 1833 | /// function even if there is no current insertion point. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1834 | void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt. |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1835 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1836 | void EmitLabelStmt(const LabelStmt &S); |
| 1837 | void EmitGotoStmt(const GotoStmt &S); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 1838 | void EmitIndirectGotoStmt(const IndirectGotoStmt &S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1839 | void EmitIfStmt(const IfStmt &S); |
| 1840 | void EmitWhileStmt(const WhileStmt &S); |
| 1841 | void EmitDoStmt(const DoStmt &S); |
| 1842 | void EmitForStmt(const ForStmt &S); |
| 1843 | void EmitReturnStmt(const ReturnStmt &S); |
| 1844 | void EmitDeclStmt(const DeclStmt &S); |
Daniel Dunbar | 0912425 | 2008-11-12 08:21:33 +0000 | [diff] [blame] | 1845 | void EmitBreakStmt(const BreakStmt &S); |
| 1846 | void EmitContinueStmt(const ContinueStmt &S); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 1847 | void EmitSwitchStmt(const SwitchStmt &S); |
| 1848 | void EmitDefaultStmt(const DefaultStmt &S); |
| 1849 | void EmitCaseStmt(const CaseStmt &S); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 1850 | void EmitCaseStmtRange(const CaseStmt &S); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 1851 | void EmitAsmStmt(const AsmStmt &S); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1852 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 1853 | void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1854 | void EmitObjCAtTryStmt(const ObjCAtTryStmt &S); |
| 1855 | void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 1856 | void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1857 | void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1858 | |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 1859 | llvm::Constant *getUnwindResumeFn(); |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 1860 | llvm::Constant *getUnwindResumeOrRethrowFn(); |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1861 | void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); |
| 1862 | void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1863 | |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 1864 | void EmitCXXTryStmt(const CXXTryStmt &S); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1865 | void EmitCXXForRangeStmt(const CXXForRangeStmt &S); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1866 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1867 | //===--------------------------------------------------------------------===// |
| 1868 | // LValue Expression Emission |
| 1869 | //===--------------------------------------------------------------------===// |
| 1870 | |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 1871 | /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type. |
| 1872 | RValue GetUndefRValue(QualType Ty); |
| 1873 | |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 1874 | /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E |
| 1875 | /// and issue an ErrorUnsupported style diagnostic (using the |
| 1876 | /// provided Name). |
| 1877 | RValue EmitUnsupportedRValue(const Expr *E, |
| 1878 | const char *Name); |
| 1879 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1880 | /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue |
| 1881 | /// an ErrorUnsupported style diagnostic (using the provided Name). |
Daniel Dunbar | 6ba82a4 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 1882 | LValue EmitUnsupportedLValue(const Expr *E, |
| 1883 | const char *Name); |
| 1884 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1885 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 1886 | /// of the expression. |
| 1887 | /// |
| 1888 | /// This can return one of two things: a simple address or a bitfield |
| 1889 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 1890 | /// guaranteed to be an LLVM pointer type. |
| 1891 | /// |
| 1892 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 1893 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 1894 | /// integer. |
| 1895 | /// |
| 1896 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 1897 | /// size, this method guarantees that the returned pointer type will point to |
| 1898 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 1899 | /// variable length type, this is not possible. |
| 1900 | /// |
| 1901 | LValue EmitLValue(const Expr *E); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1902 | |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 1903 | /// EmitCheckedLValue - Same as EmitLValue but additionally we generate |
| 1904 | /// checking code to guard against undefined behavior. This is only |
| 1905 | /// suitable when we know that the address will be used to access the |
| 1906 | /// object. |
| 1907 | LValue EmitCheckedLValue(const Expr *E); |
| 1908 | |
John McCall | 26815d9 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1909 | /// EmitToMemory - Change a scalar value from its value |
| 1910 | /// representation to its in-memory representation. |
| 1911 | llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty); |
| 1912 | |
| 1913 | /// EmitFromMemory - Change a scalar value from its memory |
| 1914 | /// representation to its value representation. |
| 1915 | llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty); |
| 1916 | |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1917 | /// EmitLoadOfScalar - Load a scalar value from an address, taking |
| 1918 | /// care to appropriately convert from the memory representation to |
| 1919 | /// the LLVM value representation. |
Mike Stump | 09429b9 | 2009-02-17 17:00:02 +0000 | [diff] [blame] | 1920 | llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1921 | unsigned Alignment, QualType Ty, |
| 1922 | llvm::MDNode *TBAAInfo = 0); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1923 | |
| 1924 | /// EmitLoadOfScalar - Load a scalar value from an address, taking |
| 1925 | /// care to appropriately convert from the memory representation to |
| 1926 | /// the LLVM value representation. The l-value must be a simple |
| 1927 | /// l-value. |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1928 | llvm::Value *EmitLoadOfScalar(LValue lvalue); |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1929 | |
| 1930 | /// EmitStoreOfScalar - Store a scalar value to an address, taking |
| 1931 | /// care to appropriately convert from the memory representation to |
| 1932 | /// the LLVM value representation. |
Mike Stump | 09429b9 | 2009-02-17 17:00:02 +0000 | [diff] [blame] | 1933 | void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1934 | bool Volatile, unsigned Alignment, QualType Ty, |
| 1935 | llvm::MDNode *TBAAInfo = 0); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1936 | |
| 1937 | /// EmitStoreOfScalar - Store a scalar value to an address, taking |
| 1938 | /// care to appropriately convert from the memory representation to |
| 1939 | /// the LLVM value representation. The l-value must be a simple |
| 1940 | /// l-value. |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1941 | void EmitStoreOfScalar(llvm::Value *value, LValue lvalue); |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1942 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1943 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 1944 | /// this method emits the address of the lvalue, then loads the result as an |
| 1945 | /// rvalue, returning the rvalue. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1946 | RValue EmitLoadOfLValue(LValue V); |
| 1947 | RValue EmitLoadOfExtVectorElementLValue(LValue V); |
| 1948 | RValue EmitLoadOfBitfieldLValue(LValue LV); |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 1949 | RValue EmitLoadOfPropertyRefLValue(LValue LV, |
| 1950 | ReturnValueSlot Return = ReturnValueSlot()); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1951 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1952 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 1953 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 1954 | /// is 'Ty'. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1955 | void EmitStoreThroughLValue(RValue Src, LValue Dst); |
| 1956 | void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst); |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 1957 | void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst); |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1958 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1959 | /// EmitStoreThroughLValue - Store Src into Dst with same constraints as |
| 1960 | /// EmitStoreThroughLValue. |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1961 | /// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1962 | /// \param Result [out] - If non-null, this will be set to a Value* for the |
| 1963 | /// bit-field contents after the store, appropriate for use as the result of |
| 1964 | /// an assignment to the bit-field. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1965 | void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1966 | llvm::Value **Result=0); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 1967 | |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1968 | /// Emit an l-value for an assignment (simple or compound) of complex type. |
| 1969 | LValue EmitComplexAssignmentLValue(const BinaryOperator *E); |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1970 | LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1971 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1972 | // Note: only available for agg return types |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 1973 | LValue EmitBinaryOperatorLValue(const BinaryOperator *E); |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1974 | LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E); |
Daniel Dunbar | 5b5c9ef | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 1975 | // Note: only available for agg return types |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 1976 | LValue EmitCallExprLValue(const CallExpr *E); |
Daniel Dunbar | 5b5c9ef | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 1977 | // Note: only available for agg return types |
| 1978 | LValue EmitVAArgExprLValue(const VAArgExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1979 | LValue EmitDeclRefLValue(const DeclRefExpr *E); |
| 1980 | LValue EmitStringLiteralLValue(const StringLiteral *E); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1981 | LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1982 | LValue EmitPredefinedLValue(const PredefinedExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1983 | LValue EmitUnaryOpLValue(const UnaryOperator *E); |
| 1984 | LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1985 | LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 1986 | LValue EmitMemberExpr(const MemberExpr *E); |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 1987 | LValue EmitObjCIsaExpr(const ObjCIsaExpr *E); |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 1988 | LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1989 | LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E); |
Chris Lattner | 75dfeda | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 1990 | LValue EmitCastLValue(const CastExpr *E); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 1991 | LValue EmitNullInitializationLValue(const CXXScalarValueInitExpr *E); |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1992 | LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 1993 | LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1994 | |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1995 | llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface, |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1996 | const ObjCIvarDecl *Ivar); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 1997 | LValue EmitLValueForAnonRecordField(llvm::Value* Base, |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1998 | const IndirectFieldDecl* Field, |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 1999 | unsigned CVRQualifiers); |
Anders Carlsson | 0ed303c | 2009-11-17 03:57:07 +0000 | [diff] [blame] | 2000 | LValue EmitLValueForField(llvm::Value* Base, const FieldDecl* Field, |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 2001 | unsigned CVRQualifiers); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2002 | |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 2003 | /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that |
| 2004 | /// if the Field is a reference, this will return the address of the reference |
| 2005 | /// and not the address of the value stored in the reference. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2006 | LValue EmitLValueForFieldInitialization(llvm::Value* Base, |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 2007 | const FieldDecl* Field, |
| 2008 | unsigned CVRQualifiers); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2009 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 2010 | LValue EmitLValueForIvar(QualType ObjectTy, |
| 2011 | llvm::Value* Base, const ObjCIvarDecl *Ivar, |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2012 | unsigned CVRQualifiers); |
| 2013 | |
Anders Carlsson | 0ed303c | 2009-11-17 03:57:07 +0000 | [diff] [blame] | 2014 | LValue EmitLValueForBitfield(llvm::Value* Base, const FieldDecl* Field, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2015 | unsigned CVRQualifiers); |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 2016 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 2017 | LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E); |
| 2018 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 2019 | LValue EmitCXXConstructLValue(const CXXConstructExpr *E); |
Anders Carlsson | e61c9e8 | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 2020 | LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 2021 | LValue EmitExprWithCleanupsLValue(const ExprWithCleanups *E); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 2022 | LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2023 | |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 2024 | LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2025 | LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 2026 | LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E); |
Chris Lattner | 6545994 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 2027 | LValue EmitStmtExprLValue(const StmtExpr *E); |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2028 | LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E); |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 2029 | LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E); |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 2030 | void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2031 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2032 | //===--------------------------------------------------------------------===// |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 2033 | // Scalar Expression Emission |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2034 | //===--------------------------------------------------------------------===// |
| 2035 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2036 | /// EmitCall - Generate a call of the given function, expecting the given |
| 2037 | /// result type, and using the given argument list which specifies both the |
| 2038 | /// LLVM arguments and the types they were derived from. |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2039 | /// |
Mike Stump | f71d232 | 2009-11-30 20:08:49 +0000 | [diff] [blame] | 2040 | /// \param TargetDecl - If given, the decl of the function in a direct call; |
| 2041 | /// used to set attributes on the call (noreturn, etc.). |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 2042 | RValue EmitCall(const CGFunctionInfo &FnInfo, |
| 2043 | llvm::Value *Callee, |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 2044 | ReturnValueSlot ReturnValue, |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2045 | const CallArgList &Args, |
David Chisnall | dd5c98f | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 2046 | const Decl *TargetDecl = 0, |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 2047 | llvm::Instruction **callOrInvoke = 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 2049 | RValue EmitCall(QualType FnType, llvm::Value *Callee, |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2050 | ReturnValueSlot ReturnValue, |
Anders Carlsson | 9864771 | 2009-05-27 01:22:39 +0000 | [diff] [blame] | 2051 | CallExpr::const_arg_iterator ArgBeg, |
| 2052 | CallExpr::const_arg_iterator ArgEnd, |
| 2053 | const Decl *TargetDecl = 0); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2054 | RValue EmitCallExpr(const CallExpr *E, |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2055 | ReturnValueSlot ReturnValue = ReturnValueSlot()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2057 | llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee, |
| 2058 | llvm::Value * const *ArgBegin, |
| 2059 | llvm::Value * const *ArgEnd, |
| 2060 | const llvm::Twine &Name = ""); |
| 2061 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 2062 | llvm::Value *BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 2063 | const llvm::Type *Ty); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2064 | llvm::Value *BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, |
Anders Carlsson | 83eedd9 | 2010-11-28 17:53:32 +0000 | [diff] [blame] | 2065 | llvm::Value *This, const llvm::Type *Ty); |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 2066 | llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 2067 | NestedNameSpecifier *Qual, |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 2068 | const llvm::Type *Ty); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 2069 | |
| 2070 | llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD, |
| 2071 | CXXDtorType Type, |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 2072 | const CXXRecordDecl *RD); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 2073 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 2074 | RValue EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 2075 | llvm::Value *Callee, |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2076 | ReturnValueSlot ReturnValue, |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 2077 | llvm::Value *This, |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 2078 | llvm::Value *VTT, |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 2079 | CallExpr::const_arg_iterator ArgBeg, |
| 2080 | CallExpr::const_arg_iterator ArgEnd); |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2081 | RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E, |
| 2082 | ReturnValueSlot ReturnValue); |
| 2083 | RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 2084 | ReturnValueSlot ReturnValue); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2085 | |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2086 | llvm::Value *EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E, |
| 2087 | const CXXMethodDecl *MD, |
| 2088 | llvm::Value *This); |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 2089 | RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2090 | const CXXMethodDecl *MD, |
| 2091 | ReturnValueSlot ReturnValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2093 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | RValue EmitBuiltinExpr(const FunctionDecl *FD, |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 2095 | unsigned BuiltinID, const CallExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2096 | |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2097 | RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue); |
Mike Stump | 09429b9 | 2009-02-17 17:00:02 +0000 | [diff] [blame] | 2098 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2099 | /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call |
| 2100 | /// is unhandled by the current target. |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 2101 | llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
| 2102 | |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 2103 | llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2104 | llvm::Value *EmitNeonCall(llvm::Function *F, |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 2105 | llvm::SmallVectorImpl<llvm::Value*> &O, |
Bob Wilson | db3d4d0 | 2010-12-08 22:37:56 +0000 | [diff] [blame] | 2106 | const char *name, |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 2107 | unsigned shift = 0, bool rightshift = false); |
Bob Wilson | cf55652 | 2010-12-07 22:40:02 +0000 | [diff] [blame] | 2108 | llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 2109 | llvm::Value *EmitNeonShiftVector(llvm::Value *V, const llvm::Type *Ty, |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 2110 | bool negateForRightShift); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2111 | |
Bill Wendling | aa51e51 | 2010-10-09 08:47:25 +0000 | [diff] [blame] | 2112 | llvm::Value *BuildVector(const llvm::SmallVectorImpl<llvm::Value*> &Ops); |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 2113 | llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
| 2114 | llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2115 | |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 2116 | llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2117 | llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 2118 | llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E); |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 2119 | RValue EmitObjCMessageExpr(const ObjCMessageExpr *E, |
| 2120 | ReturnValueSlot Return = ReturnValueSlot()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 2121 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2122 | /// Retrieves the default cleanup kind for an ARC cleanup. |
| 2123 | /// Except under -fobjc-arc-eh, ARC cleanups are normal-only. |
| 2124 | CleanupKind getARCCleanupKind() { |
| 2125 | return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions |
| 2126 | ? NormalAndEHCleanup : NormalCleanup; |
| 2127 | } |
| 2128 | |
| 2129 | // ARC primitives. |
| 2130 | void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr); |
| 2131 | void EmitARCDestroyWeak(llvm::Value *addr); |
| 2132 | llvm::Value *EmitARCLoadWeak(llvm::Value *addr); |
| 2133 | llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr); |
| 2134 | llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr, |
| 2135 | bool ignored); |
| 2136 | void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src); |
| 2137 | void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src); |
| 2138 | llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value); |
| 2139 | llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2140 | llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value, |
| 2141 | bool ignored); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2142 | llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value, |
| 2143 | bool ignored); |
| 2144 | llvm::Value *EmitARCRetain(QualType type, llvm::Value *value); |
| 2145 | llvm::Value *EmitARCRetainNonBlock(llvm::Value *value); |
| 2146 | llvm::Value *EmitARCRetainBlock(llvm::Value *value); |
| 2147 | void EmitARCRelease(llvm::Value *value, bool precise); |
| 2148 | llvm::Value *EmitARCAutorelease(llvm::Value *value); |
| 2149 | llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value); |
| 2150 | llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value); |
| 2151 | llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value); |
| 2152 | |
| 2153 | std::pair<LValue,llvm::Value*> |
| 2154 | EmitARCStoreAutoreleasing(const BinaryOperator *e); |
| 2155 | std::pair<LValue,llvm::Value*> |
| 2156 | EmitARCStoreStrong(const BinaryOperator *e, bool ignored); |
| 2157 | |
| 2158 | llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr); |
| 2159 | llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr); |
| 2160 | llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr); |
| 2161 | |
| 2162 | llvm::Value *EmitARCRetainScalarExpr(const Expr *expr); |
| 2163 | llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr); |
| 2164 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2165 | static Destroyer destroyARCStrongImprecise; |
| 2166 | static Destroyer destroyARCStrongPrecise; |
| 2167 | static Destroyer destroyARCWeak; |
| 2168 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2169 | void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr); |
| 2170 | llvm::Value *EmitObjCAutoreleasePoolPush(); |
| 2171 | llvm::Value *EmitObjCMRRAutoreleasePoolPush(); |
| 2172 | void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr); |
| 2173 | void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr); |
| 2174 | |
Anders Carlsson | 4029ca7 | 2009-05-20 00:24:07 +0000 | [diff] [blame] | 2175 | /// EmitReferenceBindingToExpr - Emits a reference binding to the passed in |
| 2176 | /// expression. Will emit a temporary variable if E is not an LValue. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2177 | RValue EmitReferenceBindingToExpr(const Expr* E, |
Anders Carlsson | 32f36ba | 2010-06-26 16:35:32 +0000 | [diff] [blame] | 2178 | const NamedDecl *InitializedDecl); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 2179 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 2180 | //===--------------------------------------------------------------------===// |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 2181 | // Expression Emission |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 2182 | //===--------------------------------------------------------------------===// |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 2183 | |
| 2184 | // Expressions are broken into three classes: scalar, complex, aggregate. |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2185 | |
| 2186 | /// EmitScalarExpr - Emit the computation of the specified expression of LLVM |
| 2187 | /// scalar type, returning the result. |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 2188 | llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2189 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 2190 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 2191 | /// specified destination type, both of which are LLVM scalar types. |
| 2192 | llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy, |
| 2193 | QualType DstTy); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2194 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2195 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2196 | /// complex type to the specified destination type, where the destination type |
| 2197 | /// is an LLVM scalar type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2198 | llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, |
| 2199 | QualType DstTy); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2200 | |
| 2201 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2202 | /// EmitAggExpr - Emit the computation of the specified expression |
| 2203 | /// of aggregate type. The result is computed into the given slot, |
| 2204 | /// which may be null to indicate that the value is not needed. |
Fariborz Jahanian | 474e2fe | 2010-09-16 00:20:07 +0000 | [diff] [blame] | 2205 | void EmitAggExpr(const Expr *E, AggValueSlot AS, bool IgnoreResult = false); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2206 | |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 2207 | /// EmitAggExprToLValue - Emit the computation of the specified expression of |
| 2208 | /// aggregate type into a temporary LValue. |
| 2209 | LValue EmitAggExprToLValue(const Expr *E); |
| 2210 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2211 | /// EmitGCMemmoveCollectable - Emit special API for structs with object |
| 2212 | /// pointers. |
| 2213 | void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 2214 | QualType Ty); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2215 | |
John McCall | 0c24c80 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2216 | /// EmitExtendGCLifetime - Given a pointer to an Objective-C object, |
| 2217 | /// make sure it survives garbage collection until this point. |
| 2218 | void EmitExtendGCLifetime(llvm::Value *object); |
| 2219 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 2220 | /// EmitComplexExpr - Emit the computation of the specified expression of |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 2221 | /// complex type, returning the result. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 2222 | ComplexPairTy EmitComplexExpr(const Expr *E, |
| 2223 | bool IgnoreReal = false, |
| 2224 | bool IgnoreImag = false); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2225 | |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 2226 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 2227 | /// of complex type, storing into the specified Value*. |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 2228 | void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr, |
| 2229 | bool DestIsVolatile); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 2230 | |
| 2231 | /// StoreComplexToAddr - Store a complex number into the specified address. |
| 2232 | void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr, |
| 2233 | bool DestIsVolatile); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 2234 | /// LoadComplexFromAddr - Load a complex number from the specified address. |
| 2235 | ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile); |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 2236 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 2237 | /// CreateStaticVarDecl - Create a zero-initialized LLVM global for |
| 2238 | /// a static local variable. |
| 2239 | llvm::GlobalVariable *CreateStaticVarDecl(const VarDecl &D, |
| 2240 | const char *Separator, |
Mike Stump | f71d232 | 2009-11-30 20:08:49 +0000 | [diff] [blame] | 2241 | llvm::GlobalValue::LinkageTypes Linkage); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2242 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 2243 | /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 2244 | /// global variable that has already been created for it. If the initializer |
| 2245 | /// has a different type than GV does, this may free GV and return a different |
| 2246 | /// one. Otherwise it just returns GV. |
| 2247 | llvm::GlobalVariable * |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 2248 | AddInitializerToStaticVarDecl(const VarDecl &D, |
| 2249 | llvm::GlobalVariable *GV); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2250 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 2251 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 2252 | /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++ |
| 2253 | /// variable with global storage. |
| 2254 | void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr); |
| 2255 | |
| 2256 | /// EmitCXXGlobalDtorRegistration - Emits a call to register the global ptr |
| 2257 | /// with the C++ runtime so that its destructor will be called at exit. |
Fariborz Jahanian | 88f4280 | 2009-11-10 19:24:06 +0000 | [diff] [blame] | 2258 | void EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 2259 | llvm::Constant *DeclPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 2261 | /// Emit code in this function to perform a guarded variable |
| 2262 | /// initialization. Guarded initializations are used when it's not |
| 2263 | /// possible to prove that an initialization will be done exactly |
| 2264 | /// once, e.g. with a static local variable or a static data member |
| 2265 | /// of a class template. |
| 2266 | void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2267 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 2268 | /// GenerateCXXGlobalInitFunc - Generates code for initializing global |
| 2269 | /// variables. |
| 2270 | void GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 2271 | llvm::Constant **Decls, |
| 2272 | unsigned NumDecls); |
| 2273 | |
| 2274 | /// GenerateCXXGlobalDtorFunc - Generates code for destroying global |
| 2275 | /// variables. |
| 2276 | void GenerateCXXGlobalDtorFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 2277 | const std::vector<std::pair<llvm::WeakVH, |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 2278 | llvm::Constant*> > &DtorsAndObjects); |
| 2279 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2280 | void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
| 2281 | const VarDecl *D, |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 2282 | llvm::GlobalVariable *Addr); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 2283 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2284 | void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 2285 | |
| 2286 | void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 2287 | const Expr *Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 2289 | RValue EmitExprWithCleanups(const ExprWithCleanups *E, |
| 2290 | AggValueSlot Slot =AggValueSlot::ignored()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2291 | |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 2292 | void EmitCXXThrowExpr(const CXXThrowExpr *E); |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 2293 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 2294 | //===--------------------------------------------------------------------===// |
| 2295 | // Internal Helpers |
| 2296 | //===--------------------------------------------------------------------===// |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2298 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 2299 | /// this statement is not executed normally, it not containing a label means |
| 2300 | /// that we can just remove the code. |
| 2301 | static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2302 | |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 2303 | /// containsBreak - Return true if the statement contains a break out of it. |
| 2304 | /// If the statement (recursively) contains a switch or loop with a break |
| 2305 | /// inside of it, this is fine. |
| 2306 | static bool containsBreak(const Stmt *S); |
| 2307 | |
Daniel Dunbar | 4bc0455 | 2008-11-12 10:12:14 +0000 | [diff] [blame] | 2308 | /// ConstantFoldsToSimpleInteger - If the specified expression does not fold |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 2309 | /// to a constant, or if it does but contains a label, return false. If it |
| 2310 | /// constant folds return true and set the boolean result in Result. |
| 2311 | bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2312 | |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 2313 | /// ConstantFoldsToSimpleInteger - If the specified expression does not fold |
| 2314 | /// to a constant, or if it does but contains a label, return false. If it |
| 2315 | /// constant folds return true and set the folded value. |
| 2316 | bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APInt &Result); |
| 2317 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 2318 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an |
| 2319 | /// if statement) to the specified blocks. Based on the condition, this might |
| 2320 | /// try to simplify the codegen of the conditional based on the branch. |
Chris Lattner | 9bc47e2 | 2008-11-12 07:46:33 +0000 | [diff] [blame] | 2321 | void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, |
Daniel Dunbar | 4bc0455 | 2008-11-12 10:12:14 +0000 | [diff] [blame] | 2322 | llvm::BasicBlock *FalseBlock); |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 2323 | |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 2324 | /// getTrapBB - Create a basic block that will call the trap intrinsic. We'll |
| 2325 | /// generate a branch around the created basic block as necessary. |
Chris Lattner | 6c552c1 | 2010-07-20 20:19:24 +0000 | [diff] [blame] | 2326 | llvm::BasicBlock *getTrapBB(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2327 | |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 2328 | /// EmitCallArg - Emit a single call argument. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2329 | void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType); |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 2330 | |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2331 | /// EmitDelegateCallArg - We are performing a delegate call; that |
| 2332 | /// is, the current function is delegating to another one. Produce |
| 2333 | /// a r-value suitable for passing the given parameter. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2334 | void EmitDelegateCallArg(CallArgList &args, const VarDecl *param); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2335 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 2336 | private: |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2337 | void EmitReturnOfRValue(RValue RV, QualType Ty); |
| 2338 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2339 | /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty |
| 2340 | /// from function arguments into \arg Dst. See ABIArgInfo::Expand. |
| 2341 | /// |
| 2342 | /// \param AI - The first function argument of the expansion. |
| 2343 | /// \return The argument following the last expanded function |
| 2344 | /// argument. |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2345 | llvm::Function::arg_iterator |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2346 | ExpandTypeFromArgs(QualType Ty, LValue Dst, |
| 2347 | llvm::Function::arg_iterator AI); |
| 2348 | |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2349 | /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type for \arg |
| 2350 | /// Ty, into individual arguments on the provided vector \arg Args. See |
| 2351 | /// ABIArgInfo::Expand. |
| 2352 | void ExpandTypeToArgs(QualType Ty, RValue Src, |
Chris Lattner | 811bf36 | 2011-07-12 06:29:11 +0000 | [diff] [blame] | 2353 | llvm::SmallVector<llvm::Value*, 16> &Args, |
| 2354 | llvm::FunctionType *IRFuncTy); |
Anders Carlsson | c8c7b18 | 2009-01-11 19:40:10 +0000 | [diff] [blame] | 2355 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | llvm::Value* EmitAsmInput(const AsmStmt &S, |
Daniel Dunbar | b84e8a6 | 2009-05-04 06:56:16 +0000 | [diff] [blame] | 2357 | const TargetInfo::ConstraintInfo &Info, |
Anders Carlsson | c8c7b18 | 2009-01-11 19:40:10 +0000 | [diff] [blame] | 2358 | const Expr *InputExpr, std::string &ConstraintStr); |
Mike Stump | 0dd9e88 | 2009-02-08 23:14:22 +0000 | [diff] [blame] | 2359 | |
Eli Friedman | 6d7cfd7 | 2010-07-16 00:55:21 +0000 | [diff] [blame] | 2360 | llvm::Value* EmitAsmInputLValue(const AsmStmt &S, |
| 2361 | const TargetInfo::ConstraintInfo &Info, |
| 2362 | LValue InputValue, QualType InputType, |
| 2363 | std::string &ConstraintStr); |
| 2364 | |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 2365 | /// EmitCallArgs - Emit call arguments for a function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | /// The CallArgTypeInfo parameter is used for iterating over the known |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2367 | /// argument types of the function being called. |
| 2368 | template<typename T> |
| 2369 | void EmitCallArgs(CallArgList& Args, const T* CallArgTypeInfo, |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 2370 | CallExpr::const_arg_iterator ArgBeg, |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2371 | CallExpr::const_arg_iterator ArgEnd) { |
| 2372 | CallExpr::const_arg_iterator Arg = ArgBeg; |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 2373 | |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2374 | // First, use the argument types that the type info knows about |
| 2375 | if (CallArgTypeInfo) { |
| 2376 | for (typename T::arg_type_iterator I = CallArgTypeInfo->arg_type_begin(), |
| 2377 | E = CallArgTypeInfo->arg_type_end(); I != E; ++I, ++Arg) { |
Eli Friedman | 44b0a3e | 2009-11-18 03:42:04 +0000 | [diff] [blame] | 2378 | assert(Arg != ArgEnd && "Running over edge of argument list!"); |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2379 | QualType ArgType = *I; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2380 | #ifndef NDEBUG |
| 2381 | QualType ActualArgType = Arg->getType(); |
| 2382 | if (ArgType->isPointerType() && ActualArgType->isPointerType()) { |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2383 | QualType ActualBaseType = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2384 | ActualArgType->getAs<PointerType>()->getPointeeType(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2385 | QualType ArgBaseType = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2386 | ArgType->getAs<PointerType>()->getPointeeType(); |
| 2387 | if (ArgBaseType->isVariableArrayType()) { |
| 2388 | if (const VariableArrayType *VAT = |
| 2389 | getContext().getAsVariableArrayType(ActualBaseType)) { |
| 2390 | if (!VAT->getSizeExpr()) |
| 2391 | ActualArgType = ArgType; |
| 2392 | } |
| 2393 | } |
| 2394 | } |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2395 | assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2396 | getTypePtr() == |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2397 | getContext().getCanonicalType(ActualArgType).getTypePtr() && |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2398 | "type mismatch in call argument!"); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2399 | #endif |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2400 | EmitCallArg(Args, *Arg, ArgType); |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2401 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | |
| 2403 | // Either we've emitted all the call args, or we have a call to a |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2404 | // variadic function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | assert((Arg == ArgEnd || CallArgTypeInfo->isVariadic()) && |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2406 | "Extra arguments in non-variadic function!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2408 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2409 | |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2410 | // If we still have any arguments, emit them using the type of the argument. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2411 | for (; Arg != ArgEnd; ++Arg) |
| 2412 | EmitCallArg(Args, *Arg, Arg->getType()); |
Anders Carlsson | af23f69 | 2009-04-18 20:20:22 +0000 | [diff] [blame] | 2413 | } |
John McCall | 492c4f9 | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 2414 | |
| 2415 | const TargetCodeGenInfo &getTargetHooks() const { |
| 2416 | return CGM.getTargetCodeGenInfo(); |
| 2417 | } |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2418 | |
| 2419 | void EmitDeclMetadata(); |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 2420 | |
| 2421 | CodeGenModule::ByrefHelpers * |
| 2422 | buildByrefHelpers(const llvm::StructType &byrefType, |
| 2423 | const AutoVarEmission &emission); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2424 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2426 | /// Helper class with most of the code for saving a value for a |
| 2427 | /// conditional expression cleanup. |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 2428 | struct DominatingLLVMValue { |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2429 | typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type; |
| 2430 | |
| 2431 | /// Answer whether the given value needs extra work to be saved. |
| 2432 | static bool needsSaving(llvm::Value *value) { |
| 2433 | // If it's not an instruction, we don't need to save. |
| 2434 | if (!isa<llvm::Instruction>(value)) return false; |
| 2435 | |
| 2436 | // If it's an instruction in the entry block, we don't need to save. |
| 2437 | llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent(); |
| 2438 | return (block != &block->getParent()->getEntryBlock()); |
| 2439 | } |
| 2440 | |
| 2441 | /// Try to save the given value. |
| 2442 | static saved_type save(CodeGenFunction &CGF, llvm::Value *value) { |
| 2443 | if (!needsSaving(value)) return saved_type(value, false); |
| 2444 | |
| 2445 | // Otherwise we need an alloca. |
| 2446 | llvm::Value *alloca = |
| 2447 | CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save"); |
| 2448 | CGF.Builder.CreateStore(value, alloca); |
| 2449 | |
| 2450 | return saved_type(alloca, true); |
| 2451 | } |
| 2452 | |
| 2453 | static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) { |
| 2454 | if (!value.getInt()) return value.getPointer(); |
| 2455 | return CGF.Builder.CreateLoad(value.getPointer()); |
| 2456 | } |
| 2457 | }; |
| 2458 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 2459 | /// A partial specialization of DominatingValue for llvm::Values that |
| 2460 | /// might be llvm::Instructions. |
| 2461 | template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue { |
| 2462 | typedef T *type; |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2463 | static type restore(CodeGenFunction &CGF, saved_type value) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 2464 | return static_cast<T*>(DominatingLLVMValue::restore(CGF, value)); |
| 2465 | } |
| 2466 | }; |
| 2467 | |
| 2468 | /// A specialization of DominatingValue for RValue. |
| 2469 | template <> struct DominatingValue<RValue> { |
| 2470 | typedef RValue type; |
| 2471 | class saved_type { |
| 2472 | enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral, |
| 2473 | AggregateAddress, ComplexAddress }; |
| 2474 | |
| 2475 | llvm::Value *Value; |
| 2476 | Kind K; |
| 2477 | saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {} |
| 2478 | |
| 2479 | public: |
| 2480 | static bool needsSaving(RValue value); |
| 2481 | static saved_type save(CodeGenFunction &CGF, RValue value); |
| 2482 | RValue restore(CodeGenFunction &CGF); |
| 2483 | |
| 2484 | // implementations in CGExprCXX.cpp |
| 2485 | }; |
| 2486 | |
| 2487 | static bool needsSaving(type value) { |
| 2488 | return saved_type::needsSaving(value); |
| 2489 | } |
| 2490 | static saved_type save(CodeGenFunction &CGF, type value) { |
| 2491 | return saved_type::save(CGF, value); |
| 2492 | } |
| 2493 | static type restore(CodeGenFunction &CGF, saved_type value) { |
| 2494 | return value.restore(CGF); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2495 | } |
| 2496 | }; |
| 2497 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2498 | } // end namespace CodeGen |
| 2499 | } // end namespace clang |
| 2500 | |
| 2501 | #endif |