LLVM  9.0.0svn
Localizer.cpp
Go to the documentation of this file.
1 //===- Localizer.cpp ---------------------- Localize some instrs -*- C++ -*-==//
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 /// \file
9 /// This file implements the Localizer class.
10 //===----------------------------------------------------------------------===//
11 
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/Support/Debug.h"
17 
18 #define DEBUG_TYPE "localizer"
19 
20 using namespace llvm;
21 
22 char Localizer::ID = 0;
24  "Move/duplicate certain instructions close to their use", false,
25  false)
26 
29 }
30 
31 void Localizer::init(MachineFunction &MF) { MRI = &MF.getRegInfo(); }
32 
33 bool Localizer::shouldLocalize(const MachineInstr &MI) {
34  switch (MI.getOpcode()) {
35  default:
36  return false;
37  // Constants-like instructions should be close to their users.
38  // We don't want long live-ranges for them.
39  case TargetOpcode::G_CONSTANT:
40  case TargetOpcode::G_FCONSTANT:
41  case TargetOpcode::G_FRAME_INDEX:
42  return true;
43  }
44 }
45 
49 }
50 
51 bool Localizer::isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
52  MachineBasicBlock *&InsertMBB) {
53  MachineInstr &MIUse = *MOUse.getParent();
54  InsertMBB = MIUse.getParent();
55  if (MIUse.isPHI())
56  InsertMBB = MIUse.getOperand(MIUse.getOperandNo(&MOUse) + 1).getMBB();
57  return InsertMBB == Def.getParent();
58 }
59 
61  // If the ISel pipeline failed, do not bother running that pass.
62  if (MF.getProperties().hasProperty(
64  return false;
65 
66  LLVM_DEBUG(dbgs() << "Localize instructions for: " << MF.getName() << '\n');
67 
68  init(MF);
69 
70  bool Changed = false;
71  // Keep track of the instructions we localized.
72  // We won't need to process them if we see them later in the CFG.
73  SmallPtrSet<MachineInstr *, 16> LocalizedInstrs;
74  DenseMap<std::pair<MachineBasicBlock *, unsigned>, unsigned> MBBWithLocalDef;
75  // TODO: Do bottom up traversal.
76  for (MachineBasicBlock &MBB : MF) {
77  for (MachineInstr &MI : MBB) {
78  if (LocalizedInstrs.count(&MI) || !shouldLocalize(MI))
79  continue;
80  LLVM_DEBUG(dbgs() << "Should localize: " << MI);
81  assert(MI.getDesc().getNumDefs() == 1 &&
82  "More than one definition not supported yet");
83  unsigned Reg = MI.getOperand(0).getReg();
84  // Check if all the users of MI are local.
85  // We are going to invalidation the list of use operands, so we
86  // can't use range iterator.
87  for (auto MOIt = MRI->use_begin(Reg), MOItEnd = MRI->use_end();
88  MOIt != MOItEnd;) {
89  MachineOperand &MOUse = *MOIt++;
90  // Check if the use is already local.
91  MachineBasicBlock *InsertMBB;
92  LLVM_DEBUG(MachineInstr &MIUse = *MOUse.getParent();
93  dbgs() << "Checking use: " << MIUse
94  << " #Opd: " << MIUse.getOperandNo(&MOUse) << '\n');
95  if (isLocalUse(MOUse, MI, InsertMBB))
96  continue;
97  LLVM_DEBUG(dbgs() << "Fixing non-local use\n");
98  Changed = true;
99  auto MBBAndReg = std::make_pair(InsertMBB, Reg);
100  auto NewVRegIt = MBBWithLocalDef.find(MBBAndReg);
101  if (NewVRegIt == MBBWithLocalDef.end()) {
102  // Create the localized instruction.
103  MachineInstr *LocalizedMI = MF.CloneMachineInstr(&MI);
104  LocalizedInstrs.insert(LocalizedMI);
105  // Don't try to be smart for the insertion point.
106  // There is no guarantee that the first seen use is the first
107  // use in the block.
108  InsertMBB->insert(InsertMBB->SkipPHIsAndLabels(InsertMBB->begin()),
109  LocalizedMI);
110 
111  // Set a new register for the definition.
112  unsigned NewReg =
113  MRI->createGenericVirtualRegister(MRI->getType(Reg));
114  MRI->setRegClassOrRegBank(NewReg, MRI->getRegClassOrRegBank(Reg));
115  LocalizedMI->getOperand(0).setReg(NewReg);
116  NewVRegIt =
117  MBBWithLocalDef.insert(std::make_pair(MBBAndReg, NewReg)).first;
118  LLVM_DEBUG(dbgs() << "Inserted: " << *LocalizedMI);
119  }
120  LLVM_DEBUG(dbgs() << "Update use with: " << printReg(NewVRegIt->second)
121  << '\n');
122  // Update the user reg.
123  MOUse.setReg(NewVRegIt->second);
124  }
125  }
126  }
127  return Changed;
128 }
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:40
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition: Utils.cpp:338
const MachineFunctionProperties & getProperties() const
Get the function properties.
This pass implements the localization mechanism described at the top of this file.
Definition: Localizer.h:38
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:508
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:408
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: Localizer.cpp:60
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:405
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:23
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:370
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Localizer.cpp:46
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:381
#define DEBUG_TYPE
Definition: Localizer.cpp:18
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:417
MachineOperand class - Representation of each machine instruction operand.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
Definition: MCInstrDesc.h:225
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:253
Representation of each machine instruction.
Definition: MachineInstr.h:63
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:122
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:413