LLVM 19.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"
22
23namespace llvm {
24class MachineBasicBlock;
25
26/// A class that wraps MachineInstrs and derives from FoldingSetNode in order to
27/// be uniqued in a CSEMap. The tradeoff here is extra memory allocations for
28/// UniqueMachineInstr vs making MachineInstr bigger.
30 friend class GISelCSEInfo;
31 const MachineInstr *MI;
32 explicit UniqueMachineInstr(const MachineInstr *MI) : MI(MI) {}
33
34public:
36};
37
38// A CSE config for fully optimized builds.
40public:
41 virtual ~CSEConfigFull() = default;
42 bool shouldCSEOpc(unsigned Opc) override;
43};
44
45// Commonly used for O0 config.
47public:
48 virtual ~CSEConfigConstantOnly() = default;
49 bool shouldCSEOpc(unsigned Opc) override;
50};
51
52// Returns the standard expected CSEConfig for the given optimization level.
53// We have this logic here so targets can make use of it from their derived
54// TargetPassConfig, but can't put this logic into TargetPassConfig directly
55// because the CodeGen library can't depend on GlobalISel.
56std::unique_ptr<CSEConfigBase>
58
59/// The CSE Analysis object.
60/// This installs itself as a delegate to the MachineFunction to track
61/// new instructions as well as deletions. It however will not be able to
62/// track instruction mutations. In such cases, recordNewInstruction should be
63/// called (for eg inside MachineIRBuilder::recordInsertion).
64/// Also because of how just the instruction can be inserted without adding any
65/// operands to the instruction, instructions are uniqued and inserted lazily.
66/// CSEInfo should assert when trying to enter an incomplete instruction into
67/// the CSEMap. There is Opcode level granularity on which instructions can be
68/// CSE'd and for now, only Generic instructions are CSEable.
70 // Make it accessible only to CSEMIRBuilder.
71 friend class CSEMIRBuilder;
72
73 BumpPtrAllocator UniqueInstrAllocator;
75 MachineRegisterInfo *MRI = nullptr;
76 MachineFunction *MF = nullptr;
77 std::unique_ptr<CSEConfigBase> CSEOpt;
78 /// Keep a cache of UniqueInstrs for each MachineInstr. In GISel,
79 /// often instructions are mutated (while their ID has completely changed).
80 /// Whenever mutation happens, invalidate the UniqueMachineInstr for the
81 /// MachineInstr
83
84 /// Store instructions that are not fully formed in TemporaryInsts.
85 /// Also because CSE insertion happens lazily, we can remove insts from this
86 /// list and avoid inserting and then removing from the CSEMap.
87 GISelWorkList<8> TemporaryInsts;
88
89 // Only used in asserts.
90 DenseMap<unsigned, unsigned> OpcodeHitTable;
91
92 bool isUniqueMachineInstValid(const UniqueMachineInstr &UMI) const;
93
94 void invalidateUniqueMachineInstr(UniqueMachineInstr *UMI);
95
96 UniqueMachineInstr *getNodeIfExists(FoldingSetNodeID &ID,
97 MachineBasicBlock *MBB, void *&InsertPos);
98
99 /// Allocate and construct a new UniqueMachineInstr for MI and return.
100 UniqueMachineInstr *getUniqueInstrForMI(const MachineInstr *MI);
101
102 void insertNode(UniqueMachineInstr *UMI, void *InsertPos = nullptr);
103
104 /// Get the MachineInstr(Unique) if it exists already in the CSEMap and the
105 /// same MachineBasicBlock.
106 MachineInstr *getMachineInstrIfExists(FoldingSetNodeID &ID,
108 void *&InsertPos);
109
110 /// Use this method to allocate a new UniqueMachineInstr for MI and insert it
111 /// into the CSEMap. MI should return true for shouldCSE(MI->getOpcode())
112 void insertInstr(MachineInstr *MI, void *InsertPos = nullptr);
113
114 bool HandlingRecordedInstrs = false;
115
116public:
117 GISelCSEInfo() = default;
118
119 virtual ~GISelCSEInfo();
120
121 void setMF(MachineFunction &MF);
122
123 Error verify();
124
125 /// Records a newly created inst in a list and lazily insert it to the CSEMap.
126 /// Sometimes, this method might be called with a partially constructed
127 /// MachineInstr,
128 // (right after BuildMI without adding any operands) - and in such cases,
129 // defer the hashing of the instruction to a later stage.
131
132 /// Use this callback to inform CSE about a newly fully created instruction.
134
135 /// Use this callback to insert all the recorded instructions. At this point,
136 /// all of these insts need to be fully constructed and should not be missing
137 /// any operands.
138 void handleRecordedInsts();
139
140 /// Remove this inst from the CSE map. If this inst has not been inserted yet,
141 /// it will be removed from the Tempinsts list if it exists.
143
144 void releaseMemory();
145
146 void setCSEConfig(std::unique_ptr<CSEConfigBase> Opt) {
147 CSEOpt = std::move(Opt);
148 }
149
150 bool shouldCSE(unsigned Opc) const;
151
152 void analyze(MachineFunction &MF);
153
154 void countOpcodeHit(unsigned Opc);
155
156 void print();
157
158 // Observer API
159 void erasingInstr(MachineInstr &MI) override;
160 void createdInstr(MachineInstr &MI) override;
161 void changingInstr(MachineInstr &MI) override;
162 void changedInstr(MachineInstr &MI) override;
163};
164
165class TargetRegisterClass;
166class RegisterBank;
167
168// Simple builder class to easily profile properties about MIs.
171 const MachineRegisterInfo &MRI;
172
173public:
175 : ID(ID), MRI(MRI) {}
176 // Profiling methods.
177 const GISelInstProfileBuilder &addNodeIDOpcode(unsigned Opc) const;
178 const GISelInstProfileBuilder &addNodeIDRegType(const LLT Ty) const;
180
182 addNodeIDRegType(const TargetRegisterClass *RC) const;
184
186
188
189 const GISelInstProfileBuilder &addNodeIDImmediate(int64_t Imm) const;
191 addNodeIDMBB(const MachineBasicBlock *MBB) const;
192
195
196 const GISelInstProfileBuilder &addNodeIDFlag(unsigned Flag) const;
198};
199
200/// Simple wrapper that does the following.
201/// 1) Lazily evaluate the MachineFunction to compute CSEable instructions.
202/// 2) Allows configuration of which instructions are CSEd through CSEConfig
203/// object. Provides a method called get which takes a CSEConfig object.
205 GISelCSEInfo Info;
206 MachineFunction *MF = nullptr;
207 bool AlreadyComputed = false;
208
209public:
210 /// Takes a CSEConfigBase object that defines what opcodes get CSEd.
211 /// If CSEConfig is already set, and the CSE Analysis has been preserved,
212 /// it will not use the new CSEOpt(use Recompute to force using the new
213 /// CSEOpt).
214 GISelCSEInfo &get(std::unique_ptr<CSEConfigBase> CSEOpt,
215 bool ReCompute = false);
216 void setMF(MachineFunction &MFunc) { MF = &MFunc; }
217 void setComputed(bool Computed) { AlreadyComputed = Computed; }
218 void releaseMemory() { Info.releaseMemory(); }
219};
220
221/// The actual analysis pass wrapper.
224
225public:
226 static char ID;
228
229 void getAnalysisUsage(AnalysisUsage &AU) const override;
230
233
234 bool runOnMachineFunction(MachineFunction &MF) override;
235
236 void releaseMemory() override {
237 Wrapper.releaseMemory();
238 Wrapper.setComputed(false);
239 }
240};
241
242} // namespace llvm
243
244#endif
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
This file defines the BumpPtrAllocator interface.
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
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
Load MIR Sample Profile
unsigned Reg
Represent the analysis usage information of a pass.
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
bool shouldCSEOpc(unsigned Opc) override
Definition: CSEInfo.cpp:73
virtual ~CSEConfigConstantOnly()=default
bool shouldCSEOpc(unsigned Opc) override
------— CSEConfigFull -------— ///
Definition: CSEInfo.cpp:37
virtual ~CSEConfigFull()=default
Defines a builder that does CSE of MachineInstructions using GISelCSEInfo.
Definition: CSEMIRBuilder.h:32
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Node - This class is used to maintain the singly linked bucket list in a folding set.
Definition: FoldingSet.h:137
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:319
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition: FoldingSet.h:521
The actual analysis pass wrapper.
Definition: CSEInfo.h:222
const GISelCSEAnalysisWrapper & getCSEWrapper() const
Definition: CSEInfo.h:231
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:236
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: CSEInfo.cpp:441
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: CSEInfo.cpp:446
GISelCSEAnalysisWrapper & getCSEWrapper()
Definition: CSEInfo.h:232
Simple wrapper that does the following.
Definition: CSEInfo.h:204
void setComputed(bool Computed)
Definition: CSEInfo.h:217
void setMF(MachineFunction &MFunc)
Definition: CSEInfo.h:216
GISelCSEInfo & get(std::unique_ptr< CSEConfigBase > CSEOpt, bool ReCompute=false)
Takes a CSEConfigBase object that defines what opcodes get CSEd.
Definition: CSEInfo.cpp:431
The CSE Analysis object.
Definition: CSEInfo.h:69
bool shouldCSE(unsigned Opc) const
Definition: CSEInfo.cpp:230
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
Definition: CSEInfo.cpp:237
void analyze(MachineFunction &MF)
Definition: CSEInfo.cpp:244
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
Definition: CSEInfo.cpp:242
void recordNewInstruction(MachineInstr *MI)
Records a newly created inst in a list and lazily insert it to the CSEMap.
Definition: CSEInfo.cpp:182
void setMF(MachineFunction &MF)
-----— GISelCSEInfo ----------—//
Definition: CSEInfo.cpp:91
virtual ~GISelCSEInfo()
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
Definition: CSEInfo.cpp:235
void countOpcodeHit(unsigned Opc)
Definition: CSEInfo.cpp:172
void setCSEConfig(std::unique_ptr< CSEConfigBase > Opt)
Definition: CSEInfo.h:146
void handleRecordedInsts()
Use this callback to insert all the recorded instructions.
Definition: CSEInfo.cpp:219
void handleRecordedInst(MachineInstr *MI)
Use this callback to inform CSE about a newly fully created instruction.
Definition: CSEInfo.cpp:189
void handleRemoveInst(MachineInstr *MI)
Remove this inst from the CSE map.
Definition: CSEInfo.cpp:211
GISelCSEInfo()=default
void releaseMemory()
Definition: CSEInfo.cpp:256
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
Definition: CSEInfo.cpp:236
Abstract class that contains various methods for clients to notify about changes.
const GISelInstProfileBuilder & addNodeIDOpcode(unsigned Opc) const
Definition: CSEInfo.cpp:335
const GISelInstProfileBuilder & addNodeIDRegNum(Register Reg) const
Definition: CSEInfo.cpp:366
const GISelInstProfileBuilder & addNodeIDFlag(unsigned Flag) const
Definition: CSEInfo.cpp:384
const GISelInstProfileBuilder & addNodeIDImmediate(int64_t Imm) const
Definition: CSEInfo.cpp:360
const GISelInstProfileBuilder & addNodeIDReg(Register Reg) const
Definition: CSEInfo.cpp:391
const GISelInstProfileBuilder & addNodeID(const MachineInstr *MI) const
Definition: CSEInfo.cpp:325
const GISelInstProfileBuilder & addNodeIDMBB(const MachineBasicBlock *MBB) const
Definition: CSEInfo.cpp:378
GISelInstProfileBuilder(FoldingSetNodeID &ID, const MachineRegisterInfo &MRI)
Definition: CSEInfo.h:174
const GISelInstProfileBuilder & addNodeIDRegType(const LLT Ty) const
Definition: CSEInfo.cpp:341
const GISelInstProfileBuilder & addNodeIDMachineOperand(const MachineOperand &MO) const
Definition: CSEInfo.cpp:406
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Representation of each machine instruction.
Definition: MachineInstr.h:68
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.
Definition: RegisterBank.h:28
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
A class that wraps MachineInstrs and derives from FoldingSetNode in order to be uniqued in a CSEMap.
Definition: CSEInfo.h:29
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< CSEConfigBase > getStandardCSEConfigForOpt(CodeGenOptLevel Level)
Definition: CSEInfo.cpp:79
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:54