LLVM  11.0.0git
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"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Support/Debug.h"
19 
20 #define DEBUG_TYPE "localizer"
21 
22 using namespace llvm;
23 
24 char Localizer::ID = 0;
26  "Move/duplicate certain instructions close to their use",
27  false, false)
30  "Move/duplicate certain instructions close to their use",
31  false, false)
32 
33 Localizer::Localizer(std::function<bool(const MachineFunction &)> F)
34  : MachineFunctionPass(ID), DoNotRunPass(F) {}
35 
37  : Localizer([](const MachineFunction &) { return false; }) {}
38 
39 void Localizer::init(MachineFunction &MF) {
40  MRI = &MF.getRegInfo();
41  TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(MF.getFunction());
42 }
43 
48 }
49 
50 bool Localizer::isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
51  MachineBasicBlock *&InsertMBB) {
52  MachineInstr &MIUse = *MOUse.getParent();
53  InsertMBB = MIUse.getParent();
54  if (MIUse.isPHI())
55  InsertMBB = MIUse.getOperand(MIUse.getOperandNo(&MOUse) + 1).getMBB();
56  return InsertMBB == Def.getParent();
57 }
58 
59 bool Localizer::localizeInterBlock(MachineFunction &MF,
60  LocalizedSetVecT &LocalizedInstrs) {
61  bool Changed = false;
62  DenseMap<std::pair<MachineBasicBlock *, unsigned>, unsigned> MBBWithLocalDef;
63 
64  // Since the IRTranslator only emits constants into the entry block, and the
65  // rest of the GISel pipeline generally emits constants close to their users,
66  // we only localize instructions in the entry block here. This might change if
67  // we start doing CSE across blocks.
68  auto &MBB = MF.front();
69  auto &TL = *MF.getSubtarget().getTargetLowering();
70  for (auto RI = MBB.rbegin(), RE = MBB.rend(); RI != RE; ++RI) {
71  MachineInstr &MI = *RI;
72  if (!TL.shouldLocalize(MI, TTI))
73  continue;
74  LLVM_DEBUG(dbgs() << "Should localize: " << MI);
75  assert(MI.getDesc().getNumDefs() == 1 &&
76  "More than one definition not supported yet");
77  Register Reg = MI.getOperand(0).getReg();
78  // Check if all the users of MI are local.
79  // We are going to invalidation the list of use operands, so we
80  // can't use range iterator.
81  for (auto MOIt = MRI->use_begin(Reg), MOItEnd = MRI->use_end();
82  MOIt != MOItEnd;) {
83  MachineOperand &MOUse = *MOIt++;
84  // Check if the use is already local.
85  MachineBasicBlock *InsertMBB;
86  LLVM_DEBUG(MachineInstr &MIUse = *MOUse.getParent();
87  dbgs() << "Checking use: " << MIUse
88  << " #Opd: " << MIUse.getOperandNo(&MOUse) << '\n');
89  if (isLocalUse(MOUse, MI, InsertMBB)) {
90  // Even if we're in the same block, if the block is very large we could
91  // still have many long live ranges. Try to do intra-block localization
92  // too.
93  LocalizedInstrs.insert(&MI);
94  continue;
95  }
96  LLVM_DEBUG(dbgs() << "Fixing non-local use\n");
97  Changed = true;
98  auto MBBAndReg = std::make_pair(InsertMBB, Reg);
99  auto NewVRegIt = MBBWithLocalDef.find(MBBAndReg);
100  if (NewVRegIt == MBBWithLocalDef.end()) {
101  // Create the localized instruction.
102  MachineInstr *LocalizedMI = MF.CloneMachineInstr(&MI);
103  LocalizedInstrs.insert(LocalizedMI);
104  MachineInstr &UseMI = *MOUse.getParent();
105  if (MRI->hasOneUse(Reg) && !UseMI.isPHI())
106  InsertMBB->insert(InsertMBB->SkipPHIsAndLabels(UseMI), LocalizedMI);
107  else
108  InsertMBB->insert(InsertMBB->SkipPHIsAndLabels(InsertMBB->begin()),
109  LocalizedMI);
110 
111  // Set a new register for the definition.
112  Register NewReg = MRI->createGenericVirtualRegister(MRI->getType(Reg));
113  MRI->setRegClassOrRegBank(NewReg, MRI->getRegClassOrRegBank(Reg));
114  LocalizedMI->getOperand(0).setReg(NewReg);
115  NewVRegIt =
116  MBBWithLocalDef.insert(std::make_pair(MBBAndReg, NewReg)).first;
117  LLVM_DEBUG(dbgs() << "Inserted: " << *LocalizedMI);
118  }
119  LLVM_DEBUG(dbgs() << "Update use with: " << printReg(NewVRegIt->second)
120  << '\n');
121  // Update the user reg.
122  MOUse.setReg(NewVRegIt->second);
123  }
124  }
125  return Changed;
126 }
127 
128 bool Localizer::localizeIntraBlock(LocalizedSetVecT &LocalizedInstrs) {
129  bool Changed = false;
130 
131  // For each already-localized instruction which has multiple users, then we
132  // scan the block top down from the current position until we hit one of them.
133 
134  // FIXME: Consider doing inst duplication if live ranges are very long due to
135  // many users, but this case may be better served by regalloc improvements.
136 
137  for (MachineInstr *MI : LocalizedInstrs) {
138  Register Reg = MI->getOperand(0).getReg();
139  MachineBasicBlock &MBB = *MI->getParent();
140  // All of the user MIs of this reg.
142  for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
143  if (!UseMI.isPHI())
144  Users.insert(&UseMI);
145  }
146  // If all the users were PHIs then they're not going to be in our block,
147  // don't try to move this instruction.
148  if (Users.empty())
149  continue;
150 
152  ++II;
153  while (II != MBB.end() && !Users.count(&*II))
154  ++II;
155 
156  LLVM_DEBUG(dbgs() << "Intra-block: moving " << *MI << " before " << *&*II
157  << "\n");
158  assert(II != MBB.end() && "Didn't find the user in the MBB");
159  MI->removeFromParent();
160  MBB.insert(II, MI);
161  Changed = true;
162  }
163  return Changed;
164 }
165 
167  // If the ISel pipeline failed, do not bother running that pass.
168  if (MF.getProperties().hasProperty(
170  return false;
171 
172  // Don't run the pass if the target asked so.
173  if (DoNotRunPass(MF))
174  return false;
175 
176  LLVM_DEBUG(dbgs() << "Localize instructions for: " << MF.getName() << '\n');
177 
178  init(MF);
179 
180  // Keep track of the instructions we localized. We'll do a second pass of
181  // intra-block localization to further reduce live ranges.
182  LocalizedSetVecT LocalizedInstrs;
183 
184  bool Changed = localizeInterBlock(MF, LocalizedInstrs);
185  Changed |= localizeIntraBlock(LocalizedInstrs);
186  return Changed;
187 }
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static char ID
Definition: Localizer.h:42
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:509
const MachineFunctionProperties & getProperties() const
Get the function properties.
This pass implements the localization mechanism described at the top of this file.
Definition: Localizer.h:40
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
Definition: MachineInstr.h:620
unsigned Reg
virtual const TargetLowering * getTargetLowering() const
F(f)
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
iv Induction Variable Users
Definition: IVUsers.cpp:52
bool isPHI() const
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineBasicBlock & MBB
INITIALIZE_PASS_BEGIN(Localizer, DEBUG_TYPE, "Move/duplicate certain instructions close to their use", false, false) INITIALIZE_PASS_END(Localizer
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:207
const RegClassOrRegBank & getRegClassOrRegBank(Register Reg) const
Return the register bank or register class of Reg.
AnalysisUsage & addRequired()
Definition: BitVector.h:959
static use_iterator use_end()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
use_iterator use_begin(Register RegNo) const
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: Localizer.cpp:166
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:453
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
void setReg(Register Reg)
Change the register this operand corresponds to.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
reverse_iterator rend()
reverse_iterator rbegin()
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
Wrapper pass for TargetTransformInfo.
Move duplicate certain instructions close to their use
Definition: Localizer.cpp:29
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineInstrBuilder & UseMI
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
bool hasOneUse(Register RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
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:364
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Localizer.cpp:44
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:375
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(Register Reg) const
const MachineBasicBlock & front() const
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define DEBUG_TYPE
Definition: Localizer.cpp:20
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:297
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:439
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
MachineInstr * CloneMachineInstr(const MachineInstr *Orig)
Create a new MachineInstr which is a copy of Orig, identical in all ways except the instruction has n...
MachineOperand class - Representation of each machine instruction operand.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
Definition: MCInstrDesc.h:235
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:280
Representation of each machine instruction.
Definition: MachineInstr.h:62
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasProperty(Property P) const
aarch64 promote const
void setRegClassOrRegBank(Register Reg, const RegClassOrRegBank &RCOrRB)
print Print MemDeps of function
IRTranslator LLVM IR MI
inst_range instructions(Function *F)
Definition: InstIterator.h:133
This pass exposes codegen information to IR-level passes.
Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
Register getReg() const
getReg - Returns the register number.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:466
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This file describes how to lower LLVM code to machine code.
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)