1 //===- llvm/CodeGen/GlobalISel/RegBankSelect.cpp - RegBankSelect -*- 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 RegBankSelect class.
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
14 #include "llvm/ADT/PostOrderIterator.h"
15 #include "llvm/CodeGen/GlobalISel/MachineLegalizer.h"
16 #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
17 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
18 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/Support/BlockFrequency.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Target/TargetSubtargetInfo.h"
25 
26 #define DEBUG_TYPE "regbankselect"
27 
28 using namespace llvm;
29 
30 static cl::opt<RegBankSelect::Mode> RegBankSelectMode(
31     cl::desc("Mode of the RegBankSelect pass"), cl::Hidden, cl::Optional,
32     cl::values(clEnumValN(RegBankSelect::Mode::Fast, "regbankselect-fast",
33                           "Run the Fast mode (default mapping)"),
34                clEnumValN(RegBankSelect::Mode::Greedy, "regbankselect-greedy",
35                           "Use the Greedy mode (best local mapping)"),
36                clEnumValEnd));
37 
38 char RegBankSelect::ID = 0;
39 INITIALIZE_PASS_BEGIN(RegBankSelect, "regbankselect",
40                       "Assign register bank of generic virtual registers",
41                       false, false);
42 INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
43 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
44 INITIALIZE_PASS_END(RegBankSelect, "regbankselect",
45                     "Assign register bank of generic virtual registers", false,
46                     false)
47 
48 RegBankSelect::RegBankSelect(Mode RunningMode)
49     : MachineFunctionPass(ID), RBI(nullptr), MRI(nullptr), TRI(nullptr),
50       MBFI(nullptr), MBPI(nullptr), OptMode(RunningMode) {
51   initializeRegBankSelectPass(*PassRegistry::getPassRegistry());
52   if (RegBankSelectMode.getNumOccurrences() != 0) {
53     OptMode = RegBankSelectMode;
54     if (RegBankSelectMode != RunningMode)
55       DEBUG(dbgs() << "RegBankSelect mode overrided by command line\n");
56   }
57 }
58 
59 void RegBankSelect::init(MachineFunction &MF) {
60   RBI = MF.getSubtarget().getRegBankInfo();
61   assert(RBI && "Cannot work without RegisterBankInfo");
62   MRI = &MF.getRegInfo();
63   TRI = MF.getSubtarget().getRegisterInfo();
64   if (OptMode != Mode::Fast) {
65     MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
66     MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
67   } else {
68     MBFI = nullptr;
69     MBPI = nullptr;
70   }
71   MIRBuilder.setMF(MF);
72 }
73 
74 void RegBankSelect::getAnalysisUsage(AnalysisUsage &AU) const {
75   if (OptMode != Mode::Fast) {
76     // We could preserve the information from these two analysis but
77     // the APIs do not allow to do so yet.
78     AU.addRequired<MachineBlockFrequencyInfo>();
79     AU.addRequired<MachineBranchProbabilityInfo>();
80   }
81   MachineFunctionPass::getAnalysisUsage(AU);
82 }
83 
84 bool RegBankSelect::assignmentMatch(
85     unsigned Reg, const RegisterBankInfo::ValueMapping &ValMapping,
86     bool &OnlyAssign) const {
87   // By default we assume we will have to repair something.
88   OnlyAssign = false;
89   // Each part of a break down needs to end up in a different register.
90   // In other word, Reg assignement does not match.
91   if (ValMapping.BreakDown.size() > 1)
92     return false;
93 
94   const RegisterBank *CurRegBank = RBI->getRegBank(Reg, *MRI, *TRI);
95   const RegisterBank *DesiredRegBrank = ValMapping.BreakDown[0].RegBank;
96   // Reg is free of assignment, a simple assignment will make the
97   // register bank to match.
98   OnlyAssign = CurRegBank == nullptr;
99   DEBUG(dbgs() << "Does assignment already match: ";
100         if (CurRegBank) dbgs() << *CurRegBank; else dbgs() << "none";
101         dbgs() << " against ";
102         assert(DesiredRegBrank && "The mapping must be valid");
103         dbgs() << *DesiredRegBrank << '\n';);
104   return CurRegBank == DesiredRegBrank;
105 }
106 
107 void RegBankSelect::repairReg(
108     MachineOperand &MO, const RegisterBankInfo::ValueMapping &ValMapping,
109     RegBankSelect::RepairingPlacement &RepairPt,
110     const iterator_range<SmallVectorImpl<unsigned>::const_iterator> &NewVRegs) {
111   assert(ValMapping.BreakDown.size() == 1 && "Not yet implemented");
112   // An empty range of new register means no repairing.
113   assert(NewVRegs.begin() != NewVRegs.end() && "We should not have to repair");
114 
115   // Assume we are repairing a use and thus, the original reg will be
116   // the source of the repairing.
117   unsigned Src = MO.getReg();
118   unsigned Dst = *NewVRegs.begin();
119 
120   // If we repair a definition, swap the source and destination for
121   // the repairing.
122   if (MO.isDef())
123     std::swap(Src, Dst);
124 
125   assert((RepairPt.getNumInsertPoints() == 1 ||
126           TargetRegisterInfo::isPhysicalRegister(Dst)) &&
127          "We are about to create several defs for Dst");
128 
129   // Build the instruction used to repair, then clone it at the right places.
130   MachineInstr *MI = MIRBuilder.buildCopy(Dst, Src);
131   MI->removeFromParent();
132   DEBUG(dbgs() << "Copy: " << PrintReg(Src) << " to: " << PrintReg(Dst)
133                << '\n');
134   // TODO:
135   // Check if MI is legal. if not, we need to legalize all the
136   // instructions we are going to insert.
137   std::unique_ptr<MachineInstr *[]> NewInstrs(
138       new MachineInstr *[RepairPt.getNumInsertPoints()]);
139   bool IsFirst = true;
140   unsigned Idx = 0;
141   for (const std::unique_ptr<InsertPoint> &InsertPt : RepairPt) {
142     MachineInstr *CurMI;
143     if (IsFirst)
144       CurMI = MI;
145     else
146       CurMI = MIRBuilder.getMF().CloneMachineInstr(MI);
147     InsertPt->insert(*CurMI);
148     NewInstrs[Idx++] = CurMI;
149     IsFirst = false;
150   }
151   // TODO:
152   // Legalize NewInstrs if need be.
153 }
154 
155 uint64_t RegBankSelect::getRepairCost(
156     const MachineOperand &MO,
157     const RegisterBankInfo::ValueMapping &ValMapping) const {
158   assert(MO.isReg() && "We should only repair register operand");
159   assert(!ValMapping.BreakDown.empty() && "Nothing to map??");
160 
161   bool IsSameNumOfValues = ValMapping.BreakDown.size() == 1;
162   const RegisterBank *CurRegBank = RBI->getRegBank(MO.getReg(), *MRI, *TRI);
163   // If MO does not have a register bank, we should have just been
164   // able to set one unless we have to break the value down.
165   assert((!IsSameNumOfValues || CurRegBank) && "We should not have to repair");
166   // Def: Val <- NewDefs
167   //     Same number of values: copy
168   //     Different number: Val = build_sequence Defs1, Defs2, ...
169   // Use: NewSources <- Val.
170   //     Same number of values: copy.
171   //     Different number: Src1, Src2, ... =
172   //           extract_value Val, Src1Begin, Src1Len, Src2Begin, Src2Len, ...
173   // We should remember that this value is available somewhere else to
174   // coalesce the value.
175 
176   if (IsSameNumOfValues) {
177     const RegisterBank *DesiredRegBrank = ValMapping.BreakDown[0].RegBank;
178     // If we repair a definition, swap the source and destination for
179     // the repairing.
180     if (MO.isDef())
181       std::swap(CurRegBank, DesiredRegBrank);
182     // TODO: It may be possible to actually avoid the copy.
183     // If we repair something where the source is defined by a copy
184     // and the source of that copy is on the right bank, we can reuse
185     // it for free.
186     // E.g.,
187     // RegToRepair<BankA> = copy AlternativeSrc<BankB>
188     // = op RegToRepair<BankA>
189     // We can simply propagate AlternativeSrc instead of copying RegToRepair
190     // into a new virtual register.
191     // We would also need to propagate this information in the
192     // repairing placement.
193     unsigned Cost =
194         RBI->copyCost(*DesiredRegBrank, *CurRegBank,
195                       RegisterBankInfo::getSizeInBits(MO.getReg(), *MRI, *TRI));
196     // TODO: use a dedicated constant for ImpossibleCost.
197     if (Cost != UINT_MAX)
198       return Cost;
199     assert(false && "Legalization not available yet");
200     // Return the legalization cost of that repairing.
201   }
202   assert(false && "Complex repairing not implemented yet");
203   return 1;
204 }
205 
206 RegisterBankInfo::InstructionMapping &RegBankSelect::findBestMapping(
207     MachineInstr &MI, RegisterBankInfo::InstructionMappings &PossibleMappings,
208     SmallVectorImpl<RepairingPlacement> &RepairPts) {
209 
210   RegisterBankInfo::InstructionMapping *BestMapping = nullptr;
211   MappingCost Cost = MappingCost::ImpossibleCost();
212   SmallVector<RepairingPlacement, 4> LocalRepairPts;
213   for (RegisterBankInfo::InstructionMapping &CurMapping : PossibleMappings) {
214     MappingCost CurCost = computeMapping(MI, CurMapping, LocalRepairPts, &Cost);
215     if (CurCost < Cost) {
216       Cost = CurCost;
217       BestMapping = &CurMapping;
218       RepairPts.clear();
219       for (RepairingPlacement &RepairPt : LocalRepairPts)
220         RepairPts.emplace_back(std::move(RepairPt));
221     }
222   }
223   assert(BestMapping && "No suitable mapping for instruction");
224   return *BestMapping;
225 }
226 
227 void RegBankSelect::tryAvoidingSplit(
228     RegBankSelect::RepairingPlacement &RepairPt, const MachineOperand &MO,
229     const RegisterBankInfo::ValueMapping &ValMapping) const {
230   const MachineInstr &MI = *MO.getParent();
231   assert(RepairPt.hasSplit() && "We should not have to adjust for split");
232   // Splitting should only occur for PHIs or between terminators,
233   // because we only do local repairing.
234   assert((MI.isPHI() || MI.isTerminator()) && "Why do we split?");
235 
236   assert(&MI.getOperand(RepairPt.getOpIdx()) == &MO &&
237          "Repairing placement does not match operand");
238 
239   // If we need splitting for phis, that means it is because we
240   // could not find an insertion point before the terminators of
241   // the predecessor block for this argument. In other words,
242   // the input value is defined by one of the terminators.
243   assert((!MI.isPHI() || !MO.isDef()) && "Need split for phi def?");
244 
245   // We split to repair the use of a phi or a terminator.
246   if (!MO.isDef()) {
247     if (MI.isTerminator()) {
248       assert(&MI != &(*MI.getParent()->getFirstTerminator()) &&
249              "Need to split for the first terminator?!");
250     } else {
251       // For the PHI case, the split may not be actually required.
252       // In the copy case, a phi is already a copy on the incoming edge,
253       // therefore there is no need to split.
254       if (ValMapping.BreakDown.size() == 1)
255         // This is a already a copy, there is nothing to do.
256         RepairPt.switchTo(RepairingPlacement::RepairingKind::Reassign);
257     }
258     return;
259   }
260 
261   // At this point, we need to repair a defintion of a terminator.
262 
263   // Technically we need to fix the def of MI on all outgoing
264   // edges of MI to keep the repairing local. In other words, we
265   // will create several definitions of the same register. This
266   // does not work for SSA unless that definition is a physical
267   // register.
268   // However, there are other cases where we can get away with
269   // that while still keeping the repairing local.
270   assert(MI.isTerminator() && MO.isDef() &&
271          "This code is for the def of a terminator");
272 
273   // Since we use RPO traversal, if we need to repair a definition
274   // this means this definition could be:
275   // 1. Used by PHIs (i.e., this VReg has been visited as part of the
276   //    uses of a phi.), or
277   // 2. Part of a target specific instruction (i.e., the target applied
278   //    some register class constraints when creating the instruction.)
279   // If the constraints come for #2, the target said that another mapping
280   // is supported so we may just drop them. Indeed, if we do not change
281   // the number of registers holding that value, the uses will get fixed
282   // when we get to them.
283   // Uses in PHIs may have already been proceeded though.
284   // If the constraints come for #1, then, those are weak constraints and
285   // no actual uses may rely on them. However, the problem remains mainly
286   // the same as for #2. If the value stays in one register, we could
287   // just switch the register bank of the definition, but we would need to
288   // account for a repairing cost for each phi we silently change.
289   //
290   // In any case, if the value needs to be broken down into several
291   // registers, the repairing is not local anymore as we need to patch
292   // every uses to rebuild the value in just one register.
293   //
294   // To summarize:
295   // - If the value is in a physical register, we can do the split and
296   //   fix locally.
297   // Otherwise if the value is in a virtual register:
298   // - If the value remains in one register, we do not have to split
299   //   just switching the register bank would do, but we need to account
300   //   in the repairing cost all the phi we changed.
301   // - If the value spans several registers, then we cannot do a local
302   //   repairing.
303 
304   // Check if this is a physical or virtual register.
305   unsigned Reg = MO.getReg();
306   if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
307     // We are going to split every outgoing edges.
308     // Check that this is possible.
309     // FIXME: The machine representation is currently broken
310     // since it also several terminators in one basic block.
311     // Because of that we would technically need a way to get
312     // the targets of just one terminator to know which edges
313     // we have to split.
314     // Assert that we do not hit the ill-formed representation.
315 
316     // If there are other terminators before that one, some of
317     // the outgoing edges may not be dominated by this definition.
318     assert(&MI == &(*MI.getParent()->getFirstTerminator()) &&
319            "Do not know which outgoing edges are relevant");
320     const MachineInstr *Next = MI.getNextNode();
321     assert((!Next || Next->isUnconditionalBranch()) &&
322            "Do not know where each terminator ends up");
323     if (Next)
324       // If the next terminator uses Reg, this means we have
325       // to split right after MI and thus we need a way to ask
326       // which outgoing edges are affected.
327       assert(!Next->readsRegister(Reg) && "Need to split between terminators");
328     // We will split all the edges and repair there.
329   } else {
330     // This is a virtual register defined by a terminator.
331     if (ValMapping.BreakDown.size() == 1) {
332       // There is nothing to repair, but we may actually lie on
333       // the repairing cost because of the PHIs already proceeded
334       // as already stated.
335       // Though the code will be correct.
336       assert(0 && "Repairing cost may not be accurate");
337     } else {
338       // We need to do non-local repairing. Basically, patch all
339       // the uses (i.e., phis) that we already proceeded.
340       // For now, just say this mapping is not possible.
341       RepairPt.switchTo(RepairingPlacement::RepairingKind::Impossible);
342     }
343   }
344 }
345 
346 RegBankSelect::MappingCost RegBankSelect::computeMapping(
347     MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping,
348     SmallVectorImpl<RepairingPlacement> &RepairPts,
349     const RegBankSelect::MappingCost *BestCost) {
350   assert((MBFI || !BestCost) && "Costs comparison require MBFI");
351 
352   // If mapped with InstrMapping, MI will have the recorded cost.
353   MappingCost Cost(MBFI ? MBFI->getBlockFreq(MI.getParent()) : 1);
354   bool Saturated = Cost.addLocalCost(InstrMapping.getCost());
355   assert(!Saturated && "Possible mapping saturated the cost");
356   DEBUG(dbgs() << "Evaluating mapping cost for: " << MI);
357   DEBUG(dbgs() << "With: " << InstrMapping << '\n');
358   RepairPts.clear();
359   if (BestCost && Cost > *BestCost)
360     return Cost;
361 
362   // Moreover, to realize this mapping, the register bank of each operand must
363   // match this mapping. In other words, we may need to locally reassign the
364   // register banks. Account for that repairing cost as well.
365   // In this context, local means in the surrounding of MI.
366   for (unsigned OpIdx = 0, EndOpIdx = MI.getNumOperands(); OpIdx != EndOpIdx;
367        ++OpIdx) {
368     const MachineOperand &MO = MI.getOperand(OpIdx);
369     if (!MO.isReg())
370       continue;
371     unsigned Reg = MO.getReg();
372     if (!Reg)
373       continue;
374     DEBUG(dbgs() << "Opd" << OpIdx);
375     const RegisterBankInfo::ValueMapping &ValMapping =
376         InstrMapping.getOperandMapping(OpIdx);
377     // If Reg is already properly mapped, this is free.
378     bool Assign;
379     if (assignmentMatch(Reg, ValMapping, Assign)) {
380       DEBUG(dbgs() << " is free (match).\n");
381       continue;
382     }
383     if (Assign) {
384       DEBUG(dbgs() << " is free (simple assignment).\n");
385       RepairPts.emplace_back(RepairingPlacement(MI, OpIdx, *TRI, *this,
386                                                 RepairingPlacement::Reassign));
387       continue;
388     }
389 
390     // Find the insertion point for the repairing code.
391     RepairPts.emplace_back(
392         RepairingPlacement(MI, OpIdx, *TRI, *this, RepairingPlacement::Insert));
393     RepairingPlacement &RepairPt = RepairPts.back();
394 
395     // If we need to split a basic block to materialize this insertion point,
396     // we may give a higher cost to this mapping.
397     // Nevertheless, we may get away with the split, so try that first.
398     if (RepairPt.hasSplit())
399       tryAvoidingSplit(RepairPt, MO, ValMapping);
400 
401     // Check that the materialization of the repairing is possible.
402     if (!RepairPt.canMaterialize())
403       return MappingCost::ImpossibleCost();
404 
405     // Account for the split cost and repair cost.
406     // Unless the cost is already saturated or we do not care about the cost.
407     if (!BestCost || Saturated)
408       continue;
409 
410     // To get accurate information we need MBFI and MBPI.
411     // Thus, if we end up here this information should be here.
412     assert(MBFI && MBPI && "Cost computation requires MBFI and MBPI");
413 
414     // FIXME: We will have to rework the repairing cost model.
415     // The repairing cost depends on the register bank that MO has.
416     // However, when we break down the value into different values,
417     // MO may not have a register bank while still needing repairing.
418     // For the fast mode, we don't compute the cost so that is fine,
419     // but still for the repairing code, we will have to make a choice.
420     // For the greedy mode, we should choose greedily what is the best
421     // choice based on the next use of MO.
422 
423     // Sums up the repairing cost of MO at each insertion point.
424     uint64_t RepairCost = getRepairCost(MO, ValMapping);
425     // Bias used for splitting: 5%.
426     const uint64_t PercentageForBias = 5;
427     uint64_t Bias = (RepairCost * PercentageForBias + 99) / 100;
428     // We should not need more than a couple of instructions to repair
429     // an assignment. In other words, the computation should not
430     // overflow because the repairing cost is free of basic block
431     // frequency.
432     assert(((RepairCost < RepairCost * PercentageForBias) &&
433             (RepairCost * PercentageForBias <
434              RepairCost * PercentageForBias + 99)) &&
435            "Repairing involves more than a billion of instructions?!");
436     for (const std::unique_ptr<InsertPoint> &InsertPt : RepairPt) {
437       assert(InsertPt->canMaterialize() && "We should not have made it here");
438       // We will applied some basic block frequency and those uses uint64_t.
439       if (!InsertPt->isSplit())
440         Saturated = Cost.addLocalCost(RepairCost);
441       else {
442         uint64_t CostForInsertPt = RepairCost;
443         // Again we shouldn't overflow here givent that
444         // CostForInsertPt is frequency free at this point.
445         assert(CostForInsertPt + Bias > CostForInsertPt &&
446                "Repairing + split bias overflows");
447         CostForInsertPt += Bias;
448         uint64_t PtCost = InsertPt->frequency(*this) * CostForInsertPt;
449         // Check if we just overflowed.
450         if ((Saturated = PtCost < CostForInsertPt))
451           Cost.saturate();
452         else
453           Saturated = Cost.addNonLocalCost(PtCost);
454       }
455 
456       // Stop looking into what it takes to repair, this is already
457       // too expensive.
458       if (BestCost && Cost > *BestCost)
459         return Cost;
460 
461       // No need to accumulate more cost information.
462       // We need to still gather the repairing information though.
463       if (Saturated)
464         break;
465     }
466   }
467   return Cost;
468 }
469 
470 void RegBankSelect::applyMapping(
471     MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping,
472     SmallVectorImpl<RegBankSelect::RepairingPlacement> &RepairPts) {
473   // OpdMapper will hold all the information needed for the rewritting.
474   RegisterBankInfo::OperandsMapper OpdMapper(MI, InstrMapping, *MRI);
475 
476   // First, place the repairing code.
477   for (RepairingPlacement &RepairPt : RepairPts) {
478     assert(RepairPt.canMaterialize() &&
479            RepairPt.getKind() != RepairingPlacement::Impossible &&
480            "This mapping is impossible");
481     assert(RepairPt.getKind() != RepairingPlacement::None &&
482            "This should not make its way in the list");
483     unsigned OpIdx = RepairPt.getOpIdx();
484     MachineOperand &MO = MI.getOperand(OpIdx);
485     const RegisterBankInfo::ValueMapping &ValMapping =
486         InstrMapping.getOperandMapping(OpIdx);
487     unsigned BreakDownSize = ValMapping.BreakDown.size();
488     (void)BreakDownSize;
489     unsigned Reg = MO.getReg();
490 
491     switch (RepairPt.getKind()) {
492     case RepairingPlacement::Reassign:
493       assert(BreakDownSize == 1 &&
494              "Reassignment should only be for simple mapping");
495       MRI->setRegBank(Reg, *ValMapping.BreakDown[0].RegBank);
496       break;
497     case RepairingPlacement::Insert:
498       OpdMapper.createVRegs(OpIdx);
499       repairReg(MO, ValMapping, RepairPt, OpdMapper.getVRegs(OpIdx));
500       break;
501     default:
502       llvm_unreachable("Other kind should not happen");
503     }
504   }
505   // Second, rewrite the instruction.
506   DEBUG(dbgs() << "Actual mapping of the operands: " << OpdMapper << '\n');
507   RBI->applyMapping(OpdMapper);
508 }
509 
510 void RegBankSelect::assignInstr(MachineInstr &MI) {
511   DEBUG(dbgs() << "Assign: " << MI);
512   // Remember the repairing placement for all the operands.
513   SmallVector<RepairingPlacement, 4> RepairPts;
514 
515   RegisterBankInfo::InstructionMapping BestMapping;
516   if (OptMode == RegBankSelect::Mode::Fast) {
517     BestMapping = RBI->getInstrMapping(MI);
518     MappingCost DefaultCost = computeMapping(MI, BestMapping, RepairPts);
519     (void)DefaultCost;
520     assert(DefaultCost != MappingCost::ImpossibleCost() &&
521            "Default mapping is not suited");
522   } else {
523     RegisterBankInfo::InstructionMappings PossibleMappings =
524         RBI->getInstrPossibleMappings(MI);
525     assert(!PossibleMappings.empty() &&
526            "Do not know how to map this instruction");
527     BestMapping = std::move(findBestMapping(MI, PossibleMappings, RepairPts));
528   }
529   // Make sure the mapping is valid for MI.
530   assert(BestMapping.verify(MI) && "Invalid instruction mapping");
531 
532   DEBUG(dbgs() << "Mapping: " << BestMapping << '\n');
533 
534   // After this call, MI may not be valid anymore.
535   // Do not use it.
536   applyMapping(MI, BestMapping, RepairPts);
537 }
538 
539 bool RegBankSelect::runOnMachineFunction(MachineFunction &MF) {
540   DEBUG(dbgs() << "Assign register banks for: " << MF.getName() << '\n');
541   const Function *F = MF.getFunction();
542   Mode SaveOptMode = OptMode;
543   if (F->hasFnAttribute(Attribute::OptimizeNone))
544     OptMode = Mode::Fast;
545   init(MF);
546 
547 #ifndef NDEBUG
548   // Check that our input is fully legal: we require the function to have the
549   // Legalized property, so it should be.
550   // FIXME: This should be in the MachineVerifier, but it can't use the
551   // MachineLegalizer as it's currently in the separate GlobalISel library.
552   if (const MachineLegalizer *MLI = MF.getSubtarget().getMachineLegalizer()) {
553     for (const MachineBasicBlock &MBB : MF) {
554       for (const MachineInstr &MI : MBB) {
555         if (isPreISelGenericOpcode(MI.getOpcode()) && !MLI->isLegal(MI)) {
556           std::string ErrStorage;
557           raw_string_ostream Err(ErrStorage);
558           Err << "Instruction is not legal: " << MI << '\n';
559           report_fatal_error(Err.str());
560         }
561       }
562     }
563   }
564 #endif
565 
566   // Walk the function and assign register banks to all operands.
567   // Use a RPOT to make sure all registers are assigned before we choose
568   // the best mapping of the current instruction.
569   ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
570   for (MachineBasicBlock *MBB : RPOT) {
571     // Set a sensible insertion point so that subsequent calls to
572     // MIRBuilder.
573     MIRBuilder.setMBB(*MBB);
574     for (MachineBasicBlock::iterator MII = MBB->begin(), End = MBB->end();
575          MII != End;) {
576       // MI might be invalidated by the assignment, so move the
577       // iterator before hand.
578       MachineInstr &MI = *MII++;
579 
580       // Ignore target-specific instructions: they should use proper regclasses.
581       if (isTargetSpecificOpcode(MI.getOpcode()))
582         continue;
583 
584       assignInstr(MI);
585     }
586   }
587   OptMode = SaveOptMode;
588   return false;
589 }
590 
591 //------------------------------------------------------------------------------
592 //                  Helper Classes Implementation
593 //------------------------------------------------------------------------------
594 RegBankSelect::RepairingPlacement::RepairingPlacement(
595     MachineInstr &MI, unsigned OpIdx, const TargetRegisterInfo &TRI, Pass &P,
596     RepairingPlacement::RepairingKind Kind)
597     // Default is, we are going to insert code to repair OpIdx.
598     : Kind(Kind),
599       OpIdx(OpIdx),
600       CanMaterialize(Kind != RepairingKind::Impossible),
601       HasSplit(false),
602       P(P) {
603   const MachineOperand &MO = MI.getOperand(OpIdx);
604   assert(MO.isReg() && "Trying to repair a non-reg operand");
605 
606   if (Kind != RepairingKind::Insert)
607     return;
608 
609   // Repairings for definitions happen after MI, uses happen before.
610   bool Before = !MO.isDef();
611 
612   // Check if we are done with MI.
613   if (!MI.isPHI() && !MI.isTerminator()) {
614     addInsertPoint(MI, Before);
615     // We are done with the initialization.
616     return;
617   }
618 
619   // Now, look for the special cases.
620   if (MI.isPHI()) {
621     // - PHI must be the first instructions:
622     //   * Before, we have to split the related incoming edge.
623     //   * After, move the insertion point past the last phi.
624     if (!Before) {
625       MachineBasicBlock::iterator It = MI.getParent()->getFirstNonPHI();
626       if (It != MI.getParent()->end())
627         addInsertPoint(*It, /*Before*/ true);
628       else
629         addInsertPoint(*(--It), /*Before*/ false);
630       return;
631     }
632     // We repair a use of a phi, we may need to split the related edge.
633     MachineBasicBlock &Pred = *MI.getOperand(OpIdx + 1).getMBB();
634     // Check if we can move the insertion point prior to the
635     // terminators of the predecessor.
636     unsigned Reg = MO.getReg();
637     MachineBasicBlock::iterator It = Pred.getLastNonDebugInstr();
638     for (auto Begin = Pred.begin(); It != Begin && It->isTerminator(); --It)
639       if (It->modifiesRegister(Reg, &TRI)) {
640         // We cannot hoist the repairing code in the predecessor.
641         // Split the edge.
642         addInsertPoint(Pred, *MI.getParent());
643         return;
644       }
645     // At this point, we can insert in Pred.
646 
647     // - If It is invalid, Pred is empty and we can insert in Pred
648     //   wherever we want.
649     // - If It is valid, It is the first non-terminator, insert after It.
650     if (It == Pred.end())
651       addInsertPoint(Pred, /*Beginning*/ false);
652     else
653       addInsertPoint(*It, /*Before*/ false);
654   } else {
655     // - Terminators must be the last instructions:
656     //   * Before, move the insert point before the first terminator.
657     //   * After, we have to split the outcoming edges.
658     unsigned Reg = MO.getReg();
659     if (Before) {
660       // Check whether Reg is defined by any terminator.
661       MachineBasicBlock::iterator It = MI;
662       for (auto Begin = MI.getParent()->begin();
663            --It != Begin && It->isTerminator();)
664         if (It->modifiesRegister(Reg, &TRI)) {
665           // Insert the repairing code right after the definition.
666           addInsertPoint(*It, /*Before*/ false);
667           return;
668         }
669       addInsertPoint(*It, /*Before*/ true);
670       return;
671     }
672     // Make sure Reg is not redefined by other terminators, otherwise
673     // we do not know how to split.
674     for (MachineBasicBlock::iterator It = MI, End = MI.getParent()->end();
675          ++It != End;)
676       // The machine verifier should reject this kind of code.
677       assert(It->modifiesRegister(Reg, &TRI) && "Do not know where to split");
678     // Split each outcoming edges.
679     MachineBasicBlock &Src = *MI.getParent();
680     for (auto &Succ : Src.successors())
681       addInsertPoint(Src, Succ);
682   }
683 }
684 
685 void RegBankSelect::RepairingPlacement::addInsertPoint(MachineInstr &MI,
686                                                        bool Before) {
687   addInsertPoint(*new InstrInsertPoint(MI, Before));
688 }
689 
690 void RegBankSelect::RepairingPlacement::addInsertPoint(MachineBasicBlock &MBB,
691                                                        bool Beginning) {
692   addInsertPoint(*new MBBInsertPoint(MBB, Beginning));
693 }
694 
695 void RegBankSelect::RepairingPlacement::addInsertPoint(MachineBasicBlock &Src,
696                                                        MachineBasicBlock &Dst) {
697   addInsertPoint(*new EdgeInsertPoint(Src, Dst, P));
698 }
699 
700 void RegBankSelect::RepairingPlacement::addInsertPoint(
701     RegBankSelect::InsertPoint &Point) {
702   CanMaterialize &= Point.canMaterialize();
703   HasSplit |= Point.isSplit();
704   InsertPoints.emplace_back(&Point);
705 }
706 
707 RegBankSelect::InstrInsertPoint::InstrInsertPoint(MachineInstr &Instr,
708                                                   bool Before)
709     : InsertPoint(), Instr(Instr), Before(Before) {
710   // Since we do not support splitting, we do not need to update
711   // liveness and such, so do not do anything with P.
712   assert((!Before || !Instr.isPHI()) &&
713          "Splitting before phis requires more points");
714   assert((!Before || !Instr.getNextNode() || !Instr.getNextNode()->isPHI()) &&
715          "Splitting between phis does not make sense");
716 }
717 
718 void RegBankSelect::InstrInsertPoint::materialize() {
719   if (isSplit()) {
720     // Slice and return the beginning of the new block.
721     // If we need to split between the terminators, we theoritically
722     // need to know where the first and second set of terminators end
723     // to update the successors properly.
724     // Now, in pratice, we should have a maximum of 2 branch
725     // instructions; one conditional and one unconditional. Therefore
726     // we know how to update the successor by looking at the target of
727     // the unconditional branch.
728     // If we end up splitting at some point, then, we should update
729     // the liveness information and such. I.e., we would need to
730     // access P here.
731     // The machine verifier should actually make sure such cases
732     // cannot happen.
733     llvm_unreachable("Not yet implemented");
734   }
735   // Otherwise the insertion point is just the current or next
736   // instruction depending on Before. I.e., there is nothing to do
737   // here.
738 }
739 
740 bool RegBankSelect::InstrInsertPoint::isSplit() const {
741   // If the insertion point is after a terminator, we need to split.
742   if (!Before)
743     return Instr.isTerminator();
744   // If we insert before an instruction that is after a terminator,
745   // we are still after a terminator.
746   return Instr.getPrevNode() && Instr.getPrevNode()->isTerminator();
747 }
748 
749 uint64_t RegBankSelect::InstrInsertPoint::frequency(const Pass &P) const {
750   // Even if we need to split, because we insert between terminators,
751   // this split has actually the same frequency as the instruction.
752   const MachineBlockFrequencyInfo *MBFI =
753       P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
754   if (!MBFI)
755     return 1;
756   return MBFI->getBlockFreq(Instr.getParent()).getFrequency();
757 }
758 
759 uint64_t RegBankSelect::MBBInsertPoint::frequency(const Pass &P) const {
760   const MachineBlockFrequencyInfo *MBFI =
761       P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
762   if (!MBFI)
763     return 1;
764   return MBFI->getBlockFreq(&MBB).getFrequency();
765 }
766 
767 void RegBankSelect::EdgeInsertPoint::materialize() {
768   // If we end up repairing twice at the same place before materializing the
769   // insertion point, we may think we have to split an edge twice.
770   // We should have a factory for the insert point such that identical points
771   // are the same instance.
772   assert(Src.isSuccessor(DstOrSplit) && DstOrSplit->isPredecessor(&Src) &&
773          "This point has already been split");
774   MachineBasicBlock *NewBB = Src.SplitCriticalEdge(DstOrSplit, P);
775   assert(NewBB && "Invalid call to materialize");
776   // We reuse the destination block to hold the information of the new block.
777   DstOrSplit = NewBB;
778 }
779 
780 uint64_t RegBankSelect::EdgeInsertPoint::frequency(const Pass &P) const {
781   const MachineBlockFrequencyInfo *MBFI =
782       P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
783   if (!MBFI)
784     return 1;
785   if (WasMaterialized)
786     return MBFI->getBlockFreq(DstOrSplit).getFrequency();
787 
788   const MachineBranchProbabilityInfo *MBPI =
789       P.getAnalysisIfAvailable<MachineBranchProbabilityInfo>();
790   if (!MBPI)
791     return 1;
792   // The basic block will be on the edge.
793   return (MBFI->getBlockFreq(&Src) * MBPI->getEdgeProbability(&Src, DstOrSplit))
794       .getFrequency();
795 }
796 
797 bool RegBankSelect::EdgeInsertPoint::canMaterialize() const {
798   // If this is not a critical edge, we should not have used this insert
799   // point. Indeed, either the successor or the predecessor should
800   // have do.
801   assert(Src.succ_size() > 1 && DstOrSplit->pred_size() > 1 &&
802          "Edge is not critical");
803   return Src.canSplitCriticalEdge(DstOrSplit);
804 }
805 
806 RegBankSelect::MappingCost::MappingCost(const BlockFrequency &LocalFreq)
807     : LocalCost(0), NonLocalCost(0), LocalFreq(LocalFreq.getFrequency()) {}
808 
809 bool RegBankSelect::MappingCost::addLocalCost(uint64_t Cost) {
810   // Check if this overflows.
811   if (LocalCost + Cost < LocalCost) {
812     saturate();
813     return true;
814   }
815   LocalCost += Cost;
816   return isSaturated();
817 }
818 
819 bool RegBankSelect::MappingCost::addNonLocalCost(uint64_t Cost) {
820   // Check if this overflows.
821   if (NonLocalCost + Cost < NonLocalCost) {
822     saturate();
823     return true;
824   }
825   NonLocalCost += Cost;
826   return isSaturated();
827 }
828 
829 bool RegBankSelect::MappingCost::isSaturated() const {
830   return LocalCost == UINT64_MAX - 1 && NonLocalCost == UINT64_MAX &&
831          LocalFreq == UINT64_MAX;
832 }
833 
834 void RegBankSelect::MappingCost::saturate() {
835   *this = ImpossibleCost();
836   --LocalCost;
837 }
838 
839 RegBankSelect::MappingCost RegBankSelect::MappingCost::ImpossibleCost() {
840   return MappingCost(UINT64_MAX, UINT64_MAX, UINT64_MAX);
841 }
842 
843 bool RegBankSelect::MappingCost::operator<(const MappingCost &Cost) const {
844   // Sort out the easy cases.
845   if (*this == Cost)
846     return false;
847   // If one is impossible to realize the other is cheaper unless it is
848   // impossible as well.
849   if ((*this == ImpossibleCost()) || (Cost == ImpossibleCost()))
850     return (*this == ImpossibleCost()) < (Cost == ImpossibleCost());
851   // If one is saturated the other is cheaper, unless it is saturated
852   // as well.
853   if (isSaturated() || Cost.isSaturated())
854     return isSaturated() < Cost.isSaturated();
855   // At this point we know both costs hold sensible values.
856 
857   // If both values have a different base frequency, there is no much
858   // we can do but to scale everything.
859   // However, if they have the same base frequency we can avoid making
860   // complicated computation.
861   uint64_t ThisLocalAdjust;
862   uint64_t OtherLocalAdjust;
863   if (LLVM_LIKELY(LocalFreq == Cost.LocalFreq)) {
864 
865     // At this point, we know the local costs are comparable.
866     // Do the case that do not involve potential overflow first.
867     if (NonLocalCost == Cost.NonLocalCost)
868       // Since the non-local costs do not discriminate on the result,
869       // just compare the local costs.
870       return LocalCost < Cost.LocalCost;
871 
872     // The base costs are comparable so we may only keep the relative
873     // value to increase our chances of avoiding overflows.
874     ThisLocalAdjust = 0;
875     OtherLocalAdjust = 0;
876     if (LocalCost < Cost.LocalCost)
877       OtherLocalAdjust = Cost.LocalCost - LocalCost;
878     else
879       ThisLocalAdjust = LocalCost - Cost.LocalCost;
880 
881   } else {
882     ThisLocalAdjust = LocalCost;
883     OtherLocalAdjust = Cost.LocalCost;
884   }
885 
886   // The non-local costs are comparable, just keep the relative value.
887   uint64_t ThisNonLocalAdjust = 0;
888   uint64_t OtherNonLocalAdjust = 0;
889   if (NonLocalCost < Cost.NonLocalCost)
890     OtherNonLocalAdjust = Cost.NonLocalCost - NonLocalCost;
891   else
892     ThisNonLocalAdjust = NonLocalCost - Cost.NonLocalCost;
893   // Scale everything to make them comparable.
894   uint64_t ThisScaledCost = ThisLocalAdjust * LocalFreq;
895   // Check for overflow on that operation.
896   bool ThisOverflows = ThisLocalAdjust && (ThisScaledCost < ThisLocalAdjust ||
897                                            ThisScaledCost < LocalFreq);
898   uint64_t OtherScaledCost = OtherLocalAdjust * Cost.LocalFreq;
899   // Check for overflow on the last operation.
900   bool OtherOverflows =
901       OtherLocalAdjust &&
902       (OtherScaledCost < OtherLocalAdjust || OtherScaledCost < Cost.LocalFreq);
903   // Add the non-local costs.
904   ThisOverflows |= ThisNonLocalAdjust &&
905                    ThisScaledCost + ThisNonLocalAdjust < ThisNonLocalAdjust;
906   ThisScaledCost += ThisNonLocalAdjust;
907   OtherOverflows |= OtherNonLocalAdjust &&
908                     OtherScaledCost + OtherNonLocalAdjust < OtherNonLocalAdjust;
909   OtherScaledCost += OtherNonLocalAdjust;
910   // If both overflows, we cannot compare without additional
911   // precision, e.g., APInt. Just give up on that case.
912   if (ThisOverflows && OtherOverflows)
913     return false;
914   // If one overflows but not the other, we can still compare.
915   if (ThisOverflows || OtherOverflows)
916     return ThisOverflows < OtherOverflows;
917   // Otherwise, just compare the values.
918   return ThisScaledCost < OtherScaledCost;
919 }
920 
921 bool RegBankSelect::MappingCost::operator==(const MappingCost &Cost) const {
922   return LocalCost == Cost.LocalCost && NonLocalCost == Cost.NonLocalCost &&
923          LocalFreq == Cost.LocalFreq;
924 }
925