xref: /llvm-project/llvm/lib/CodeGen/GlobalISel/Localizer.cpp (revision 146882242fb5a69b1a4114dbb9f280c9da89f6cb)
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 
12 #include "llvm/CodeGen/GlobalISel/Localizer.h"
13 #include "llvm/Analysis/TargetTransformInfo.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/CodeGen/MachineRegisterInfo.h"
17 #include "llvm/Support/Debug.h"
18 
19 #define DEBUG_TYPE "localizer"
20 
21 using namespace llvm;
22 
23 char Localizer::ID = 0;
24 INITIALIZE_PASS_BEGIN(Localizer, DEBUG_TYPE,
25                       "Move/duplicate certain instructions close to their use",
26                       false, false)
27 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
28 INITIALIZE_PASS_END(Localizer, DEBUG_TYPE,
29                     "Move/duplicate certain instructions close to their use",
30                     false, false)
31 
32 Localizer::Localizer() : MachineFunctionPass(ID) {
33   initializeLocalizerPass(*PassRegistry::getPassRegistry());
34 }
35 
36 void Localizer::init(MachineFunction &MF) {
37   MRI = &MF.getRegInfo();
38   TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(MF.getFunction());
39 }
40 
41 bool Localizer::shouldLocalize(const MachineInstr &MI) {
42   // Assuming a spill and reload of a value has a cost of 1 instruction each,
43   // this helper function computes the maximum number of uses we should consider
44   // for remat. E.g. on arm64 global addresses take 2 insts to materialize. We
45   // break even in terms of code size when the original MI has 2 users vs
46   // choosing to potentially spill. Any more than 2 users we we have a net code
47   // size increase. This doesn't take into account register pressure though.
48   auto maxUses = [](unsigned RematCost) {
49     // A cost of 1 means remats are basically free.
50     if (RematCost == 1)
51       return UINT_MAX;
52     if (RematCost == 2)
53       return 2U;
54 
55     // Remat is too expensive, only sink if there's one user.
56     if (RematCost > 2)
57       return 1U;
58     llvm_unreachable("Unexpected remat cost");
59   };
60 
61   // Helper to walk through uses and terminate if we've reached a limit. Saves
62   // us spending time traversing uses if all we want to know is if it's >= min.
63   auto isUsesAtMost = [&](unsigned Reg, unsigned MaxUses) {
64     unsigned NumUses = 0;
65     auto UI = MRI->use_instr_nodbg_begin(Reg), UE = MRI->use_instr_nodbg_end();
66     for (; UI != UE && NumUses < MaxUses; ++UI) {
67       NumUses++;
68     }
69     // If we haven't reached the end yet then there are more than MaxUses users.
70     return UI == UE;
71   };
72 
73   switch (MI.getOpcode()) {
74   default:
75     return false;
76   // Constants-like instructions should be close to their users.
77   // We don't want long live-ranges for them.
78   case TargetOpcode::G_CONSTANT:
79   case TargetOpcode::G_FCONSTANT:
80   case TargetOpcode::G_FRAME_INDEX:
81     return true;
82   case TargetOpcode::G_GLOBAL_VALUE: {
83     unsigned RematCost = TTI->getGISelRematGlobalCost();
84     unsigned Reg = MI.getOperand(0).getReg();
85     unsigned MaxUses = maxUses(RematCost);
86     if (MaxUses == UINT_MAX)
87       return true; // Remats are "free" so always localize.
88     bool B = isUsesAtMost(Reg, MaxUses);
89     return B;
90   }
91   }
92 }
93 
94 void Localizer::getAnalysisUsage(AnalysisUsage &AU) const {
95   AU.addRequired<TargetTransformInfoWrapperPass>();
96   getSelectionDAGFallbackAnalysisUsage(AU);
97   MachineFunctionPass::getAnalysisUsage(AU);
98 }
99 
100 bool Localizer::isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
101                            MachineBasicBlock *&InsertMBB) {
102   MachineInstr &MIUse = *MOUse.getParent();
103   InsertMBB = MIUse.getParent();
104   if (MIUse.isPHI())
105     InsertMBB = MIUse.getOperand(MIUse.getOperandNo(&MOUse) + 1).getMBB();
106   return InsertMBB == Def.getParent();
107 }
108 
109 bool Localizer::localizeInterBlock(
110     MachineFunction &MF, SmallPtrSetImpl<MachineInstr *> &LocalizedInstrs) {
111   bool Changed = false;
112   DenseMap<std::pair<MachineBasicBlock *, unsigned>, unsigned> MBBWithLocalDef;
113 
114   // Since the IRTranslator only emits constants into the entry block, and the
115   // rest of the GISel pipeline generally emits constants close to their users,
116   // we only localize instructions in the entry block here. This might change if
117   // we start doing CSE across blocks.
118   auto &MBB = MF.front();
119   for (MachineInstr &MI : MBB) {
120     if (LocalizedInstrs.count(&MI) || !shouldLocalize(MI))
121       continue;
122     LLVM_DEBUG(dbgs() << "Should localize: " << MI);
123     assert(MI.getDesc().getNumDefs() == 1 &&
124            "More than one definition not supported yet");
125     unsigned Reg = MI.getOperand(0).getReg();
126     // Check if all the users of MI are local.
127     // We are going to invalidation the list of use operands, so we
128     // can't use range iterator.
129     for (auto MOIt = MRI->use_begin(Reg), MOItEnd = MRI->use_end();
130          MOIt != MOItEnd;) {
131       MachineOperand &MOUse = *MOIt++;
132       // Check if the use is already local.
133       MachineBasicBlock *InsertMBB;
134       LLVM_DEBUG(MachineInstr &MIUse = *MOUse.getParent();
135                  dbgs() << "Checking use: " << MIUse
136                         << " #Opd: " << MIUse.getOperandNo(&MOUse) << '\n');
137       if (isLocalUse(MOUse, MI, InsertMBB))
138         continue;
139       LLVM_DEBUG(dbgs() << "Fixing non-local use\n");
140       Changed = true;
141       auto MBBAndReg = std::make_pair(InsertMBB, Reg);
142       auto NewVRegIt = MBBWithLocalDef.find(MBBAndReg);
143       if (NewVRegIt == MBBWithLocalDef.end()) {
144         // Create the localized instruction.
145         MachineInstr *LocalizedMI = MF.CloneMachineInstr(&MI);
146         LocalizedInstrs.insert(LocalizedMI);
147         MachineInstr &UseMI = *MOUse.getParent();
148         if (MRI->hasOneUse(Reg) && !UseMI.isPHI())
149           InsertMBB->insert(InsertMBB->SkipPHIsAndLabels(UseMI), LocalizedMI);
150         else
151           InsertMBB->insert(InsertMBB->SkipPHIsAndLabels(InsertMBB->begin()),
152                             LocalizedMI);
153 
154         // Set a new register for the definition.
155         unsigned NewReg = MRI->createGenericVirtualRegister(MRI->getType(Reg));
156         MRI->setRegClassOrRegBank(NewReg, MRI->getRegClassOrRegBank(Reg));
157         LocalizedMI->getOperand(0).setReg(NewReg);
158         NewVRegIt =
159             MBBWithLocalDef.insert(std::make_pair(MBBAndReg, NewReg)).first;
160         LLVM_DEBUG(dbgs() << "Inserted: " << *LocalizedMI);
161       }
162       LLVM_DEBUG(dbgs() << "Update use with: " << printReg(NewVRegIt->second)
163                         << '\n');
164       // Update the user reg.
165       MOUse.setReg(NewVRegIt->second);
166     }
167   }
168   return Changed;
169 }
170 
171 bool Localizer::localizeIntraBlock(
172     SmallPtrSetImpl<MachineInstr *> &LocalizedInstrs) {
173   bool Changed = false;
174 
175   // For each already-localized instruction which has multiple users, then we
176   // scan the block top down from the current position until we hit one of them.
177 
178   // FIXME: Consider doing inst duplication if live ranges are very long due to
179   // many users, but this case may be better served by regalloc improvements.
180 
181   for (MachineInstr *MI : LocalizedInstrs) {
182     unsigned Reg = MI->getOperand(0).getReg();
183     MachineBasicBlock &MBB = *MI->getParent();
184     // If the instruction has a single use, we would have already moved it right
185     // before its user in localizeInterBlock().
186     if (MRI->hasOneUse(Reg))
187       continue;
188 
189     // All of the user MIs of this reg.
190     SmallPtrSet<MachineInstr *, 32> Users;
191     for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg))
192       Users.insert(&UseMI);
193 
194     MachineBasicBlock::iterator II(MI);
195     ++II;
196     while (II != MBB.end() && !Users.count(&*II))
197       ++II;
198 
199     LLVM_DEBUG(dbgs() << "Intra-block: moving " << *MI << " before " << *&*II
200                       << "\n");
201     assert(II != MBB.end() && "Didn't find the user in the MBB");
202     MI->removeFromParent();
203     MBB.insert(II, MI);
204     Changed = true;
205   }
206   return Changed;
207 }
208 
209 bool Localizer::runOnMachineFunction(MachineFunction &MF) {
210   // If the ISel pipeline failed, do not bother running that pass.
211   if (MF.getProperties().hasProperty(
212           MachineFunctionProperties::Property::FailedISel))
213     return false;
214 
215   LLVM_DEBUG(dbgs() << "Localize instructions for: " << MF.getName() << '\n');
216 
217   init(MF);
218 
219   // Keep track of the instructions we localized. We'll do a second pass of
220   // intra-block localization to further reduce live ranges.
221   SmallPtrSet<MachineInstr *, 32> LocalizedInstrs;
222 
223   bool Changed = localizeInterBlock(MF, LocalizedInstrs);
224   return Changed |= localizeIntraBlock(LocalizedInstrs);
225 }
226