LLVM  16.0.0git
SaveAndRestore.h
Go to the documentation of this file.
1 //===-- SaveAndRestore.h - Utility -------------------------------*- 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 /// \file
10 /// This file provides utility classes that use RAII to save and restore
11 /// values.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_SUPPORT_SAVEANDRESTORE_H
16 #define LLVM_SUPPORT_SAVEANDRESTORE_H
17 
18 #include <utility>
19 
20 namespace llvm {
21 
22 /// A utility class that uses RAII to save and restore the value of a variable.
23 template <typename T> struct SaveAndRestore {
24  SaveAndRestore(T &X) : X(X), OldValue(X) {}
25  SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) { X = NewValue; }
26  SaveAndRestore(T &X, T &&NewValue) : X(X), OldValue(std::move(X)) {
27  X = std::move(NewValue);
28  }
29  ~SaveAndRestore() { X = std::move(OldValue); }
30  const T &get() { return OldValue; }
31 
32 private:
33  T &X;
34  T OldValue;
35 };
36 
37 // User-defined CTAD guides.
38 template <typename T> SaveAndRestore(T &) -> SaveAndRestore<T>;
39 template <typename T> SaveAndRestore(T &, const T &) -> SaveAndRestore<T>;
40 template <typename T> SaveAndRestore(T &, T &&) -> SaveAndRestore<T>;
41 
42 } // namespace llvm
43 
44 #endif
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::SaveAndRestore::SaveAndRestore
SaveAndRestore(T &X)
Definition: SaveAndRestore.h:24
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::SaveAndRestore::get
const T & get()
Definition: SaveAndRestore.h:30
llvm::SaveAndRestore::~SaveAndRestore
~SaveAndRestore()
Definition: SaveAndRestore.h:29
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:1861
llvm::SaveAndRestore
A utility class that uses RAII to save and restore the value of a variable.
Definition: SaveAndRestore.h:23
std
Definition: BitVector.h:851
llvm::SaveAndRestore
SaveAndRestore(T &) -> SaveAndRestore< T >
llvm::SaveAndRestore::SaveAndRestore
SaveAndRestore(T &X, const T &NewValue)
Definition: SaveAndRestore.h:25
llvm::SaveAndRestore::SaveAndRestore
SaveAndRestore(T &X, T &&NewValue)
Definition: SaveAndRestore.h:26