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 MachineFunction *MF = nullptr;
80 std::unique_ptr<CSEConfigBase> CSEOpt;
81 /// Keep a cache of UniqueInstrs for each MachineInstr. In GISel,
82 /// often instructions are mutated (while their ID has completely changed).
83 /// Whenever mutation happens, invalidate the UniqueMachineInstr for the
84 /// MachineInstr
86
87 /// Store instructions that are not fully formed in TemporaryInsts.
88 /// Also because CSE insertion happens lazily, we can remove insts from this
89 /// list and avoid inserting and then removing from the CSEMap.
90 GISelWorkList<8> TemporaryInsts;
91
92 // Only used in asserts.
93 DenseMap<unsigned, unsigned> OpcodeHitTable;
94
95 bool isUniqueMachineInstValid(const UniqueMachineInstr &UMI) const;
96
97 void invalidateUniqueMachineInstr(UniqueMachineInstr *UMI);
98
99 UniqueMachineInstr *getNodeIfExists(FoldingSetNodeID &ID,
101 FoldingSetInsertToken &Token);
102
103 /// Allocate and construct a new UniqueMachineInstr for MI and return.
104 UniqueMachineInstr *getUniqueInstrForMI(const MachineInstr *MI);
105
106 void insertNode(UniqueMachineInstr *UMI, FoldingSetInsertToken Token = {});
107
108 /// Get the MachineInstr(Unique) if it exists already in the CSEMap and the
109 /// same MachineBasicBlock.
110 MachineInstr *getMachineInstrIfExists(FoldingSetNodeID &ID,
112 FoldingSetInsertToken &Token);
113
114 /// Use this method to allocate a new UniqueMachineInstr for MI and insert it
115 /// into the CSEMap. MI should return true for shouldCSE(MI->getOpcode())
116 void insertInstr(MachineInstr *MI, FoldingSetInsertToken Token = {});
117
118 bool HandlingRecordedInstrs = false;
119
120public:
121 GISelCSEInfo() = default;
122
123 ~GISelCSEInfo() override;
124
125 void setMF(MachineFunction &MF);
126
127 Error verify();
128
129 /// Records a newly created inst in a list and lazily insert it to the CSEMap.
130 /// Sometimes, this method might be called with a partially constructed
131 /// MachineInstr,
132 // (right after BuildMI without adding any operands) - and in such cases,
133 // defer the hashing of the instruction to a later stage.
135
136 /// Use this callback to inform CSE about a newly fully created instruction.
138
139 /// Use this callback to insert all the recorded instructions. At this point,
140 /// all of these insts need to be fully constructed and should not be missing
141 /// any operands.
142 void handleRecordedInsts();
143
144 /// Remove this inst from the CSE map. If this inst has not been inserted yet,
145 /// it will be removed from the Tempinsts list if it exists.
147
148 void releaseMemory();
149
150 void setCSEConfig(std::unique_ptr<CSEConfigBase> Opt) {
151 CSEOpt = std::move(Opt);
152 }
153
154 bool shouldCSE(unsigned Opc) const;
155
156 void analyze(MachineFunction &MF);
157
158 void countOpcodeHit(unsigned Opc);
159
160 void print();
161
162 // Observer API
163 void erasingInstr(MachineInstr &MI) override;
164 void createdInstr(MachineInstr &MI) override;
165 void changingInstr(MachineInstr &MI) override;
166 void changedInstr(MachineInstr &MI) override;
167};
168
169class MCRegisterClass;
170using TargetRegisterClass = MCRegisterClass;
171class RegisterBank;
172
173// Simple builder class to easily profile properties about MIs.
209
210/// Simple wrapper that does the following.
211/// 1) Lazily evaluate the MachineFunction to compute CSEable instructions.
212/// 2) Allows configuration of which instructions are CSEd through CSEConfig
213/// object. Provides a method called get which takes a CSEConfig object.
215 GISelCSEInfo Info;
216 MachineFunction *MF = nullptr;
217 bool AlreadyComputed = false;
218
219public:
220 /// Takes a CSEConfigBase object that defines what opcodes get CSEd.
221 /// If CSEConfig is already set, and the CSE Analysis has been preserved,
222 /// it will not use the new CSEOpt(use Recompute to force using the new
223 /// CSEOpt).
224 LLVM_ABI GISelCSEInfo &get(std::unique_ptr<CSEConfigBase> CSEOpt);
225 void setMF(MachineFunction &MFunc) { MF = &MFunc; }
226 void setComputed(bool Computed) { AlreadyComputed = Computed; }
227 void releaseMemory() { Info.releaseMemory(); }
228};
229
230class GISelCSEAnalysis : public AnalysisInfoMixin<GISelCSEAnalysis> {
232 LLVM_ABI static AnalysisKey Key;
233 TargetMachine *TM;
234
235public:
236 using Result = std::unique_ptr<GISelCSEInfo>;
238
241};
242
243/// The actual analysis pass wrapper.
246
247public:
248 static char ID;
250
251 void getAnalysisUsage(AnalysisUsage &AU) const override;
252
253 const GISelCSEAnalysisWrapper &getCSEWrapper() const { return Wrapper; }
255
256 bool runOnMachineFunction(MachineFunction &MF) override;
257
258 void releaseMemory() override {
259 Wrapper.releaseMemory();
260 Wrapper.setComputed(false);
261 }
262};
263
264} // namespace llvm
265
266#endif
unsigned 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:215
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
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:300
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:214
const GISelCSEAnalysisWrapper & getCSEWrapper() const
Definition CSEInfo.h:253
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:258
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition CSEInfo.cpp:460
GISelCSEAnalysisWrapper & getCSEWrapper()
Definition CSEInfo.h:254
Simple wrapper that does the following.
Definition CSEInfo.h:214
void setComputed(bool Computed)
Definition CSEInfo.h:226
LLVM_ABI GISelCSEInfo & get(std::unique_ptr< CSEConfigBase > CSEOpt)
Takes a CSEConfigBase object that defines what opcodes get CSEd.
Definition CSEInfo.cpp:439
void setMF(MachineFunction &MFunc)
Definition CSEInfo.h:225
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Definition CSEInfo.cpp:452
std::unique_ptr< GISelCSEInfo > Result
Definition CSEInfo.h:236
GISelCSEAnalysis(TargetMachine *TM)
Definition CSEInfo.h:237
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:187
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:150
void handleRecordedInsts()
Use this callback to insert all the recorded instructions.
Definition CSEInfo.cpp:224
void handleRecordedInst(MachineInstr *MI)
Use this callback to inform CSE about a newly fully created instruction.
Definition CSEInfo.cpp:194
void handleRemoveInst(MachineInstr *MI)
Remove this inst from the CSE map.
Definition CSEInfo.cpp:216
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:339
LLVM_ABI const GISelInstProfileBuilder & addNodeIDRegNum(Register Reg) const
Definition CSEInfo.cpp:384
LLVM_ABI const GISelInstProfileBuilder & addNodeIDFlag(unsigned Flag) const
Definition CSEInfo.cpp:402
LLVM_ABI const GISelInstProfileBuilder & addNodeIDImmediate(int64_t Imm) const
Definition CSEInfo.cpp:378
LLVM_ABI const GISelInstProfileBuilder & addNodeIDReg(Register Reg) const
Definition CSEInfo.cpp:409
LLVM_ABI const GISelInstProfileBuilder & addNodeID(const MachineInstr *MI) const
Definition CSEInfo.cpp:329
LLVM_ABI const GISelInstProfileBuilder & addNodeIDMBB(const MachineBasicBlock *MBB) const
Definition CSEInfo.cpp:396
GISelInstProfileBuilder(FoldingSetNodeID &ID, const MachineRegisterInfo &MRI)
Definition CSEInfo.h:179
LLVM_ABI const GISelInstProfileBuilder & addNodeIDRegType(const LLT Ty) const
Definition CSEInfo.cpp:345
LLVM_ABI const GISelInstProfileBuilder & addNodeIDMachineOperand(const MachineOperand &MO) const
Definition CSEInfo.cpp:414
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.
FoldingSetBase::Node FoldingSetNode
Definition FoldingSet.h:415
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI std::unique_ptr< CSEConfigBase > getStandardCSEConfigForOpt(CodeGenOptLevel Level)
Definition CSEInfo.cpp:85
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:149
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:390
FoldingSetImpl< T, Trait > FoldingSet
This template class is used to instantiate a specialized implementation of the folding set to the nod...
Definition FoldingSet.h:540
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.