1 //===- CSEInfo.cpp ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
12 #include "llvm/CodeGen/GlobalISel/CSEInfo.h"
13 #include "llvm/CodeGen/MachineRegisterInfo.h"
15 #define DEBUG_TYPE "cseinfo"
18 char llvm::GISelCSEAnalysisWrapperPass::ID = 0;
19 INITIALIZE_PASS_BEGIN(GISelCSEAnalysisWrapperPass, DEBUG_TYPE,
20 "Analysis containing CSE Info", false, true)
21 INITIALIZE_PASS_END(GISelCSEAnalysisWrapperPass, DEBUG_TYPE,
22 "Analysis containing CSE Info", false, true)
24 /// -------- UniqueMachineInstr -------------//
26 void UniqueMachineInstr::Profile(FoldingSetNodeID &ID) {
27 GISelInstProfileBuilder(ID, MI->getMF()->getRegInfo()).addNodeID(MI);
29 /// -----------------------------------------
31 /// --------- CSEConfig ---------- ///
32 bool CSEConfig::shouldCSEOpc(unsigned Opc) {
36 case TargetOpcode::G_ADD:
37 case TargetOpcode::G_AND:
38 case TargetOpcode::G_ASHR:
39 case TargetOpcode::G_LSHR:
40 case TargetOpcode::G_MUL:
41 case TargetOpcode::G_OR:
42 case TargetOpcode::G_SHL:
43 case TargetOpcode::G_SUB:
44 case TargetOpcode::G_XOR:
45 case TargetOpcode::G_UDIV:
46 case TargetOpcode::G_SDIV:
47 case TargetOpcode::G_UREM:
48 case TargetOpcode::G_SREM:
49 case TargetOpcode::G_CONSTANT:
50 case TargetOpcode::G_FCONSTANT:
51 case TargetOpcode::G_ZEXT:
52 case TargetOpcode::G_SEXT:
53 case TargetOpcode::G_ANYEXT:
54 case TargetOpcode::G_UNMERGE_VALUES:
55 case TargetOpcode::G_TRUNC:
61 bool CSEConfigConstantOnly::shouldCSEOpc(unsigned Opc) {
62 return Opc == TargetOpcode::G_CONSTANT;
64 /// -----------------------------------------
66 /// -------- GISelCSEInfo -------------//
67 void GISelCSEInfo::setMF(MachineFunction &MF) {
69 this->MRI = &MF.getRegInfo();
72 GISelCSEInfo::~GISelCSEInfo() {}
74 bool GISelCSEInfo::isUniqueMachineInstValid(
75 const UniqueMachineInstr &UMI) const {
76 // Should we check here and assert that the instruction has been fully
78 // FIXME: Any other checks required to be done here? Remove this method if
83 void GISelCSEInfo::invalidateUniqueMachineInstr(UniqueMachineInstr *UMI) {
84 bool Removed = CSEMap.RemoveNode(UMI);
86 assert(Removed && "Invalidation called on invalid UMI");
87 // FIXME: Should UMI be deallocated/destroyed?
90 UniqueMachineInstr *GISelCSEInfo::getNodeIfExists(FoldingSetNodeID &ID,
91 MachineBasicBlock *MBB,
93 auto *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
95 if (!isUniqueMachineInstValid(*Node)) {
96 invalidateUniqueMachineInstr(Node);
100 if (Node->MI->getParent() != MBB)
106 void GISelCSEInfo::insertNode(UniqueMachineInstr *UMI, void *InsertPos) {
107 handleRecordedInsts();
109 UniqueMachineInstr *MaybeNewNode = UMI;
111 CSEMap.InsertNode(UMI, InsertPos);
113 MaybeNewNode = CSEMap.GetOrInsertNode(UMI);
114 if (MaybeNewNode != UMI) {
115 // A similar node exists in the folding set. Let's ignore this one.
118 assert(InstrMapping.count(UMI->MI) == 0 &&
119 "This instruction should not be in the map");
120 InstrMapping[UMI->MI] = MaybeNewNode;
123 UniqueMachineInstr *GISelCSEInfo::getUniqueInstrForMI(const MachineInstr *MI) {
124 assert(shouldCSE(MI->getOpcode()) && "Trying to CSE an unsupported Node");
125 auto *Node = new (UniqueInstrAllocator) UniqueMachineInstr(MI);
129 void GISelCSEInfo::insertInstr(MachineInstr *MI, void *InsertPos) {
131 // If it exists in temporary insts, remove it.
132 TemporaryInsts.remove(MI);
133 auto *Node = getUniqueInstrForMI(MI);
134 insertNode(Node, InsertPos);
137 MachineInstr *GISelCSEInfo::getMachineInstrIfExists(FoldingSetNodeID &ID,
138 MachineBasicBlock *MBB,
140 handleRecordedInsts();
141 if (auto *Inst = getNodeIfExists(ID, MBB, InsertPos)) {
142 LLVM_DEBUG(dbgs() << "CSEInfo: Found Instr " << *Inst->MI << "\n";);
143 return const_cast<MachineInstr *>(Inst->MI);
148 void GISelCSEInfo::countOpcodeHit(unsigned Opc) {
150 if (OpcodeHitTable.count(Opc))
151 OpcodeHitTable[Opc] += 1;
153 OpcodeHitTable[Opc] = 1;
158 void GISelCSEInfo::recordNewInstruction(MachineInstr *MI) {
159 if (shouldCSE(MI->getOpcode())) {
160 TemporaryInsts.insert(MI);
161 LLVM_DEBUG(dbgs() << "CSEInfo: Recording new MI" << *MI << "\n";);
165 void GISelCSEInfo::handleRecordedInst(MachineInstr *MI) {
166 assert(shouldCSE(MI->getOpcode()) && "Invalid instruction for CSE");
167 auto *UMI = InstrMapping.lookup(MI);
168 LLVM_DEBUG(dbgs() << "CSEInfo: Handling recorded MI" << *MI << "\n";);
170 // Invalidate this MI.
171 invalidateUniqueMachineInstr(UMI);
172 InstrMapping.erase(MI);
174 /// Now insert the new instruction.
176 /// We'll reuse the same UniqueMachineInstr to avoid the new
178 *UMI = UniqueMachineInstr(MI);
179 insertNode(UMI, nullptr);
181 /// This is a new instruction. Allocate a new UniqueMachineInstr and
187 void GISelCSEInfo::handleRemoveInst(MachineInstr *MI) {
188 if (auto *UMI = InstrMapping.lookup(MI)) {
189 invalidateUniqueMachineInstr(UMI);
190 InstrMapping.erase(MI);
192 TemporaryInsts.remove(MI);
195 void GISelCSEInfo::handleRecordedInsts() {
196 while (!TemporaryInsts.empty()) {
197 auto *MI = TemporaryInsts.pop_back_val();
198 handleRecordedInst(MI);
202 bool GISelCSEInfo::shouldCSE(unsigned Opc) const {
203 // Only GISel opcodes are CSEable
204 if (!isPreISelGenericOpcode(Opc))
206 assert(CSEOpt.get() && "CSEConfig not set");
207 return CSEOpt->shouldCSEOpc(Opc);
210 void GISelCSEInfo::erasingInstr(MachineInstr &MI) { handleRemoveInst(&MI); }
211 void GISelCSEInfo::createdInstr(MachineInstr &MI) { recordNewInstruction(&MI); }
212 void GISelCSEInfo::changingInstr(MachineInstr &MI) {
213 // For now, perform erase, followed by insert.
217 void GISelCSEInfo::changedInstr(MachineInstr &MI) { changingInstr(MI); }
219 void GISelCSEInfo::analyze(MachineFunction &MF) {
221 for (auto &MBB : MF) {
224 for (MachineInstr &MI : MBB) {
225 if (!shouldCSE(MI.getOpcode()))
227 LLVM_DEBUG(dbgs() << "CSEInfo::Add MI: " << MI << "\n";);
233 void GISelCSEInfo::releaseMemory() {
236 InstrMapping.clear();
237 UniqueInstrAllocator.Reset();
238 TemporaryInsts.clear();
243 OpcodeHitTable.clear();
247 void GISelCSEInfo::print() {
249 for (auto &It : OpcodeHitTable) {
250 dbgs() << "CSE Count for Opc " << It.first << " : " << It.second << "\n";
254 /// -----------------------------------------
255 // ---- Profiling methods for FoldingSetNode --- //
256 const GISelInstProfileBuilder &
257 GISelInstProfileBuilder::addNodeID(const MachineInstr *MI) const {
258 addNodeIDMBB(MI->getParent());
259 addNodeIDOpcode(MI->getOpcode());
260 for (auto &Op : MI->operands())
261 addNodeIDMachineOperand(Op);
262 addNodeIDFlag(MI->getFlags());
266 const GISelInstProfileBuilder &
267 GISelInstProfileBuilder::addNodeIDOpcode(unsigned Opc) const {
272 const GISelInstProfileBuilder &
273 GISelInstProfileBuilder::addNodeIDRegType(const LLT &Ty) const {
274 uint64_t Val = Ty.getUniqueRAWLLTData();
279 const GISelInstProfileBuilder &
280 GISelInstProfileBuilder::addNodeIDRegType(const TargetRegisterClass *RC) const {
285 const GISelInstProfileBuilder &
286 GISelInstProfileBuilder::addNodeIDRegType(const RegisterBank *RB) const {
291 const GISelInstProfileBuilder &
292 GISelInstProfileBuilder::addNodeIDImmediate(int64_t Imm) const {
297 const GISelInstProfileBuilder &
298 GISelInstProfileBuilder::addNodeIDRegNum(unsigned Reg) const {
303 const GISelInstProfileBuilder &
304 GISelInstProfileBuilder::addNodeIDRegType(const unsigned Reg) const {
305 addNodeIDMachineOperand(MachineOperand::CreateReg(Reg, false));
309 const GISelInstProfileBuilder &
310 GISelInstProfileBuilder::addNodeIDMBB(const MachineBasicBlock *MBB) const {
315 const GISelInstProfileBuilder &
316 GISelInstProfileBuilder::addNodeIDFlag(unsigned Flag) const {
322 const GISelInstProfileBuilder &GISelInstProfileBuilder::addNodeIDMachineOperand(
323 const MachineOperand &MO) const {
325 unsigned Reg = MO.getReg();
327 addNodeIDRegNum(Reg);
328 LLT Ty = MRI.getType(Reg);
330 addNodeIDRegType(Ty);
331 auto *RB = MRI.getRegBankOrNull(Reg);
333 addNodeIDRegType(RB);
334 auto *RC = MRI.getRegClassOrNull(Reg);
336 addNodeIDRegType(RC);
337 assert(!MO.isImplicit() && "Unhandled case");
338 } else if (MO.isImm())
339 ID.AddInteger(MO.getImm());
340 else if (MO.isCImm())
341 ID.AddPointer(MO.getCImm());
342 else if (MO.isFPImm())
343 ID.AddPointer(MO.getFPImm());
344 else if (MO.isPredicate())
345 ID.AddInteger(MO.getPredicate());
347 llvm_unreachable("Unhandled operand type");
348 // Handle other types
352 GISelCSEInfo &GISelCSEAnalysisWrapper::get(std::unique_ptr<CSEConfig> CSEOpt,
354 if (!AlreadyComputed || Recompute) {
355 Info.setCSEConfig(std::move(CSEOpt));
357 AlreadyComputed = true;
361 void GISelCSEAnalysisWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
362 AU.setPreservesAll();
363 MachineFunctionPass::getAnalysisUsage(AU);
366 bool GISelCSEAnalysisWrapperPass::runOnMachineFunction(MachineFunction &MF) {