clang  3.7.0
EHScopeStack.h
Go to the documentation of this file.
1 //===-- 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 
16 #ifndef LLVM_CLANG_LIB_CODEGEN_EHSCOPESTACK_H
17 #define LLVM_CLANG_LIB_CODEGEN_EHSCOPESTACK_H
18 
19 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Value.h"
25 
26 namespace clang {
27 namespace CodeGen {
28 
29 class 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.
37 struct 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 
56 template <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.
66 template <class T> struct DominatingValue : InvariantValue<T> {};
67 
68 template <class T, bool mightBeInstruction =
69  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>
73 template <class T> struct DominatingPointer<T,false> : InvariantValue<T*> {};
74 // template <class T> struct DominatingPointer<T,true> at end of file
75 
76 template <class T> struct DominatingValue<T*> : DominatingPointer<T> {};
77 
78 enum CleanupKind : unsigned {
79  /// Denotes a cleanup that should run when a scope is exited using exceptional
80  /// control flow (a throw statement leading to stack unwinding, ).
81  EHCleanup = 0x1,
82 
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, ...).
86 
88 
93 };
94 
95 /// A stack of scopes which respond to exceptions, including cleanups
96 /// and catch blocks.
97 class EHScopeStack {
98 public:
99  /// A saved depth on the scope stack. This is necessary because
100  /// pushing scopes onto the stack invalidates iterators.
102  friend class EHScopeStack;
103 
104  /// Offset from StartOfData to EndOfBuffer.
105  ptrdiff_t Size;
106 
107  stable_iterator(ptrdiff_t Size) : Size(Size) {}
108 
109  public:
110  static stable_iterator invalid() { return stable_iterator(-1); }
111  stable_iterator() : Size(-1) {}
112 
113  bool isValid() const { return Size >= 0; }
114 
115  /// Returns true if this scope encloses I.
116  /// Returns false if I is invalid.
117  /// This scope must be valid.
118  bool encloses(stable_iterator I) const { return Size <= I.Size; }
119 
120  /// Returns true if this scope strictly encloses I: that is,
121  /// if it encloses I and is not I.
122  /// Returns false is I is invalid.
123  /// This scope must be valid.
124  bool strictlyEncloses(stable_iterator I) const { return Size < I.Size; }
125 
127  return A.Size == B.Size;
128  }
130  return A.Size != B.Size;
131  }
132  };
133 
134  /// Information for lazily generating a cleanup. Subclasses must be
135  /// POD-like: cleanups will not be destructed, and they will be
136  /// allocated on the cleanup stack and freely copied and moved
137  /// around.
138  ///
139  /// Cleanup implementations should generally be declared in an
140  /// anonymous namespace.
141  class Cleanup {
142  // Anchor the construction vtable.
143  virtual void anchor();
144  public:
145  /// Generation flags.
146  class Flags {
147  enum {
148  F_IsForEH = 0x1,
149  F_IsNormalCleanupKind = 0x2,
150  F_IsEHCleanupKind = 0x4
151  };
152  unsigned flags;
153 
154  public:
155  Flags() : flags(0) {}
156 
157  /// isForEH - true if the current emission is for an EH cleanup.
158  bool isForEHCleanup() const { return flags & F_IsForEH; }
159  bool isForNormalCleanup() const { return !isForEHCleanup(); }
160  void setIsForEHCleanup() { flags |= F_IsForEH; }
161 
162  bool isNormalCleanupKind() const { return flags & F_IsNormalCleanupKind; }
163  void setIsNormalCleanupKind() { flags |= F_IsNormalCleanupKind; }
164 
165  /// isEHCleanupKind - true if the cleanup was pushed as an EH
166  /// cleanup.
167  bool isEHCleanupKind() const { return flags & F_IsEHCleanupKind; }
168  void setIsEHCleanupKind() { flags |= F_IsEHCleanupKind; }
169  };
170 
171  // Provide a virtual destructor to suppress a very common warning
172  // that unfortunately cannot be suppressed without this. Cleanups
173  // should not rely on this destructor ever being called.
174  virtual ~Cleanup() {}
175 
176  /// Emit the cleanup. For normal cleanups, this is run in the
177  /// same EH context as when the cleanup was pushed, i.e. the
178  /// immediately-enclosing context of the cleanup scope. For
179  /// EH cleanups, this is run in a terminate context.
180  ///
181  // \param flags cleanup kind.
182  virtual void Emit(CodeGenFunction &CGF, Flags flags) = 0;
183  };
184 
185  /// ConditionalCleanup stores the saved form of its parameters,
186  /// then restores them and performs the cleanup.
187  template <class T, class... As> class ConditionalCleanup : public Cleanup {
188  typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
189  SavedTuple Saved;
190 
191  template <std::size_t... Is>
192  T restore(CodeGenFunction &CGF, llvm::index_sequence<Is...>) {
193  // It's important that the restores are emitted in order. The braced init
194  // list guarentees that.
195  return T{DominatingValue<As>::restore(CGF, std::get<Is>(Saved))...};
196  }
197 
198  void Emit(CodeGenFunction &CGF, Flags flags) override {
199  restore(CGF, llvm::index_sequence_for<As...>()).Emit(CGF, flags);
200  }
201 
202  public:
204  : Saved(A...) {}
205 
206  ConditionalCleanup(SavedTuple Tuple) : Saved(std::move(Tuple)) {}
207  };
208 
209 private:
210  // The implementation for this class is in CGException.h and
211  // CGException.cpp; the definition is here because it's used as a
212  // member of CodeGenFunction.
213 
214  /// The start of the scope-stack buffer, i.e. the allocated pointer
215  /// for the buffer. All of these pointers are either simultaneously
216  /// null or simultaneously valid.
217  char *StartOfBuffer;
218 
219  /// The end of the buffer.
220  char *EndOfBuffer;
221 
222  /// The first valid entry in the buffer.
223  char *StartOfData;
224 
225  /// The innermost normal cleanup on the stack.
226  stable_iterator InnermostNormalCleanup;
227 
228  /// The innermost EH scope on the stack.
229  stable_iterator InnermostEHScope;
230 
231  /// The current set of branch fixups. A branch fixup is a jump to
232  /// an as-yet unemitted label, i.e. a label for which we don't yet
233  /// know the EH stack depth. Whenever we pop a cleanup, we have
234  /// to thread all the current branch fixups through it.
235  ///
236  /// Fixups are recorded as the Use of the respective branch or
237  /// switch statement. The use points to the final destination.
238  /// When popping out of a cleanup, these uses are threaded through
239  /// the cleanup and adjusted to point to the new cleanup.
240  ///
241  /// Note that branches are allowed to jump into protected scopes
242  /// in certain situations; e.g. the following code is legal:
243  /// struct A { ~A(); }; // trivial ctor, non-trivial dtor
244  /// goto foo;
245  /// A a;
246  /// foo:
247  /// bar();
248  SmallVector<BranchFixup, 8> BranchFixups;
249 
250  char *allocate(size_t Size);
251 
252  void *pushCleanup(CleanupKind K, size_t DataSize);
253 
254 public:
255  EHScopeStack() : StartOfBuffer(nullptr), EndOfBuffer(nullptr),
256  StartOfData(nullptr), InnermostNormalCleanup(stable_end()),
257  InnermostEHScope(stable_end()) {}
258  ~EHScopeStack() { delete[] StartOfBuffer; }
259 
260  /// Push a lazily-created cleanup on the stack.
261  template <class T, class... As> void pushCleanup(CleanupKind Kind, As... A) {
262  void *Buffer = pushCleanup(Kind, sizeof(T));
263  Cleanup *Obj = new (Buffer) T(A...);
264  (void) Obj;
265  }
266 
267  /// Push a lazily-created cleanup on the stack. Tuple version.
268  template <class T, class... As>
269  void pushCleanupTuple(CleanupKind Kind, std::tuple<As...> A) {
270  void *Buffer = pushCleanup(Kind, sizeof(T));
271  Cleanup *Obj = new (Buffer) T(std::move(A));
272  (void) Obj;
273  }
274 
275  // Feel free to add more variants of the following:
276 
277  /// Push a cleanup with non-constant storage requirements on the
278  /// stack. The cleanup type must provide an additional static method:
279  /// static size_t getExtraSize(size_t);
280  /// The argument to this method will be the value N, which will also
281  /// be passed as the first argument to the constructor.
282  ///
283  /// The data stored in the extra storage must obey the same
284  /// restrictions as normal cleanup member data.
285  ///
286  /// The pointer returned from this method is valid until the cleanup
287  /// stack is modified.
288  template <class T, class... As>
289  T *pushCleanupWithExtra(CleanupKind Kind, size_t N, As... A) {
290  void *Buffer = pushCleanup(Kind, sizeof(T) + T::getExtraSize(N));
291  return new (Buffer) T(N, A...);
292  }
293 
294  void pushCopyOfCleanup(CleanupKind Kind, const void *Cleanup, size_t Size) {
295  void *Buffer = pushCleanup(Kind, Size);
296  std::memcpy(Buffer, Cleanup, Size);
297  }
298 
299  /// Pops a cleanup scope off the stack. This is private to CGCleanup.cpp.
300  void popCleanup();
301 
302  /// Push a set of catch handlers on the stack. The catch is
303  /// uninitialized and will need to have the given number of handlers
304  /// set on it.
305  class EHCatchScope *pushCatch(unsigned NumHandlers);
306 
307  /// Pops a catch scope off the stack. This is private to CGException.cpp.
308  void popCatch();
309 
310  /// Push an exceptions filter on the stack.
311  class EHFilterScope *pushFilter(unsigned NumFilters);
312 
313  /// Pops an exceptions filter off the stack.
314  void popFilter();
315 
316  /// Push a terminate handler on the stack.
317  void pushTerminate();
318 
319  /// Pops a terminate handler off the stack.
320  void popTerminate();
321 
322  // Returns true iff the current scope is either empty or contains only
323  // lifetime markers, i.e. no real cleanup code
324  bool containsOnlyLifetimeMarkers(stable_iterator Old) const;
325 
326  /// Determines whether the exception-scopes stack is empty.
327  bool empty() const { return StartOfData == EndOfBuffer; }
328 
329  bool requiresLandingPad() const {
330  return InnermostEHScope != stable_end();
331  }
332 
333  /// Determines whether there are any normal cleanups on the stack.
334  bool hasNormalCleanups() const {
335  return InnermostNormalCleanup != stable_end();
336  }
337 
338  /// Returns the innermost normal cleanup on the stack, or
339  /// stable_end() if there are no normal cleanups.
341  return InnermostNormalCleanup;
342  }
343  stable_iterator getInnermostActiveNormalCleanup() const;
344 
346  return InnermostEHScope;
347  }
348 
349  stable_iterator getInnermostActiveEHScope() const;
350 
351  /// An unstable reference to a scope-stack depth. Invalidated by
352  /// pushes but not pops.
353  class iterator;
354 
355  /// Returns an iterator pointing to the innermost EH scope.
356  iterator begin() const;
357 
358  /// Returns an iterator pointing to the outermost EH scope.
359  iterator end() const;
360 
361  /// Create a stable reference to the top of the EH stack. The
362  /// returned reference is valid until that scope is popped off the
363  /// stack.
365  return stable_iterator(EndOfBuffer - StartOfData);
366  }
367 
368  /// Create a stable reference to the bottom of the EH stack.
370  return stable_iterator(0);
371  }
372 
373  /// Translates an iterator into a stable_iterator.
374  stable_iterator stabilize(iterator it) const;
375 
376  /// Turn a stable reference to a scope depth into a unstable pointer
377  /// to the EH stack.
378  iterator find(stable_iterator save) const;
379 
380  /// Removes the cleanup pointed to by the given stable_iterator.
381  void removeCleanup(stable_iterator save);
382 
383  /// Add a branch fixup to the current cleanup scope.
385  assert(hasNormalCleanups() && "adding fixup in scope without cleanups");
386  BranchFixups.push_back(BranchFixup());
387  return BranchFixups.back();
388  }
389 
390  unsigned getNumBranchFixups() const { return BranchFixups.size(); }
392  assert(I < getNumBranchFixups());
393  return BranchFixups[I];
394  }
395 
396  /// Pops lazily-removed fixups from the end of the list. This
397  /// should only be called by procedures which have just popped a
398  /// cleanup or resolved one or more fixups.
399  void popNullFixups();
400 
401  /// Clears the branch-fixups list. This should only be called by
402  /// ResolveAllBranchFixups.
403  void clearFixups() { BranchFixups.clear(); }
404 };
405 
406 } // namespace CodeGen
407 } // namespace clang
408 
409 #endif
void pushTerminate()
Push a terminate handler on the stack.
Definition: CGCleanup.cpp:240
iterator end() const
Returns an iterator pointing to the outermost EH scope.
Definition: CGCleanup.h:522
ConditionalCleanup(typename DominatingValue< As >::saved_type...A)
Definition: EHScopeStack.h:203
void pushCleanup(CleanupKind Kind, As...A)
Push a lazily-created cleanup on the stack.
Definition: EHScopeStack.h:261
static stable_iterator stable_end()
Create a stable reference to the bottom of the EH stack.
Definition: EHScopeStack.h:369
stable_iterator getInnermostActiveEHScope() const
Definition: CGCleanup.cpp:150
static saved_type save(CodeGenFunction &CGF, type value)
Definition: EHScopeStack.h:60
T * pushCleanupWithExtra(CleanupKind Kind, size_t N, As...A)
Definition: EHScopeStack.h:289
void pushCleanupTuple(CleanupKind Kind, std::tuple< As...> A)
Push a lazily-created cleanup on the stack. Tuple version.
Definition: EHScopeStack.h:269
stable_iterator stabilize(iterator it) const
Translates an iterator into a stable_iterator.
Definition: CGCleanup.h:549
class EHCatchScope * pushCatch(unsigned NumHandlers)
Definition: CGCleanup.cpp:232
iterator begin() const
Returns an iterator pointing to the innermost EH scope.
Definition: CGCleanup.h:518
BranchFixup & getBranchFixup(unsigned I)
Definition: EHScopeStack.h:391
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
void popFilter()
Pops an exceptions filter off the stack.
Definition: CGCleanup.cpp:223
iterator find(stable_iterator save) const
Definition: CGCleanup.h:542
unsigned getNumBranchFixups() const
Definition: EHScopeStack.h:390
static type restore(CodeGenFunction &CGF, saved_type value)
Definition: EHScopeStack.h:61
llvm::BranchInst * InitialBranch
The initial branch of the fixup.
Definition: EHScopeStack.h:53
bool empty() const
Determines whether the exception-scopes stack is empty.
Definition: EHScopeStack.h:327
void removeCleanup(stable_iterator save)
Removes the cleanup pointed to by the given stable_iterator.
stable_iterator stable_begin() const
Definition: EHScopeStack.h:364
bool containsOnlyLifetimeMarkers(stable_iterator Old) const
Definition: CGCleanup.cpp:128
void popCleanup()
Pops a cleanup scope off the stack. This is private to CGCleanup.cpp.
Definition: CGCleanup.cpp:190
llvm::BasicBlock * OptimisticBranchBlock
Definition: EHScopeStack.h:41
void popCatch()
Pops a catch scope off the stack. This is private to CGException.cpp.
Definition: CGCleanup.h:526
#define false
Definition: stdbool.h:33
llvm::BasicBlock * Destination
Definition: EHScopeStack.h:47
Kind
static bool needsSaving(type value)
Definition: EHScopeStack.h:59
void pushCopyOfCleanup(CleanupKind Kind, const void *Cleanup, size_t Size)
Definition: EHScopeStack.h:294
unsigned DestinationIndex
The destination index value.
Definition: EHScopeStack.h:50
virtual void Emit(CodeGenFunction &CGF, Flags flags)=0
friend bool operator==(stable_iterator A, stable_iterator B)
Definition: EHScopeStack.h:126
__SIZE_TYPE__ size_t
Definition: stddef.h:62
class EHFilterScope * pushFilter(unsigned NumFilters)
Push an exceptions filter on the stack.
Definition: CGCleanup.cpp:215
bool encloses(stable_iterator I) const
Definition: EHScopeStack.h:118
BranchFixup & addBranchFixup()
Add a branch fixup to the current cleanup scope.
Definition: EHScopeStack.h:384
friend bool operator!=(stable_iterator A, stable_iterator B)
Definition: EHScopeStack.h:129
__PTRDIFF_TYPE__ ptrdiff_t
Definition: stddef.h:51
stable_iterator getInnermostActiveNormalCleanup() const
Definition: CGCleanup.cpp:140
void popTerminate()
Pops a terminate handler off the stack.
Definition: CGCleanup.h:534
bool hasNormalCleanups() const
Determines whether there are any normal cleanups on the stack.
Definition: EHScopeStack.h:334
stable_iterator getInnermostEHScope() const
Definition: EHScopeStack.h:345
bool strictlyEncloses(stable_iterator I) const
Definition: EHScopeStack.h:124
stable_iterator getInnermostNormalCleanup() const
Definition: EHScopeStack.h:340
bool isForEHCleanup() const
isForEH - true if the current emission is for an EH cleanup.
Definition: EHScopeStack.h:158
bool requiresLandingPad() const
Definition: EHScopeStack.h:329