16#define DEBUG_TYPE "cseinfo"
23 "Analysis containing CSE Info",
false,
true)
37 case TargetOpcode::G_ADD:
38 case TargetOpcode::G_AND:
39 case TargetOpcode::G_ASHR:
40 case TargetOpcode::G_LSHR:
41 case TargetOpcode::G_MUL:
42 case TargetOpcode::G_OR:
43 case TargetOpcode::G_SHL:
44 case TargetOpcode::G_SUB:
45 case TargetOpcode::G_XOR:
46 case TargetOpcode::G_UDIV:
47 case TargetOpcode::G_SDIV:
48 case TargetOpcode::G_UREM:
49 case TargetOpcode::G_SREM:
50 case TargetOpcode::G_CONSTANT:
51 case TargetOpcode::G_FCONSTANT:
52 case TargetOpcode::G_IMPLICIT_DEF:
53 case TargetOpcode::G_ZEXT:
54 case TargetOpcode::G_SEXT:
55 case TargetOpcode::G_ANYEXT:
56 case TargetOpcode::G_UNMERGE_VALUES:
57 case TargetOpcode::G_TRUNC:
58 case TargetOpcode::G_PTR_ADD:
59 case TargetOpcode::G_EXTRACT:
60 case TargetOpcode::G_SELECT:
61 case TargetOpcode::G_BUILD_VECTOR:
62 case TargetOpcode::G_BUILD_VECTOR_TRUNC:
63 case TargetOpcode::G_SEXT_INREG:
64 case TargetOpcode::G_FADD:
65 case TargetOpcode::G_FSUB:
66 case TargetOpcode::G_FMUL:
67 case TargetOpcode::G_FDIV:
68 case TargetOpcode::G_FABS:
70 case TargetOpcode::G_FMAXNUM:
71 case TargetOpcode::G_FMINNUM:
72 case TargetOpcode::G_FMAXNUM_IEEE:
73 case TargetOpcode::G_FMINNUM_IEEE:
80 return Opc == TargetOpcode::G_CONSTANT ||
Opc == TargetOpcode::G_FCONSTANT ||
81 Opc == TargetOpcode::G_IMPLICIT_DEF;
84std::unique_ptr<CSEConfigBase>
86 std::unique_ptr<CSEConfigBase> Config;
88 Config = std::make_unique<CSEConfigConstantOnly>();
90 Config = std::make_unique<CSEConfigFull>();
104bool GISelCSEInfo::isUniqueMachineInstValid(
114 bool Removed = CSEMap.RemoveNode(UMI);
116 assert(Removed &&
"Invalidation called on invalid UMI");
123 auto *
Node = CSEMap.FindNodeOrInsertPos(
ID, InsertPos);
125 if (!isUniqueMachineInstValid(*Node)) {
126 invalidateUniqueMachineInstr(Node);
130 if (
Node->MI->getParent() !=
MBB)
139 UniqueMachineInstr *MaybeNewNode = UMI;
141 CSEMap.InsertNode(UMI, InsertPos);
143 MaybeNewNode = CSEMap.GetOrInsertNode(UMI);
144 if (MaybeNewNode != UMI) {
148 assert(InstrMapping.count(UMI->MI) == 0 &&
149 "This instruction should not be in the map");
150 InstrMapping[UMI->MI] = MaybeNewNode;
155 auto *
Node =
new (UniqueInstrAllocator) UniqueMachineInstr(
MI);
159void GISelCSEInfo::insertInstr(
MachineInstr *
MI,
void *InsertPos) {
162 TemporaryInsts.remove(
MI);
163 auto *
Node = getUniqueInstrForMI(
MI);
164 insertNode(Node, InsertPos);
171 if (
auto *Inst = getNodeIfExists(
ID,
MBB, InsertPos)) {
173 return const_cast<MachineInstr *
>(Inst->MI);
180 ++OpcodeHitTable[
Opc];
187 TemporaryInsts.insert(
MI);
194 auto *UMI = InstrMapping.lookup(
MI);
198 invalidateUniqueMachineInstr(UMI);
199 InstrMapping.erase(
MI);
206 insertNode(UMI,
nullptr);
215 if (
auto *UMI = InstrMapping.lookup(
MI)) {
216 invalidateUniqueMachineInstr(UMI);
217 InstrMapping.erase(
MI);
219 TemporaryInsts.remove(
MI);
223 if (HandlingRecordedInstrs)
225 HandlingRecordedInstrs =
true;
226 while (!TemporaryInsts.empty()) {
227 auto *
MI = TemporaryInsts.pop_back_val();
230 HandlingRecordedInstrs =
false;
234 assert(CSEOpt.get() &&
"CSEConfig not set");
235 return CSEOpt->shouldCSEOpc(
Opc);
249 for (
auto &
MBB : MF) {
262 InstrMapping.clear();
263 UniqueInstrAllocator.Reset();
264 TemporaryInsts.clear();
269 OpcodeHitTable.clear();
277 return OS.
str().c_str();
287 for (
auto &It : InstrMapping) {
292 CSEMap.FindNodeOrInsertPos(TmpID, InsertPos);
293 if (FoundNode != It.second)
295 "CSEMap mismatch, InstrMapping has MIs without "
296 "corresponding Nodes in CSEMap:\n%s",
303 if (!InstrMapping.count(UMI.MI))
305 "Node in CSE without InstrMapping:\n%s",
308 if (InstrMapping[UMI.MI] != &UMI)
310 "Mismatch in CSE mapping:\n%s\n%s",
320 for (
auto &It : OpcodeHitTable)
321 dbgs() <<
"CSEInfo::CSE Hit for Opc " << It.first <<
" : " << It.second
331 for (
const auto &
Op :
MI->operands())
345 uint64_t Val = Ty.getUniqueRAWLLTData();
384 ID.AddInteger(Reg.id());
423 }
else if (MO.
isImm())
424 ID.AddInteger(MO.
getImm());
440 if (!AlreadyComputed || Recompute) {
441 Info.releaseMemory();
442 Info.setCSEConfig(std::move(CSEOpt));
444 AlreadyComputed =
true;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static const char * stringify(const MachineInstr *MI, std::string &S)
Provides analysis for continuously CSEing during GISel passes.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
bool shouldCSEOpc(unsigned Opc) override
bool shouldCSEOpc(unsigned Opc) override
------— CSEConfigFull -------— ///
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
The actual analysis pass wrapper.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
GISelCSEAnalysisWrapperPass()
LLVM_ABI GISelCSEInfo & get(std::unique_ptr< CSEConfigBase > CSEOpt, bool ReCompute=false)
Takes a CSEConfigBase object that defines what opcodes get CSEd.
bool shouldCSE(unsigned Opc) const
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
void analyze(MachineFunction &MF)
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
void recordNewInstruction(MachineInstr *MI)
Records a newly created inst in a list and lazily insert it to the CSEMap.
void setMF(MachineFunction &MF)
-----— GISelCSEInfo ----------—//
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
void countOpcodeHit(unsigned Opc)
void handleRecordedInsts()
Use this callback to insert all the recorded instructions.
void handleRecordedInst(MachineInstr *MI)
Use this callback to inform CSE about a newly fully created instruction.
void handleRemoveInst(MachineInstr *MI)
Remove this inst from the CSE map.
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
LLVM_ABI const GISelInstProfileBuilder & addNodeIDOpcode(unsigned Opc) const
LLVM_ABI const GISelInstProfileBuilder & addNodeIDRegNum(Register Reg) const
LLVM_ABI const GISelInstProfileBuilder & addNodeIDFlag(unsigned Flag) const
LLVM_ABI const GISelInstProfileBuilder & addNodeIDImmediate(int64_t Imm) const
LLVM_ABI const GISelInstProfileBuilder & addNodeIDReg(Register Reg) const
LLVM_ABI const GISelInstProfileBuilder & addNodeID(const MachineInstr *MI) const
LLVM_ABI const GISelInstProfileBuilder & addNodeIDMBB(const MachineBasicBlock *MBB) const
GISelInstProfileBuilder(FoldingSetNodeID &ID, const MachineRegisterInfo &MRI)
LLVM_ABI const GISelInstProfileBuilder & addNodeIDRegType(const LLT Ty) const
LLVM_ABI const GISelInstProfileBuilder & addNodeIDMachineOperand(const MachineOperand &MO) const
MachineFunctionPass(char &ID)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
const ConstantInt * getCImm() const
bool isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
const ConstantFP * getFPImm() const
unsigned getPredicate() const
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
bool isFPImm() const
isFPImm - Tests if this is a MO_FPImmediate operand.
This class implements the register bank concept.
Wrapper class representing virtual and physical registers.
A class that wraps MachineInstrs and derives from FoldingSetNode in order to be uniqued in a CSEMap.
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
NodeAddr< NodeBase * > Node
This is an optimization pass for GlobalISel generic memory operations.
PointerUnion< const TargetRegisterClass *, const RegisterBank * > RegClassOrRegBank
Convenient type to represent either a register class or a register bank.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
LLVM_ABI std::unique_ptr< CSEConfigBase > getStandardCSEConfigForOpt(CodeGenOptLevel Level)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
CodeGenOptLevel
Code generation optimization level.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
All attributes(register class or bank and low-level type) a virtual register can have.