LLVM 23.0.0git
RegBankSelect.cpp
Go to the documentation of this file.
1//==- llvm/CodeGen/GlobalISel/RegBankSelect.cpp - RegBankSelect --*- 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 RegBankSelect class.
10//===----------------------------------------------------------------------===//
11
14#include "llvm/ADT/STLExtras.h"
32#include "llvm/Config/llvm-config.h"
33#include "llvm/IR/Function.h"
35#include "llvm/Pass.h"
39#include "llvm/Support/Debug.h"
43#include <algorithm>
44#include <cassert>
45#include <cstdint>
46#include <limits>
47#include <memory>
48#include <optional>
49#include <utility>
50
51#define DEBUG_TYPE "regbankselect"
52
53using namespace llvm;
54
55/// Cost value representing an impossible or invalid repairing.
56/// This matches the value returned by RegisterBankInfo::copyCost() and
57/// RegisterBankInfo::getBreakDownCost() when the cost cannot be computed.
58static constexpr unsigned ImpossibleRepairCost =
59 std::numeric_limits<unsigned>::max();
60
62 cl::desc("Mode of the RegBankSelect pass"), cl::Hidden, cl::Optional,
64 "Run the Fast mode (default mapping)"),
65 clEnumValN(RegBankSelect::Mode::Greedy, "regbankselect-greedy",
66 "Use the Greedy mode (best local mapping)")));
67
68char RegBankSelect::ID = 0;
69
71 "Assign register bank of generic virtual registers",
72 false, false);
77 "Assign register bank of generic virtual registers", false,
78 false)
79
81 : MachineFunctionPass(ID), OptMode(RunningMode) {
82 if (RegBankSelectMode.getNumOccurrences() != 0) {
83 OptMode = RegBankSelectMode;
84 if (RegBankSelectMode != RunningMode)
85 LLVM_DEBUG(dbgs() << "RegBankSelect mode overrided by command line\n");
86 }
87}
88
91 assert(RBI && "Cannot work without RegisterBankInfo");
92 MRI = &MF.getRegInfo();
94 if (OptMode != Mode::Fast) {
97 } else {
98 MBFI = nullptr;
99 MBPI = nullptr;
100 }
101 MIRBuilder.setMF(MF);
102 MORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
103}
104
106 if (OptMode != Mode::Fast) {
107 // We could preserve the information from these two analysis but
108 // the APIs do not allow to do so yet.
111 }
115}
116
118 Register Reg, const RegisterBankInfo::ValueMapping &ValMapping,
119 bool &OnlyAssign) const {
120 // By default we assume we will have to repair something.
121 OnlyAssign = false;
122 // Each part of a break down needs to end up in a different register.
123 // In other word, Reg assignment does not match.
124 if (ValMapping.NumBreakDowns != 1)
125 return false;
126
127 const RegisterBank *CurRegBank = RBI->getRegBank(Reg, *MRI, *TRI);
128 const RegisterBank *DesiredRegBank = ValMapping.BreakDown[0].RegBank;
129 // Reg is free of assignment, a simple assignment will make the
130 // register bank to match.
131 OnlyAssign = CurRegBank == nullptr;
132 LLVM_DEBUG(dbgs() << "Does assignment already match: ";
133 if (CurRegBank) dbgs() << *CurRegBank; else dbgs() << "none";
134 dbgs() << " against ";
135 assert(DesiredRegBank && "The mapping must be valid");
136 dbgs() << *DesiredRegBank << '\n';);
137 return CurRegBank == DesiredRegBank;
138}
139
141 MachineOperand &MO, const RegisterBankInfo::ValueMapping &ValMapping,
144
145 assert(ValMapping.NumBreakDowns == (unsigned)size(NewVRegs) &&
146 "need new vreg for each breakdown");
147
148 // An empty range of new register means no repairing.
149 assert(!NewVRegs.empty() && "We should not have to repair");
150
152 if (ValMapping.NumBreakDowns == 1) {
153 // Assume we are repairing a use and thus, the original reg will be
154 // the source of the repairing.
155 Register Src = MO.getReg();
156 Register Dst = *NewVRegs.begin();
157
158 // If we repair a definition, swap the source and destination for
159 // the repairing.
160 if (MO.isDef())
161 std::swap(Src, Dst);
162
163 assert((RepairPt.getNumInsertPoints() == 1 || Dst.isPhysical()) &&
164 "We are about to create several defs for Dst");
165
166 // Build the instruction used to repair, then clone it at the right
167 // places. Avoiding buildCopy bypasses the check that Src and Dst have the
168 // same types because the type is a placeholder when this function is called.
169 MI = MIRBuilder.buildInstrNoInsert(TargetOpcode::COPY)
170 .addDef(Dst)
171 .addUse(Src);
172 LLVM_DEBUG(dbgs() << "Copy: " << printReg(Src) << ':'
173 << printRegClassOrBank(Src, *MRI, TRI)
174 << " to: " << printReg(Dst) << ':'
175 << printRegClassOrBank(Dst, *MRI, TRI) << '\n');
176 } else {
177 // TODO: Support with G_IMPLICIT_DEF + G_INSERT sequence or G_EXTRACT
178 // sequence.
179 assert(ValMapping.partsAllUniform() && "irregular breakdowns not supported");
180
181 LLT RegTy = MRI->getType(MO.getReg());
182 if (MO.isDef()) {
183 unsigned MergeOp;
184 if (RegTy.isVector()) {
185 if (ValMapping.NumBreakDowns == RegTy.getNumElements())
186 MergeOp = TargetOpcode::G_BUILD_VECTOR;
187 else {
188 assert(
189 (ValMapping.BreakDown[0].Length * ValMapping.NumBreakDowns ==
190 RegTy.getSizeInBits()) &&
191 (ValMapping.BreakDown[0].Length % RegTy.getScalarSizeInBits() ==
192 0) &&
193 "don't understand this value breakdown");
194
195 MergeOp = TargetOpcode::G_CONCAT_VECTORS;
196 }
197 } else
198 MergeOp = TargetOpcode::G_MERGE_VALUES;
199
200 auto MergeBuilder =
201 MIRBuilder.buildInstrNoInsert(MergeOp)
202 .addDef(MO.getReg());
203
204 for (Register SrcReg : NewVRegs)
205 MergeBuilder.addUse(SrcReg);
206
207 MI = MergeBuilder;
208 } else {
209 MachineInstrBuilder UnMergeBuilder =
210 MIRBuilder.buildInstrNoInsert(TargetOpcode::G_UNMERGE_VALUES);
211 for (Register DefReg : NewVRegs)
212 UnMergeBuilder.addDef(DefReg);
213
214 UnMergeBuilder.addUse(MO.getReg());
215 MI = UnMergeBuilder;
216 }
217 }
218
219 if (RepairPt.getNumInsertPoints() != 1)
220 report_fatal_error("need testcase to support multiple insertion points");
221
222 // TODO:
223 // Check if MI is legal. if not, we need to legalize all the
224 // instructions we are going to insert.
225 std::unique_ptr<MachineInstr *[]> NewInstrs(
226 new MachineInstr *[RepairPt.getNumInsertPoints()]);
227 bool IsFirst = true;
228 unsigned Idx = 0;
229 for (const std::unique_ptr<InsertPoint> &InsertPt : RepairPt) {
230 MachineInstr *CurMI;
231 if (IsFirst)
232 CurMI = MI;
233 else
234 CurMI = MIRBuilder.getMF().CloneMachineInstr(MI);
235 InsertPt->insert(*CurMI);
236 NewInstrs[Idx++] = CurMI;
237 IsFirst = false;
238 }
239 // TODO:
240 // Legalize NewInstrs if need be.
241 return true;
242}
243
245 const MachineOperand &MO,
246 const RegisterBankInfo::ValueMapping &ValMapping) const {
247 assert(MO.isReg() && "We should only repair register operand");
248 assert(ValMapping.NumBreakDowns && "Nothing to map??");
249
250 bool IsSameNumOfValues = ValMapping.NumBreakDowns == 1;
251 const RegisterBank *CurRegBank = RBI->getRegBank(MO.getReg(), *MRI, *TRI);
252 // If MO does not have a register bank, we should have just been
253 // able to set one unless we have to break the value down.
254 assert(CurRegBank || MO.isDef());
255
256 // Def: Val <- NewDefs
257 // Same number of values: copy
258 // Different number: Val = build_sequence Defs1, Defs2, ...
259 // Use: NewSources <- Val.
260 // Same number of values: copy.
261 // Different number: Src1, Src2, ... =
262 // extract_value Val, Src1Begin, Src1Len, Src2Begin, Src2Len, ...
263 // We should remember that this value is available somewhere else to
264 // coalesce the value.
265
266 if (ValMapping.NumBreakDowns != 1)
267 return RBI->getBreakDownCost(ValMapping, CurRegBank);
268
269 if (IsSameNumOfValues) {
270 const RegisterBank *DesiredRegBank = ValMapping.BreakDown[0].RegBank;
271 // If we repair a definition, swap the source and destination for
272 // the repairing.
273 if (MO.isDef())
274 std::swap(CurRegBank, DesiredRegBank);
275 // TODO: It may be possible to actually avoid the copy.
276 // If we repair something where the source is defined by a copy
277 // and the source of that copy is on the right bank, we can reuse
278 // it for free.
279 // E.g.,
280 // RegToRepair<BankA> = copy AlternativeSrc<BankB>
281 // = op RegToRepair<BankA>
282 // We can simply propagate AlternativeSrc instead of copying RegToRepair
283 // into a new virtual register.
284 // We would also need to propagate this information in the
285 // repairing placement.
286 unsigned Cost = RBI->copyCost(*DesiredRegBank, *CurRegBank,
287 RBI->getSizeInBits(MO.getReg(), *MRI, *TRI));
289 return Cost;
290 // Return the legalization cost of that repairing.
291 }
293}
294
298 assert(!PossibleMappings.empty() &&
299 "Do not know how to map this instruction");
300
301 const RegisterBankInfo::InstructionMapping *BestMapping = nullptr;
304 for (const RegisterBankInfo::InstructionMapping *CurMapping :
305 PossibleMappings) {
306 MappingCost CurCost =
307 computeMapping(MI, *CurMapping, LocalRepairPts, &Cost);
308 if (CurCost < Cost) {
309 LLVM_DEBUG(dbgs() << "New best: " << CurCost << '\n');
310 Cost = CurCost;
311 BestMapping = CurMapping;
312 RepairPts.clear();
313 for (RepairingPlacement &RepairPt : LocalRepairPts)
314 RepairPts.emplace_back(std::move(RepairPt));
315 }
316 }
317 if (!BestMapping && MI.getMF()->getTarget().Options.GlobalISelAbort !=
319 // If none of the mapping worked that means they are all impossible.
320 // Thus, pick the first one and set an impossible repairing point.
321 // It will trigger the failed isel mode.
322 BestMapping = *PossibleMappings.begin();
323 RepairPts.emplace_back(
325 } else
326 assert(BestMapping && "No suitable mapping for instruction");
327 return *BestMapping;
328}
329
332 const RegisterBankInfo::ValueMapping &ValMapping) const {
333 const MachineInstr &MI = *MO.getParent();
334 assert(RepairPt.hasSplit() && "We should not have to adjust for split");
335 // Splitting should only occur for PHIs or between terminators,
336 // because we only do local repairing.
337 assert((MI.isPHI() || MI.isTerminator()) && "Why do we split?");
338
339 assert(&MI.getOperand(RepairPt.getOpIdx()) == &MO &&
340 "Repairing placement does not match operand");
341
342 // If we need splitting for phis, that means it is because we
343 // could not find an insertion point before the terminators of
344 // the predecessor block for this argument. In other words,
345 // the input value is defined by one of the terminators.
346 assert((!MI.isPHI() || !MO.isDef()) && "Need split for phi def?");
347
348 // We split to repair the use of a phi or a terminator.
349 if (!MO.isDef()) {
350 if (MI.isTerminator()) {
351 assert(&MI != &(*MI.getParent()->getFirstTerminator()) &&
352 "Need to split for the first terminator?!");
353 } else {
354 // For the PHI case, the split may not be actually required.
355 // In the copy case, a phi is already a copy on the incoming edge,
356 // therefore there is no need to split.
357 if (ValMapping.NumBreakDowns == 1)
358 // This is a already a copy, there is nothing to do.
360 }
361 return;
362 }
363
364 // At this point, we need to repair a defintion of a terminator.
365
366 // Technically we need to fix the def of MI on all outgoing
367 // edges of MI to keep the repairing local. In other words, we
368 // will create several definitions of the same register. This
369 // does not work for SSA unless that definition is a physical
370 // register.
371 // However, there are other cases where we can get away with
372 // that while still keeping the repairing local.
373 assert(MI.isTerminator() && MO.isDef() &&
374 "This code is for the def of a terminator");
375
376 // Since we use RPO traversal, if we need to repair a definition
377 // this means this definition could be:
378 // 1. Used by PHIs (i.e., this VReg has been visited as part of the
379 // uses of a phi.), or
380 // 2. Part of a target specific instruction (i.e., the target applied
381 // some register class constraints when creating the instruction.)
382 // If the constraints come for #2, the target said that another mapping
383 // is supported so we may just drop them. Indeed, if we do not change
384 // the number of registers holding that value, the uses will get fixed
385 // when we get to them.
386 // Uses in PHIs may have already been proceeded though.
387 // If the constraints come for #1, then, those are weak constraints and
388 // no actual uses may rely on them. However, the problem remains mainly
389 // the same as for #2. If the value stays in one register, we could
390 // just switch the register bank of the definition, but we would need to
391 // account for a repairing cost for each phi we silently change.
392 //
393 // In any case, if the value needs to be broken down into several
394 // registers, the repairing is not local anymore as we need to patch
395 // every uses to rebuild the value in just one register.
396 //
397 // To summarize:
398 // - If the value is in a physical register, we can do the split and
399 // fix locally.
400 // Otherwise if the value is in a virtual register:
401 // - If the value remains in one register, we do not have to split
402 // just switching the register bank would do, but we need to account
403 // in the repairing cost all the phi we changed.
404 // - If the value spans several registers, then we cannot do a local
405 // repairing.
406
407 // Check if this is a physical or virtual register.
408 Register Reg = MO.getReg();
409 if (Reg.isPhysical()) {
410 // We are going to split every outgoing edges.
411 // Check that this is possible.
412 // FIXME: The machine representation is currently broken
413 // since it also several terminators in one basic block.
414 // Because of that we would technically need a way to get
415 // the targets of just one terminator to know which edges
416 // we have to split.
417 // Assert that we do not hit the ill-formed representation.
418
419 // If there are other terminators before that one, some of
420 // the outgoing edges may not be dominated by this definition.
421 assert(&MI == &(*MI.getParent()->getFirstTerminator()) &&
422 "Do not know which outgoing edges are relevant");
423 const MachineInstr *Next = MI.getNextNode();
424 assert((!Next || Next->isUnconditionalBranch()) &&
425 "Do not know where each terminator ends up");
426 if (Next)
427 // If the next terminator uses Reg, this means we have
428 // to split right after MI and thus we need a way to ask
429 // which outgoing edges are affected.
430 assert(!Next->readsRegister(Reg, /*TRI=*/nullptr) &&
431 "Need to split between terminators");
432 // We will split all the edges and repair there.
433 } else {
434 // This is a virtual register defined by a terminator.
435 if (ValMapping.NumBreakDowns == 1) {
436 // There is nothing to repair, but we may actually lie on
437 // the repairing cost because of the PHIs already proceeded
438 // as already stated.
439 // Though the code will be correct.
440 assert(false && "Repairing cost may not be accurate");
441 } else {
442 // We need to do non-local repairing. Basically, patch all
443 // the uses (i.e., phis) that we already proceeded.
444 // For now, just say this mapping is not possible.
446 }
447 }
448}
449
453 const RegBankSelect::MappingCost *BestCost) {
454 assert((MBFI || !BestCost) && "Costs comparison require MBFI");
455
456 if (!InstrMapping.isValid())
458
459 // If mapped with InstrMapping, MI will have the recorded cost.
460 MappingCost Cost(MBFI ? MBFI->getBlockFreq(MI.getParent())
461 : BlockFrequency(1));
462 bool Saturated = Cost.addLocalCost(InstrMapping.getCost());
463 assert(!Saturated && "Possible mapping saturated the cost");
464 LLVM_DEBUG(dbgs() << "Evaluating mapping cost for: " << MI);
465 LLVM_DEBUG(dbgs() << "With: " << InstrMapping << '\n');
466 RepairPts.clear();
467 if (BestCost && Cost > *BestCost) {
468 LLVM_DEBUG(dbgs() << "Mapping is too expensive from the start\n");
469 return Cost;
470 }
471 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
472
473 // Moreover, to realize this mapping, the register bank of each operand must
474 // match this mapping. In other words, we may need to locally reassign the
475 // register banks. Account for that repairing cost as well.
476 // In this context, local means in the surrounding of MI.
477 for (unsigned OpIdx = 0, EndOpIdx = InstrMapping.getNumOperands();
478 OpIdx != EndOpIdx; ++OpIdx) {
479 const MachineOperand &MO = MI.getOperand(OpIdx);
480 if (!MO.isReg())
481 continue;
482 Register Reg = MO.getReg();
483 if (!Reg)
484 continue;
485 LLT Ty = MRI.getType(Reg);
486 if (!Ty.isValid())
487 continue;
488
489 LLVM_DEBUG(dbgs() << "Opd" << OpIdx << '\n');
490 const RegisterBankInfo::ValueMapping &ValMapping =
491 InstrMapping.getOperandMapping(OpIdx);
492 // If Reg is already properly mapped, this is free.
493 bool Assign;
494 if (assignmentMatch(Reg, ValMapping, Assign)) {
495 LLVM_DEBUG(dbgs() << "=> is free (match).\n");
496 continue;
497 }
498 if (Assign) {
499 LLVM_DEBUG(dbgs() << "=> is free (simple assignment).\n");
500 RepairPts.emplace_back(RepairingPlacement(MI, OpIdx, *TRI, *this,
502 continue;
503 }
504
505 // Find the insertion point for the repairing code.
506 RepairPts.emplace_back(
508 RepairingPlacement &RepairPt = RepairPts.back();
509
510 // If we need to split a basic block to materialize this insertion point,
511 // we may give a higher cost to this mapping.
512 // Nevertheless, we may get away with the split, so try that first.
513 if (RepairPt.hasSplit())
514 tryAvoidingSplit(RepairPt, MO, ValMapping);
515
516 // Check that the materialization of the repairing is possible.
517 if (!RepairPt.canMaterialize()) {
518 LLVM_DEBUG(dbgs() << "Mapping involves impossible repairing\n");
520 }
521
522 // Account for the split cost and repair cost.
523 // Unless the cost is already saturated or we do not care about the cost.
524 if (!BestCost || Saturated)
525 continue;
526
527 // To get accurate information we need MBFI and MBPI.
528 // Thus, if we end up here this information should be here.
529 assert(MBFI && MBPI && "Cost computation requires MBFI and MBPI");
530
531 // FIXME: We will have to rework the repairing cost model.
532 // The repairing cost depends on the register bank that MO has.
533 // However, when we break down the value into different values,
534 // MO may not have a register bank while still needing repairing.
535 // For the fast mode, we don't compute the cost so that is fine,
536 // but still for the repairing code, we will have to make a choice.
537 // For the greedy mode, we should choose greedily what is the best
538 // choice based on the next use of MO.
539
540 // Sums up the repairing cost of MO at each insertion point.
541 uint64_t RepairCost = getRepairCost(MO, ValMapping);
542
543 // This is an impossible to repair cost.
544 if (RepairCost == ImpossibleRepairCost)
546
547 // Bias used for splitting: 5%.
548 const uint64_t PercentageForBias = 5;
549 uint64_t Bias = (RepairCost * PercentageForBias + 99) / 100;
550 // We should not need more than a couple of instructions to repair
551 // an assignment. In other words, the computation should not
552 // overflow because the repairing cost is free of basic block
553 // frequency.
554 assert(((RepairCost < RepairCost * PercentageForBias) &&
555 (RepairCost * PercentageForBias <
556 RepairCost * PercentageForBias + 99)) &&
557 "Repairing involves more than a billion of instructions?!");
558 for (const std::unique_ptr<InsertPoint> &InsertPt : RepairPt) {
559 assert(InsertPt->canMaterialize() && "We should not have made it here");
560 // We will applied some basic block frequency and those uses uint64_t.
561 if (!InsertPt->isSplit())
562 Saturated = Cost.addLocalCost(RepairCost);
563 else {
564 uint64_t CostForInsertPt = RepairCost;
565 // Again we shouldn't overflow here givent that
566 // CostForInsertPt is frequency free at this point.
567 assert(CostForInsertPt + Bias > CostForInsertPt &&
568 "Repairing + split bias overflows");
569 CostForInsertPt += Bias;
570 uint64_t PtCost = InsertPt->frequency(*this) * CostForInsertPt;
571 // Check if we just overflowed.
572 if ((Saturated = PtCost < CostForInsertPt))
573 Cost.saturate();
574 else
575 Saturated = Cost.addNonLocalCost(PtCost);
576 }
577
578 // Stop looking into what it takes to repair, this is already
579 // too expensive.
580 if (BestCost && Cost > *BestCost) {
581 LLVM_DEBUG(dbgs() << "Mapping is too expensive, stop processing\n");
582 return Cost;
583 }
584
585 // No need to accumulate more cost information.
586 // We need to still gather the repairing information though.
587 if (Saturated)
588 break;
589 }
590 }
591 LLVM_DEBUG(dbgs() << "Total cost is: " << Cost << "\n");
592 return Cost;
593}
594
598 // OpdMapper will hold all the information needed for the rewriting.
599 std::optional<RegisterBankInfo::OperandsMapper> OpdMapper;
600
601 // First, place the repairing code.
602 for (RepairingPlacement &RepairPt : RepairPts) {
603 if (!RepairPt.canMaterialize() ||
604 RepairPt.getKind() == RepairingPlacement::Impossible)
605 return false;
606 assert(RepairPt.getKind() != RepairingPlacement::None &&
607 "This should not make its way in the list");
608 unsigned OpIdx = RepairPt.getOpIdx();
609 MachineOperand &MO = MI.getOperand(OpIdx);
610 const RegisterBankInfo::ValueMapping &ValMapping =
611 InstrMapping.getOperandMapping(OpIdx);
612 Register Reg = MO.getReg();
613
614 switch (RepairPt.getKind()) {
616 assert(ValMapping.NumBreakDowns == 1 &&
617 "Reassignment should only be for simple mapping");
618 MRI->setRegBank(Reg, *ValMapping.BreakDown[0].RegBank);
619 break;
621 // Don't insert additional instruction for debug instruction.
622 if (MI.isDebugInstr())
623 break;
624 if (!OpdMapper)
625 OpdMapper.emplace(MI, InstrMapping, *MRI);
626 OpdMapper->createVRegs(OpIdx);
627 if (!repairReg(MO, ValMapping, RepairPt, OpdMapper->getVRegs(OpIdx)))
628 return false;
629 break;
630 default:
631 llvm_unreachable("Other kind should not happen");
632 }
633 }
634
635 // Default mappings only need rewriting when repairs create new operands.
636 if (!OpdMapper && InstrMapping.getID() == RegisterBankInfo::DefaultMappingID)
637 return true;
638
639 if (!OpdMapper)
640 OpdMapper.emplace(MI, InstrMapping, *MRI);
641 // Second, rewrite the instruction.
642 LLVM_DEBUG(dbgs() << "Actual mapping of the operands: " << *OpdMapper
643 << '\n');
644 RBI->applyMapping(MIRBuilder, *OpdMapper);
645
646 return true;
647}
648
650 LLVM_DEBUG(dbgs() << "Assign: " << MI);
651
652 unsigned Opc = MI.getOpcode();
654 assert((Opc == TargetOpcode::G_ASSERT_ZEXT ||
655 Opc == TargetOpcode::G_ASSERT_SEXT ||
656 Opc == TargetOpcode::G_ASSERT_ALIGN) &&
657 "Unexpected hint opcode!");
658 // The only correct mapping for these is to always use the source register
659 // bank.
660 const RegisterBank *RB =
661 RBI->getRegBank(MI.getOperand(1).getReg(), *MRI, *TRI);
662 // We can assume every instruction above this one has a selected register
663 // bank.
664 assert(RB && "Expected source register to have a register bank?");
665 LLVM_DEBUG(dbgs() << "... Hint always uses source's register bank.\n");
666 MRI->setRegBank(MI.getOperand(0).getReg(), *RB);
667 return true;
668 }
669
670 // Remember the repairing placement for all the operands.
672
673 const RegisterBankInfo::InstructionMapping *BestMapping;
675 BestMapping = &RBI->getInstrMapping(MI);
676 MappingCost DefaultCost = computeMapping(MI, *BestMapping, RepairPts);
677 (void)DefaultCost;
678 if (DefaultCost == MappingCost::ImpossibleCost())
679 return false;
680 } else {
682 RBI->getInstrPossibleMappings(MI);
683 if (PossibleMappings.empty())
684 return false;
685 BestMapping = &findBestMapping(MI, PossibleMappings, RepairPts);
686 }
687 // Make sure the mapping is valid for MI.
688 assert(BestMapping->verify(MI) && "Invalid instruction mapping");
689
690 LLVM_DEBUG(dbgs() << "Best Mapping: " << *BestMapping << '\n');
691
692 // After this call, MI may not be valid anymore.
693 // Do not use it.
694 return applyMapping(MI, *BestMapping, RepairPts);
695}
696
698 // Walk the function and assign register banks to all operands.
699 // Use a RPOT to make sure all registers are assigned before we choose
700 // the best mapping of the current instruction.
702 for (MachineBasicBlock *MBB : RPOT) {
703 // Set a sensible insertion point so that subsequent calls to
704 // MIRBuilder.
705 MIRBuilder.setMBB(*MBB);
707 make_pointer_range(reverse(MBB->instrs())));
708
709 while (!WorkList.empty()) {
710 MachineInstr &MI = *WorkList.pop_back_val();
711
712 // Ignore target-specific post-isel instructions: they should use proper
713 // regclasses.
714 if (isTargetSpecificOpcode(MI.getOpcode()) && !MI.isPreISelOpcode())
715 continue;
716
717 // Ignore inline asm instructions: they should use physical
718 // registers/regclasses
719 if (MI.isInlineAsm())
720 continue;
721
722 // Ignore IMPLICIT_DEF which must have a regclass.
723 if (MI.isImplicitDef())
724 continue;
725
726 if (!assignInstr(MI)) {
727 reportGISelFailure(MF, *MORE, "gisel-regbankselect",
728 "unable to map instruction", MI);
729 return false;
730 }
731 }
732 }
733
734 return true;
735}
736
738#ifndef NDEBUG
740 if (const MachineInstr *MI = machineFunctionIsIllegal(MF)) {
741 reportGISelFailure(MF, *MORE, "gisel-regbankselect",
742 "instruction is not legal", *MI);
743 return false;
744 }
745 }
746#endif
747 return true;
748}
749
751 // If the ISel pipeline failed, do not bother running that pass.
752 if (MF.getProperties().hasFailedISel())
753 return false;
754
755 LLVM_DEBUG(dbgs() << "Assign register banks for: " << MF.getName() << '\n');
756 const Function &F = MF.getFunction();
757 Mode SaveOptMode = OptMode;
758 if (F.hasOptNone())
760 init(MF);
761
762#ifndef NDEBUG
763 if (!checkFunctionIsLegal(MF))
764 return false;
765#endif
766
768
769 OptMode = SaveOptMode;
770 return false;
771}
772
773//------------------------------------------------------------------------------
774// Helper Classes Implementation
775//------------------------------------------------------------------------------
777 MachineInstr &MI, unsigned OpIdx, const TargetRegisterInfo &TRI, Pass &P,
779 // Default is, we are going to insert code to repair OpIdx.
780 : Kind(Kind), OpIdx(OpIdx),
781 CanMaterialize(Kind != RepairingKind::Impossible), P(P) {
782 const MachineOperand &MO = MI.getOperand(OpIdx);
783 assert(MO.isReg() && "Trying to repair a non-reg operand");
784
785 if (Kind != RepairingKind::Insert)
786 return;
787
788 // Repairings for definitions happen after MI, uses happen before.
789 bool Before = !MO.isDef();
790
791 // Check if we are done with MI.
792 if (!MI.isPHI() && !MI.isTerminator()) {
793 addInsertPoint(MI, Before);
794 // We are done with the initialization.
795 return;
796 }
797
798 // Now, look for the special cases.
799 if (MI.isPHI()) {
800 // - PHI must be the first instructions:
801 // * Before, we have to split the related incoming edge.
802 // * After, move the insertion point past the last phi.
803 if (!Before) {
804 MachineBasicBlock::iterator It = MI.getParent()->getFirstNonPHI();
805 if (It != MI.getParent()->end())
806 addInsertPoint(*It, /*Before*/ true);
807 else
808 addInsertPoint(*(--It), /*Before*/ false);
809 return;
810 }
811 // We repair a use of a phi, we may need to split the related edge.
812 MachineBasicBlock &Pred = *MI.getOperand(OpIdx + 1).getMBB();
813 // Check if we can move the insertion point prior to the
814 // terminators of the predecessor.
815 Register Reg = MO.getReg();
816 MachineBasicBlock::iterator It = Pred.getLastNonDebugInstr();
817 for (auto Begin = Pred.begin(); It != Begin && It->isTerminator(); --It)
818 if (It->modifiesRegister(Reg, &TRI)) {
819 // We cannot hoist the repairing code in the predecessor.
820 // Split the edge.
821 addInsertPoint(Pred, *MI.getParent());
822 return;
823 }
824 // At this point, we can insert in Pred.
825
826 // - If It is invalid, Pred is empty and we can insert in Pred
827 // wherever we want.
828 // - If It is valid, It is the first non-terminator, insert after It.
829 if (It == Pred.end())
830 addInsertPoint(Pred, /*Beginning*/ false);
831 else
832 addInsertPoint(*It, /*Before*/ false);
833 } else {
834 // - Terminators must be the last instructions:
835 // * Before, move the insert point before the first terminator.
836 // * After, we have to split the outcoming edges.
837 if (Before) {
838 // Check whether Reg is defined by any terminator.
840 auto REnd = MI.getParent()->rend();
841
842 for (; It != REnd && It->isTerminator(); ++It) {
843 assert(!It->modifiesRegister(MO.getReg(), &TRI) &&
844 "copy insertion in middle of terminators not handled");
845 }
846
847 if (It == REnd) {
848 addInsertPoint(*MI.getParent()->begin(), true);
849 return;
850 }
851
852 // We are sure to be right before the first terminator.
853 addInsertPoint(*It, /*Before*/ false);
854 return;
855 }
856 // Make sure Reg is not redefined by other terminators, otherwise
857 // we do not know how to split.
858 for (MachineBasicBlock::iterator It = MI, End = MI.getParent()->end();
859 ++It != End;)
860 // The machine verifier should reject this kind of code.
861 assert(It->modifiesRegister(MO.getReg(), &TRI) &&
862 "Do not know where to split");
863 // Split each outcoming edges.
864 MachineBasicBlock &Src = *MI.getParent();
865 for (auto &Succ : Src.successors())
866 addInsertPoint(Src, Succ);
867 }
868}
869
874
879
884
887 CanMaterialize &= Point.canMaterialize();
888 HasSplit |= Point.isSplit();
889 InsertPoints.emplace_back(&Point);
890}
891
893 bool Before)
894 : Instr(Instr), Before(Before) {
895 // Since we do not support splitting, we do not need to update
896 // liveness and such, so do not do anything with P.
897 assert((!Before || !Instr.isPHI()) &&
898 "Splitting before phis requires more points");
899 assert((!Before || !Instr.getNextNode() || !Instr.getNextNode()->isPHI()) &&
900 "Splitting between phis does not make sense");
901}
902
904 if (isSplit()) {
905 // Slice and return the beginning of the new block.
906 // If we need to split between the terminators, we theoritically
907 // need to know where the first and second set of terminators end
908 // to update the successors properly.
909 // Now, in pratice, we should have a maximum of 2 branch
910 // instructions; one conditional and one unconditional. Therefore
911 // we know how to update the successor by looking at the target of
912 // the unconditional branch.
913 // If we end up splitting at some point, then, we should update
914 // the liveness information and such. I.e., we would need to
915 // access P here.
916 // The machine verifier should actually make sure such cases
917 // cannot happen.
918 llvm_unreachable("Not yet implemented");
919 }
920 // Otherwise the insertion point is just the current or next
921 // instruction depending on Before. I.e., there is nothing to do
922 // here.
923}
924
926 // If the insertion point is after a terminator, we need to split.
927 if (!Before)
928 return Instr.isTerminator();
929 // If we insert before an instruction that is after a terminator,
930 // we are still after a terminator.
931 return Instr.getPrevNode() && Instr.getPrevNode()->isTerminator();
932}
933
935 // Even if we need to split, because we insert between terminators,
936 // this split has actually the same frequency as the instruction.
937 const auto *MBFIWrapper =
938 P.getAnalysisIfAvailable<MachineBlockFrequencyInfoWrapperPass>();
939 if (!MBFIWrapper)
940 return 1;
941 return MBFIWrapper->getMBFI().getBlockFreq(Instr.getParent()).getFrequency();
942}
943
945 const auto *MBFIWrapper =
946 P.getAnalysisIfAvailable<MachineBlockFrequencyInfoWrapperPass>();
947 if (!MBFIWrapper)
948 return 1;
950}
951
953 // If we end up repairing twice at the same place before materializing the
954 // insertion point, we may think we have to split an edge twice.
955 // We should have a factory for the insert point such that identical points
956 // are the same instance.
957 assert(Src.isSuccessor(DstOrSplit) && DstOrSplit->isPredecessor(&Src) &&
958 "This point has already been split");
959 MachineBasicBlock *NewBB = Src.SplitCriticalEdge(DstOrSplit, P);
960 assert(NewBB && "Invalid call to materialize");
961 // We reuse the destination block to hold the information of the new block.
962 DstOrSplit = NewBB;
963}
964
966 const auto *MBFIWrapper =
967 P.getAnalysisIfAvailable<MachineBlockFrequencyInfoWrapperPass>();
968 if (!MBFIWrapper)
969 return 1;
970 const auto *MBFI = &MBFIWrapper->getMBFI();
971 if (WasMaterialized)
972 return MBFI->getBlockFreq(DstOrSplit).getFrequency();
973
974 auto *MBPIWrapper =
975 P.getAnalysisIfAvailable<MachineBranchProbabilityInfoWrapperPass>();
977 MBPIWrapper ? &MBPIWrapper->getMBPI() : nullptr;
978 if (!MBPI)
979 return 1;
980 // The basic block will be on the edge.
981 return (MBFI->getBlockFreq(&Src) * MBPI->getEdgeProbability(&Src, DstOrSplit))
982 .getFrequency();
983}
984
986 // If this is not a critical edge, we should not have used this insert
987 // point. Indeed, either the successor or the predecessor should
988 // have do.
989 assert(Src.succ_size() > 1 && DstOrSplit->pred_size() > 1 &&
990 "Edge is not critical");
991 return Src.canSplitCriticalEdge(DstOrSplit);
992}
993
994RegBankSelect::MappingCost::MappingCost(BlockFrequency LocalFreq)
995 : LocalFreq(LocalFreq.getFrequency()) {}
996
998 // Check if this overflows.
999 if (LocalCost + Cost < LocalCost) {
1000 saturate();
1001 return true;
1002 }
1003 LocalCost += Cost;
1004 return isSaturated();
1005}
1006
1008 // Check if this overflows.
1009 if (NonLocalCost + Cost < NonLocalCost) {
1010 saturate();
1011 return true;
1012 }
1013 NonLocalCost += Cost;
1014 return isSaturated();
1015}
1016
1017bool RegBankSelect::MappingCost::isSaturated() const {
1018 return LocalCost == UINT64_MAX - 1 && NonLocalCost == UINT64_MAX &&
1019 LocalFreq == UINT64_MAX;
1020}
1021
1023 *this = ImpossibleCost();
1024 --LocalCost;
1025}
1026
1030
1031bool RegBankSelect::MappingCost::operator<(const MappingCost &Cost) const {
1032 // Sort out the easy cases.
1033 if (*this == Cost)
1034 return false;
1035 // If one is impossible to realize the other is cheaper unless it is
1036 // impossible as well.
1037 if ((*this == ImpossibleCost()) || (Cost == ImpossibleCost()))
1038 return (*this == ImpossibleCost()) < (Cost == ImpossibleCost());
1039 // If one is saturated the other is cheaper, unless it is saturated
1040 // as well.
1041 if (isSaturated() || Cost.isSaturated())
1042 return isSaturated() < Cost.isSaturated();
1043 // At this point we know both costs hold sensible values.
1044
1045 // If both values have a different base frequency, there is no much
1046 // we can do but to scale everything.
1047 // However, if they have the same base frequency we can avoid making
1048 // complicated computation.
1049 uint64_t ThisLocalAdjust;
1050 uint64_t OtherLocalAdjust;
1051 if (LLVM_LIKELY(LocalFreq == Cost.LocalFreq)) {
1052
1053 // At this point, we know the local costs are comparable.
1054 // Do the case that do not involve potential overflow first.
1055 if (NonLocalCost == Cost.NonLocalCost)
1056 // Since the non-local costs do not discriminate on the result,
1057 // just compare the local costs.
1058 return LocalCost < Cost.LocalCost;
1059
1060 // The base costs are comparable so we may only keep the relative
1061 // value to increase our chances of avoiding overflows.
1062 ThisLocalAdjust = 0;
1063 OtherLocalAdjust = 0;
1064 if (LocalCost < Cost.LocalCost)
1065 OtherLocalAdjust = Cost.LocalCost - LocalCost;
1066 else
1067 ThisLocalAdjust = LocalCost - Cost.LocalCost;
1068 } else {
1069 ThisLocalAdjust = LocalCost;
1070 OtherLocalAdjust = Cost.LocalCost;
1071 }
1072
1073 // The non-local costs are comparable, just keep the relative value.
1074 uint64_t ThisNonLocalAdjust = 0;
1075 uint64_t OtherNonLocalAdjust = 0;
1076 if (NonLocalCost < Cost.NonLocalCost)
1077 OtherNonLocalAdjust = Cost.NonLocalCost - NonLocalCost;
1078 else
1079 ThisNonLocalAdjust = NonLocalCost - Cost.NonLocalCost;
1080 // Scale everything to make them comparable.
1081 uint64_t ThisScaledCost = ThisLocalAdjust * LocalFreq;
1082 // Check for overflow on that operation.
1083 bool ThisOverflows = ThisLocalAdjust && (ThisScaledCost < ThisLocalAdjust ||
1084 ThisScaledCost < LocalFreq);
1085 uint64_t OtherScaledCost = OtherLocalAdjust * Cost.LocalFreq;
1086 // Check for overflow on the last operation.
1087 bool OtherOverflows =
1088 OtherLocalAdjust &&
1089 (OtherScaledCost < OtherLocalAdjust || OtherScaledCost < Cost.LocalFreq);
1090 // Add the non-local costs.
1091 ThisOverflows |= ThisNonLocalAdjust &&
1092 ThisScaledCost + ThisNonLocalAdjust < ThisNonLocalAdjust;
1093 ThisScaledCost += ThisNonLocalAdjust;
1094 OtherOverflows |= OtherNonLocalAdjust &&
1095 OtherScaledCost + OtherNonLocalAdjust < OtherNonLocalAdjust;
1096 OtherScaledCost += OtherNonLocalAdjust;
1097 // If both overflows, we cannot compare without additional
1098 // precision, e.g., APInt. Just give up on that case.
1099 if (ThisOverflows && OtherOverflows)
1100 return false;
1101 // If one overflows but not the other, we can still compare.
1102 if (ThisOverflows || OtherOverflows)
1103 return ThisOverflows < OtherOverflows;
1104 // Otherwise, just compare the values.
1105 return ThisScaledCost < OtherScaledCost;
1106}
1107
1108bool RegBankSelect::MappingCost::operator==(const MappingCost &Cost) const {
1109 return LocalCost == Cost.LocalCost && NonLocalCost == Cost.NonLocalCost &&
1110 LocalFreq == Cost.LocalFreq;
1111}
1112
1113#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1115 print(dbgs());
1116 dbgs() << '\n';
1117}
1118#endif
1119
1121 if (*this == ImpossibleCost()) {
1122 OS << "impossible";
1123 return;
1124 }
1125 if (isSaturated()) {
1126 OS << "saturated";
1127 return;
1128 }
1129 OS << LocalFreq << " * " << LocalCost << " + " << NonLocalCost;
1130}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
#define DEBUG_TYPE
IRTranslator LLVM IR MI
Interface for Targets to specify which operations they can successfully select and how the others sho...
#define F(x, y, z)
Definition MD5.cpp:54
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
MachineInstr unsigned OpIdx
#define P(N)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static constexpr unsigned ImpossibleRepairCost
Cost value representing an impossible or invalid repairing.
static cl::opt< RegBankSelect::Mode > RegBankSelectMode(cl::desc("Mode of the RegBankSelect pass"), cl::Hidden, cl::Optional, cl::values(clEnumValN(RegBankSelect::Mode::Fast, "regbankselect-fast", "Run the Fast mode (default mapping)"), clEnumValN(RegBankSelect::Mode::Greedy, "regbankselect-greedy", "Use the Greedy mode (best local mapping)")))
This file describes the interface of the MachineFunctionPass responsible for assigning the generic vi...
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
Target-Independent Code Generator Pass Configuration Options pass.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
constexpr unsigned getScalarSizeInBits() const
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
const MachineBlockFrequencyInfo & getMBFI() const
Definition MBFIWrapper.h:37
MachineBasicBlock * SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P, std::vector< SparseBitVector<> > *LiveInSets=nullptr, MachineDomTreeUpdater *MDTU=nullptr)
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
Insertion point on an edge.
uint64_t frequency(const Pass &P) const override
Frequency of the insertion point.
bool canMaterialize() const override
Check whether this insertion point can be materialized.
Abstract class used to represent an insertion point in a CFG.
bool WasMaterialized
Tell if the insert point has already been materialized.
virtual void materialize()=0
Materialize the insertion point.
virtual bool canMaterialize() const
Check whether this insertion point can be materialized.
virtual bool isSplit() const
Does this point involve splitting an edge or block?
Insertion point before or after an instruction.
InstrInsertPoint(MachineInstr &Instr, bool Before=true)
Create an insertion point before (Before=true) or after Instr.
bool isSplit() const override
Does this point involve splitting an edge or block?
uint64_t frequency(const Pass &P) const override
Frequency of the insertion point.
Insertion point at the beginning or end of a basic block.
uint64_t frequency(const Pass &P) const override
Frequency of the insertion point.
Helper class used to represent the cost for mapping an instruction.
void saturate()
Saturate the cost to the maximal representable value.
bool operator==(const MappingCost &Cost) const
Check if this is equal to Cost.
bool addLocalCost(uint64_t Cost)
Add Cost to the local cost.
void dump() const
Print this on dbgs() stream.
static MappingCost ImpossibleCost()
Return an instance of MappingCost that represents an impossible mapping.
bool addNonLocalCost(uint64_t Cost)
Add Cost to the non-local cost.
bool operator<(const MappingCost &Cost) const
Check if this is less than Cost.
void print(raw_ostream &OS) const
Print this on OS;.
Struct used to represent the placement of a repairing point for a given operand.
RepairingPlacement(MachineInstr &MI, unsigned OpIdx, const TargetRegisterInfo &TRI, Pass &P, RepairingKind Kind=RepairingKind::Insert)
Create a repairing placement for the OpIdx-th operand of MI.
RepairingKind
Define the kind of action this repairing needs.
@ Insert
Reparing code needs to happen before InsertPoints.
@ None
Nothing to repair, just drop this action.
@ Reassign
(Re)assign the register bank of the operand.
@ Impossible
Mark this repairing placement as impossible.
void switchTo(RepairingKind NewKind)
Change the type of this repairing placement to NewKind.
void addInsertPoint(MachineBasicBlock &MBB, bool Beginning)
This pass implements the reg bank selector pass used in the GlobalISel pipeline.
Mode
List of the modes supported by the RegBankSelect pass.
@ Fast
Assign the register banks as fast as possible (default).
@ Greedy
Greedily minimize the cost of assigning register banks.
bool checkFunctionIsLegal(MachineFunction &MF) const
Check that our input is fully legal: we require the function to have the Legalized property,...
MachineIRBuilder MIRBuilder
Helper class used for every code morphing.
MachineBlockFrequencyInfo * MBFI
Get the frequency of blocks.
Mode OptMode
Optimization mode of the pass.
const RegisterBankInfo::InstructionMapping & findBestMapping(MachineInstr &MI, RegisterBankInfo::InstructionMappings &PossibleMappings, SmallVectorImpl< RepairingPlacement > &RepairPts)
Find the best mapping for MI from PossibleMappings.
bool assignInstr(MachineInstr &MI)
Assign the register bank of each operand of MI.
bool assignRegisterBanks(MachineFunction &MF)
Walk through MF and assign a register bank to every virtual register that are still mapped to nothing...
void init(MachineFunction &MF)
Initialize the field members using MF.
void tryAvoidingSplit(RegBankSelect::RepairingPlacement &RepairPt, const MachineOperand &MO, const RegisterBankInfo::ValueMapping &ValMapping) const
When RepairPt involves splitting to repair MO for the given ValMapping, try to change the way we repa...
const TargetRegisterInfo * TRI
Information on the register classes for the current function.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineBranchProbabilityInfo * MBPI
Get the frequency of the edges.
bool assignmentMatch(Register Reg, const RegisterBankInfo::ValueMapping &ValMapping, bool &OnlyAssign) const
Check if Reg is already assigned what is described by ValMapping.
uint64_t getRepairCost(const MachineOperand &MO, const RegisterBankInfo::ValueMapping &ValMapping) const
Return the cost of the instruction needed to map MO to ValMapping.
MappingCost computeMapping(MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping, SmallVectorImpl< RepairingPlacement > &RepairPts, const MappingCost *BestCost=nullptr)
Compute the cost of mapping MI with InstrMapping and compute the repairing placement for such mapping...
bool repairReg(MachineOperand &MO, const RegisterBankInfo::ValueMapping &ValMapping, RegBankSelect::RepairingPlacement &RepairPt, const iterator_range< SmallVectorImpl< Register >::const_iterator > &NewVRegs)
Insert repairing code for Reg as specified by ValMapping.
MachineRegisterInfo * MRI
MRI contains all the register class/bank information that this pass uses and updates.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const RegisterBankInfo * RBI
Interface to the target lowering info related to register banks.
std::unique_ptr< MachineOptimizationRemarkEmitter > MORE
Current optimization remark emitter. Used to report failures.
bool applyMapping(MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping, SmallVectorImpl< RepairingPlacement > &RepairPts)
Apply Mapping to MI.
Helper class that represents how the value of an instruction may be mapped and what is the related co...
unsigned getNumOperands() const
Get the number of operands.
bool verify(const MachineInstr &MI) const
Verifiy that this mapping makes sense for MI.
bool isValid() const
Check whether this object is valid.
static const unsigned DefaultMappingID
Identifier used when the related instruction mapping instance is generated by target independent code...
SmallVector< const InstructionMapping *, 4 > InstructionMappings
Convenient type to represent the alternatives for mapping an instruction.
This class implements the register bank concept.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
typename SuperClass::const_iterator const_iterator
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Target-Independent Code Generator Pass Configuration Options.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const RegisterBankInfo * getRegBankInfo() const
If the information for the register banks is available, return it.
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1668
InstructionCost Cost
bool isPreISelGenericOptimizationHint(unsigned Opcode)
LLVM_ABI cl::opt< bool > DisableGISelLegalityCheck
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void reportGISelFailure(MachineFunction &MF, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel error as a missed optimization remark to the LLVMContext's diagnostic stream.
Definition Utils.cpp:258
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI Printable printRegClassOrBank(Register Reg, const MachineRegisterInfo &RegInfo, const TargetRegisterInfo *TRI)
Create Printable object to print register classes or register banks on a raw_ostream.
const MachineInstr * machineFunctionIsIllegal(const MachineFunction &MF)
Checks that MIR is fully legal, returns an illegal instruction if it's not, nullptr otherwise.
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition Utils.cpp:1147
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
bool isTargetSpecificOpcode(unsigned Opcode)
Check whether the given Opcode is a target-specific opcode.
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:368
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:876
const RegisterBank * RegBank
Register bank where the partial value lives.
unsigned Length
Length of this mapping in bits.
Helper struct that represents how a value is mapped through different register banks.
unsigned NumBreakDowns
Number of partial mapping to break down this value.
const PartialMapping * BreakDown
How the value is broken down between the different register banks.