LLVM 22.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
20
21namespace llvm {
22class MachineInstr;
23class MachineRegisterInfo;
24
25/// Abstract class that contains various methods for clients to notify about
26/// changes. This should be the preferred way for APIs to notify changes.
27/// Typically calling erasingInstr/createdInstr multiple times should not affect
28/// the result. The observer would likely need to check if it was already
29/// notified earlier (consider using GISelWorkList).
31 SmallPtrSet<MachineInstr *, 4> ChangingAllUsesOfReg;
32
33public:
34 virtual ~GISelChangeObserver() = default;
35
36 /// An instruction is about to be erased.
37 virtual void erasingInstr(MachineInstr &MI) = 0;
38
39 /// An instruction has been created and inserted into the function.
40 /// Note that the instruction might not be a fully fledged instruction at this
41 /// point and won't be if the MachineFunction::Delegate is calling it. This is
42 /// because the delegate only sees the construction of the MachineInstr before
43 /// operands have been added.
44 virtual void createdInstr(MachineInstr &MI) = 0;
45
46 /// This instruction is about to be mutated in some way.
47 virtual void changingInstr(MachineInstr &MI) = 0;
48
49 /// This instruction was mutated in some way.
50 virtual void changedInstr(MachineInstr &MI) = 0;
51
52 /// All the instructions using the given register are being changed.
53 /// For convenience, finishedChangingAllUsesOfReg() will report the completion
54 /// of the changes. The use list may change between this call and
55 /// finishedChangingAllUsesOfReg().
58 /// All instructions reported as changing by changingAllUsesOfReg() have
59 /// finished being changed.
61};
62
63/// Simple wrapper observer that takes several observers, and calls
64/// each one for each event. If there are multiple observers (say CSE,
65/// Legalizer, Combiner), it's sufficient to register this to the machine
66/// function as the delegate.
68 public GISelChangeObserver {
70
71public:
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 = llvm::find(Observers, O);
80 if (It != Observers.end())
81 Observers.erase(It);
82 }
83 // Removes all observers
84 void clearObservers() { Observers.clear(); }
85
86 // API for Observer.
87 void erasingInstr(MachineInstr &MI) override {
88 for (auto &O : Observers)
89 O->erasingInstr(MI);
90 }
91 void createdInstr(MachineInstr &MI) override {
92 for (auto &O : Observers)
93 O->createdInstr(MI);
94 }
95 void changingInstr(MachineInstr &MI) override {
96 for (auto &O : Observers)
97 O->changingInstr(MI);
98 }
99 void changedInstr(MachineInstr &MI) override {
100 for (auto &O : Observers)
101 O->changedInstr(MI);
102 }
103 // API for MachineFunction::Delegate
106};
107
108/// A simple RAII based Delegate installer.
109/// Use this in a scope to install a delegate to the MachineFunction and reset
110/// it at the end of the scope.
112 MachineFunction &MF;
114
115public:
119};
120
121/// A simple RAII based Observer installer.
122/// Use this in a scope to install the Observer to the MachineFunction and reset
123/// it at the end of the scope.
125 MachineFunction &MF;
126
127public:
129 GISelChangeObserver &Observer);
131};
132
133/// Class to install both of the above.
137
138public:
140 : DelI(MF, &Wrapper), ObsI(MF, Wrapper) {}
142};
143
144/// A simple RAII based Observer installer.
145/// Use this in a scope to install the Observer to the MachineFunction and reset
146/// it at the end of the scope.
148public:
151 GISelChangeObserver &TemporaryObserver);
153
154private:
155 GISelObserverWrapper &Observers;
156 GISelChangeObserver &TemporaryObserver;
157};
158
159} // namespace llvm
160#endif
unsigned const MachineRegisterInfo * MRI
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
#define LLVM_ABI
Definition: Compiler.h:213
IRTranslator LLVM IR MI
Register Reg
This file defines the SmallPtrSet class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Abstract class that contains various methods for clients to notify about changes.
virtual void changingInstr(MachineInstr &MI)=0
This instruction is about to be mutated in some way.
LLVM_ABI void finishedChangingAllUsesOfReg()
All instructions reported as changing by changingAllUsesOfReg() have finished being changed.
virtual void changedInstr(MachineInstr &MI)=0
This instruction was mutated in some way.
virtual ~GISelChangeObserver()=default
virtual void createdInstr(MachineInstr &MI)=0
An instruction has been created and inserted into the function.
virtual void erasingInstr(MachineInstr &MI)=0
An instruction is about to be erased.
LLVM_ABI void changingAllUsesOfReg(const MachineRegisterInfo &MRI, Register Reg)
All the instructions using the given register are being changed.
Simple wrapper observer that takes several observers, and calls each one for each event.
void MF_HandleInsertion(MachineInstr &MI) override
Callback after an insertion. This should not modify the MI directly.
void removeObserver(GISelChangeObserver *O)
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
void MF_HandleRemoval(MachineInstr &MI) override
Callback before a removal. This should not modify the MI directly.
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
void addObserver(GISelChangeObserver *O)
GISelObserverWrapper(ArrayRef< GISelChangeObserver * > Obs)
Representation of each machine instruction.
Definition: MachineInstr.h:72
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A simple RAII based Delegate installer.
Class to install both of the above.
RAIIMFObsDelInstaller(MachineFunction &MF, GISelObserverWrapper &Wrapper)
A simple RAII based Observer installer.
A simple RAII based Observer installer.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
iterator erase(const_iterator CI)
Definition: SmallVector.h:738
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1770