LLVM 24.0.0git
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/// \file
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"
25
26namespace llvm {
28
29/// A class that wraps MachineInstrs and derives from FoldingSetNode in order to
30/// be uniqued in a CSEMap. The tradeoff here is extra memory allocations for
31/// UniqueMachineInstr vs making MachineInstr bigger.
32class UniqueMachineInstr : public FoldingSetNode {
33 friend class GISelCSEInfo;
34 const MachineInstr *MI;
35 explicit UniqueMachineInstr(const MachineInstr *MI) : MI(MI) {}
36
37public:
39};
40
41// A CSE config for fully optimized builds.
43public:
44 ~CSEConfigFull() override = default;
45 bool shouldCSEOpc(unsigned Opc) override;
46};
47
48// Commonly used for O0 config.
50public:
51 ~CSEConfigConstantOnly() override = default;
52 bool shouldCSEOpc(unsigned Opc) override;
53};
54
55// Returns the standard expected CSEConfig for the given optimization level.
56// We have this logic here so targets can make use of it from their derived
57// TargetPassConfig, but can't put this logic into TargetPassConfig directly
58// because the CodeGen library can't depend on GlobalISel.
59LLVM_ABI std::unique_ptr<CSEConfigBase>
61
62/// The CSE Analysis object.
63/// This installs itself as a delegate to the MachineFunction to track
64/// new instructions as well as deletions. It however will not be able to
65/// track instruction mutations. In such cases, recordNewInstruction should be
66/// called (for eg inside MachineIRBuilder::recordInsertion).
67/// Also because of how just the instruction can be inserted without adding any
68/// operands to the instruction, instructions are uniqued and inserted lazily.
69/// CSEInfo should assert when trying to enter an incomplete instruction into
70/// the CSEMap. There is Opcode level granularity on which instructions can be
71/// CSE'd and for now, only Generic instructions are CSEable.
73 // Make it accessible only to CSEMIRBuilder.
74 friend class CSEMIRBuilder;
75
76 BumpPtrAllocator UniqueInstrAllocator;
78 MachineRegisterInfo *MRI = 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,
100 FoldingSetInsertToken &Token);
101
102 /// Allocate and construct a new UniqueMachineInstr for MI and return.
103 UniqueMachineInstr *getUniqueInstrForMI(const MachineInstr *MI);
104
105 void insertNode(UniqueMachineInstr *UMI, FoldingSetInsertToken Token = {});
106
107 /// Get the MachineInstr(Unique) if it exists already in the CSEMap and the
108 /// same MachineBasicBlock.
109 MachineInstr *getMachineInstrIfExists(FoldingSetNodeID &ID,
111 FoldingSetInsertToken &Token);
112
113 /// Use this method to allocate a new UniqueMachineInstr for MI and insert it
114 /// into the CSEMap. MI should return true for shouldCSE(MI->getOpcode())
115 void insertInstr(MachineInstr *MI, FoldingSetInsertToken Token = {});
116
117 bool HandlingRecordedInstrs = false;
118
119public:
120 GISelCSEInfo() = default;
121
122 ~GISelCSEInfo() override;
123
124 void setMF(MachineFunction &MF);
125
126 Error verify();
127
128 /// Records a newly created inst in a list and lazily insert it to the CSEMap.
129 /// Sometimes, this method might be called with a partially constructed
130 /// MachineInstr,
131 // (right after BuildMI without adding any operands) - and in such cases,
132 // defer the hashing of the instruction to a later stage.
134
135 /// Use this callback to inform CSE about a newly fully created instruction.
137
138 /// Use this callback to insert all the recorded instructions. At this point,
139 /// all of these insts need to be fully constructed and should not be missing
140 /// any operands.
141 void handleRecordedInsts();
142
143 /// Remove this inst from the CSE map. If this inst has not been inserted yet,
144 /// it will be removed from the Tempinsts list if it exists.
146
147 void releaseMemory();
148
149 void setCSEConfig(std::unique_ptr<CSEConfigBase> Opt) {
150 CSEOpt = std::move(Opt);
151 }
152
153 bool shouldCSE(unsigned Opc) const;
154
155 void analyze(MachineFunction &MF);
156
157 void countOpcodeHit(unsigned Opc);
158
159 void print();
160
161 // Observer API
162 void erasingInstr(MachineInstr &MI) override;
163 void createdInstr(MachineInstr &MI) override;
164 void changingInstr(MachineInstr &MI) override;
165 void changedInstr(MachineInstr &MI) override;
166};
167
168class MCRegisterClass;
169using TargetRegisterClass = MCRegisterClass;
170class RegisterBank;
171
172// Simple builder class to easily profile properties about MIs.
208
209/// Simple wrapper that does the following.
210/// 1) Lazily evaluate the MachineFunction to compute CSEable instructions.
211/// 2) Allows configuration of which instructions are CSEd through CSEConfig
212/// object. Provides a method called get which takes a CSEConfig object.
214 GISelCSEInfo Info;
215 MachineFunction *MF = nullptr;
216 bool AlreadyComputed = false;
217
218public:
219 /// Takes a CSEConfigBase object that defines what opcodes get CSEd.
220 /// If CSEConfig is already set, and the CSE Analysis has been preserved,
221 /// it will not use the new CSEOpt(use Recompute to force using the new
222 /// CSEOpt).
223 LLVM_ABI GISelCSEInfo &get(std::unique_ptr<CSEConfigBase> CSEOpt);
224 void setMF(MachineFunction &MFunc) { MF = &MFunc; }
225 void setComputed(bool Computed) { AlreadyComputed = Computed; }
226 void releaseMemory() { Info.releaseMemory(); }
227};
228
229class GISelCSEAnalysis : public AnalysisInfoMixin<GISelCSEAnalysis> {
231 LLVM_ABI static AnalysisKey Key;
232 TargetMachine *TM;
233
234public:
235 using Result = std::unique_ptr<GISelCSEInfo>;
237
240};
241
242/// The actual analysis pass wrapper.
245
246public:
247 static char ID;
249
250 void getAnalysisUsage(AnalysisUsage &AU) const override;
251
252 const GISelCSEAnalysisWrapper &getCSEWrapper() const { return Wrapper; }
254
255 bool runOnMachineFunction(MachineFunction &MF) override;
256
257 void releaseMemory() override {
258 Wrapper.releaseMemory();
259 Wrapper.setComputed(false);
260 }
261};
262
263} // namespace llvm
264
265#endif
uint64_t Imm
MachineBasicBlock & MBB
This file defines the BumpPtrAllocator interface.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_ABI
Definition Compiler.h:211
This file defines a hash set that can be used to remove duplication of nodes in a graph.
This contains common code to allow clients to notify changes to machine instr.
IRTranslator LLVM IR MI
unsigned Opc
Register Reg
ppc ctr loops verify
Represent the analysis usage information of a pass.
bool shouldCSEOpc(unsigned Opc) override
Definition CSEInfo.cpp:79
~CSEConfigConstantOnly() override=default
bool shouldCSEOpc(unsigned Opc) override
------— CSEConfigFull -------— ///
Definition CSEInfo.cpp:33
~CSEConfigFull() override=default
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Insertion token: a failed lookup fills it in, the matching insert consumes it.
Definition FoldingSet.h:284
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:162
FoldingSetNode()=default
const GISelCSEAnalysisWrapper & getCSEWrapper() const
Definition CSEInfo.h:252
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:257
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition CSEInfo.cpp:459
GISelCSEAnalysisWrapper & getCSEWrapper()
Definition CSEInfo.h:253
Simple wrapper that does the following.
Definition CSEInfo.h:213
void setComputed(bool Computed)
Definition CSEInfo.h:225
LLVM_ABI GISelCSEInfo & get(std::unique_ptr< CSEConfigBase > CSEOpt)
Takes a CSEConfigBase object that defines what opcodes get CSEd.
Definition CSEInfo.cpp:435
void setMF(MachineFunction &MFunc)
Definition CSEInfo.h:224
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Definition CSEInfo.cpp:448
std::unique_ptr< GISelCSEInfo > Result
Definition CSEInfo.h:235
GISelCSEAnalysis(TargetMachine *TM)
Definition CSEInfo.h:236
The CSE Analysis object.
Definition CSEInfo.h:72
~GISelCSEInfo() override
void recordNewInstruction(MachineInstr *MI)
Records a newly created inst in a list and lazily insert it to the CSEMap.
Definition CSEInfo.cpp:184
friend class CSEMIRBuilder
Definition CSEInfo.h:74
void setMF(MachineFunction &MF)
-----— GISelCSEInfo ----------—//
Definition CSEInfo.cpp:97
void setCSEConfig(std::unique_ptr< CSEConfigBase > Opt)
Definition CSEInfo.h:149
void handleRecordedInsts()
Use this callback to insert all the recorded instructions.
Definition CSEInfo.cpp:221
void handleRecordedInst(MachineInstr *MI)
Use this callback to inform CSE about a newly fully created instruction.
Definition CSEInfo.cpp:191
void handleRemoveInst(MachineInstr *MI)
Remove this inst from the CSE map.
Definition CSEInfo.cpp:213
GISelCSEInfo()=default
Abstract class that contains various methods for clients to notify about changes.
LLVM_ABI const GISelInstProfileBuilder & addNodeIDOpcode(unsigned Opc) const
Definition CSEInfo.cpp:335
LLVM_ABI const GISelInstProfileBuilder & addNodeIDRegNum(Register Reg) const
Definition CSEInfo.cpp:380
LLVM_ABI const GISelInstProfileBuilder & addNodeIDFlag(unsigned Flag) const
Definition CSEInfo.cpp:398
LLVM_ABI const GISelInstProfileBuilder & addNodeIDImmediate(int64_t Imm) const
Definition CSEInfo.cpp:374
LLVM_ABI const GISelInstProfileBuilder & addNodeIDReg(Register Reg) const
Definition CSEInfo.cpp:405
LLVM_ABI const GISelInstProfileBuilder & addNodeID(const MachineInstr *MI) const
Definition CSEInfo.cpp:325
LLVM_ABI const GISelInstProfileBuilder & addNodeIDMBB(const MachineBasicBlock *MBB) const
Definition CSEInfo.cpp:392
GISelInstProfileBuilder(FoldingSetNodeID &ID, const MachineRegisterInfo &MRI)
Definition CSEInfo.h:178
LLVM_ABI const GISelInstProfileBuilder & addNodeIDRegType(const LLT Ty) const
Definition CSEInfo.cpp:341
LLVM_ABI const GISelInstProfileBuilder & addNodeIDMachineOperand(const MachineOperand &MO) const
Definition CSEInfo.cpp:410
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
This class implements the register bank concept.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Primary interface to the complete machine description for the target machine.
A class that wraps MachineInstrs and derives from FoldingSetNode in order to be uniqued in a CSEMap.
Definition CSEInfo.h:32
LLVM_ABI void Profile(FoldingSetNodeID &ID)
friend class GISelCSEInfo
Definition CSEInfo.h:33
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI std::unique_ptr< CSEConfigBase > getStandardCSEConfigForOpt(CodeGenOptLevel Level)
Definition CSEInfo.cpp:85
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
FoldingSetImpl< T, Trait > FoldingSet
This template class is used to instantiate a specialized implementation of the folding set to the nod...
Definition FoldingSet.h:556
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
All attributes(register class or bank and low-level type) a virtual register can have.