LLVM  13.0.0git
ScopeExit.h
Go to the documentation of this file.
1 //===- llvm/ADT/ScopeExit.h - Execute code at scope exit --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the make_scope_exit function, which executes user-defined
10 // cleanup logic at scope exit.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ADT_SCOPEEXIT_H
15 #define LLVM_ADT_SCOPEEXIT_H
16 
17 #include "llvm/Support/Compiler.h"
18 
19 #include <type_traits>
20 #include <utility>
21 
22 namespace llvm {
23 namespace detail {
24 
25 template <typename Callable> class scope_exit {
26  Callable ExitFunction;
27  bool Engaged = true; // False once moved-from or release()d.
28 
29 public:
30  template <typename Fp>
31  explicit scope_exit(Fp &&F) : ExitFunction(std::forward<Fp>(F)) {}
32 
34  : ExitFunction(std::move(Rhs.ExitFunction)), Engaged(Rhs.Engaged) {
35  Rhs.release();
36  }
37  scope_exit(const scope_exit &) = delete;
38  scope_exit &operator=(scope_exit &&) = delete;
39  scope_exit &operator=(const scope_exit &) = delete;
40 
41  void release() { Engaged = false; }
42 
44  if (Engaged)
45  ExitFunction();
46  }
47 };
48 
49 } // end namespace detail
50 
51 // Keeps the callable object that is passed in, and execute it at the
52 // destruction of the returned object (usually at the scope exit where the
53 // returned object is kept).
54 //
55 // Interface is specified by p0052r2.
56 template <typename Callable>
58 make_scope_exit(Callable &&F) {
60  std::forward<Callable>(F));
61 }
62 
63 } // end namespace llvm
64 
65 #endif
llvm::detail::scope_exit::release
void release()
Definition: ScopeExit.h:41
llvm
Definition: AllocatorList.h:23
llvm::detail::scope_exit::scope_exit
scope_exit(Fp &&F)
Definition: ScopeExit.h:31
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::detail::scope_exit::~scope_exit
~scope_exit()
Definition: ScopeExit.h:43
llvm::make_scope_exit
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:58
llvm::detail::scope_exit
Definition: ScopeExit.h:25
type
AMD64 Optimization Manual has some nice information about optimizing integer multiplication by a constant How much of it applies to Intel s X86 implementation There are definite trade offs to xmm0 cvttss2siq rdx jb L3 subss xmm0 rax cvttss2siq rdx xorq rdx rax ret instead of xmm1 cvttss2siq rcx movaps xmm2 subss xmm2 cvttss2siq rax rdx xorq rax ucomiss xmm0 cmovb rax ret Seems like the jb branch has high likelihood of being taken It would have saved a few instructions It s not possible to reference and DH registers in an instruction requiring REX prefix divb and mulb both produce results in AH If isel emits a CopyFromReg which gets turned into a movb and that can be allocated a r8b r15b To get around isel emits a CopyFromReg from AX and then right shift it down by and truncate it It s not pretty but it works We need some register allocation magic to make the hack go which would often require a callee saved register Callees usually need to keep this value live for most of their body so it doesn t add a significant burden on them We currently implement this in however this is suboptimal because it means that it would be quite awkward to implement the optimization for callers A better implementation would be to relax the LLVM IR rules for sret arguments to allow a function with an sret argument to have a non void return type
Definition: README-X86-64.txt:70
llvm::detail::scope_exit::scope_exit
scope_exit(scope_exit &&Rhs)
Definition: ScopeExit.h:33
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1540
Compiler.h
std
Definition: BitVector.h:838
LLVM_NODISCARD
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:161
llvm::detail::scope_exit::operator=
scope_exit & operator=(scope_exit &&)=delete