| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#ifndef LLVM_CODEGEN_GLOBALISEL_CSEINFO_H |
| 13 |
#define LLVM_CODEGEN_GLOBALISEL_CSEINFO_H |
| 14 |
|
| 15 |
#include "llvm/ADT/FoldingSet.h" |
| 16 |
#include "llvm/CodeGen/CSEConfigBase.h" |
| 17 |
#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h" |
| 18 |
#include "llvm/CodeGen/GlobalISel/GISelWorkList.h" |
| 19 |
#include "llvm/CodeGen/MachineFunctionPass.h" |
| 20 |
#include "llvm/Support/Allocator.h" |
| 21 |
#include "llvm/Support/CodeGen.h" |
| 22 |
|
| 23 |
namespace llvm { |
| 24 |
class MachineBasicBlock; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class UniqueMachineInstr : public FoldingSetNode { |
| 30 |
friend class GISelCSEInfo; |
| 31 |
const MachineInstr *MI; |
| 32 |
explicit UniqueMachineInstr(const MachineInstr *MI) : MI(MI) {} |
| 33 |
|
| 34 |
public: |
| 35 |
void Profile(FoldingSetNodeID &ID); |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
class CSEConfigFull : public CSEConfigBase { |
| 40 |
public: |
| 41 |
virtual ~CSEConfigFull() = default; |
| 42 |
virtual bool shouldCSEOpc(unsigned Opc) override; |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
class CSEConfigConstantOnly : public CSEConfigBase { |
| 47 |
public: |
| 48 |
virtual ~CSEConfigConstantOnly() = default; |
| 49 |
virtual bool shouldCSEOpc(unsigned Opc) override; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
std::unique_ptr<CSEConfigBase> |
| 57 |
getStandardCSEConfigForOpt(CodeGenOpt::Level Level); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
class GISelCSEInfo : public GISelChangeObserver { |
| 70 |
|
| 71 |
friend class CSEMIRBuilder; |
| 72 |
|
| 73 |
BumpPtrAllocator UniqueInstrAllocator; |
| 74 |
FoldingSet<UniqueMachineInstr> CSEMap; |
| 75 |
MachineRegisterInfo *MRI = nullptr; |
| 76 |
MachineFunction *MF = nullptr; |
| 77 |
std::unique_ptr<CSEConfigBase> CSEOpt; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
DenseMap<const MachineInstr *, UniqueMachineInstr *> InstrMapping; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
GISelWorkList<8> TemporaryInsts; |
| 88 |
|
| 89 |
|
| 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 |
|
| 100 |
UniqueMachineInstr *getUniqueInstrForMI(const MachineInstr *MI); |
| 101 |
|
| 102 |
void insertNode(UniqueMachineInstr *UMI, void *InsertPos = nullptr); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
MachineInstr *getMachineInstrIfExists(FoldingSetNodeID &ID, |
| 107 |
MachineBasicBlock *MBB, |
| 108 |
void *&InsertPos); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
void insertInstr(MachineInstr *MI, void *InsertPos = nullptr); |
| 113 |
|
| 114 |
public: |
| 115 |
GISelCSEInfo() = default; |
| 116 |
|
| 117 |
virtual ~GISelCSEInfo(); |
| 118 |
|
| 119 |
void setMF(MachineFunction &MF); |
| 120 |
|
| 121 |
Error verify(); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
void recordNewInstruction(MachineInstr *MI); |
| 129 |
|
| 130 |
|
| 131 |
void handleRecordedInst(MachineInstr *MI); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
void handleRecordedInsts(); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
void handleRemoveInst(MachineInstr *MI); |
| 141 |
|
| 142 |
void releaseMemory(); |
| 143 |
|
| 144 |
void setCSEConfig(std::unique_ptr<CSEConfigBase> Opt) { |
| 145 |
CSEOpt = std::move(Opt); |
| 146 |
} |
| 147 |
|
| 148 |
bool shouldCSE(unsigned Opc) const; |
| 149 |
|
| 150 |
void analyze(MachineFunction &MF); |
| 151 |
|
| 152 |
void countOpcodeHit(unsigned Opc); |
| 153 |
|
| 154 |
void print(); |
| 155 |
|
| 156 |
|
| 157 |
void erasingInstr(MachineInstr &MI) override; |
| 158 |
void createdInstr(MachineInstr &MI) override; |
| 159 |
void changingInstr(MachineInstr &MI) override; |
| 160 |
void changedInstr(MachineInstr &MI) override; |
| 161 |
}; |
| 162 |
|
| 163 |
class TargetRegisterClass; |
| 164 |
class RegisterBank; |
| 165 |
|
| 166 |
|
| 167 |
class GISelInstProfileBuilder { |
| 168 |
FoldingSetNodeID &ID; |
| 169 |
const MachineRegisterInfo &MRI; |
| 170 |
|
| 171 |
public: |
| 172 |
GISelInstProfileBuilder(FoldingSetNodeID &ID, const MachineRegisterInfo &MRI) |
| 173 |
: ID(ID), MRI(MRI) {} |
| 174 |
|
| 175 |
const GISelInstProfileBuilder &addNodeIDOpcode(unsigned Opc) const; |
| 176 |
const GISelInstProfileBuilder &addNodeIDRegType(const LLT Ty) const; |
| 177 |
const GISelInstProfileBuilder &addNodeIDRegType(const Register) const; |
| 178 |
|
| 179 |
const GISelInstProfileBuilder & |
| 180 |
addNodeIDRegType(const TargetRegisterClass *RC) const; |
| 181 |
const GISelInstProfileBuilder &addNodeIDRegType(const RegisterBank *RB) const; |
| 182 |
|
| 183 |
const GISelInstProfileBuilder &addNodeIDRegNum(Register Reg) const; |
| 184 |
|
| 185 |
const GISelInstProfileBuilder &addNodeIDReg(Register Reg) const; |
| 186 |
|
| 187 |
const GISelInstProfileBuilder &addNodeIDImmediate(int64_t Imm) const; |
| 188 |
const GISelInstProfileBuilder & |
| 189 |
addNodeIDMBB(const MachineBasicBlock *MBB) const; |
| 190 |
|
| 191 |
const GISelInstProfileBuilder & |
| 192 |
addNodeIDMachineOperand(const MachineOperand &MO) const; |
| 193 |
|
| 194 |
const GISelInstProfileBuilder &addNodeIDFlag(unsigned Flag) const; |
| 195 |
const GISelInstProfileBuilder &addNodeID(const MachineInstr *MI) const; |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
class GISelCSEAnalysisWrapper { |
| 203 |
GISelCSEInfo Info; |
| 204 |
MachineFunction *MF = nullptr; |
| 205 |
bool AlreadyComputed = false; |
| 206 |
|
| 207 |
public: |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
GISelCSEInfo &get(std::unique_ptr<CSEConfigBase> CSEOpt, |
| 213 |
bool ReCompute = false); |
| 214 |
void setMF(MachineFunction &MFunc) { MF = &MFunc; } |
| 215 |
void setComputed(bool Computed) { AlreadyComputed = Computed; } |
| 216 |
void releaseMemory() { Info.releaseMemory(); } |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
class GISelCSEAnalysisWrapperPass : public MachineFunctionPass { |
| 221 |
GISelCSEAnalysisWrapper Wrapper; |
| 222 |
|
| 223 |
public: |
| 224 |
static char ID; |
| 225 |
GISelCSEAnalysisWrapperPass(); |
| 226 |
|
| 227 |
void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 228 |
|
| 229 |
const GISelCSEAnalysisWrapper &getCSEWrapper() const { return Wrapper; } |
| 230 |
GISelCSEAnalysisWrapper &getCSEWrapper() { return Wrapper; } |
| 231 |
|
| 232 |
bool runOnMachineFunction(MachineFunction &MF) override; |
| 233 |
|
| 234 |
void releaseMemory() override { |
| 235 |
Wrapper.releaseMemory(); |
| 236 |
Wrapper.setComputed(false); |
| 237 |
} |
| 238 |
}; |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
#endif |