LLVM  13.0.0git
GISelChangeObserver.h
Go to the documentation of this file.
1 //===----- llvm/CodeGen/GlobalISel/GISelChangeObserver.h --------*- 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 /// \file
9 /// This contains common code to allow clients to notify changes to machine
10 /// instr.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CODEGEN_GLOBALISEL_GISELCHANGEOBSERVER_H
15 #define LLVM_CODEGEN_GLOBALISEL_GISELCHANGEOBSERVER_H
16 
17 #include "llvm/ADT/SmallPtrSet.h"
19 
20 namespace llvm {
21 class MachineInstr;
22 class MachineRegisterInfo;
23 
24 /// Abstract class that contains various methods for clients to notify about
25 /// changes. This should be the preferred way for APIs to notify changes.
26 /// Typically calling erasingInstr/createdInstr multiple times should not affect
27 /// the result. The observer would likely need to check if it was already
28 /// notified earlier (consider using GISelWorkList).
30  SmallPtrSet<MachineInstr *, 4> ChangingAllUsesOfReg;
31 
32 public:
33  virtual ~GISelChangeObserver() {}
34 
35  /// An instruction is about to be erased.
36  virtual void erasingInstr(MachineInstr &MI) = 0;
37 
38  /// An instruction has been created and inserted into the function.
39  /// Note that the instruction might not be a fully fledged instruction at this
40  /// point and won't be if the MachineFunction::Delegate is calling it. This is
41  /// because the delegate only sees the construction of the MachineInstr before
42  /// operands have been added.
43  virtual void createdInstr(MachineInstr &MI) = 0;
44 
45  /// This instruction is about to be mutated in some way.
46  virtual void changingInstr(MachineInstr &MI) = 0;
47 
48  /// This instruction was mutated in some way.
49  virtual void changedInstr(MachineInstr &MI) = 0;
50 
51  /// All the instructions using the given register are being changed.
52  /// For convenience, finishedChangingAllUsesOfReg() will report the completion
53  /// of the changes. The use list may change between this call and
54  /// finishedChangingAllUsesOfReg().
56  /// All instructions reported as changing by changingAllUsesOfReg() have
57  /// finished being changed.
59 
60 };
61 
62 /// Simple wrapper observer that takes several observers, and calls
63 /// each one for each event. If there are multiple observers (say CSE,
64 /// Legalizer, Combiner), it's sufficient to register this to the machine
65 /// function as the delegate.
67  public GISelChangeObserver {
69 
70 public:
71  GISelObserverWrapper() = default;
73  : Observers(Obs.begin(), Obs.end()) {}
74  // Adds an observer.
75  void addObserver(GISelChangeObserver *O) { Observers.push_back(O); }
76  // Removes an observer from the list and does nothing if observer is not
77  // present.
79  auto It = std::find(Observers.begin(), Observers.end(), O);
80  if (It != Observers.end())
81  Observers.erase(It);
82  }
83  // API for Observer.
84  void erasingInstr(MachineInstr &MI) override {
85  for (auto &O : Observers)
86  O->erasingInstr(MI);
87  }
88  void createdInstr(MachineInstr &MI) override {
89  for (auto &O : Observers)
90  O->createdInstr(MI);
91  }
92  void changingInstr(MachineInstr &MI) override {
93  for (auto &O : Observers)
94  O->changingInstr(MI);
95  }
96  void changedInstr(MachineInstr &MI) override {
97  for (auto &O : Observers)
98  O->changedInstr(MI);
99  }
100  // API for MachineFunction::Delegate
103 };
104 
105 /// A simple RAII based Delegate installer.
106 /// Use this in a scope to install a delegate to the MachineFunction and reset
107 /// it at the end of the scope.
109  MachineFunction &MF;
110  MachineFunction::Delegate *Delegate;
111 
112 public:
115 };
116 
117 /// A simple RAII based Observer installer.
118 /// Use this in a scope to install the Observer to the MachineFunction and reset
119 /// it at the end of the scope.
121  MachineFunction &MF;
122 
123 public:
126 };
127 
128 /// Class to install both of the above.
132 
133 public:
135  : DelI(MF, &Wrapper), ObsI(MF, Wrapper) {}
136  ~RAIIMFObsDelInstaller() = default;
137 };
138 
139 } // namespace llvm
140 #endif
llvm::GISelChangeObserver::erasingInstr
virtual void erasingInstr(MachineInstr &MI)=0
An instruction is about to be erased.
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:100
llvm
Definition: AllocatorList.h:23
llvm::SmallVectorImpl::erase
iterator erase(const_iterator CI)
Definition: SmallVector.h:705
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::RAIIMFObserverInstaller::~RAIIMFObserverInstaller
~RAIIMFObserverInstaller()
Definition: GISelChangeObserver.cpp:48
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
Wrapper
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
Definition: AMDGPUAliasAnalysis.cpp:30
llvm::GISelChangeObserver::finishedChangingAllUsesOfReg
void finishedChangingAllUsesOfReg()
All instructions reported as changing by changingAllUsesOfReg() have finished being changed.
Definition: GISelChangeObserver.cpp:26
llvm::RAIIDelegateInstaller::~RAIIDelegateInstaller
~RAIIDelegateInstaller()
Definition: GISelChangeObserver.cpp:40
llvm::GISelObserverWrapper::removeObserver
void removeObserver(GISelChangeObserver *O)
Definition: GISelChangeObserver.h:78
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
llvm::RAIIMFObserverInstaller
A simple RAII based Observer installer.
Definition: GISelChangeObserver.h:120
llvm::GISelObserverWrapper::MF_HandleRemoval
void MF_HandleRemoval(MachineInstr &MI) override
Callback before a removal. This should not modify the MI directly.
Definition: GISelChangeObserver.h:102
llvm::RAIIDelegateInstaller::RAIIDelegateInstaller
RAIIDelegateInstaller(MachineFunction &MF, MachineFunction::Delegate *Del)
Definition: GISelChangeObserver.cpp:32
llvm::GISelObserverWrapper::createdInstr
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
Definition: GISelChangeObserver.h:88
llvm::RAIIDelegateInstaller
A simple RAII based Delegate installer.
Definition: GISelChangeObserver.h:108
llvm::GISelChangeObserver::changingAllUsesOfReg
void changingAllUsesOfReg(const MachineRegisterInfo &MRI, Register Reg)
All the instructions using the given register are being changed.
Definition: GISelChangeObserver.cpp:18
llvm::GISelObserverWrapper::changingInstr
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
Definition: GISelChangeObserver.h:92
llvm::GISelObserverWrapper::GISelObserverWrapper
GISelObserverWrapper()=default
llvm::MachineFunction::Delegate
Definition: MachineFunction.h:395
SmallPtrSet.h
llvm::GISelChangeObserver::changingInstr
virtual void changingInstr(MachineInstr &MI)=0
This instruction is about to be mutated in some way.
llvm::GISelObserverWrapper::changedInstr
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
Definition: GISelChangeObserver.h:96
llvm::GISelChangeObserver::~GISelChangeObserver
virtual ~GISelChangeObserver()
Definition: GISelChangeObserver.h:33
llvm::GISelChangeObserver::changedInstr
virtual void changedInstr(MachineInstr &MI)=0
This instruction was mutated in some way.
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:132
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::find
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1502
llvm::RAIIMFObsDelInstaller::~RAIIMFObsDelInstaller
~RAIIMFObsDelInstaller()=default
llvm::GISelChangeObserver::createdInstr
virtual void createdInstr(MachineInstr &MI)=0
An instruction has been created and inserted into the function.
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::GISelObserverWrapper::MF_HandleInsertion
void MF_HandleInsertion(MachineInstr &MI) override
Callback after an insertion. This should not modify the MI directly.
Definition: GISelChangeObserver.h:101
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::GISelChangeObserver
Abstract class that contains various methods for clients to notify about changes.
Definition: GISelChangeObserver.h:29
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
llvm::GISelObserverWrapper::GISelObserverWrapper
GISelObserverWrapper(ArrayRef< GISelChangeObserver * > Obs)
Definition: GISelChangeObserver.h:72
llvm::GISelObserverWrapper::addObserver
void addObserver(GISelChangeObserver *O)
Definition: GISelChangeObserver.h:75
llvm::RAIIMFObserverInstaller::RAIIMFObserverInstaller
RAIIMFObserverInstaller(MachineFunction &MF, GISelChangeObserver &Observer)
Definition: GISelChangeObserver.cpp:42
llvm::GISelObserverWrapper
Simple wrapper observer that takes several observers, and calls each one for each event.
Definition: GISelChangeObserver.h:66
llvm::RAIIMFObsDelInstaller::RAIIMFObsDelInstaller
RAIIMFObsDelInstaller(MachineFunction &MF, GISelObserverWrapper &Wrapper)
Definition: GISelChangeObserver.h:134
llvm::GISelObserverWrapper::erasingInstr
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
Definition: GISelChangeObserver.h:84
MachineFunction.h
llvm::RAIIMFObsDelInstaller
Class to install both of the above.
Definition: GISelChangeObserver.h:129