LLVM  10.0.0svn
CSEInfo.h
Go to the documentation of this file.
1 //===- llvm/CodeGen/GlobalISel/CSEInfo.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 /// Provides analysis for continuously CSEing during GISel passes.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_CODEGEN_GLOBALISEL_CSEINFO_H
13 #define LLVM_CODEGEN_GLOBALISEL_CSEINFO_H
14 
15 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/Allocator.h"
25 
26 namespace llvm {
27 
28 /// A class that wraps MachineInstrs and derives from FoldingSetNode in order to
29 /// be uniqued in a CSEMap. The tradeoff here is extra memory allocations for
30 /// UniqueMachineInstr vs making MachineInstr bigger.
32  friend class GISelCSEInfo;
33  const MachineInstr *MI;
34  explicit UniqueMachineInstr(const MachineInstr *MI) : MI(MI) {}
35 
36 public:
38 };
39 
40 // A CSE config for fully optimized builds.
41 class CSEConfigFull : public CSEConfigBase {
42 public:
43  virtual ~CSEConfigFull() = default;
44  virtual bool shouldCSEOpc(unsigned Opc) override;
45 };
46 
47 // Commonly used for O0 config.
49 public:
50  virtual ~CSEConfigConstantOnly() = default;
51  virtual bool shouldCSEOpc(unsigned Opc) override;
52 };
53 
54 // Returns the standard expected CSEConfig for the given optimization level.
55 // We have this logic here so targets can make use of it from their derived
56 // TargetPassConfig, but can't put this logic into TargetPassConfig directly
57 // because the CodeGen library can't depend on GlobalISel.
58 std::unique_ptr<CSEConfigBase>
60 
61 /// The CSE Analysis object.
62 /// This installs itself as a delegate to the MachineFunction to track
63 /// new instructions as well as deletions. It however will not be able to
64 /// track instruction mutations. In such cases, recordNewInstruction should be
65 /// called (for eg inside MachineIRBuilder::recordInsertion).
66 /// Also because of how just the instruction can be inserted without adding any
67 /// operands to the instruction, instructions are uniqued and inserted lazily.
68 /// CSEInfo should assert when trying to enter an incomplete instruction into
69 /// the CSEMap. There is Opcode level granularity on which instructions can be
70 /// CSE'd and for now, only Generic instructions are CSEable.
72  // Make it accessible only to CSEMIRBuilder.
73  friend class CSEMIRBuilder;
74 
75  BumpPtrAllocator UniqueInstrAllocator;
77  MachineRegisterInfo *MRI = nullptr;
78  MachineFunction *MF = nullptr;
79  std::unique_ptr<CSEConfigBase> CSEOpt;
80  /// Keep a cache of UniqueInstrs for each MachineInstr. In GISel,
81  /// often instructions are mutated (while their ID has completely changed).
82  /// Whenever mutation happens, invalidate the UniqueMachineInstr for the
83  /// MachineInstr
85 
86  /// Store instructions that are not fully formed in TemporaryInsts.
87  /// Also because CSE insertion happens lazily, we can remove insts from this
88  /// list and avoid inserting and then removing from the CSEMap.
89  GISelWorkList<8> TemporaryInsts;
90 
91  // Only used in asserts.
92  DenseMap<unsigned, unsigned> OpcodeHitTable;
93 
94  bool isUniqueMachineInstValid(const UniqueMachineInstr &UMI) const;
95 
96  void invalidateUniqueMachineInstr(UniqueMachineInstr *UMI);
97 
98  UniqueMachineInstr *getNodeIfExists(FoldingSetNodeID &ID,
99  MachineBasicBlock *MBB, void *&InsertPos);
100 
101  /// Allocate and construct a new UniqueMachineInstr for MI and return.
102  UniqueMachineInstr *getUniqueInstrForMI(const MachineInstr *MI);
103 
104  void insertNode(UniqueMachineInstr *UMI, void *InsertPos = nullptr);
105 
106  /// Get the MachineInstr(Unique) if it exists already in the CSEMap and the
107  /// same MachineBasicBlock.
108  MachineInstr *getMachineInstrIfExists(FoldingSetNodeID &ID,
109  MachineBasicBlock *MBB,
110  void *&InsertPos);
111 
112  /// Use this method to allocate a new UniqueMachineInstr for MI and insert it
113  /// into the CSEMap. MI should return true for shouldCSE(MI->getOpcode())
114  void insertInstr(MachineInstr *MI, void *InsertPos = nullptr);
115 
116 public:
117  GISelCSEInfo() = default;
118 
119  virtual ~GISelCSEInfo();
120 
121  void setMF(MachineFunction &MF);
122 
123  /// Records a newly created inst in a list and lazily insert it to the CSEMap.
124  /// Sometimes, this method might be called with a partially constructed
125  /// MachineInstr,
126  // (right after BuildMI without adding any operands) - and in such cases,
127  // defer the hashing of the instruction to a later stage.
128  void recordNewInstruction(MachineInstr *MI);
129 
130  /// Use this callback to inform CSE about a newly fully created instruction.
131  void handleRecordedInst(MachineInstr *MI);
132 
133  /// Use this callback to insert all the recorded instructions. At this point,
134  /// all of these insts need to be fully constructed and should not be missing
135  /// any operands.
136  void handleRecordedInsts();
137 
138  /// Remove this inst from the CSE map. If this inst has not been inserted yet,
139  /// it will be removed from the Tempinsts list if it exists.
140  void handleRemoveInst(MachineInstr *MI);
141 
142  void releaseMemory();
143 
144  void setCSEConfig(std::unique_ptr<CSEConfigBase> Opt) {
145  CSEOpt = std::move(Opt);
146  }
147 
148  bool shouldCSE(unsigned Opc) const;
149 
150  void analyze(MachineFunction &MF);
151 
152  void countOpcodeHit(unsigned Opc);
153 
154  void print();
155 
156  // Observer API
157  void erasingInstr(MachineInstr &MI) override;
158  void createdInstr(MachineInstr &MI) override;
159  void changingInstr(MachineInstr &MI) override;
160  void changedInstr(MachineInstr &MI) override;
161 };
162 
163 class TargetRegisterClass;
164 class RegisterBank;
165 
166 // Simple builder class to easily profile properties about MIs.
169  const MachineRegisterInfo &MRI;
170 
171 public:
173  : ID(ID), MRI(MRI) {}
174  // Profiling methods.
175  const GISelInstProfileBuilder &addNodeIDOpcode(unsigned Opc) const;
176  const GISelInstProfileBuilder &addNodeIDRegType(const LLT &Ty) const;
177  const GISelInstProfileBuilder &addNodeIDRegType(const unsigned) const;
178 
180  addNodeIDRegType(const TargetRegisterClass *RC) const;
181  const GISelInstProfileBuilder &addNodeIDRegType(const RegisterBank *RB) const;
182 
183  const GISelInstProfileBuilder &addNodeIDRegNum(unsigned Reg) const;
184 
185  const GISelInstProfileBuilder &addNodeIDImmediate(int64_t Imm) const;
187  addNodeIDMBB(const MachineBasicBlock *MBB) const;
188 
190  addNodeIDMachineOperand(const MachineOperand &MO) const;
191 
192  const GISelInstProfileBuilder &addNodeIDFlag(unsigned Flag) const;
193  const GISelInstProfileBuilder &addNodeID(const MachineInstr *MI) const;
194 };
195 
196 /// Simple wrapper that does the following.
197 /// 1) Lazily evaluate the MachineFunction to compute CSEable instructions.
198 /// 2) Allows configuration of which instructions are CSEd through CSEConfig
199 /// object. Provides a method called get which takes a CSEConfig object.
202  MachineFunction *MF = nullptr;
203  bool AlreadyComputed = false;
204 
205 public:
206  /// Takes a CSEConfigBase object that defines what opcodes get CSEd.
207  /// If CSEConfig is already set, and the CSE Analysis has been preserved,
208  /// it will not use the new CSEOpt(use Recompute to force using the new
209  /// CSEOpt).
210  GISelCSEInfo &get(std::unique_ptr<CSEConfigBase> CSEOpt,
211  bool ReCompute = false);
212  void setMF(MachineFunction &MFunc) { MF = &MFunc; }
213  void setComputed(bool Computed) { AlreadyComputed = Computed; }
214  void releaseMemory() { Info.releaseMemory(); }
215 };
216 
217 /// The actual analysis pass wrapper.
220 
221 public:
222  static char ID;
225  }
226 
227  void getAnalysisUsage(AnalysisUsage &AU) const override;
228 
229  const GISelCSEAnalysisWrapper &getCSEWrapper() const { return Wrapper; }
231 
232  bool runOnMachineFunction(MachineFunction &MF) override;
233 
234  void releaseMemory() override {
235  Wrapper.releaseMemory();
236  Wrapper.setComputed(false);
237  }
238 };
239 
240 } // namespace llvm
241 
242 #endif
The CSE Analysis object.
Definition: CSEInfo.h:71
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void setComputed(bool Computed)
Definition: CSEInfo.h:213
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned Reg
void initializeGISelCSEAnalysisWrapperPassPass(PassRegistry &)
The actual analysis pass wrapper.
Definition: CSEInfo.h:218
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void Profile(FoldingSetNodeID &ID)
-----— UniqueMachineInstr ----------—//
Definition: CSEInfo.cpp:25
void setMF(MachineFunction &MFunc)
Definition: CSEInfo.h:212
const GISelCSEAnalysisWrapper & getCSEWrapper() const
Definition: CSEInfo.h:229
void releaseMemory()
Definition: CSEInfo.cpp:243
friend class GISelCSEInfo
Definition: CSEInfo.h:32
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:117
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:305
std::unique_ptr< CSEConfigBase > getStandardCSEConfigForOpt(CodeGenOpt::Level Level)
Definition: CSEInfo.cpp:65
Abstract class that contains various methods for clients to notify about changes. ...
unsigned const MachineRegisterInfo * MRI
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: CSEInfo.h:234
Represent the analysis usage information of a pass.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Simple wrapper that does the following.
Definition: CSEInfo.h:200
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition: FoldingSet.h:473
MachineOperand class - Representation of each machine instruction operand.
This class implements the register bank concept.
Definition: RegisterBank.h:28
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
Defines a builder that does CSE of MachineInstructions using GISelCSEInfo.
Definition: CSEMIRBuilder.h:32
Node - This class is used to maintain the singly linked bucket list in a folding set.
Definition: FoldingSet.h:135
void setCSEConfig(std::unique_ptr< CSEConfigBase > Opt)
Definition: CSEInfo.h:144
GISelCSEAnalysisWrapper & getCSEWrapper()
Definition: CSEInfo.h:230
IRTranslator LLVM IR MI
A class that wraps MachineInstrs and derives from FoldingSetNode in order to be uniqued in a CSEMap...
Definition: CSEInfo.h:31
This header defines various interfaces for pass management in LLVM.
GISelInstProfileBuilder(FoldingSetNodeID &ID, const MachineRegisterInfo &MRI)
Definition: CSEInfo.h:172