LLVM  8.0.1
GISelChangeObserver.h
Go to the documentation of this file.
1 //===----- llvm/CodeGen/GlobalISel/GISelChangeObserver.h ------------------===//
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 /// This contains common code to allow clients to notify changes to machine
11 /// instr.
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  /// An instruction was created and inserted into the function.
38  virtual void createdInstr(MachineInstr &MI) = 0;
39  /// This instruction is about to be mutated in some way.
40  virtual void changingInstr(MachineInstr &MI) = 0;
41  /// This instruction was mutated in some way.
42  virtual void changedInstr(MachineInstr &MI) = 0;
43 
44  /// All the instructions using the given register are being changed.
45  /// For convenience, finishedChangingAllUsesOfReg() will report the completion
46  /// of the changes. The use list may change between this call and
47  /// finishedChangingAllUsesOfReg().
48  void changingAllUsesOfReg(const MachineRegisterInfo &MRI, unsigned Reg);
49  /// All instructions reported as changing by changingAllUsesOfReg() have
50  /// finished being changed.
52 
53 };
54 
55 /// Simple wrapper observer that takes several observers, and calls
56 /// each one for each event. If there are multiple observers (say CSE,
57 /// Legalizer, Combiner), it's sufficient to register this to the machine
58 /// function as the delegate.
60  public GISelChangeObserver {
62 
63 public:
64  GISelObserverWrapper() = default;
66  : Observers(Obs.begin(), Obs.end()) {}
67  // Adds an observer.
68  void addObserver(GISelChangeObserver *O) { Observers.push_back(O); }
69  // Removes an observer from the list and does nothing if observer is not
70  // present.
72  auto It = std::find(Observers.begin(), Observers.end(), O);
73  if (It != Observers.end())
74  Observers.erase(It);
75  }
76  // API for Observer.
77  void erasingInstr(MachineInstr &MI) override {
78  for (auto &O : Observers)
79  O->erasingInstr(MI);
80  }
81  void createdInstr(MachineInstr &MI) override {
82  for (auto &O : Observers)
83  O->createdInstr(MI);
84  }
85  void changingInstr(MachineInstr &MI) override {
86  for (auto &O : Observers)
87  O->changingInstr(MI);
88  }
89  void changedInstr(MachineInstr &MI) override {
90  for (auto &O : Observers)
91  O->changedInstr(MI);
92  }
93  // API for MachineFunction::Delegate
95  void MF_HandleRemoval(MachineInstr &MI) override { erasingInstr(MI); }
96 };
97 
98 /// A simple RAII based CSEInfo installer.
99 /// Use this in a scope to install a delegate to the MachineFunction and reset
100 /// it at the end of the scope.
102  MachineFunction &MF;
103  MachineFunction::Delegate *Delegate;
104 
105 public:
108 };
109 
110 } // namespace llvm
111 #endif
A simple RAII based CSEInfo installer.
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:259
void MF_HandleRemoval(MachineInstr &MI) override
Callback before a removal. This should not modify the MI directly.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:250
This class represents lattice values for constants.
Definition: AllocatorList.h:24
unsigned Reg
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void MF_HandleInsertion(MachineInstr &MI) override
Callback after an insertion. This should not modify the MI directly.
void finishedChangingAllUsesOfReg()
All instructions reported as changing by changingAllUsesOfReg() have finished being changed...
Abstract class that contains various methods for clients to notify about changes. ...
unsigned const MachineRegisterInfo * MRI
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
virtual void createdInstr(MachineInstr &MI)=0
An instruction was created and inserted into the function.
void createdInstr(MachineInstr &MI) override
An instruction was created and inserted into the function.
GISelObserverWrapper(ArrayRef< GISelChangeObserver *> Obs)
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
virtual void erasingInstr(MachineInstr &MI)=0
An instruction is about to be erased.
iterator erase(const_iterator CI)
Definition: SmallVector.h:445
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1207
void changingAllUsesOfReg(const MachineRegisterInfo &MRI, unsigned Reg)
All the instructions using the given register are being changed.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
virtual void changingInstr(MachineInstr &MI)=0
This instruction is about to be mutated in some way.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:64
void addObserver(GISelChangeObserver *O)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
void removeObserver(GISelChangeObserver *O)
IRTranslator LLVM IR MI
Simple wrapper observer that takes several observers, and calls each one for each event...
virtual void changedInstr(MachineInstr &MI)=0
This instruction was mutated in some way.