LLVM  9.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"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/Allocator.h"
24 
25 namespace llvm {
26 
27 /// A class that wraps MachineInstrs and derives from FoldingSetNode in order to
28 /// be uniqued in a CSEMap. The tradeoff here is extra memory allocations for
29 /// UniqueMachineInstr vs making MachineInstr bigger.
31  friend class GISelCSEInfo;
32  const MachineInstr *MI;
33  explicit UniqueMachineInstr(const MachineInstr *MI) : MI(MI) {}
34 
35 public:
37 };
38 
39 // Class representing some configuration that can be done during CSE analysis.
40 // Currently it only supports shouldCSE method that each pass can set.
41 class CSEConfig {
42 public:
43  virtual ~CSEConfig() = default;
44  // Hook for defining which Generic instructions should be CSEd.
45  // GISelCSEInfo currently only calls this hook when dealing with generic
46  // opcodes.
47  virtual bool shouldCSEOpc(unsigned Opc);
48 };
49 
50 // TODO: Find a better place for this.
51 // Commonly used for O0 config.
53 public:
54  virtual ~CSEConfigConstantOnly() = default;
55  virtual bool shouldCSEOpc(unsigned Opc) override;
56 };
57 
58 /// The CSE Analysis object.
59 /// This installs itself as a delegate to the MachineFunction to track
60 /// new instructions as well as deletions. It however will not be able to
61 /// track instruction mutations. In such cases, recordNewInstruction should be
62 /// called (for eg inside MachineIRBuilder::recordInsertion).
63 /// Also because of how just the instruction can be inserted without adding any
64 /// operands to the instruction, instructions are uniqued and inserted lazily.
65 /// CSEInfo should assert when trying to enter an incomplete instruction into
66 /// the CSEMap. There is Opcode level granularity on which instructions can be
67 /// CSE'd and for now, only Generic instructions are CSEable.
69  // Make it accessible only to CSEMIRBuilder.
70  friend class CSEMIRBuilder;
71 
72  BumpPtrAllocator UniqueInstrAllocator;
74  MachineRegisterInfo *MRI = nullptr;
75  MachineFunction *MF = nullptr;
76  std::unique_ptr<CSEConfig> CSEOpt;
77  /// Keep a cache of UniqueInstrs for each MachineInstr. In GISel,
78  /// often instructions are mutated (while their ID has completely changed).
79  /// Whenever mutation happens, invalidate the UniqueMachineInstr for the
80  /// MachineInstr
82 
83  /// Store instructions that are not fully formed in TemporaryInsts.
84  /// Also because CSE insertion happens lazily, we can remove insts from this
85  /// list and avoid inserting and then removing from the CSEMap.
86  GISelWorkList<8> TemporaryInsts;
87 
88  // Only used in asserts.
89  DenseMap<unsigned, unsigned> OpcodeHitTable;
90 
91  bool isUniqueMachineInstValid(const UniqueMachineInstr &UMI) const;
92 
93  void invalidateUniqueMachineInstr(UniqueMachineInstr *UMI);
94 
95  UniqueMachineInstr *getNodeIfExists(FoldingSetNodeID &ID,
96  MachineBasicBlock *MBB, void *&InsertPos);
97 
98  /// Allocate and construct a new UniqueMachineInstr for MI and return.
99  UniqueMachineInstr *getUniqueInstrForMI(const MachineInstr *MI);
100 
101  void insertNode(UniqueMachineInstr *UMI, void *InsertPos = nullptr);
102 
103  /// Get the MachineInstr(Unique) if it exists already in the CSEMap and the
104  /// same MachineBasicBlock.
105  MachineInstr *getMachineInstrIfExists(FoldingSetNodeID &ID,
106  MachineBasicBlock *MBB,
107  void *&InsertPos);
108 
109  /// Use this method to allocate a new UniqueMachineInstr for MI and insert it
110  /// into the CSEMap. MI should return true for shouldCSE(MI->getOpcode())
111  void insertInstr(MachineInstr *MI, void *InsertPos = nullptr);
112 
113 public:
114  GISelCSEInfo() = default;
115 
116  virtual ~GISelCSEInfo();
117 
118  void setMF(MachineFunction &MF);
119 
120  /// Records a newly created inst in a list and lazily insert it to the CSEMap.
121  /// Sometimes, this method might be called with a partially constructed
122  /// MachineInstr,
123  // (right after BuildMI without adding any operands) - and in such cases,
124  // defer the hashing of the instruction to a later stage.
125  void recordNewInstruction(MachineInstr *MI);
126 
127  /// Use this callback to inform CSE about a newly fully created instruction.
128  void handleRecordedInst(MachineInstr *MI);
129 
130  /// Use this callback to insert all the recorded instructions. At this point,
131  /// all of these insts need to be fully constructed and should not be missing
132  /// any operands.
133  void handleRecordedInsts();
134 
135  /// Remove this inst from the CSE map. If this inst has not been inserted yet,
136  /// it will be removed from the Tempinsts list if it exists.
137  void handleRemoveInst(MachineInstr *MI);
138 
139  void releaseMemory();
140 
141  void setCSEConfig(std::unique_ptr<CSEConfig> Opt) { CSEOpt = std::move(Opt); }
142 
143  bool shouldCSE(unsigned Opc) const;
144 
145  void analyze(MachineFunction &MF);
146 
147  void countOpcodeHit(unsigned Opc);
148 
149  void print();
150 
151  // Observer API
152  void erasingInstr(MachineInstr &MI) override;
153  void createdInstr(MachineInstr &MI) override;
154  void changingInstr(MachineInstr &MI) override;
155  void changedInstr(MachineInstr &MI) override;
156 };
157 
158 class TargetRegisterClass;
159 class RegisterBank;
160 
161 // Simple builder class to easily profile properties about MIs.
164  const MachineRegisterInfo &MRI;
165 
166 public:
168  : ID(ID), MRI(MRI) {}
169  // Profiling methods.
170  const GISelInstProfileBuilder &addNodeIDOpcode(unsigned Opc) const;
171  const GISelInstProfileBuilder &addNodeIDRegType(const LLT &Ty) const;
172  const GISelInstProfileBuilder &addNodeIDRegType(const unsigned) const;
173 
175  addNodeIDRegType(const TargetRegisterClass *RC) const;
176  const GISelInstProfileBuilder &addNodeIDRegType(const RegisterBank *RB) const;
177 
178  const GISelInstProfileBuilder &addNodeIDRegNum(unsigned Reg) const;
179 
180  const GISelInstProfileBuilder &addNodeIDImmediate(int64_t Imm) const;
182  addNodeIDMBB(const MachineBasicBlock *MBB) const;
183 
185  addNodeIDMachineOperand(const MachineOperand &MO) const;
186 
187  const GISelInstProfileBuilder &addNodeIDFlag(unsigned Flag) const;
188  const GISelInstProfileBuilder &addNodeID(const MachineInstr *MI) const;
189 };
190 
191 /// Simple wrapper that does the following.
192 /// 1) Lazily evaluate the MachineFunction to compute CSEable instructions.
193 /// 2) Allows configuration of which instructions are CSEd through CSEConfig
194 /// object. Provides a method called get which takes a CSEConfig object.
197  MachineFunction *MF = nullptr;
198  bool AlreadyComputed = false;
199 
200 public:
201  /// Takes a CSEConfig object that defines what opcodes get CSEd.
202  /// If CSEConfig is already set, and the CSE Analysis has been preserved,
203  /// it will not use the new CSEOpt(use Recompute to force using the new
204  /// CSEOpt).
205  GISelCSEInfo &get(std::unique_ptr<CSEConfig> CSEOpt, bool ReCompute = false);
206  void setMF(MachineFunction &MFunc) { MF = &MFunc; }
207  void setComputed(bool Computed) { AlreadyComputed = Computed; }
208  void releaseMemory() { Info.releaseMemory(); }
209 };
210 
211 /// The actual analysis pass wrapper.
214 
215 public:
216  static char ID;
219  }
220 
221  void getAnalysisUsage(AnalysisUsage &AU) const override;
222 
223  const GISelCSEAnalysisWrapper &getCSEWrapper() const { return Wrapper; }
225 
226  bool runOnMachineFunction(MachineFunction &MF) override;
227 
228  void releaseMemory() override {
229  Wrapper.releaseMemory();
230  Wrapper.setComputed(false);
231  }
232 };
233 
234 } // namespace llvm
235 
236 #endif
The CSE Analysis object.
Definition: CSEInfo.h:68
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void setComputed(bool Computed)
Definition: CSEInfo.h:207
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned Reg
void initializeGISelCSEAnalysisWrapperPassPass(PassRegistry &)
The actual analysis pass wrapper.
Definition: CSEInfo.h:212
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:206
const GISelCSEAnalysisWrapper & getCSEWrapper() const
Definition: CSEInfo.h:223
void releaseMemory()
Definition: CSEInfo.cpp:232
friend class GISelCSEInfo
Definition: CSEInfo.h:31
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
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:228
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:195
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.
void setCSEConfig(std::unique_ptr< CSEConfig > Opt)
Definition: CSEInfo.h:141
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:63
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
GISelCSEAnalysisWrapper & getCSEWrapper()
Definition: CSEInfo.h:224
IRTranslator LLVM IR MI
A class that wraps MachineInstrs and derives from FoldingSetNode in order to be uniqued in a CSEMap...
Definition: CSEInfo.h:30
This header defines various interfaces for pass management in LLVM.
GISelInstProfileBuilder(FoldingSetNodeID &ID, const MachineRegisterInfo &MRI)
Definition: CSEInfo.h:167