blob: b4fba596a29df433d6959b235f7c6bbfe003436e [file] [log] [blame]
Reid Klecknerd29f1342013-06-19 17:07:50 +00001//===-- EHScopeStack.h - Stack for cleanup IR generation --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes should be the minimum interface required for other parts of
11// CodeGen to emit cleanups. The implementation is in CGCleanup.cpp and other
12// implemenentation details that are not widely needed are in CGCleanup.h.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000016#ifndef LLVM_CLANG_LIB_CODEGEN_EHSCOPESTACK_H
17#define LLVM_CLANG_LIB_CODEGEN_EHSCOPESTACK_H
Reid Klecknerd29f1342013-06-19 17:07:50 +000018
19#include "clang/Basic/LLVM.h"
Benjamin Kramer51680bc2015-03-12 23:41:40 +000020#include "llvm/ADT/STLExtras.h"
Reid Klecknerd29f1342013-06-19 17:07:50 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/IR/BasicBlock.h"
Reid Klecknerd29f1342013-06-19 17:07:50 +000023#include "llvm/IR/Instructions.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000024#include "llvm/IR/Value.h"
Reid Klecknerd29f1342013-06-19 17:07:50 +000025
26namespace clang {
27namespace CodeGen {
28
29class CodeGenFunction;
30
31/// A branch fixup. These are required when emitting a goto to a
32/// label which hasn't been emitted yet. The goto is optimistically
33/// emitted as a branch to the basic block for the label, and (if it
34/// occurs in a scope with non-trivial cleanups) a fixup is added to
35/// the innermost cleanup. When a (normal) cleanup is popped, any
36/// unresolved fixups in that scope are threaded through the cleanup.
37struct BranchFixup {
38 /// The block containing the terminator which needs to be modified
39 /// into a switch if this fixup is resolved into the current scope.
40 /// If null, LatestBranch points directly to the destination.
41 llvm::BasicBlock *OptimisticBranchBlock;
42
43 /// The ultimate destination of the branch.
44 ///
45 /// This can be set to null to indicate that this fixup was
46 /// successfully resolved.
47 llvm::BasicBlock *Destination;
48
49 /// The destination index value.
50 unsigned DestinationIndex;
51
52 /// The initial branch of the fixup.
53 llvm::BranchInst *InitialBranch;
54};
55
56template <class T> struct InvariantValue {
57 typedef T type;
58 typedef T saved_type;
59 static bool needsSaving(type value) { return false; }
60 static saved_type save(CodeGenFunction &CGF, type value) { return value; }
61 static type restore(CodeGenFunction &CGF, saved_type value) { return value; }
62};
63
64/// A metaprogramming class for ensuring that a value will dominate an
65/// arbitrary position in a function.
66template <class T> struct DominatingValue : InvariantValue<T> {};
67
68template <class T, bool mightBeInstruction =
Benjamin Kramered2f4762014-03-07 14:30:23 +000069 std::is_base_of<llvm::Value, T>::value &&
70 !std::is_base_of<llvm::Constant, T>::value &&
71 !std::is_base_of<llvm::BasicBlock, T>::value>
Reid Klecknerd29f1342013-06-19 17:07:50 +000072struct DominatingPointer;
73template <class T> struct DominatingPointer<T,false> : InvariantValue<T*> {};
74// template <class T> struct DominatingPointer<T,true> at end of file
75
76template <class T> struct DominatingValue<T*> : DominatingPointer<T> {};
77
Reid Klecknerc311aba2014-10-31 23:33:56 +000078enum CleanupKind : unsigned {
Nico Weberebf9a052015-02-25 03:58:36 +000079 /// Denotes a cleanup that should run when a scope is exited using exceptional
80 /// control flow (a throw statement leading to stack unwinding, ).
Reid Klecknerd29f1342013-06-19 17:07:50 +000081 EHCleanup = 0x1,
Nico Weberebf9a052015-02-25 03:58:36 +000082
83 /// Denotes a cleanup that should run when a scope is exited using normal
84 /// control flow (falling off the end of the scope, return, goto, ...).
Reid Klecknerd29f1342013-06-19 17:07:50 +000085 NormalCleanup = 0x2,
Nico Weberebf9a052015-02-25 03:58:36 +000086
Reid Klecknerd29f1342013-06-19 17:07:50 +000087 NormalAndEHCleanup = EHCleanup | NormalCleanup,
88
89 InactiveCleanup = 0x4,
90 InactiveEHCleanup = EHCleanup | InactiveCleanup,
91 InactiveNormalCleanup = NormalCleanup | InactiveCleanup,
92 InactiveNormalAndEHCleanup = NormalAndEHCleanup | InactiveCleanup
93};
94
95/// A stack of scopes which respond to exceptions, including cleanups
96/// and catch blocks.
97class EHScopeStack {
98public:
James Y Knight8041e592015-07-17 21:58:11 +000099 /* Should switch to alignof(uint64_t) instead of 8, when EHCleanupScope can */
100 enum { ScopeStackAlignment = 8 };
James Y Knight53c76162015-07-17 18:21:37 +0000101
Reid Klecknerd29f1342013-06-19 17:07:50 +0000102 /// A saved depth on the scope stack. This is necessary because
103 /// pushing scopes onto the stack invalidates iterators.
104 class stable_iterator {
105 friend class EHScopeStack;
106
107 /// Offset from StartOfData to EndOfBuffer.
108 ptrdiff_t Size;
109
110 stable_iterator(ptrdiff_t Size) : Size(Size) {}
111
112 public:
113 static stable_iterator invalid() { return stable_iterator(-1); }
114 stable_iterator() : Size(-1) {}
115
116 bool isValid() const { return Size >= 0; }
117
118 /// Returns true if this scope encloses I.
119 /// Returns false if I is invalid.
120 /// This scope must be valid.
121 bool encloses(stable_iterator I) const { return Size <= I.Size; }
122
123 /// Returns true if this scope strictly encloses I: that is,
124 /// if it encloses I and is not I.
125 /// Returns false is I is invalid.
126 /// This scope must be valid.
127 bool strictlyEncloses(stable_iterator I) const { return Size < I.Size; }
128
129 friend bool operator==(stable_iterator A, stable_iterator B) {
130 return A.Size == B.Size;
131 }
132 friend bool operator!=(stable_iterator A, stable_iterator B) {
133 return A.Size != B.Size;
134 }
135 };
136
137 /// Information for lazily generating a cleanup. Subclasses must be
138 /// POD-like: cleanups will not be destructed, and they will be
139 /// allocated on the cleanup stack and freely copied and moved
140 /// around.
141 ///
142 /// Cleanup implementations should generally be declared in an
143 /// anonymous namespace.
144 class Cleanup {
145 // Anchor the construction vtable.
146 virtual void anchor();
David Blaikiefb2b7962015-08-18 22:09:28 +0000147
Reid Klecknerd29f1342013-06-19 17:07:50 +0000148 public:
David Blaikiefb2b7962015-08-18 22:09:28 +0000149 Cleanup(const Cleanup &) = default;
David Blaikie50ccaa62015-08-18 22:10:49 +0000150 Cleanup(Cleanup &&) {}
David Blaikiefb2b7962015-08-18 22:09:28 +0000151 Cleanup() = default;
152
Reid Klecknerd29f1342013-06-19 17:07:50 +0000153 /// Generation flags.
154 class Flags {
155 enum {
156 F_IsForEH = 0x1,
157 F_IsNormalCleanupKind = 0x2,
158 F_IsEHCleanupKind = 0x4
159 };
160 unsigned flags;
161
162 public:
163 Flags() : flags(0) {}
164
165 /// isForEH - true if the current emission is for an EH cleanup.
166 bool isForEHCleanup() const { return flags & F_IsForEH; }
167 bool isForNormalCleanup() const { return !isForEHCleanup(); }
168 void setIsForEHCleanup() { flags |= F_IsForEH; }
169
170 bool isNormalCleanupKind() const { return flags & F_IsNormalCleanupKind; }
171 void setIsNormalCleanupKind() { flags |= F_IsNormalCleanupKind; }
172
173 /// isEHCleanupKind - true if the cleanup was pushed as an EH
174 /// cleanup.
175 bool isEHCleanupKind() const { return flags & F_IsEHCleanupKind; }
176 void setIsEHCleanupKind() { flags |= F_IsEHCleanupKind; }
177 };
178
179 // Provide a virtual destructor to suppress a very common warning
180 // that unfortunately cannot be suppressed without this. Cleanups
181 // should not rely on this destructor ever being called.
182 virtual ~Cleanup() {}
183
184 /// Emit the cleanup. For normal cleanups, this is run in the
185 /// same EH context as when the cleanup was pushed, i.e. the
186 /// immediately-enclosing context of the cleanup scope. For
187 /// EH cleanups, this is run in a terminate context.
188 ///
189 // \param flags cleanup kind.
190 virtual void Emit(CodeGenFunction &CGF, Flags flags) = 0;
191 };
192
Benjamin Kramer51680bc2015-03-12 23:41:40 +0000193 /// ConditionalCleanup stores the saved form of its parameters,
Reid Klecknerd29f1342013-06-19 17:07:50 +0000194 /// then restores them and performs the cleanup.
Benjamin Kramer51680bc2015-03-12 23:41:40 +0000195 template <class T, class... As> class ConditionalCleanup : public Cleanup {
196 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
197 SavedTuple Saved;
198
199 template <std::size_t... Is>
200 T restore(CodeGenFunction &CGF, llvm::index_sequence<Is...>) {
201 // It's important that the restores are emitted in order. The braced init
202 // list guarentees that.
203 return T{DominatingValue<As>::restore(CGF, std::get<Is>(Saved))...};
204 }
Reid Klecknerd29f1342013-06-19 17:07:50 +0000205
Craig Topper4f12f102014-03-12 06:41:41 +0000206 void Emit(CodeGenFunction &CGF, Flags flags) override {
Benjamin Kramer51680bc2015-03-12 23:41:40 +0000207 restore(CGF, llvm::index_sequence_for<As...>()).Emit(CGF, flags);
Reid Klecknerd29f1342013-06-19 17:07:50 +0000208 }
209
210 public:
Benjamin Kramer51680bc2015-03-12 23:41:40 +0000211 ConditionalCleanup(typename DominatingValue<As>::saved_type... A)
212 : Saved(A...) {}
Reid Klecknerd29f1342013-06-19 17:07:50 +0000213
Benjamin Kramer51680bc2015-03-12 23:41:40 +0000214 ConditionalCleanup(SavedTuple Tuple) : Saved(std::move(Tuple)) {}
Reid Klecknerd29f1342013-06-19 17:07:50 +0000215 };
216
217private:
218 // The implementation for this class is in CGException.h and
219 // CGException.cpp; the definition is here because it's used as a
220 // member of CodeGenFunction.
221
222 /// The start of the scope-stack buffer, i.e. the allocated pointer
223 /// for the buffer. All of these pointers are either simultaneously
224 /// null or simultaneously valid.
225 char *StartOfBuffer;
226
227 /// The end of the buffer.
228 char *EndOfBuffer;
229
230 /// The first valid entry in the buffer.
231 char *StartOfData;
232
233 /// The innermost normal cleanup on the stack.
234 stable_iterator InnermostNormalCleanup;
235
236 /// The innermost EH scope on the stack.
237 stable_iterator InnermostEHScope;
238
239 /// The current set of branch fixups. A branch fixup is a jump to
240 /// an as-yet unemitted label, i.e. a label for which we don't yet
241 /// know the EH stack depth. Whenever we pop a cleanup, we have
242 /// to thread all the current branch fixups through it.
243 ///
244 /// Fixups are recorded as the Use of the respective branch or
245 /// switch statement. The use points to the final destination.
246 /// When popping out of a cleanup, these uses are threaded through
247 /// the cleanup and adjusted to point to the new cleanup.
248 ///
249 /// Note that branches are allowed to jump into protected scopes
250 /// in certain situations; e.g. the following code is legal:
251 /// struct A { ~A(); }; // trivial ctor, non-trivial dtor
252 /// goto foo;
253 /// A a;
254 /// foo:
255 /// bar();
256 SmallVector<BranchFixup, 8> BranchFixups;
257
258 char *allocate(size_t Size);
James Y Knight53c76162015-07-17 18:21:37 +0000259 void deallocate(size_t Size);
Reid Klecknerd29f1342013-06-19 17:07:50 +0000260
261 void *pushCleanup(CleanupKind K, size_t DataSize);
262
263public:
Craig Topper8a13c412014-05-21 05:09:00 +0000264 EHScopeStack() : StartOfBuffer(nullptr), EndOfBuffer(nullptr),
265 StartOfData(nullptr), InnermostNormalCleanup(stable_end()),
Reid Klecknerd29f1342013-06-19 17:07:50 +0000266 InnermostEHScope(stable_end()) {}
267 ~EHScopeStack() { delete[] StartOfBuffer; }
268
Reid Klecknerd29f1342013-06-19 17:07:50 +0000269 /// Push a lazily-created cleanup on the stack.
Benjamin Kramerc582c892015-02-15 20:11:22 +0000270 template <class T, class... As> void pushCleanup(CleanupKind Kind, As... A) {
James Y Knight53c76162015-07-17 18:21:37 +0000271 static_assert(llvm::AlignOf<T>::Alignment <= ScopeStackAlignment,
272 "Cleanup's alignment is too large.");
Reid Klecknerd29f1342013-06-19 17:07:50 +0000273 void *Buffer = pushCleanup(Kind, sizeof(T));
Benjamin Kramerc582c892015-02-15 20:11:22 +0000274 Cleanup *Obj = new (Buffer) T(A...);
Reid Klecknerd29f1342013-06-19 17:07:50 +0000275 (void) Obj;
276 }
277
Benjamin Kramer51680bc2015-03-12 23:41:40 +0000278 /// Push a lazily-created cleanup on the stack. Tuple version.
279 template <class T, class... As>
Benjamin Kramer7f1f6b52015-03-12 23:46:55 +0000280 void pushCleanupTuple(CleanupKind Kind, std::tuple<As...> A) {
James Y Knight53c76162015-07-17 18:21:37 +0000281 static_assert(llvm::AlignOf<T>::Alignment <= ScopeStackAlignment,
282 "Cleanup's alignment is too large.");
Benjamin Kramer51680bc2015-03-12 23:41:40 +0000283 void *Buffer = pushCleanup(Kind, sizeof(T));
284 Cleanup *Obj = new (Buffer) T(std::move(A));
285 (void) Obj;
286 }
287
Reid Klecknerd29f1342013-06-19 17:07:50 +0000288 // Feel free to add more variants of the following:
289
290 /// Push a cleanup with non-constant storage requirements on the
291 /// stack. The cleanup type must provide an additional static method:
292 /// static size_t getExtraSize(size_t);
293 /// The argument to this method will be the value N, which will also
294 /// be passed as the first argument to the constructor.
295 ///
296 /// The data stored in the extra storage must obey the same
297 /// restrictions as normal cleanup member data.
298 ///
299 /// The pointer returned from this method is valid until the cleanup
300 /// stack is modified.
Benjamin Kramerc582c892015-02-15 20:11:22 +0000301 template <class T, class... As>
Benjamin Kramer583089c2015-02-15 20:24:47 +0000302 T *pushCleanupWithExtra(CleanupKind Kind, size_t N, As... A) {
James Y Knight53c76162015-07-17 18:21:37 +0000303 static_assert(llvm::AlignOf<T>::Alignment <= ScopeStackAlignment,
304 "Cleanup's alignment is too large.");
Reid Klecknerd29f1342013-06-19 17:07:50 +0000305 void *Buffer = pushCleanup(Kind, sizeof(T) + T::getExtraSize(N));
Benjamin Kramerc582c892015-02-15 20:11:22 +0000306 return new (Buffer) T(N, A...);
Reid Klecknerd29f1342013-06-19 17:07:50 +0000307 }
308
309 void pushCopyOfCleanup(CleanupKind Kind, const void *Cleanup, size_t Size) {
310 void *Buffer = pushCleanup(Kind, Size);
311 std::memcpy(Buffer, Cleanup, Size);
312 }
313
314 /// Pops a cleanup scope off the stack. This is private to CGCleanup.cpp.
315 void popCleanup();
316
317 /// Push a set of catch handlers on the stack. The catch is
318 /// uninitialized and will need to have the given number of handlers
319 /// set on it.
320 class EHCatchScope *pushCatch(unsigned NumHandlers);
321
322 /// Pops a catch scope off the stack. This is private to CGException.cpp.
323 void popCatch();
324
325 /// Push an exceptions filter on the stack.
326 class EHFilterScope *pushFilter(unsigned NumFilters);
327
328 /// Pops an exceptions filter off the stack.
329 void popFilter();
330
331 /// Push a terminate handler on the stack.
332 void pushTerminate();
333
334 /// Pops a terminate handler off the stack.
335 void popTerminate();
336
David Majnemerdbf10452015-07-31 17:58:45 +0000337 void pushCatchEnd(llvm::BasicBlock *CatchEndBlockBB);
338
339 void popCatchEnd();
340
David Majnemerdc012fa2015-04-22 21:38:15 +0000341 // Returns true iff the current scope is either empty or contains only
342 // lifetime markers, i.e. no real cleanup code
343 bool containsOnlyLifetimeMarkers(stable_iterator Old) const;
344
Reid Klecknerd29f1342013-06-19 17:07:50 +0000345 /// Determines whether the exception-scopes stack is empty.
346 bool empty() const { return StartOfData == EndOfBuffer; }
347
348 bool requiresLandingPad() const {
349 return InnermostEHScope != stable_end();
350 }
351
352 /// Determines whether there are any normal cleanups on the stack.
353 bool hasNormalCleanups() const {
354 return InnermostNormalCleanup != stable_end();
355 }
356
357 /// Returns the innermost normal cleanup on the stack, or
358 /// stable_end() if there are no normal cleanups.
359 stable_iterator getInnermostNormalCleanup() const {
360 return InnermostNormalCleanup;
361 }
362 stable_iterator getInnermostActiveNormalCleanup() const;
363
364 stable_iterator getInnermostEHScope() const {
365 return InnermostEHScope;
366 }
367
368 stable_iterator getInnermostActiveEHScope() const;
369
370 /// An unstable reference to a scope-stack depth. Invalidated by
371 /// pushes but not pops.
372 class iterator;
373
374 /// Returns an iterator pointing to the innermost EH scope.
375 iterator begin() const;
376
377 /// Returns an iterator pointing to the outermost EH scope.
378 iterator end() const;
379
380 /// Create a stable reference to the top of the EH stack. The
381 /// returned reference is valid until that scope is popped off the
382 /// stack.
383 stable_iterator stable_begin() const {
384 return stable_iterator(EndOfBuffer - StartOfData);
385 }
386
387 /// Create a stable reference to the bottom of the EH stack.
388 static stable_iterator stable_end() {
389 return stable_iterator(0);
390 }
391
392 /// Translates an iterator into a stable_iterator.
393 stable_iterator stabilize(iterator it) const;
394
395 /// Turn a stable reference to a scope depth into a unstable pointer
396 /// to the EH stack.
397 iterator find(stable_iterator save) const;
398
399 /// Removes the cleanup pointed to by the given stable_iterator.
400 void removeCleanup(stable_iterator save);
401
402 /// Add a branch fixup to the current cleanup scope.
403 BranchFixup &addBranchFixup() {
404 assert(hasNormalCleanups() && "adding fixup in scope without cleanups");
405 BranchFixups.push_back(BranchFixup());
406 return BranchFixups.back();
407 }
408
409 unsigned getNumBranchFixups() const { return BranchFixups.size(); }
410 BranchFixup &getBranchFixup(unsigned I) {
411 assert(I < getNumBranchFixups());
412 return BranchFixups[I];
413 }
414
415 /// Pops lazily-removed fixups from the end of the list. This
416 /// should only be called by procedures which have just popped a
417 /// cleanup or resolved one or more fixups.
418 void popNullFixups();
419
420 /// Clears the branch-fixups list. This should only be called by
421 /// ResolveAllBranchFixups.
422 void clearFixups() { BranchFixups.clear(); }
423};
424
425} // namespace CodeGen
426} // namespace clang
427
428#endif