LLVM  10.0.0svn
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 //
9 /// This contains common code to allow clients to notify changes to machine
10 /// instr.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CODEGEN_GLOBALISEL_GISELCHANGEOBSERVER_H
14 #define LLVM_CODEGEN_GLOBALISEL_GISELCHANGEOBSERVER_H
15 
16 #include "llvm/ADT/SmallPtrSet.h"
18 
19 namespace llvm {
20 class MachineInstr;
21 class MachineRegisterInfo;
22 
23 /// Abstract class that contains various methods for clients to notify about
24 /// changes. This should be the preferred way for APIs to notify changes.
25 /// Typically calling erasingInstr/createdInstr multiple times should not affect
26 /// the result. The observer would likely need to check if it was already
27 /// notified earlier (consider using GISelWorkList).
29  SmallPtrSet<MachineInstr *, 4> ChangingAllUsesOfReg;
30 
31 public:
32  virtual ~GISelChangeObserver() {}
33 
34  /// An instruction is about to be erased.
35  virtual void erasingInstr(MachineInstr &MI) = 0;
36 
37  /// An instruction has been created and inserted into the function.
38  /// Note that the instruction might not be a fully fledged instruction at this
39  /// point and won't be if the MachineFunction::Delegate is calling it. This is
40  /// because the delegate only sees the construction of the MachineInstr before
41  /// operands have been added.
42  virtual void createdInstr(MachineInstr &MI) = 0;
43 
44  /// This instruction is about to be mutated in some way.
45  virtual void changingInstr(MachineInstr &MI) = 0;
46 
47  /// This instruction was mutated in some way.
48  virtual void changedInstr(MachineInstr &MI) = 0;
49 
50  /// All the instructions using the given register are being changed.
51  /// For convenience, finishedChangingAllUsesOfReg() will report the completion
52  /// of the changes. The use list may change between this call and
53  /// finishedChangingAllUsesOfReg().
54  void changingAllUsesOfReg(const MachineRegisterInfo &MRI, unsigned Reg);
55  /// All instructions reported as changing by changingAllUsesOfReg() have
56  /// finished being changed.
58 
59 };
60 
61 /// Simple wrapper observer that takes several observers, and calls
62 /// each one for each event. If there are multiple observers (say CSE,
63 /// Legalizer, Combiner), it's sufficient to register this to the machine
64 /// function as the delegate.
66  public GISelChangeObserver {
68 
69 public:
70  GISelObserverWrapper() = default;
72  : Observers(Obs.begin(), Obs.end()) {}
73  // Adds an observer.
74  void addObserver(GISelChangeObserver *O) { Observers.push_back(O); }
75  // Removes an observer from the list and does nothing if observer is not
76  // present.
78  auto It = std::find(Observers.begin(), Observers.end(), O);
79  if (It != Observers.end())
80  Observers.erase(It);
81  }
82  // API for Observer.
83  void erasingInstr(MachineInstr &MI) override {
84  for (auto &O : Observers)
85  O->erasingInstr(MI);
86  }
87  void createdInstr(MachineInstr &MI) override {
88  for (auto &O : Observers)
89  O->createdInstr(MI);
90  }
91  void changingInstr(MachineInstr &MI) override {
92  for (auto &O : Observers)
93  O->changingInstr(MI);
94  }
95  void changedInstr(MachineInstr &MI) override {
96  for (auto &O : Observers)
97  O->changedInstr(MI);
98  }
99  // API for MachineFunction::Delegate
101  void MF_HandleRemoval(MachineInstr &MI) override { erasingInstr(MI); }
102 };
103 
104 /// A simple RAII based CSEInfo installer.
105 /// Use this in a scope to install a delegate to the MachineFunction and reset
106 /// it at the end of the scope.
108  MachineFunction &MF;
109  MachineFunction::Delegate *Delegate;
110 
111 public:
114 };
115 
116 } // namespace llvm
117 #endif
A simple RAII based CSEInfo installer.
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
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:224
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:32
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
virtual void createdInstr(MachineInstr &MI)=0
An instruction has been created and inserted into the function.
void createdInstr(MachineInstr &MI) override
An instruction has been 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:434
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:1186
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:417
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
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:63
void addObserver(GISelChangeObserver *O)
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.