blob: 9efe48adec710fd278de7868f12b047bedaaff32 [file] [log] [blame]
Quentin Colombet8e8e85c2016-04-05 19:06:01 +00001//===- 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"
Quentin Colombetcfd97b92016-05-20 00:35:26 +000014#include "llvm/ADT/PostOrderIterator.h"
Ahmed Bougacha24d0d4d2016-08-02 15:10:32 +000015#include "llvm/CodeGen/GlobalISel/MachineLegalizer.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000016#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
Quentin Colombet55650752016-05-20 00:49:10 +000017#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
18#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Quentin Colombeta5530122016-05-20 17:36:54 +000020#include "llvm/IR/Function.h"
Quentin Colombetcfd97b92016-05-20 00:35:26 +000021#include "llvm/Support/BlockFrequency.h"
Quentin Colombeta41272f2016-06-08 15:49:23 +000022#include "llvm/Support/CommandLine.h"
Quentin Colombete16f5612016-04-07 23:53:55 +000023#include "llvm/Support/Debug.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000024#include "llvm/Target/TargetSubtargetInfo.h"
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000025
26#define DEBUG_TYPE "regbankselect"
27
28using namespace llvm;
29
Quentin Colombeta41272f2016-06-08 15:49:23 +000030static 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
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000038char RegBankSelect::ID = 0;
Quentin Colombet25fcef72016-05-20 17:54:09 +000039INITIALIZE_PASS_BEGIN(RegBankSelect, "regbankselect",
40 "Assign register bank of generic virtual registers",
41 false, false);
42INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
43INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
44INITIALIZE_PASS_END(RegBankSelect, "regbankselect",
45 "Assign register bank of generic virtual registers", false,
Tim Northover884b47e2016-07-26 03:29:18 +000046 false)
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000047
Quentin Colombet46df7222016-05-20 16:55:35 +000048RegBankSelect::RegBankSelect(Mode RunningMode)
Quentin Colombet25fcef72016-05-20 17:54:09 +000049 : MachineFunctionPass(ID), RBI(nullptr), MRI(nullptr), TRI(nullptr),
50 MBFI(nullptr), MBPI(nullptr), OptMode(RunningMode) {
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000051 initializeRegBankSelectPass(*PassRegistry::getPassRegistry());
Quentin Colombeta41272f2016-06-08 15:49:23 +000052 if (RegBankSelectMode.getNumOccurrences() != 0) {
53 OptMode = RegBankSelectMode;
54 if (RegBankSelectMode != RunningMode)
55 DEBUG(dbgs() << "RegBankSelect mode overrided by command line\n");
56 }
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000057}
58
Quentin Colombet40ad5732016-04-07 18:19:27 +000059void RegBankSelect::init(MachineFunction &MF) {
60 RBI = MF.getSubtarget().getRegBankInfo();
61 assert(RBI && "Cannot work without RegisterBankInfo");
62 MRI = &MF.getRegInfo();
Quentin Colombetaac71a42016-04-07 21:32:23 +000063 TRI = MF.getSubtarget().getRegisterInfo();
Quentin Colombet25fcef72016-05-20 17:54:09 +000064 if (OptMode != Mode::Fast) {
65 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
66 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
67 } else {
68 MBFI = nullptr;
69 MBPI = nullptr;
70 }
Quentin Colombet40ad5732016-04-07 18:19:27 +000071 MIRBuilder.setMF(MF);
72}
73
Quentin Colombet25fcef72016-05-20 17:54:09 +000074void 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
Quentin Colombet40ad5732016-04-07 18:19:27 +000084bool RegBankSelect::assignmentMatch(
Quentin Colombet0d77da42016-05-20 00:42:57 +000085 unsigned Reg, const RegisterBankInfo::ValueMapping &ValMapping,
86 bool &OnlyAssign) const {
87 // By default we assume we will have to repair something.
88 OnlyAssign = false;
Quentin Colombet40ad5732016-04-07 18:19:27 +000089 // 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
Quentin Colombet6d6d6af2016-04-08 16:48:16 +000094 const RegisterBank *CurRegBank = RBI->getRegBank(Reg, *MRI, *TRI);
95 const RegisterBank *DesiredRegBrank = ValMapping.BreakDown[0].RegBank;
Quentin Colombet0d77da42016-05-20 00:42:57 +000096 // Reg is free of assignment, a simple assignment will make the
97 // register bank to match.
98 OnlyAssign = CurRegBank == nullptr;
Quentin Colombet6d6d6af2016-04-08 16:48:16 +000099 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;
Quentin Colombet40ad5732016-04-07 18:19:27 +0000105}
106
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000107void RegBankSelect::repairReg(
108 MachineOperand &MO, const RegisterBankInfo::ValueMapping &ValMapping,
109 RegBankSelect::RepairingPlacement &RepairPt,
Quentin Colombet06ef4e22016-06-08 16:24:55 +0000110 const iterator_range<SmallVectorImpl<unsigned>::const_iterator> &NewVRegs) {
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000111 assert(ValMapping.BreakDown.size() == 1 && "Not yet implemented");
Quentin Colombetf33e3652016-06-08 16:30:55 +0000112 // An empty range of new register means no repairing.
113 assert(NewVRegs.begin() != NewVRegs.end() && "We should not have to repair");
114
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000115 // 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();
Quentin Colombet904a2c72016-04-12 00:12:59 +0000119
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000120 // If we repair a definition, swap the source and destination for
121 // the repairing.
122 if (MO.isDef())
Quentin Colombet904a2c72016-04-12 00:12:59 +0000123 std::swap(Src, Dst);
Quentin Colombet904a2c72016-04-12 00:12:59 +0000124
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000125 assert((RepairPt.getNumInsertPoints() == 1 ||
126 TargetRegisterInfo::isPhysicalRegister(Dst)) &&
127 "We are about to create several defs for Dst");
Quentin Colombet904a2c72016-04-12 00:12:59 +0000128
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000129 // Build the instruction used to repair, then clone it at the right places.
Tim Northover756eca32016-07-26 16:45:30 +0000130 MachineInstr *MI = MIRBuilder.buildCopy(Dst, Src);
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000131 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.
Quentin Colombet40ad5732016-04-07 18:19:27 +0000153}
154
Quentin Colombetf2723a22016-05-21 01:43:25 +0000155uint64_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);
Quentin Colombetd6886bd2016-06-08 17:39:43 +0000182 // 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.
Quentin Colombetcfbdee22016-06-08 01:11:03 +0000193 unsigned Cost =
194 RBI->copyCost(*DesiredRegBrank, *CurRegBank,
195 RegisterBankInfo::getSizeInBits(MO.getReg(), *MRI, *TRI));
Quentin Colombetf2723a22016-05-21 01:43:25 +0000196 // 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
Quentin Colombet79fe1be2016-05-20 18:37:33 +0000206RegisterBankInfo::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
Quentin Colombetf75c2bf2016-05-20 16:36:12 +0000227void 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
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000346RegBankSelect::MappingCost RegBankSelect::computeMapping(
347 MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping,
Quentin Colombet6e80dbc2016-05-20 18:00:46 +0000348 SmallVectorImpl<RepairingPlacement> &RepairPts,
349 const RegBankSelect::MappingCost *BestCost) {
350 assert((MBFI || !BestCost) && "Costs comparison require MBFI");
Quentin Colombete16f5612016-04-07 23:53:55 +0000351
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000352 // If mapped with InstrMapping, MI will have the recorded cost.
Quentin Colombet25fcef72016-05-20 17:54:09 +0000353 MappingCost Cost(MBFI ? MBFI->getBlockFreq(MI.getParent()) : 1);
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000354 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();
Quentin Colombet6e80dbc2016-05-20 18:00:46 +0000359 if (BestCost && Cost > *BestCost)
360 return Cost;
361
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000362 // 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;
Quentin Colombet40ad5732016-04-07 18:19:27 +0000367 ++OpIdx) {
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000368 const MachineOperand &MO = MI.getOperand(OpIdx);
Quentin Colombet40ad5732016-04-07 18:19:27 +0000369 if (!MO.isReg())
370 continue;
371 unsigned Reg = MO.getReg();
372 if (!Reg)
373 continue;
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000374 DEBUG(dbgs() << "Opd" << OpIdx);
Quentin Colombet40ad5732016-04-07 18:19:27 +0000375 const RegisterBankInfo::ValueMapping &ValMapping =
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000376 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");
Quentin Colombet40ad5732016-04-07 18:19:27 +0000381 continue;
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000382 }
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;
Quentin Colombet40ad5732016-04-07 18:19:27 +0000388 }
Quentin Colombet904a2c72016-04-12 00:12:59 +0000389
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000390 // 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
Quentin Colombetf75c2bf2016-05-20 16:36:12 +0000395 // 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
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000401 // 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.
Quentin Colombet6e80dbc2016-05-20 18:00:46 +0000406 // Unless the cost is already saturated or we do not care about the cost.
407 if (!BestCost || Saturated)
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000408 continue;
409
Quentin Colombet6e80dbc2016-05-20 18:00:46 +0000410 // 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
Quentin Colombet6feaf8202016-06-08 15:40:32 +0000414 // 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
Quentin Colombetf2723a22016-05-21 01:43:25 +0000423 // Sums up the repairing cost of MO at each insertion point.
424 uint64_t RepairCost = getRepairCost(MO, ValMapping);
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000425 // 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 }
Quentin Colombet6e80dbc2016-05-20 18:00:46 +0000455
456 // Stop looking into what it takes to repair, this is already
457 // too expensive.
458 if (BestCost && Cost > *BestCost)
459 return Cost;
460
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000461 // No need to accumulate more cost information.
462 // We need to still gather the repairing information though.
463 if (Saturated)
464 break;
465 }
Quentin Colombet40ad5732016-04-07 18:19:27 +0000466 }
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000467 return Cost;
468}
469
470void RegBankSelect::applyMapping(
471 MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping,
472 SmallVectorImpl<RegBankSelect::RepairingPlacement> &RepairPts) {
Quentin Colombetf33e3652016-06-08 16:30:55 +0000473 // OpdMapper will hold all the information needed for the rewritting.
474 RegisterBankInfo::OperandsMapper OpdMapper(MI, InstrMapping, *MRI);
475
Quentin Colombetec5c93d2016-06-08 16:45:04 +0000476 // First, place the repairing code.
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000477 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();
Quentin Colombet86be3742016-06-08 17:39:47 +0000488 (void)BreakDownSize;
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000489 unsigned Reg = MO.getReg();
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000490
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:
Quentin Colombetf33e3652016-06-08 16:30:55 +0000498 OpdMapper.createVRegs(OpIdx);
499 repairReg(MO, ValMapping, RepairPt, OpdMapper.getVRegs(OpIdx));
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000500 break;
501 default:
502 llvm_unreachable("Other kind should not happen");
503 }
504 }
505 // Second, rewrite the instruction.
Quentin Colombet33406452016-06-08 21:55:30 +0000506 DEBUG(dbgs() << "Actual mapping of the operands: " << OpdMapper << '\n');
Quentin Colombetec5c93d2016-06-08 16:45:04 +0000507 RBI->applyMapping(OpdMapper);
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000508}
509
510void RegBankSelect::assignInstr(MachineInstr &MI) {
511 DEBUG(dbgs() << "Assign: " << MI);
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000512 // Remember the repairing placement for all the operands.
513 SmallVector<RepairingPlacement, 4> RepairPts;
514
Quentin Colombet79fe1be2016-05-20 18:37:33 +0000515 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 }
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000529 // Make sure the mapping is valid for MI.
Quentin Colombet79fe1be2016-05-20 18:37:33 +0000530 assert(BestMapping.verify(MI) && "Invalid instruction mapping");
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000531
Quentin Colombet79fe1be2016-05-20 18:37:33 +0000532 DEBUG(dbgs() << "Mapping: " << BestMapping << '\n');
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000533
Quentin Colombet9400bfb2016-06-08 21:55:29 +0000534 // After this call, MI may not be valid anymore.
535 // Do not use it.
Quentin Colombet79fe1be2016-05-20 18:37:33 +0000536 applyMapping(MI, BestMapping, RepairPts);
Quentin Colombet40ad5732016-04-07 18:19:27 +0000537}
538
Quentin Colombet8e8e85c2016-04-05 19:06:01 +0000539bool RegBankSelect::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet60495242016-08-27 00:18:24 +0000540 // If the ISel pipeline failed, do not bother running that pass.
541 if (MF.getProperties().hasProperty(
542 MachineFunctionProperties::Property::FailedISel))
543 return false;
544
Quentin Colombete16f5612016-04-07 23:53:55 +0000545 DEBUG(dbgs() << "Assign register banks for: " << MF.getName() << '\n');
Quentin Colombeta5530122016-05-20 17:36:54 +0000546 const Function *F = MF.getFunction();
547 Mode SaveOptMode = OptMode;
548 if (F->hasFnAttribute(Attribute::OptimizeNone))
549 OptMode = Mode::Fast;
Quentin Colombet40ad5732016-04-07 18:19:27 +0000550 init(MF);
Ahmed Bougacha24d0d4d2016-08-02 15:10:32 +0000551
552#ifndef NDEBUG
553 // Check that our input is fully legal: we require the function to have the
554 // Legalized property, so it should be.
555 // FIXME: This should be in the MachineVerifier, but it can't use the
556 // MachineLegalizer as it's currently in the separate GlobalISel library.
557 if (const MachineLegalizer *MLI = MF.getSubtarget().getMachineLegalizer()) {
558 for (const MachineBasicBlock &MBB : MF) {
559 for (const MachineInstr &MI : MBB) {
560 if (isPreISelGenericOpcode(MI.getOpcode()) && !MLI->isLegal(MI)) {
561 std::string ErrStorage;
562 raw_string_ostream Err(ErrStorage);
563 Err << "Instruction is not legal: " << MI << '\n';
564 report_fatal_error(Err.str());
565 }
566 }
567 }
568 }
569#endif
570
Quentin Colombet40ad5732016-04-07 18:19:27 +0000571 // Walk the function and assign register banks to all operands.
Quentin Colombetab8c21f2016-04-08 17:19:10 +0000572 // Use a RPOT to make sure all registers are assigned before we choose
573 // the best mapping of the current instruction.
574 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000575 for (MachineBasicBlock *MBB : RPOT) {
576 // Set a sensible insertion point so that subsequent calls to
577 // MIRBuilder.
578 MIRBuilder.setMBB(*MBB);
Quentin Colombetec5c93d2016-06-08 16:45:04 +0000579 for (MachineBasicBlock::iterator MII = MBB->begin(), End = MBB->end();
580 MII != End;) {
581 // MI might be invalidated by the assignment, so move the
582 // iterator before hand.
Ahmed Bougacha45eb3b92016-08-02 11:41:16 +0000583 MachineInstr &MI = *MII++;
584
585 // Ignore target-specific instructions: they should use proper regclasses.
586 if (isTargetSpecificOpcode(MI.getOpcode()))
587 continue;
588
589 assignInstr(MI);
Quentin Colombetec5c93d2016-06-08 16:45:04 +0000590 }
Quentin Colombetd84d00b2016-05-20 00:55:51 +0000591 }
Quentin Colombeta5530122016-05-20 17:36:54 +0000592 OptMode = SaveOptMode;
Quentin Colombet8e8e85c2016-04-05 19:06:01 +0000593 return false;
594}
Quentin Colombetcfd97b92016-05-20 00:35:26 +0000595
596//------------------------------------------------------------------------------
Quentin Colombet55650752016-05-20 00:49:10 +0000597// Helper Classes Implementation
Quentin Colombetcfd97b92016-05-20 00:35:26 +0000598//------------------------------------------------------------------------------
Quentin Colombet55650752016-05-20 00:49:10 +0000599RegBankSelect::RepairingPlacement::RepairingPlacement(
600 MachineInstr &MI, unsigned OpIdx, const TargetRegisterInfo &TRI, Pass &P,
601 RepairingPlacement::RepairingKind Kind)
602 // Default is, we are going to insert code to repair OpIdx.
603 : Kind(Kind),
604 OpIdx(OpIdx),
605 CanMaterialize(Kind != RepairingKind::Impossible),
606 HasSplit(false),
607 P(P) {
608 const MachineOperand &MO = MI.getOperand(OpIdx);
609 assert(MO.isReg() && "Trying to repair a non-reg operand");
610
611 if (Kind != RepairingKind::Insert)
612 return;
613
614 // Repairings for definitions happen after MI, uses happen before.
615 bool Before = !MO.isDef();
616
617 // Check if we are done with MI.
618 if (!MI.isPHI() && !MI.isTerminator()) {
619 addInsertPoint(MI, Before);
620 // We are done with the initialization.
621 return;
622 }
623
624 // Now, look for the special cases.
625 if (MI.isPHI()) {
626 // - PHI must be the first instructions:
627 // * Before, we have to split the related incoming edge.
628 // * After, move the insertion point past the last phi.
629 if (!Before) {
630 MachineBasicBlock::iterator It = MI.getParent()->getFirstNonPHI();
631 if (It != MI.getParent()->end())
632 addInsertPoint(*It, /*Before*/ true);
633 else
634 addInsertPoint(*(--It), /*Before*/ false);
635 return;
636 }
637 // We repair a use of a phi, we may need to split the related edge.
638 MachineBasicBlock &Pred = *MI.getOperand(OpIdx + 1).getMBB();
639 // Check if we can move the insertion point prior to the
640 // terminators of the predecessor.
641 unsigned Reg = MO.getReg();
642 MachineBasicBlock::iterator It = Pred.getLastNonDebugInstr();
643 for (auto Begin = Pred.begin(); It != Begin && It->isTerminator(); --It)
644 if (It->modifiesRegister(Reg, &TRI)) {
645 // We cannot hoist the repairing code in the predecessor.
646 // Split the edge.
647 addInsertPoint(Pred, *MI.getParent());
648 return;
649 }
650 // At this point, we can insert in Pred.
651
652 // - If It is invalid, Pred is empty and we can insert in Pred
653 // wherever we want.
654 // - If It is valid, It is the first non-terminator, insert after It.
655 if (It == Pred.end())
656 addInsertPoint(Pred, /*Beginning*/ false);
657 else
658 addInsertPoint(*It, /*Before*/ false);
659 } else {
660 // - Terminators must be the last instructions:
661 // * Before, move the insert point before the first terminator.
662 // * After, we have to split the outcoming edges.
663 unsigned Reg = MO.getReg();
664 if (Before) {
665 // Check whether Reg is defined by any terminator.
666 MachineBasicBlock::iterator It = MI;
667 for (auto Begin = MI.getParent()->begin();
668 --It != Begin && It->isTerminator();)
669 if (It->modifiesRegister(Reg, &TRI)) {
670 // Insert the repairing code right after the definition.
671 addInsertPoint(*It, /*Before*/ false);
672 return;
673 }
674 addInsertPoint(*It, /*Before*/ true);
675 return;
676 }
677 // Make sure Reg is not redefined by other terminators, otherwise
678 // we do not know how to split.
679 for (MachineBasicBlock::iterator It = MI, End = MI.getParent()->end();
680 ++It != End;)
681 // The machine verifier should reject this kind of code.
682 assert(It->modifiesRegister(Reg, &TRI) && "Do not know where to split");
683 // Split each outcoming edges.
684 MachineBasicBlock &Src = *MI.getParent();
685 for (auto &Succ : Src.successors())
686 addInsertPoint(Src, Succ);
687 }
688}
689
690void RegBankSelect::RepairingPlacement::addInsertPoint(MachineInstr &MI,
691 bool Before) {
692 addInsertPoint(*new InstrInsertPoint(MI, Before));
693}
694
695void RegBankSelect::RepairingPlacement::addInsertPoint(MachineBasicBlock &MBB,
696 bool Beginning) {
697 addInsertPoint(*new MBBInsertPoint(MBB, Beginning));
698}
699
700void RegBankSelect::RepairingPlacement::addInsertPoint(MachineBasicBlock &Src,
701 MachineBasicBlock &Dst) {
702 addInsertPoint(*new EdgeInsertPoint(Src, Dst, P));
703}
704
705void RegBankSelect::RepairingPlacement::addInsertPoint(
706 RegBankSelect::InsertPoint &Point) {
707 CanMaterialize &= Point.canMaterialize();
708 HasSplit |= Point.isSplit();
709 InsertPoints.emplace_back(&Point);
710}
711
712RegBankSelect::InstrInsertPoint::InstrInsertPoint(MachineInstr &Instr,
713 bool Before)
714 : InsertPoint(), Instr(Instr), Before(Before) {
715 // Since we do not support splitting, we do not need to update
716 // liveness and such, so do not do anything with P.
717 assert((!Before || !Instr.isPHI()) &&
718 "Splitting before phis requires more points");
719 assert((!Before || !Instr.getNextNode() || !Instr.getNextNode()->isPHI()) &&
720 "Splitting between phis does not make sense");
721}
722
723void RegBankSelect::InstrInsertPoint::materialize() {
724 if (isSplit()) {
725 // Slice and return the beginning of the new block.
726 // If we need to split between the terminators, we theoritically
727 // need to know where the first and second set of terminators end
728 // to update the successors properly.
729 // Now, in pratice, we should have a maximum of 2 branch
730 // instructions; one conditional and one unconditional. Therefore
731 // we know how to update the successor by looking at the target of
732 // the unconditional branch.
733 // If we end up splitting at some point, then, we should update
734 // the liveness information and such. I.e., we would need to
735 // access P here.
736 // The machine verifier should actually make sure such cases
737 // cannot happen.
738 llvm_unreachable("Not yet implemented");
739 }
740 // Otherwise the insertion point is just the current or next
741 // instruction depending on Before. I.e., there is nothing to do
742 // here.
743}
744
745bool RegBankSelect::InstrInsertPoint::isSplit() const {
746 // If the insertion point is after a terminator, we need to split.
747 if (!Before)
748 return Instr.isTerminator();
749 // If we insert before an instruction that is after a terminator,
750 // we are still after a terminator.
751 return Instr.getPrevNode() && Instr.getPrevNode()->isTerminator();
752}
753
754uint64_t RegBankSelect::InstrInsertPoint::frequency(const Pass &P) const {
755 // Even if we need to split, because we insert between terminators,
756 // this split has actually the same frequency as the instruction.
757 const MachineBlockFrequencyInfo *MBFI =
758 P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
759 if (!MBFI)
760 return 1;
761 return MBFI->getBlockFreq(Instr.getParent()).getFrequency();
762}
763
764uint64_t RegBankSelect::MBBInsertPoint::frequency(const Pass &P) const {
765 const MachineBlockFrequencyInfo *MBFI =
766 P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
767 if (!MBFI)
768 return 1;
769 return MBFI->getBlockFreq(&MBB).getFrequency();
770}
771
772void RegBankSelect::EdgeInsertPoint::materialize() {
773 // If we end up repairing twice at the same place before materializing the
774 // insertion point, we may think we have to split an edge twice.
775 // We should have a factory for the insert point such that identical points
776 // are the same instance.
777 assert(Src.isSuccessor(DstOrSplit) && DstOrSplit->isPredecessor(&Src) &&
778 "This point has already been split");
779 MachineBasicBlock *NewBB = Src.SplitCriticalEdge(DstOrSplit, P);
780 assert(NewBB && "Invalid call to materialize");
781 // We reuse the destination block to hold the information of the new block.
782 DstOrSplit = NewBB;
783}
784
785uint64_t RegBankSelect::EdgeInsertPoint::frequency(const Pass &P) const {
786 const MachineBlockFrequencyInfo *MBFI =
787 P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
788 if (!MBFI)
789 return 1;
790 if (WasMaterialized)
791 return MBFI->getBlockFreq(DstOrSplit).getFrequency();
792
793 const MachineBranchProbabilityInfo *MBPI =
794 P.getAnalysisIfAvailable<MachineBranchProbabilityInfo>();
795 if (!MBPI)
796 return 1;
797 // The basic block will be on the edge.
798 return (MBFI->getBlockFreq(&Src) * MBPI->getEdgeProbability(&Src, DstOrSplit))
799 .getFrequency();
800}
801
802bool RegBankSelect::EdgeInsertPoint::canMaterialize() const {
803 // If this is not a critical edge, we should not have used this insert
804 // point. Indeed, either the successor or the predecessor should
805 // have do.
806 assert(Src.succ_size() > 1 && DstOrSplit->pred_size() > 1 &&
807 "Edge is not critical");
808 return Src.canSplitCriticalEdge(DstOrSplit);
809}
810
Quentin Colombetcfd97b92016-05-20 00:35:26 +0000811RegBankSelect::MappingCost::MappingCost(const BlockFrequency &LocalFreq)
812 : LocalCost(0), NonLocalCost(0), LocalFreq(LocalFreq.getFrequency()) {}
813
814bool RegBankSelect::MappingCost::addLocalCost(uint64_t Cost) {
815 // Check if this overflows.
816 if (LocalCost + Cost < LocalCost) {
817 saturate();
818 return true;
819 }
820 LocalCost += Cost;
821 return isSaturated();
822}
823
824bool RegBankSelect::MappingCost::addNonLocalCost(uint64_t Cost) {
825 // Check if this overflows.
826 if (NonLocalCost + Cost < NonLocalCost) {
827 saturate();
828 return true;
829 }
830 NonLocalCost += Cost;
831 return isSaturated();
832}
833
834bool RegBankSelect::MappingCost::isSaturated() const {
835 return LocalCost == UINT64_MAX - 1 && NonLocalCost == UINT64_MAX &&
836 LocalFreq == UINT64_MAX;
837}
838
839void RegBankSelect::MappingCost::saturate() {
840 *this = ImpossibleCost();
841 --LocalCost;
842}
843
844RegBankSelect::MappingCost RegBankSelect::MappingCost::ImpossibleCost() {
845 return MappingCost(UINT64_MAX, UINT64_MAX, UINT64_MAX);
846}
847
848bool RegBankSelect::MappingCost::operator<(const MappingCost &Cost) const {
849 // Sort out the easy cases.
850 if (*this == Cost)
851 return false;
852 // If one is impossible to realize the other is cheaper unless it is
853 // impossible as well.
854 if ((*this == ImpossibleCost()) || (Cost == ImpossibleCost()))
855 return (*this == ImpossibleCost()) < (Cost == ImpossibleCost());
856 // If one is saturated the other is cheaper, unless it is saturated
857 // as well.
858 if (isSaturated() || Cost.isSaturated())
859 return isSaturated() < Cost.isSaturated();
860 // At this point we know both costs hold sensible values.
861
862 // If both values have a different base frequency, there is no much
863 // we can do but to scale everything.
864 // However, if they have the same base frequency we can avoid making
865 // complicated computation.
866 uint64_t ThisLocalAdjust;
867 uint64_t OtherLocalAdjust;
868 if (LLVM_LIKELY(LocalFreq == Cost.LocalFreq)) {
869
870 // At this point, we know the local costs are comparable.
871 // Do the case that do not involve potential overflow first.
872 if (NonLocalCost == Cost.NonLocalCost)
873 // Since the non-local costs do not discriminate on the result,
874 // just compare the local costs.
875 return LocalCost < Cost.LocalCost;
876
877 // The base costs are comparable so we may only keep the relative
878 // value to increase our chances of avoiding overflows.
879 ThisLocalAdjust = 0;
880 OtherLocalAdjust = 0;
881 if (LocalCost < Cost.LocalCost)
882 OtherLocalAdjust = Cost.LocalCost - LocalCost;
883 else
884 ThisLocalAdjust = LocalCost - Cost.LocalCost;
885
886 } else {
887 ThisLocalAdjust = LocalCost;
888 OtherLocalAdjust = Cost.LocalCost;
889 }
890
891 // The non-local costs are comparable, just keep the relative value.
892 uint64_t ThisNonLocalAdjust = 0;
893 uint64_t OtherNonLocalAdjust = 0;
894 if (NonLocalCost < Cost.NonLocalCost)
895 OtherNonLocalAdjust = Cost.NonLocalCost - NonLocalCost;
896 else
897 ThisNonLocalAdjust = NonLocalCost - Cost.NonLocalCost;
898 // Scale everything to make them comparable.
899 uint64_t ThisScaledCost = ThisLocalAdjust * LocalFreq;
900 // Check for overflow on that operation.
901 bool ThisOverflows = ThisLocalAdjust && (ThisScaledCost < ThisLocalAdjust ||
902 ThisScaledCost < LocalFreq);
903 uint64_t OtherScaledCost = OtherLocalAdjust * Cost.LocalFreq;
904 // Check for overflow on the last operation.
905 bool OtherOverflows =
906 OtherLocalAdjust &&
907 (OtherScaledCost < OtherLocalAdjust || OtherScaledCost < Cost.LocalFreq);
908 // Add the non-local costs.
909 ThisOverflows |= ThisNonLocalAdjust &&
910 ThisScaledCost + ThisNonLocalAdjust < ThisNonLocalAdjust;
911 ThisScaledCost += ThisNonLocalAdjust;
912 OtherOverflows |= OtherNonLocalAdjust &&
913 OtherScaledCost + OtherNonLocalAdjust < OtherNonLocalAdjust;
914 OtherScaledCost += OtherNonLocalAdjust;
915 // If both overflows, we cannot compare without additional
916 // precision, e.g., APInt. Just give up on that case.
917 if (ThisOverflows && OtherOverflows)
918 return false;
919 // If one overflows but not the other, we can still compare.
920 if (ThisOverflows || OtherOverflows)
921 return ThisOverflows < OtherOverflows;
922 // Otherwise, just compare the values.
923 return ThisScaledCost < OtherScaledCost;
924}
925
926bool RegBankSelect::MappingCost::operator==(const MappingCost &Cost) const {
927 return LocalCost == Cost.LocalCost && NonLocalCost == Cost.NonLocalCost &&
928 LocalFreq == Cost.LocalFreq;
929}