LLVM  10.0.0
CSEInfo.cpp
Go to the documentation of this file.
1 //===- CSEInfo.cpp ------------------------------===//
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 //
9 //
10 //===----------------------------------------------------------------------===//
13 #include "llvm/InitializePasses.h"
14 
15 #define DEBUG_TYPE "cseinfo"
16 
17 using namespace llvm;
22 }
24  "Analysis containing CSE Info", false, true)
26  "Analysis containing CSE Info", false, true)
27 
28 /// -------- UniqueMachineInstr -------------//
29 
31  GISelInstProfileBuilder(ID, MI->getMF()->getRegInfo()).addNodeID(MI);
32 }
33 /// -----------------------------------------
34 
35 /// --------- CSEConfigFull ---------- ///
36 bool CSEConfigFull::shouldCSEOpc(unsigned Opc) {
37  switch (Opc) {
38  default:
39  break;
40  case TargetOpcode::G_ADD:
41  case TargetOpcode::G_AND:
42  case TargetOpcode::G_ASHR:
43  case TargetOpcode::G_LSHR:
44  case TargetOpcode::G_MUL:
45  case TargetOpcode::G_OR:
46  case TargetOpcode::G_SHL:
47  case TargetOpcode::G_SUB:
48  case TargetOpcode::G_XOR:
49  case TargetOpcode::G_UDIV:
50  case TargetOpcode::G_SDIV:
51  case TargetOpcode::G_UREM:
52  case TargetOpcode::G_SREM:
53  case TargetOpcode::G_CONSTANT:
54  case TargetOpcode::G_FCONSTANT:
55  case TargetOpcode::G_ZEXT:
56  case TargetOpcode::G_SEXT:
57  case TargetOpcode::G_ANYEXT:
58  case TargetOpcode::G_UNMERGE_VALUES:
59  case TargetOpcode::G_TRUNC:
60  case TargetOpcode::G_PTR_ADD:
61  return true;
62  }
63  return false;
64 }
65 
67  return Opc == TargetOpcode::G_CONSTANT;
68 }
69 
70 std::unique_ptr<CSEConfigBase>
72  std::unique_ptr<CSEConfigBase> Config;
73  if (Level == CodeGenOpt::None)
74  Config = std::make_unique<CSEConfigConstantOnly>();
75  else
76  Config = std::make_unique<CSEConfigFull>();
77  return Config;
78 }
79 
80 /// -----------------------------------------
81 
82 /// -------- GISelCSEInfo -------------//
84  this->MF = &MF;
85  this->MRI = &MF.getRegInfo();
86 }
87 
89 
90 bool GISelCSEInfo::isUniqueMachineInstValid(
91  const UniqueMachineInstr &UMI) const {
92  // Should we check here and assert that the instruction has been fully
93  // constructed?
94  // FIXME: Any other checks required to be done here? Remove this method if
95  // none.
96  return true;
97 }
98 
99 void GISelCSEInfo::invalidateUniqueMachineInstr(UniqueMachineInstr *UMI) {
100  bool Removed = CSEMap.RemoveNode(UMI);
101  (void)Removed;
102  assert(Removed && "Invalidation called on invalid UMI");
103  // FIXME: Should UMI be deallocated/destroyed?
104 }
105 
106 UniqueMachineInstr *GISelCSEInfo::getNodeIfExists(FoldingSetNodeID &ID,
107  MachineBasicBlock *MBB,
108  void *&InsertPos) {
109  auto *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
110  if (Node) {
111  if (!isUniqueMachineInstValid(*Node)) {
112  invalidateUniqueMachineInstr(Node);
113  return nullptr;
114  }
115 
116  if (Node->MI->getParent() != MBB)
117  return nullptr;
118  }
119  return Node;
120 }
121 
122 void GISelCSEInfo::insertNode(UniqueMachineInstr *UMI, void *InsertPos) {
123  handleRecordedInsts();
124  assert(UMI);
125  UniqueMachineInstr *MaybeNewNode = UMI;
126  if (InsertPos)
127  CSEMap.InsertNode(UMI, InsertPos);
128  else
129  MaybeNewNode = CSEMap.GetOrInsertNode(UMI);
130  if (MaybeNewNode != UMI) {
131  // A similar node exists in the folding set. Let's ignore this one.
132  return;
133  }
134  assert(InstrMapping.count(UMI->MI) == 0 &&
135  "This instruction should not be in the map");
136  InstrMapping[UMI->MI] = MaybeNewNode;
137 }
138 
139 UniqueMachineInstr *GISelCSEInfo::getUniqueInstrForMI(const MachineInstr *MI) {
140  assert(shouldCSE(MI->getOpcode()) && "Trying to CSE an unsupported Node");
141  auto *Node = new (UniqueInstrAllocator) UniqueMachineInstr(MI);
142  return Node;
143 }
144 
145 void GISelCSEInfo::insertInstr(MachineInstr *MI, void *InsertPos) {
146  assert(MI);
147  // If it exists in temporary insts, remove it.
148  TemporaryInsts.remove(MI);
149  auto *Node = getUniqueInstrForMI(MI);
150  insertNode(Node, InsertPos);
151 }
152 
153 MachineInstr *GISelCSEInfo::getMachineInstrIfExists(FoldingSetNodeID &ID,
154  MachineBasicBlock *MBB,
155  void *&InsertPos) {
156  handleRecordedInsts();
157  if (auto *Inst = getNodeIfExists(ID, MBB, InsertPos)) {
158  LLVM_DEBUG(dbgs() << "CSEInfo::Found Instr " << *Inst->MI;);
159  return const_cast<MachineInstr *>(Inst->MI);
160  }
161  return nullptr;
162 }
163 
164 void GISelCSEInfo::countOpcodeHit(unsigned Opc) {
165 #ifndef NDEBUG
166  if (OpcodeHitTable.count(Opc))
167  OpcodeHitTable[Opc] += 1;
168  else
169  OpcodeHitTable[Opc] = 1;
170 #endif
171  // Else do nothing.
172 }
173 
175  if (shouldCSE(MI->getOpcode())) {
176  TemporaryInsts.insert(MI);
177  LLVM_DEBUG(dbgs() << "CSEInfo::Recording new MI " << *MI);
178  }
179 }
180 
182  assert(shouldCSE(MI->getOpcode()) && "Invalid instruction for CSE");
183  auto *UMI = InstrMapping.lookup(MI);
184  LLVM_DEBUG(dbgs() << "CSEInfo::Handling recorded MI " << *MI);
185  if (UMI) {
186  // Invalidate this MI.
187  invalidateUniqueMachineInstr(UMI);
188  InstrMapping.erase(MI);
189  }
190  /// Now insert the new instruction.
191  if (UMI) {
192  /// We'll reuse the same UniqueMachineInstr to avoid the new
193  /// allocation.
194  *UMI = UniqueMachineInstr(MI);
195  insertNode(UMI, nullptr);
196  } else {
197  /// This is a new instruction. Allocate a new UniqueMachineInstr and
198  /// Insert.
199  insertInstr(MI);
200  }
201 }
202 
204  if (auto *UMI = InstrMapping.lookup(MI)) {
205  invalidateUniqueMachineInstr(UMI);
206  InstrMapping.erase(MI);
207  }
208  TemporaryInsts.remove(MI);
209 }
210 
212  while (!TemporaryInsts.empty()) {
213  auto *MI = TemporaryInsts.pop_back_val();
214  handleRecordedInst(MI);
215  }
216 }
217 
218 bool GISelCSEInfo::shouldCSE(unsigned Opc) const {
219  // Only GISel opcodes are CSEable
220  if (!isPreISelGenericOpcode(Opc))
221  return false;
222  assert(CSEOpt.get() && "CSEConfig not set");
223  return CSEOpt->shouldCSEOpc(Opc);
224 }
225 
226 void GISelCSEInfo::erasingInstr(MachineInstr &MI) { handleRemoveInst(&MI); }
227 void GISelCSEInfo::createdInstr(MachineInstr &MI) { recordNewInstruction(&MI); }
229  // For now, perform erase, followed by insert.
230  erasingInstr(MI);
231  createdInstr(MI);
232 }
233 void GISelCSEInfo::changedInstr(MachineInstr &MI) { changingInstr(MI); }
234 
236  setMF(MF);
237  for (auto &MBB : MF) {
238  if (MBB.empty())
239  continue;
240  for (MachineInstr &MI : MBB) {
241  if (!shouldCSE(MI.getOpcode()))
242  continue;
243  LLVM_DEBUG(dbgs() << "CSEInfo::Add MI: " << MI);
244  insertInstr(&MI);
245  }
246  }
247 }
248 
250  print();
251  CSEMap.clear();
252  InstrMapping.clear();
253  UniqueInstrAllocator.Reset();
254  TemporaryInsts.clear();
255  CSEOpt.reset();
256  MRI = nullptr;
257  MF = nullptr;
258 #ifndef NDEBUG
259  OpcodeHitTable.clear();
260 #endif
261 }
262 
264  LLVM_DEBUG(for (auto &It
265  : OpcodeHitTable) {
266  dbgs() << "CSEInfo::CSE Hit for Opc " << It.first << " : " << It.second
267  << "\n";
268  };);
269 }
270 /// -----------------------------------------
271 // ---- Profiling methods for FoldingSetNode --- //
274  addNodeIDMBB(MI->getParent());
275  addNodeIDOpcode(MI->getOpcode());
276  for (auto &Op : MI->operands())
277  addNodeIDMachineOperand(Op);
278  addNodeIDFlag(MI->getFlags());
279  return *this;
280 }
281 
284  ID.AddInteger(Opc);
285  return *this;
286 }
287 
290  uint64_t Val = Ty.getUniqueRAWLLTData();
291  ID.AddInteger(Val);
292  return *this;
293 }
294 
297  ID.AddPointer(RC);
298  return *this;
299 }
300 
303  ID.AddPointer(RB);
304  return *this;
305 }
306 
309  ID.AddInteger(Imm);
310  return *this;
311 }
312 
315  ID.AddInteger(Reg);
316  return *this;
317 }
318 
321  addNodeIDMachineOperand(MachineOperand::CreateReg(Reg, false));
322  return *this;
323 }
324 
327  ID.AddPointer(MBB);
328  return *this;
329 }
330 
333  if (Flag)
334  ID.AddInteger(Flag);
335  return *this;
336 }
337 
339  const MachineOperand &MO) const {
340  if (MO.isReg()) {
341  Register Reg = MO.getReg();
342  if (!MO.isDef())
343  addNodeIDRegNum(Reg);
344  LLT Ty = MRI.getType(Reg);
345  if (Ty.isValid())
346  addNodeIDRegType(Ty);
347  auto *RB = MRI.getRegBankOrNull(Reg);
348  if (RB)
349  addNodeIDRegType(RB);
350  auto *RC = MRI.getRegClassOrNull(Reg);
351  if (RC)
352  addNodeIDRegType(RC);
353  assert(!MO.isImplicit() && "Unhandled case");
354  } else if (MO.isImm())
355  ID.AddInteger(MO.getImm());
356  else if (MO.isCImm())
357  ID.AddPointer(MO.getCImm());
358  else if (MO.isFPImm())
359  ID.AddPointer(MO.getFPImm());
360  else if (MO.isPredicate())
361  ID.AddInteger(MO.getPredicate());
362  else
363  llvm_unreachable("Unhandled operand type");
364  // Handle other types
365  return *this;
366 }
367 
368 GISelCSEInfo &
369 GISelCSEAnalysisWrapper::get(std::unique_ptr<CSEConfigBase> CSEOpt,
370  bool Recompute) {
371  if (!AlreadyComputed || Recompute) {
372  Info.setCSEConfig(std::move(CSEOpt));
373  Info.analyze(*MF);
374  AlreadyComputed = true;
375  }
376  return Info;
377 }
379  AU.setPreservesAll();
381 }
382 
384  releaseMemory();
385  Wrapper.setMF(MF);
386  return false;
387 }
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
Definition: CSEInfo.cpp:233
virtual ~GISelCSEInfo()
Definition: CSEInfo.cpp:88
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition: FoldingSet.cpp:51
The CSE Analysis object.
Definition: CSEInfo.h:71
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: CSEInfo.cpp:383
unsigned Reg
const GISelInstProfileBuilder & addNodeIDImmediate(int64_t Imm) const
Definition: CSEInfo.cpp:308
void initializeGISelCSEAnalysisWrapperPassPass(PassRegistry &)
The actual analysis pass wrapper.
Definition: CSEInfo.h:218
block Block Frequency true
void setMF(MachineFunction &MF)
Definition: CSEInfo.cpp:83
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:492
const GISelInstProfileBuilder & addNodeIDRegNum(unsigned Reg) const
Definition: CSEInfo.cpp:314
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
const GISelInstProfileBuilder & addNodeIDFlag(unsigned Flag) const
Definition: CSEInfo.cpp:332
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
Definition: CSEInfo.cpp:228
const GISelInstProfileBuilder & addNodeIDOpcode(unsigned Opc) const
Definition: CSEInfo.cpp:283
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: CSEInfo.cpp:378
const ConstantFP * getFPImm() const
separate const offset from Split GEPs to a variadic base and a constant offset for better CSE
void AddInteger(signed I)
Definition: FoldingSet.cpp:60
virtual bool shouldCSEOpc(unsigned Opc) override
Definition: CSEInfo.cpp:66
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:426
const GISelInstProfileBuilder & addNodeIDRegType(const LLT &Ty) const
Definition: CSEInfo.cpp:289
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
Definition: CSEInfo.cpp:226
void setMF(MachineFunction &MFunc)
Definition: CSEInfo.h:212
bool isFPImm() const
isFPImm - Tests if this is a MO_FPImmediate operand.
bool isPredicate() const
void releaseMemory()
Definition: CSEInfo.cpp:249
bool shouldCSE(unsigned Opc) const
Definition: CSEInfo.cpp:218
void handleRemoveInst(MachineInstr *MI)
Remove this inst from the CSE map.
Definition: CSEInfo.cpp:203
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
virtual bool shouldCSEOpc(unsigned Opc) override
Definition: CSEInfo.cpp:36
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:139
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:305
std::unique_ptr< CSEConfigBase > getStandardCSEConfigForOpt(CodeGenOpt::Level Level)
Definition: CSEInfo.cpp:71
INITIALIZE_PASS_BEGIN(GISelCSEAnalysisWrapperPass, DEBUG_TYPE, "Analysis containing CSE Info", false, true) INITIALIZE_PASS_END(GISelCSEAnalysisWrapperPass
unsigned const MachineRegisterInfo * MRI
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
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:232
bool isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
const GISelInstProfileBuilder & addNodeID(const MachineInstr *MI) const
Definition: CSEInfo.cpp:273
Represent the analysis usage information of a pass.
void countOpcodeHit(unsigned Opc)
Definition: CSEInfo.cpp:164
GISelCSEInfo & get(std::unique_ptr< CSEConfigBase > CSEOpt, bool ReCompute=false)
Takes a CSEConfigBase object that defines what opcodes get CSEd.
Definition: CSEInfo.cpp:369
bool isValid() 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)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void handleRecordedInst(MachineInstr *MI)
Use this callback to inform CSE about a newly fully created instruction.
Definition: CSEInfo.cpp:181
void recordNewInstruction(MachineInstr *MI)
Records a newly created inst in a list and lazily insert it to the CSEMap.
Definition: CSEInfo.cpp:174
MachineOperand class - Representation of each machine instruction operand.
void analyze(MachineFunction &MF)
Definition: CSEInfo.cpp:235
This class implements the register bank concept.
Definition: RegisterBank.h:28
int64_t getImm() const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void setPreservesAll()
Set by analyses that do not transform their input at all.
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:271
Representation of each machine instruction.
Definition: MachineInstr.h:63
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const GISelInstProfileBuilder & addNodeIDMBB(const MachineBasicBlock *MBB) const
Definition: CSEInfo.cpp:326
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:128
bool isReg() const
isReg - Tests if this is a MO_Register operand.
uint16_t getFlags() const
Return the MI flags bitvector.
Definition: MachineInstr.h:307
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel...
Definition: TargetOpcodes.h:30
const GISelInstProfileBuilder & addNodeIDMachineOperand(const MachineOperand &MO) const
Definition: CSEInfo.cpp:338
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
Definition: CSEInfo.cpp:227
IRTranslator LLVM IR MI
#define DEBUG_TYPE
Definition: CSEInfo.cpp:15
A class that wraps MachineInstrs and derives from FoldingSetNode in order to be uniqued in a CSEMap...
Definition: CSEInfo.h:31
Register getReg() const
getReg - Returns the register number.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
void handleRecordedInsts()
Use this callback to insert all the recorded instructions.
Definition: CSEInfo.cpp:211
const ConstantInt * getCImm() const
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
bool isImplicit() const
unsigned getPredicate() const