LLVM  8.0.1
Localizer.cpp
Go to the documentation of this file.
1 //===- Localizer.cpp ---------------------- Localize some instrs -*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// This file implements the Localizer class.
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/Support/Debug.h"
18 
19 #define DEBUG_TYPE "localizer"
20 
21 using namespace llvm;
22 
23 char Localizer::ID = 0;
25  "Move/duplicate certain instructions close to their use", false,
26  false)
27 
30 }
31 
32 void Localizer::init(MachineFunction &MF) { MRI = &MF.getRegInfo(); }
33 
34 bool Localizer::shouldLocalize(const MachineInstr &MI) {
35  switch (MI.getOpcode()) {
36  default:
37  return false;
38  // Constants-like instructions should be close to their users.
39  // We don't want long live-ranges for them.
40  case TargetOpcode::G_CONSTANT:
41  case TargetOpcode::G_FCONSTANT:
42  case TargetOpcode::G_FRAME_INDEX:
43  return true;
44  }
45 }
46 
50 }
51 
52 bool Localizer::isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
53  MachineBasicBlock *&InsertMBB) {
54  MachineInstr &MIUse = *MOUse.getParent();
55  InsertMBB = MIUse.getParent();
56  if (MIUse.isPHI())
57  InsertMBB = MIUse.getOperand(MIUse.getOperandNo(&MOUse) + 1).getMBB();
58  return InsertMBB == Def.getParent();
59 }
60 
62  // If the ISel pipeline failed, do not bother running that pass.
63  if (MF.getProperties().hasProperty(
65  return false;
66 
67  LLVM_DEBUG(dbgs() << "Localize instructions for: " << MF.getName() << '\n');
68 
69  init(MF);
70 
71  bool Changed = false;
72  // Keep track of the instructions we localized.
73  // We won't need to process them if we see them later in the CFG.
74  SmallPtrSet<MachineInstr *, 16> LocalizedInstrs;
75  DenseMap<std::pair<MachineBasicBlock *, unsigned>, unsigned> MBBWithLocalDef;
76  // TODO: Do bottom up traversal.
77  for (MachineBasicBlock &MBB : MF) {
78  for (MachineInstr &MI : MBB) {
79  if (LocalizedInstrs.count(&MI) || !shouldLocalize(MI))
80  continue;
81  LLVM_DEBUG(dbgs() << "Should localize: " << MI);
82  assert(MI.getDesc().getNumDefs() == 1 &&
83  "More than one definition not supported yet");
84  unsigned Reg = MI.getOperand(0).getReg();
85  // Check if all the users of MI are local.
86  // We are going to invalidation the list of use operands, so we
87  // can't use range iterator.
88  for (auto MOIt = MRI->use_begin(Reg), MOItEnd = MRI->use_end();
89  MOIt != MOItEnd;) {
90  MachineOperand &MOUse = *MOIt++;
91  // Check if the use is already local.
92  MachineBasicBlock *InsertMBB;
93  LLVM_DEBUG(MachineInstr &MIUse = *MOUse.getParent();
94  dbgs() << "Checking use: " << MIUse
95  << " #Opd: " << MIUse.getOperandNo(&MOUse) << '\n');
96  if (isLocalUse(MOUse, MI, InsertMBB))
97  continue;
98  LLVM_DEBUG(dbgs() << "Fixing non-local use\n");
99  Changed = true;
100  auto MBBAndReg = std::make_pair(InsertMBB, Reg);
101  auto NewVRegIt = MBBWithLocalDef.find(MBBAndReg);
102  if (NewVRegIt == MBBWithLocalDef.end()) {
103  // Create the localized instruction.
104  MachineInstr *LocalizedMI = MF.CloneMachineInstr(&MI);
105  LocalizedInstrs.insert(LocalizedMI);
106  // Don't try to be smart for the insertion point.
107  // There is no guarantee that the first seen use is the first
108  // use in the block.
109  InsertMBB->insert(InsertMBB->SkipPHIsAndLabels(InsertMBB->begin()),
110  LocalizedMI);
111 
112  // Set a new register for the definition.
113  unsigned NewReg =
114  MRI->createGenericVirtualRegister(MRI->getType(Reg));
115  MRI->setRegClassOrRegBank(NewReg, MRI->getRegClassOrRegBank(Reg));
116  LocalizedMI->getOperand(0).setReg(NewReg);
117  NewVRegIt =
118  MBBWithLocalDef.insert(std::make_pair(MBBAndReg, NewReg)).first;
119  LLVM_DEBUG(dbgs() << "Inserted: " << *LocalizedMI);
120  }
121  LLVM_DEBUG(dbgs() << "Update use with: " << printReg(NewVRegIt->second)
122  << '\n');
123  // Update the user reg.
124  MOUse.setReg(NewVRegIt->second);
125  }
126  }
127  }
128  return Changed;
129 }
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static char ID
Definition: Localizer.h:41
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition: Utils.cpp:289
const MachineFunctionProperties & getProperties() const
Get the function properties.
This pass implements the localization mechanism described at the top of this file.
Definition: Localizer.h:39
unsigned getReg() const
getReg - Returns the register number.
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
Definition: MachineInstr.h:509
unsigned Reg
LLT getType(unsigned Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register...
bool isPHI() const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:221
static use_iterator use_end()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
void initializeLocalizerPass(PassRegistry &)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:409
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: Localizer.cpp:61
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:406
const RegClassOrRegBank & getRegClassOrRegBank(unsigned Reg) const
Return the register bank or register class of Reg.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
INITIALIZE_PASS(Localizer, DEBUG_TYPE, "Move/duplicate certain instructions close to their use", false, false) Localizer
Definition: Localizer.cpp:24
iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:371
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Localizer.cpp:47
Represent the analysis usage information of a pass.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
#define DEBUG_TYPE
Definition: Localizer.cpp:19
unsigned createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
MachineOperand class - Representation of each machine instruction operand.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
Definition: MCInstrDesc.h:226
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:254
Representation of each machine instruction.
Definition: MachineInstr.h:64
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
use_iterator use_begin(unsigned RegNo) const
void setReg(unsigned Reg)
Change the register this operand corresponds to.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasProperty(Property P) const
IRTranslator LLVM IR MI
void setRegClassOrRegBank(unsigned Reg, const RegClassOrRegBank &RCOrRB)
#define LLVM_DEBUG(X)
Definition: Debug.h:123
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414