2017-06-27 06:44:03 +08:00
|
|
|
//==- llvm/CodeGen/GlobalISel/RegBankSelect.cpp - RegBankSelect --*- C++ -*-==//
|
2016-04-06 03:06:01 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-06 03:06:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// This file implements the RegBankSelect class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
|
2016-05-20 08:35:26 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2017-06-27 06:44:03 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-10-15 06:18:18 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
|
2016-04-08 02:19:27 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
|
2017-06-27 06:44:03 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
|
2017-02-24 05:05:42 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/Utils.h"
|
2017-06-27 06:44:03 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2016-05-20 08:49:10 +08:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
2017-06-27 06:44:03 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
|
|
|
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
|
2016-04-08 02:19:27 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetOpcodes.h"
|
2016-08-27 10:38:27 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-06-27 06:44:03 +08:00
|
|
|
#include "llvm/IR/Attributes.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2017-06-27 06:44:03 +08:00
|
|
|
#include "llvm/Pass.h"
|
2016-05-20 08:35:26 +08:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
2016-06-08 23:49:23 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-06-27 06:44:03 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2016-04-08 07:53:55 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-06-27 06:44:03 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2016-04-06 03:06:01 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "regbankselect"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-06-08 23:49:23 +08:00
|
|
|
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",
|
2016-10-09 03:41:06 +08:00
|
|
|
"Use the Greedy mode (best local mapping)")));
|
2016-06-08 23:49:23 +08:00
|
|
|
|
2016-04-06 03:06:01 +08:00
|
|
|
char RegBankSelect::ID = 0;
|
2017-06-27 06:44:03 +08:00
|
|
|
|
2016-09-24 01:50:06 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(RegBankSelect, DEBUG_TYPE,
|
2016-05-21 01:54:09 +08:00
|
|
|
"Assign register bank of generic virtual registers",
|
|
|
|
false, false);
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
|
2016-08-27 10:38:27 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
|
2016-09-24 01:50:06 +08:00
|
|
|
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,
|
2016-05-21 01:54:09 +08:00
|
|
|
"Assign register bank of generic virtual registers", false,
|
2016-07-26 11:29:18 +08:00
|
|
|
false)
|
2016-04-06 03:06:01 +08:00
|
|
|
|
2016-05-21 00:55:35 +08:00
|
|
|
RegBankSelect::RegBankSelect(Mode RunningMode)
|
2017-06-27 06:44:03 +08:00
|
|
|
: MachineFunctionPass(ID), OptMode(RunningMode) {
|
2016-06-08 23:49:23 +08:00
|
|
|
if (RegBankSelectMode.getNumOccurrences() != 0) {
|
|
|
|
OptMode = RegBankSelectMode;
|
|
|
|
if (RegBankSelectMode != RunningMode)
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "RegBankSelect mode overrided by command line\n");
|
2016-06-08 23:49:23 +08:00
|
|
|
}
|
2016-04-06 03:06:01 +08:00
|
|
|
}
|
|
|
|
|
2016-04-08 02:19:27 +08:00
|
|
|
void RegBankSelect::init(MachineFunction &MF) {
|
|
|
|
RBI = MF.getSubtarget().getRegBankInfo();
|
|
|
|
assert(RBI && "Cannot work without RegisterBankInfo");
|
|
|
|
MRI = &MF.getRegInfo();
|
2016-04-08 05:32:23 +08:00
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
2016-08-27 10:38:27 +08:00
|
|
|
TPC = &getAnalysis<TargetPassConfig>();
|
2016-05-21 01:54:09 +08:00
|
|
|
if (OptMode != Mode::Fast) {
|
|
|
|
MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
|
|
|
|
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
|
|
|
|
} else {
|
|
|
|
MBFI = nullptr;
|
|
|
|
MBPI = nullptr;
|
|
|
|
}
|
2016-04-08 02:19:27 +08:00
|
|
|
MIRBuilder.setMF(MF);
|
2019-08-15 23:54:37 +08:00
|
|
|
MORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
|
2016-04-08 02:19:27 +08:00
|
|
|
}
|
|
|
|
|
2016-05-21 01:54:09 +08:00
|
|
|
void RegBankSelect::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
if (OptMode != Mode::Fast) {
|
|
|
|
// We could preserve the information from these two analysis but
|
|
|
|
// the APIs do not allow to do so yet.
|
|
|
|
AU.addRequired<MachineBlockFrequencyInfo>();
|
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
|
|
|
}
|
2016-08-27 10:38:27 +08:00
|
|
|
AU.addRequired<TargetPassConfig>();
|
2018-07-13 08:08:38 +08:00
|
|
|
getSelectionDAGFallbackAnalysisUsage(AU);
|
2016-05-21 01:54:09 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2016-04-08 02:19:27 +08:00
|
|
|
bool RegBankSelect::assignmentMatch(
|
2019-06-28 09:47:44 +08:00
|
|
|
Register Reg, const RegisterBankInfo::ValueMapping &ValMapping,
|
2016-05-20 08:42:57 +08:00
|
|
|
bool &OnlyAssign) const {
|
|
|
|
// By default we assume we will have to repair something.
|
|
|
|
OnlyAssign = false;
|
2016-04-08 02:19:27 +08:00
|
|
|
// Each part of a break down needs to end up in a different register.
|
2019-01-08 09:25:47 +08:00
|
|
|
// In other word, Reg assignment does not match.
|
2018-12-18 17:27:29 +08:00
|
|
|
if (ValMapping.NumBreakDowns != 1)
|
2016-04-08 02:19:27 +08:00
|
|
|
return false;
|
|
|
|
|
2016-04-09 00:48:16 +08:00
|
|
|
const RegisterBank *CurRegBank = RBI->getRegBank(Reg, *MRI, *TRI);
|
2019-12-23 02:46:40 +08:00
|
|
|
const RegisterBank *DesiredRegBank = ValMapping.BreakDown[0].RegBank;
|
2016-05-20 08:42:57 +08:00
|
|
|
// Reg is free of assignment, a simple assignment will make the
|
|
|
|
// register bank to match.
|
|
|
|
OnlyAssign = CurRegBank == nullptr;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Does assignment already match: ";
|
|
|
|
if (CurRegBank) dbgs() << *CurRegBank; else dbgs() << "none";
|
|
|
|
dbgs() << " against ";
|
2019-12-23 02:46:40 +08:00
|
|
|
assert(DesiredRegBank && "The mapping must be valid");
|
|
|
|
dbgs() << *DesiredRegBank << '\n';);
|
|
|
|
return CurRegBank == DesiredRegBank;
|
2016-04-08 02:19:27 +08:00
|
|
|
}
|
|
|
|
|
2016-08-27 10:38:27 +08:00
|
|
|
bool RegBankSelect::repairReg(
|
2016-05-20 08:55:51 +08:00
|
|
|
MachineOperand &MO, const RegisterBankInfo::ValueMapping &ValMapping,
|
|
|
|
RegBankSelect::RepairingPlacement &RepairPt,
|
2019-06-28 09:47:44 +08:00
|
|
|
const iterator_range<SmallVectorImpl<Register>::const_iterator> &NewVRegs) {
|
2019-01-25 06:47:04 +08:00
|
|
|
|
2019-05-17 13:53:39 +08:00
|
|
|
assert(ValMapping.NumBreakDowns == (unsigned)size(NewVRegs) &&
|
|
|
|
"need new vreg for each breakdown");
|
2019-01-25 06:47:04 +08:00
|
|
|
|
2016-06-09 00:30:55 +08:00
|
|
|
// An empty range of new register means no repairing.
|
2019-10-08 02:14:24 +08:00
|
|
|
assert(!NewVRegs.empty() && "We should not have to repair");
|
2016-06-09 00:30:55 +08:00
|
|
|
|
2019-01-25 06:47:04 +08:00
|
|
|
MachineInstr *MI;
|
|
|
|
if (ValMapping.NumBreakDowns == 1) {
|
|
|
|
// Assume we are repairing a use and thus, the original reg will be
|
|
|
|
// the source of the repairing.
|
2019-06-28 09:47:44 +08:00
|
|
|
Register Src = MO.getReg();
|
|
|
|
Register Dst = *NewVRegs.begin();
|
2019-01-25 06:47:04 +08:00
|
|
|
|
|
|
|
// If we repair a definition, swap the source and destination for
|
|
|
|
// the repairing.
|
|
|
|
if (MO.isDef())
|
|
|
|
std::swap(Src, Dst);
|
|
|
|
|
|
|
|
assert((RepairPt.getNumInsertPoints() == 1 ||
|
2019-08-02 07:27:28 +08:00
|
|
|
Register::isPhysicalRegister(Dst)) &&
|
2019-01-25 06:47:04 +08:00
|
|
|
"We are about to create several defs for Dst");
|
|
|
|
|
|
|
|
// Build the instruction used to repair, then clone it at the right
|
|
|
|
// places. Avoiding buildCopy bypasses the check that Src and Dst have the
|
|
|
|
// same types because the type is a placeholder when this function is called.
|
|
|
|
MI = MIRBuilder.buildInstrNoInsert(TargetOpcode::COPY)
|
|
|
|
.addDef(Dst)
|
|
|
|
.addUse(Src);
|
|
|
|
LLVM_DEBUG(dbgs() << "Copy: " << printReg(Src) << " to: " << printReg(Dst)
|
|
|
|
<< '\n');
|
|
|
|
} else {
|
|
|
|
// TODO: Support with G_IMPLICIT_DEF + G_INSERT sequence or G_EXTRACT
|
|
|
|
// sequence.
|
|
|
|
assert(ValMapping.partsAllUniform() && "irregular breakdowns not supported");
|
|
|
|
|
|
|
|
LLT RegTy = MRI->getType(MO.getReg());
|
|
|
|
if (MO.isDef()) {
|
2019-02-26 06:24:13 +08:00
|
|
|
unsigned MergeOp;
|
|
|
|
if (RegTy.isVector()) {
|
|
|
|
if (ValMapping.NumBreakDowns == RegTy.getNumElements())
|
|
|
|
MergeOp = TargetOpcode::G_BUILD_VECTOR;
|
|
|
|
else {
|
|
|
|
assert(
|
|
|
|
(ValMapping.BreakDown[0].Length * ValMapping.NumBreakDowns ==
|
|
|
|
RegTy.getSizeInBits()) &&
|
|
|
|
(ValMapping.BreakDown[0].Length % RegTy.getScalarSizeInBits() ==
|
|
|
|
0) &&
|
|
|
|
"don't understand this value breakdown");
|
|
|
|
|
|
|
|
MergeOp = TargetOpcode::G_CONCAT_VECTORS;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
MergeOp = TargetOpcode::G_MERGE_VALUES;
|
2019-01-25 06:47:04 +08:00
|
|
|
|
2019-01-25 07:42:01 +08:00
|
|
|
auto MergeBuilder =
|
2019-01-25 06:47:04 +08:00
|
|
|
MIRBuilder.buildInstrNoInsert(MergeOp)
|
|
|
|
.addDef(MO.getReg());
|
|
|
|
|
2019-06-28 09:47:44 +08:00
|
|
|
for (Register SrcReg : NewVRegs)
|
2019-01-25 06:47:04 +08:00
|
|
|
MergeBuilder.addUse(SrcReg);
|
|
|
|
|
|
|
|
MI = MergeBuilder;
|
|
|
|
} else {
|
|
|
|
MachineInstrBuilder UnMergeBuilder =
|
|
|
|
MIRBuilder.buildInstrNoInsert(TargetOpcode::G_UNMERGE_VALUES);
|
2019-06-28 09:47:44 +08:00
|
|
|
for (Register DefReg : NewVRegs)
|
2019-01-25 06:47:04 +08:00
|
|
|
UnMergeBuilder.addDef(DefReg);
|
|
|
|
|
|
|
|
UnMergeBuilder.addUse(MO.getReg());
|
|
|
|
MI = UnMergeBuilder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RepairPt.getNumInsertPoints() != 1)
|
|
|
|
report_fatal_error("need testcase to support multiple insertion points");
|
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
// TODO:
|
|
|
|
// Check if MI is legal. if not, we need to legalize all the
|
|
|
|
// instructions we are going to insert.
|
|
|
|
std::unique_ptr<MachineInstr *[]> NewInstrs(
|
|
|
|
new MachineInstr *[RepairPt.getNumInsertPoints()]);
|
|
|
|
bool IsFirst = true;
|
|
|
|
unsigned Idx = 0;
|
|
|
|
for (const std::unique_ptr<InsertPoint> &InsertPt : RepairPt) {
|
|
|
|
MachineInstr *CurMI;
|
|
|
|
if (IsFirst)
|
|
|
|
CurMI = MI;
|
|
|
|
else
|
|
|
|
CurMI = MIRBuilder.getMF().CloneMachineInstr(MI);
|
|
|
|
InsertPt->insert(*CurMI);
|
|
|
|
NewInstrs[Idx++] = CurMI;
|
|
|
|
IsFirst = false;
|
|
|
|
}
|
|
|
|
// TODO:
|
|
|
|
// Legalize NewInstrs if need be.
|
2016-08-27 10:38:27 +08:00
|
|
|
return true;
|
2016-04-08 02:19:27 +08:00
|
|
|
}
|
|
|
|
|
2016-05-21 09:43:25 +08:00
|
|
|
uint64_t RegBankSelect::getRepairCost(
|
|
|
|
const MachineOperand &MO,
|
|
|
|
const RegisterBankInfo::ValueMapping &ValMapping) const {
|
|
|
|
assert(MO.isReg() && "We should only repair register operand");
|
2016-09-23 08:14:30 +08:00
|
|
|
assert(ValMapping.NumBreakDowns && "Nothing to map??");
|
2016-05-21 09:43:25 +08:00
|
|
|
|
2016-09-23 08:14:30 +08:00
|
|
|
bool IsSameNumOfValues = ValMapping.NumBreakDowns == 1;
|
2016-05-21 09:43:25 +08:00
|
|
|
const RegisterBank *CurRegBank = RBI->getRegBank(MO.getReg(), *MRI, *TRI);
|
|
|
|
// If MO does not have a register bank, we should have just been
|
|
|
|
// able to set one unless we have to break the value down.
|
2019-01-25 06:47:04 +08:00
|
|
|
assert(CurRegBank || MO.isDef());
|
|
|
|
|
2016-05-21 09:43:25 +08:00
|
|
|
// Def: Val <- NewDefs
|
|
|
|
// Same number of values: copy
|
|
|
|
// Different number: Val = build_sequence Defs1, Defs2, ...
|
|
|
|
// Use: NewSources <- Val.
|
|
|
|
// Same number of values: copy.
|
|
|
|
// Different number: Src1, Src2, ... =
|
|
|
|
// extract_value Val, Src1Begin, Src1Len, Src2Begin, Src2Len, ...
|
|
|
|
// We should remember that this value is available somewhere else to
|
|
|
|
// coalesce the value.
|
|
|
|
|
2019-01-25 06:47:04 +08:00
|
|
|
if (ValMapping.NumBreakDowns != 1)
|
|
|
|
return RBI->getBreakDownCost(ValMapping, CurRegBank);
|
|
|
|
|
2016-05-21 09:43:25 +08:00
|
|
|
if (IsSameNumOfValues) {
|
2019-12-23 02:46:40 +08:00
|
|
|
const RegisterBank *DesiredRegBank = ValMapping.BreakDown[0].RegBank;
|
2016-05-21 09:43:25 +08:00
|
|
|
// If we repair a definition, swap the source and destination for
|
|
|
|
// the repairing.
|
|
|
|
if (MO.isDef())
|
2019-12-23 02:46:40 +08:00
|
|
|
std::swap(CurRegBank, DesiredRegBank);
|
2016-06-09 01:39:43 +08:00
|
|
|
// TODO: It may be possible to actually avoid the copy.
|
|
|
|
// If we repair something where the source is defined by a copy
|
|
|
|
// and the source of that copy is on the right bank, we can reuse
|
|
|
|
// it for free.
|
|
|
|
// E.g.,
|
|
|
|
// RegToRepair<BankA> = copy AlternativeSrc<BankB>
|
|
|
|
// = op RegToRepair<BankA>
|
|
|
|
// We can simply propagate AlternativeSrc instead of copying RegToRepair
|
|
|
|
// into a new virtual register.
|
|
|
|
// We would also need to propagate this information in the
|
|
|
|
// repairing placement.
|
2019-12-23 02:46:40 +08:00
|
|
|
unsigned Cost = RBI->copyCost(*DesiredRegBank, *CurRegBank,
|
2017-10-14 05:16:15 +08:00
|
|
|
RBI->getSizeInBits(MO.getReg(), *MRI, *TRI));
|
2016-05-21 09:43:25 +08:00
|
|
|
// TODO: use a dedicated constant for ImpossibleCost.
|
2017-06-27 06:44:03 +08:00
|
|
|
if (Cost != std::numeric_limits<unsigned>::max())
|
2016-05-21 09:43:25 +08:00
|
|
|
return Cost;
|
|
|
|
// Return the legalization cost of that repairing.
|
|
|
|
}
|
2017-06-27 06:44:03 +08:00
|
|
|
return std::numeric_limits<unsigned>::max();
|
2016-05-21 09:43:25 +08:00
|
|
|
}
|
|
|
|
|
2017-05-06 06:48:22 +08:00
|
|
|
const RegisterBankInfo::InstructionMapping &RegBankSelect::findBestMapping(
|
2016-05-21 02:37:33 +08:00
|
|
|
MachineInstr &MI, RegisterBankInfo::InstructionMappings &PossibleMappings,
|
|
|
|
SmallVectorImpl<RepairingPlacement> &RepairPts) {
|
2016-08-27 10:38:27 +08:00
|
|
|
assert(!PossibleMappings.empty() &&
|
|
|
|
"Do not know how to map this instruction");
|
2016-05-21 02:37:33 +08:00
|
|
|
|
2017-05-06 06:48:22 +08:00
|
|
|
const RegisterBankInfo::InstructionMapping *BestMapping = nullptr;
|
2016-05-21 02:37:33 +08:00
|
|
|
MappingCost Cost = MappingCost::ImpossibleCost();
|
|
|
|
SmallVector<RepairingPlacement, 4> LocalRepairPts;
|
2017-05-06 06:48:22 +08:00
|
|
|
for (const RegisterBankInfo::InstructionMapping *CurMapping :
|
|
|
|
PossibleMappings) {
|
|
|
|
MappingCost CurCost =
|
|
|
|
computeMapping(MI, *CurMapping, LocalRepairPts, &Cost);
|
2016-05-21 02:37:33 +08:00
|
|
|
if (CurCost < Cost) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "New best: " << CurCost << '\n');
|
2016-05-21 02:37:33 +08:00
|
|
|
Cost = CurCost;
|
2017-05-06 06:48:22 +08:00
|
|
|
BestMapping = CurMapping;
|
2016-05-21 02:37:33 +08:00
|
|
|
RepairPts.clear();
|
|
|
|
for (RepairingPlacement &RepairPt : LocalRepairPts)
|
|
|
|
RepairPts.emplace_back(std::move(RepairPt));
|
|
|
|
}
|
|
|
|
}
|
2016-08-27 10:38:27 +08:00
|
|
|
if (!BestMapping && !TPC->isGlobalISelAbortEnabled()) {
|
|
|
|
// If none of the mapping worked that means they are all impossible.
|
|
|
|
// Thus, pick the first one and set an impossible repairing point.
|
|
|
|
// It will trigger the failed isel mode.
|
2017-05-06 06:48:22 +08:00
|
|
|
BestMapping = *PossibleMappings.begin();
|
2016-08-27 10:38:27 +08:00
|
|
|
RepairPts.emplace_back(
|
|
|
|
RepairingPlacement(MI, 0, *TRI, *this, RepairingPlacement::Impossible));
|
|
|
|
} else
|
|
|
|
assert(BestMapping && "No suitable mapping for instruction");
|
2016-05-21 02:37:33 +08:00
|
|
|
return *BestMapping;
|
|
|
|
}
|
|
|
|
|
2016-05-21 00:36:12 +08:00
|
|
|
void RegBankSelect::tryAvoidingSplit(
|
|
|
|
RegBankSelect::RepairingPlacement &RepairPt, const MachineOperand &MO,
|
|
|
|
const RegisterBankInfo::ValueMapping &ValMapping) const {
|
|
|
|
const MachineInstr &MI = *MO.getParent();
|
|
|
|
assert(RepairPt.hasSplit() && "We should not have to adjust for split");
|
|
|
|
// Splitting should only occur for PHIs or between terminators,
|
|
|
|
// because we only do local repairing.
|
|
|
|
assert((MI.isPHI() || MI.isTerminator()) && "Why do we split?");
|
|
|
|
|
|
|
|
assert(&MI.getOperand(RepairPt.getOpIdx()) == &MO &&
|
|
|
|
"Repairing placement does not match operand");
|
|
|
|
|
|
|
|
// If we need splitting for phis, that means it is because we
|
|
|
|
// could not find an insertion point before the terminators of
|
|
|
|
// the predecessor block for this argument. In other words,
|
|
|
|
// the input value is defined by one of the terminators.
|
|
|
|
assert((!MI.isPHI() || !MO.isDef()) && "Need split for phi def?");
|
|
|
|
|
|
|
|
// We split to repair the use of a phi or a terminator.
|
|
|
|
if (!MO.isDef()) {
|
|
|
|
if (MI.isTerminator()) {
|
|
|
|
assert(&MI != &(*MI.getParent()->getFirstTerminator()) &&
|
|
|
|
"Need to split for the first terminator?!");
|
|
|
|
} else {
|
|
|
|
// For the PHI case, the split may not be actually required.
|
|
|
|
// In the copy case, a phi is already a copy on the incoming edge,
|
|
|
|
// therefore there is no need to split.
|
2016-09-23 08:14:30 +08:00
|
|
|
if (ValMapping.NumBreakDowns == 1)
|
2016-05-21 00:36:12 +08:00
|
|
|
// This is a already a copy, there is nothing to do.
|
|
|
|
RepairPt.switchTo(RepairingPlacement::RepairingKind::Reassign);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we need to repair a defintion of a terminator.
|
|
|
|
|
|
|
|
// Technically we need to fix the def of MI on all outgoing
|
|
|
|
// edges of MI to keep the repairing local. In other words, we
|
|
|
|
// will create several definitions of the same register. This
|
|
|
|
// does not work for SSA unless that definition is a physical
|
|
|
|
// register.
|
|
|
|
// However, there are other cases where we can get away with
|
|
|
|
// that while still keeping the repairing local.
|
|
|
|
assert(MI.isTerminator() && MO.isDef() &&
|
|
|
|
"This code is for the def of a terminator");
|
|
|
|
|
|
|
|
// Since we use RPO traversal, if we need to repair a definition
|
|
|
|
// this means this definition could be:
|
|
|
|
// 1. Used by PHIs (i.e., this VReg has been visited as part of the
|
|
|
|
// uses of a phi.), or
|
|
|
|
// 2. Part of a target specific instruction (i.e., the target applied
|
|
|
|
// some register class constraints when creating the instruction.)
|
|
|
|
// If the constraints come for #2, the target said that another mapping
|
|
|
|
// is supported so we may just drop them. Indeed, if we do not change
|
|
|
|
// the number of registers holding that value, the uses will get fixed
|
|
|
|
// when we get to them.
|
|
|
|
// Uses in PHIs may have already been proceeded though.
|
|
|
|
// If the constraints come for #1, then, those are weak constraints and
|
|
|
|
// no actual uses may rely on them. However, the problem remains mainly
|
|
|
|
// the same as for #2. If the value stays in one register, we could
|
|
|
|
// just switch the register bank of the definition, but we would need to
|
|
|
|
// account for a repairing cost for each phi we silently change.
|
|
|
|
//
|
|
|
|
// In any case, if the value needs to be broken down into several
|
|
|
|
// registers, the repairing is not local anymore as we need to patch
|
|
|
|
// every uses to rebuild the value in just one register.
|
|
|
|
//
|
|
|
|
// To summarize:
|
|
|
|
// - If the value is in a physical register, we can do the split and
|
|
|
|
// fix locally.
|
|
|
|
// Otherwise if the value is in a virtual register:
|
|
|
|
// - If the value remains in one register, we do not have to split
|
|
|
|
// just switching the register bank would do, but we need to account
|
|
|
|
// in the repairing cost all the phi we changed.
|
|
|
|
// - If the value spans several registers, then we cannot do a local
|
|
|
|
// repairing.
|
|
|
|
|
|
|
|
// Check if this is a physical or virtual register.
|
2019-06-28 09:47:44 +08:00
|
|
|
Register Reg = MO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isPhysicalRegister(Reg)) {
|
2016-05-21 00:36:12 +08:00
|
|
|
// We are going to split every outgoing edges.
|
|
|
|
// Check that this is possible.
|
|
|
|
// FIXME: The machine representation is currently broken
|
|
|
|
// since it also several terminators in one basic block.
|
|
|
|
// Because of that we would technically need a way to get
|
|
|
|
// the targets of just one terminator to know which edges
|
|
|
|
// we have to split.
|
|
|
|
// Assert that we do not hit the ill-formed representation.
|
|
|
|
|
|
|
|
// If there are other terminators before that one, some of
|
|
|
|
// the outgoing edges may not be dominated by this definition.
|
|
|
|
assert(&MI == &(*MI.getParent()->getFirstTerminator()) &&
|
|
|
|
"Do not know which outgoing edges are relevant");
|
|
|
|
const MachineInstr *Next = MI.getNextNode();
|
|
|
|
assert((!Next || Next->isUnconditionalBranch()) &&
|
|
|
|
"Do not know where each terminator ends up");
|
|
|
|
if (Next)
|
|
|
|
// If the next terminator uses Reg, this means we have
|
|
|
|
// to split right after MI and thus we need a way to ask
|
|
|
|
// which outgoing edges are affected.
|
|
|
|
assert(!Next->readsRegister(Reg) && "Need to split between terminators");
|
|
|
|
// We will split all the edges and repair there.
|
|
|
|
} else {
|
|
|
|
// This is a virtual register defined by a terminator.
|
2016-09-23 08:14:30 +08:00
|
|
|
if (ValMapping.NumBreakDowns == 1) {
|
2016-05-21 00:36:12 +08:00
|
|
|
// There is nothing to repair, but we may actually lie on
|
|
|
|
// the repairing cost because of the PHIs already proceeded
|
|
|
|
// as already stated.
|
|
|
|
// Though the code will be correct.
|
2017-06-27 06:44:03 +08:00
|
|
|
assert(false && "Repairing cost may not be accurate");
|
2016-05-21 00:36:12 +08:00
|
|
|
} else {
|
|
|
|
// We need to do non-local repairing. Basically, patch all
|
|
|
|
// the uses (i.e., phis) that we already proceeded.
|
|
|
|
// For now, just say this mapping is not possible.
|
|
|
|
RepairPt.switchTo(RepairingPlacement::RepairingKind::Impossible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
RegBankSelect::MappingCost RegBankSelect::computeMapping(
|
|
|
|
MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping,
|
2016-05-21 02:00:46 +08:00
|
|
|
SmallVectorImpl<RepairingPlacement> &RepairPts,
|
|
|
|
const RegBankSelect::MappingCost *BestCost) {
|
|
|
|
assert((MBFI || !BestCost) && "Costs comparison require MBFI");
|
2016-04-08 07:53:55 +08:00
|
|
|
|
2016-12-07 02:38:38 +08:00
|
|
|
if (!InstrMapping.isValid())
|
|
|
|
return MappingCost::ImpossibleCost();
|
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
// If mapped with InstrMapping, MI will have the recorded cost.
|
2016-05-21 01:54:09 +08:00
|
|
|
MappingCost Cost(MBFI ? MBFI->getBlockFreq(MI.getParent()) : 1);
|
2016-05-20 08:55:51 +08:00
|
|
|
bool Saturated = Cost.addLocalCost(InstrMapping.getCost());
|
|
|
|
assert(!Saturated && "Possible mapping saturated the cost");
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Evaluating mapping cost for: " << MI);
|
|
|
|
LLVM_DEBUG(dbgs() << "With: " << InstrMapping << '\n');
|
2016-05-20 08:55:51 +08:00
|
|
|
RepairPts.clear();
|
2017-01-11 08:48:41 +08:00
|
|
|
if (BestCost && Cost > *BestCost) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Mapping is too expensive from the start\n");
|
2016-05-21 02:00:46 +08:00
|
|
|
return Cost;
|
2017-01-11 08:48:41 +08:00
|
|
|
}
|
2016-05-21 02:00:46 +08:00
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
// Moreover, to realize this mapping, the register bank of each operand must
|
|
|
|
// match this mapping. In other words, we may need to locally reassign the
|
|
|
|
// register banks. Account for that repairing cost as well.
|
|
|
|
// In this context, local means in the surrounding of MI.
|
2016-09-30 03:51:46 +08:00
|
|
|
for (unsigned OpIdx = 0, EndOpIdx = InstrMapping.getNumOperands();
|
|
|
|
OpIdx != EndOpIdx; ++OpIdx) {
|
2016-05-20 08:55:51 +08:00
|
|
|
const MachineOperand &MO = MI.getOperand(OpIdx);
|
2016-04-08 02:19:27 +08:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
2019-06-28 09:47:44 +08:00
|
|
|
Register Reg = MO.getReg();
|
2016-04-08 02:19:27 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Opd" << OpIdx << '\n');
|
2016-04-08 02:19:27 +08:00
|
|
|
const RegisterBankInfo::ValueMapping &ValMapping =
|
2016-05-20 08:55:51 +08:00
|
|
|
InstrMapping.getOperandMapping(OpIdx);
|
|
|
|
// If Reg is already properly mapped, this is free.
|
|
|
|
bool Assign;
|
|
|
|
if (assignmentMatch(Reg, ValMapping, Assign)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "=> is free (match).\n");
|
2016-05-20 08:55:51 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Assign) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "=> is free (simple assignment).\n");
|
2016-05-20 08:55:51 +08:00
|
|
|
RepairPts.emplace_back(RepairingPlacement(MI, OpIdx, *TRI, *this,
|
|
|
|
RepairingPlacement::Reassign));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the insertion point for the repairing code.
|
|
|
|
RepairPts.emplace_back(
|
|
|
|
RepairingPlacement(MI, OpIdx, *TRI, *this, RepairingPlacement::Insert));
|
|
|
|
RepairingPlacement &RepairPt = RepairPts.back();
|
|
|
|
|
2016-05-21 00:36:12 +08:00
|
|
|
// If we need to split a basic block to materialize this insertion point,
|
|
|
|
// we may give a higher cost to this mapping.
|
|
|
|
// Nevertheless, we may get away with the split, so try that first.
|
|
|
|
if (RepairPt.hasSplit())
|
|
|
|
tryAvoidingSplit(RepairPt, MO, ValMapping);
|
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
// Check that the materialization of the repairing is possible.
|
2017-01-11 08:48:41 +08:00
|
|
|
if (!RepairPt.canMaterialize()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Mapping involves impossible repairing\n");
|
2016-05-20 08:55:51 +08:00
|
|
|
return MappingCost::ImpossibleCost();
|
2017-01-11 08:48:41 +08:00
|
|
|
}
|
2016-05-20 08:55:51 +08:00
|
|
|
|
|
|
|
// Account for the split cost and repair cost.
|
2016-05-21 02:00:46 +08:00
|
|
|
// Unless the cost is already saturated or we do not care about the cost.
|
|
|
|
if (!BestCost || Saturated)
|
2016-04-08 02:19:27 +08:00
|
|
|
continue;
|
|
|
|
|
2016-05-21 02:00:46 +08:00
|
|
|
// To get accurate information we need MBFI and MBPI.
|
|
|
|
// Thus, if we end up here this information should be here.
|
|
|
|
assert(MBFI && MBPI && "Cost computation requires MBFI and MBPI");
|
|
|
|
|
2016-06-08 23:40:32 +08:00
|
|
|
// FIXME: We will have to rework the repairing cost model.
|
|
|
|
// The repairing cost depends on the register bank that MO has.
|
|
|
|
// However, when we break down the value into different values,
|
|
|
|
// MO may not have a register bank while still needing repairing.
|
|
|
|
// For the fast mode, we don't compute the cost so that is fine,
|
|
|
|
// but still for the repairing code, we will have to make a choice.
|
|
|
|
// For the greedy mode, we should choose greedily what is the best
|
|
|
|
// choice based on the next use of MO.
|
|
|
|
|
2016-05-21 09:43:25 +08:00
|
|
|
// Sums up the repairing cost of MO at each insertion point.
|
|
|
|
uint64_t RepairCost = getRepairCost(MO, ValMapping);
|
2017-05-15 17:52:33 +08:00
|
|
|
|
|
|
|
// This is an impossible to repair cost.
|
2017-06-27 06:44:03 +08:00
|
|
|
if (RepairCost == std::numeric_limits<unsigned>::max())
|
2018-07-25 11:08:35 +08:00
|
|
|
return MappingCost::ImpossibleCost();
|
2017-05-15 17:52:33 +08:00
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
// Bias used for splitting: 5%.
|
|
|
|
const uint64_t PercentageForBias = 5;
|
|
|
|
uint64_t Bias = (RepairCost * PercentageForBias + 99) / 100;
|
|
|
|
// We should not need more than a couple of instructions to repair
|
|
|
|
// an assignment. In other words, the computation should not
|
|
|
|
// overflow because the repairing cost is free of basic block
|
|
|
|
// frequency.
|
|
|
|
assert(((RepairCost < RepairCost * PercentageForBias) &&
|
|
|
|
(RepairCost * PercentageForBias <
|
|
|
|
RepairCost * PercentageForBias + 99)) &&
|
|
|
|
"Repairing involves more than a billion of instructions?!");
|
|
|
|
for (const std::unique_ptr<InsertPoint> &InsertPt : RepairPt) {
|
|
|
|
assert(InsertPt->canMaterialize() && "We should not have made it here");
|
|
|
|
// We will applied some basic block frequency and those uses uint64_t.
|
|
|
|
if (!InsertPt->isSplit())
|
|
|
|
Saturated = Cost.addLocalCost(RepairCost);
|
|
|
|
else {
|
|
|
|
uint64_t CostForInsertPt = RepairCost;
|
|
|
|
// Again we shouldn't overflow here givent that
|
|
|
|
// CostForInsertPt is frequency free at this point.
|
|
|
|
assert(CostForInsertPt + Bias > CostForInsertPt &&
|
|
|
|
"Repairing + split bias overflows");
|
|
|
|
CostForInsertPt += Bias;
|
|
|
|
uint64_t PtCost = InsertPt->frequency(*this) * CostForInsertPt;
|
|
|
|
// Check if we just overflowed.
|
|
|
|
if ((Saturated = PtCost < CostForInsertPt))
|
|
|
|
Cost.saturate();
|
|
|
|
else
|
|
|
|
Saturated = Cost.addNonLocalCost(PtCost);
|
2016-04-12 08:12:59 +08:00
|
|
|
}
|
2016-05-21 02:00:46 +08:00
|
|
|
|
|
|
|
// Stop looking into what it takes to repair, this is already
|
|
|
|
// too expensive.
|
2017-01-11 08:48:41 +08:00
|
|
|
if (BestCost && Cost > *BestCost) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Mapping is too expensive, stop processing\n");
|
2016-05-21 02:00:46 +08:00
|
|
|
return Cost;
|
2017-01-11 08:48:41 +08:00
|
|
|
}
|
2016-05-21 02:00:46 +08:00
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
// No need to accumulate more cost information.
|
|
|
|
// We need to still gather the repairing information though.
|
|
|
|
if (Saturated)
|
|
|
|
break;
|
2016-04-08 02:19:27 +08:00
|
|
|
}
|
2016-05-20 08:55:51 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Total cost is: " << Cost << "\n");
|
2016-05-20 08:55:51 +08:00
|
|
|
return Cost;
|
|
|
|
}
|
|
|
|
|
2016-08-27 10:38:27 +08:00
|
|
|
bool RegBankSelect::applyMapping(
|
2016-05-20 08:55:51 +08:00
|
|
|
MachineInstr &MI, const RegisterBankInfo::InstructionMapping &InstrMapping,
|
|
|
|
SmallVectorImpl<RegBankSelect::RepairingPlacement> &RepairPts) {
|
2019-01-08 09:25:47 +08:00
|
|
|
// OpdMapper will hold all the information needed for the rewriting.
|
2016-06-09 00:30:55 +08:00
|
|
|
RegisterBankInfo::OperandsMapper OpdMapper(MI, InstrMapping, *MRI);
|
|
|
|
|
2016-06-09 00:45:04 +08:00
|
|
|
// First, place the repairing code.
|
2016-05-20 08:55:51 +08:00
|
|
|
for (RepairingPlacement &RepairPt : RepairPts) {
|
2016-08-27 10:38:27 +08:00
|
|
|
if (!RepairPt.canMaterialize() ||
|
|
|
|
RepairPt.getKind() == RepairingPlacement::Impossible)
|
|
|
|
return false;
|
2016-05-20 08:55:51 +08:00
|
|
|
assert(RepairPt.getKind() != RepairingPlacement::None &&
|
|
|
|
"This should not make its way in the list");
|
|
|
|
unsigned OpIdx = RepairPt.getOpIdx();
|
|
|
|
MachineOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
const RegisterBankInfo::ValueMapping &ValMapping =
|
|
|
|
InstrMapping.getOperandMapping(OpIdx);
|
2019-06-28 09:47:44 +08:00
|
|
|
Register Reg = MO.getReg();
|
2016-04-12 08:12:59 +08:00
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
switch (RepairPt.getKind()) {
|
|
|
|
case RepairingPlacement::Reassign:
|
2016-09-23 08:14:30 +08:00
|
|
|
assert(ValMapping.NumBreakDowns == 1 &&
|
2016-05-20 08:55:51 +08:00
|
|
|
"Reassignment should only be for simple mapping");
|
|
|
|
MRI->setRegBank(Reg, *ValMapping.BreakDown[0].RegBank);
|
|
|
|
break;
|
|
|
|
case RepairingPlacement::Insert:
|
2016-06-09 00:30:55 +08:00
|
|
|
OpdMapper.createVRegs(OpIdx);
|
2016-08-27 10:38:27 +08:00
|
|
|
if (!repairReg(MO, ValMapping, RepairPt, OpdMapper.getVRegs(OpIdx)))
|
|
|
|
return false;
|
2016-05-20 08:55:51 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Other kind should not happen");
|
|
|
|
}
|
2016-04-08 02:19:27 +08:00
|
|
|
}
|
2017-06-28 05:41:40 +08:00
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
// Second, rewrite the instruction.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Actual mapping of the operands: " << OpdMapper << '\n');
|
2016-06-09 00:45:04 +08:00
|
|
|
RBI->applyMapping(OpdMapper);
|
2017-06-28 05:41:40 +08:00
|
|
|
|
2016-08-27 10:38:27 +08:00
|
|
|
return true;
|
2016-05-20 08:55:51 +08:00
|
|
|
}
|
|
|
|
|
2016-08-27 10:38:27 +08:00
|
|
|
bool RegBankSelect::assignInstr(MachineInstr &MI) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Assign: " << MI);
|
2021-01-29 02:16:44 +08:00
|
|
|
|
2021-02-18 02:57:10 +08:00
|
|
|
unsigned Opc = MI.getOpcode();
|
|
|
|
if (isPreISelGenericOptimizationHint(Opc)) {
|
|
|
|
assert((Opc == TargetOpcode::G_ASSERT_ZEXT ||
|
|
|
|
Opc == TargetOpcode::G_ASSERT_SEXT) &&
|
|
|
|
"Unexpected hint opcode!");
|
2021-01-29 02:16:44 +08:00
|
|
|
// The only correct mapping for these is to always use the source register
|
|
|
|
// bank.
|
|
|
|
const RegisterBank *RB = MRI->getRegBankOrNull(MI.getOperand(1).getReg());
|
|
|
|
// We can assume every instruction above this one has a selected register
|
|
|
|
// bank.
|
|
|
|
assert(RB && "Expected source register to have a register bank?");
|
2021-02-18 02:57:10 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "... Hint always uses source's register bank.\n");
|
2021-01-29 02:16:44 +08:00
|
|
|
MRI->setRegBank(MI.getOperand(0).getReg(), *RB);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-20 08:55:51 +08:00
|
|
|
// Remember the repairing placement for all the operands.
|
|
|
|
SmallVector<RepairingPlacement, 4> RepairPts;
|
|
|
|
|
2017-05-06 06:48:22 +08:00
|
|
|
const RegisterBankInfo::InstructionMapping *BestMapping;
|
2016-05-21 02:37:33 +08:00
|
|
|
if (OptMode == RegBankSelect::Mode::Fast) {
|
2017-05-06 06:48:22 +08:00
|
|
|
BestMapping = &RBI->getInstrMapping(MI);
|
|
|
|
MappingCost DefaultCost = computeMapping(MI, *BestMapping, RepairPts);
|
2016-05-21 02:37:33 +08:00
|
|
|
(void)DefaultCost;
|
2016-08-27 10:38:27 +08:00
|
|
|
if (DefaultCost == MappingCost::ImpossibleCost())
|
|
|
|
return false;
|
2016-05-21 02:37:33 +08:00
|
|
|
} else {
|
|
|
|
RegisterBankInfo::InstructionMappings PossibleMappings =
|
|
|
|
RBI->getInstrPossibleMappings(MI);
|
2016-08-27 10:38:27 +08:00
|
|
|
if (PossibleMappings.empty())
|
|
|
|
return false;
|
2017-05-06 06:48:22 +08:00
|
|
|
BestMapping = &findBestMapping(MI, PossibleMappings, RepairPts);
|
2016-05-21 02:37:33 +08:00
|
|
|
}
|
2016-05-20 08:55:51 +08:00
|
|
|
// Make sure the mapping is valid for MI.
|
2017-05-06 06:48:22 +08:00
|
|
|
assert(BestMapping->verify(MI) && "Invalid instruction mapping");
|
2016-05-20 08:55:51 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Best Mapping: " << *BestMapping << '\n');
|
2016-05-20 08:55:51 +08:00
|
|
|
|
2016-06-09 05:55:29 +08:00
|
|
|
// After this call, MI may not be valid anymore.
|
|
|
|
// Do not use it.
|
2017-05-06 06:48:22 +08:00
|
|
|
return applyMapping(MI, *BestMapping, RepairPts);
|
2016-04-08 02:19:27 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 03:06:01 +08:00
|
|
|
bool RegBankSelect::runOnMachineFunction(MachineFunction &MF) {
|
2016-08-27 08:18:24 +08:00
|
|
|
// If the ISel pipeline failed, do not bother running that pass.
|
|
|
|
if (MF.getProperties().hasProperty(
|
|
|
|
MachineFunctionProperties::Property::FailedISel))
|
|
|
|
return false;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Assign register banks for: " << MF.getName() << '\n');
|
2017-12-16 06:22:58 +08:00
|
|
|
const Function &F = MF.getFunction();
|
2016-05-21 01:36:54 +08:00
|
|
|
Mode SaveOptMode = OptMode;
|
2019-04-05 06:40:06 +08:00
|
|
|
if (F.hasOptNone())
|
2016-05-21 01:36:54 +08:00
|
|
|
OptMode = Mode::Fast;
|
2016-04-08 02:19:27 +08:00
|
|
|
init(MF);
|
2016-08-02 23:10:32 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Check that our input is fully legal: we require the function to have the
|
|
|
|
// Legalized property, so it should be.
|
[GlobalISel][AArch64] Adding -disable-gisel-legality-check CL option
Currently it's impossible to test InstructionSelect pass with MIR which
is considered illegal by the Legalizer in Assert builds. In early stages
of porting an existing backend from SelectionDAG ISel to GlobalISel,
however, we would have very basic CallLowering, Legalizer, and
RegBankSelect implementations, but rather functional Instruction Select
with quite a few patterns selectable due to the semi-automatic porting
process borrowing them from SelectionDAG ISel.
As we are trying to define legality as a property of being selectable by
the instruction selector, it would be nice to be able to easily check
what the selector can do in its current state w/o the legality check
provided by the Legalizer getting in the way.
It also seems beneficial to have a regression testing set up that would
not allow the selector to silently regress in its support of the MIR not
supported yet by the previous passes in the GlobalISel pipeline.
This commit adds -disable-gisel-legality-check command line option to
llc that disables those legality checks in RegBankSelect and
InstructionSelect passes.
It also adds quite a few MIR test cases for AArch64's Instruction
Selector. Every one of them would fail on the legality check at the
moment, but will select just fine if the check is disabled. Every test
MachineFunction is intended to exercise a specific selection rule and
that rule only, encoded in the MachineFunction's name by the rule's
number, ID, and index of its GIM_Try opcode in TableGen'erated
MatchTable (-optimize-match-table=false).
Reviewers: ab, dsanders, qcolombet, rovka
Reviewed By: bogner
Subscribers: kristof.beyls, volkan, aditya_nandakumar, aemerson,
rengolin, t.p.northover, javed.absar, llvm-commits
Differential Revision: https://reviews.llvm.org/D42886
llvm-svn: 326396
2018-03-01 08:27:48 +08:00
|
|
|
// FIXME: This should be in the MachineVerifier.
|
|
|
|
if (!DisableGISelLegalityCheck)
|
|
|
|
if (const MachineInstr *MI = machineFunctionIsIllegal(MF)) {
|
|
|
|
reportGISelFailure(MF, *TPC, *MORE, "gisel-regbankselect",
|
|
|
|
"instruction is not legal", *MI);
|
|
|
|
return false;
|
2016-08-02 23:10:32 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-08 02:19:27 +08:00
|
|
|
// Walk the function and assign register banks to all operands.
|
2016-04-09 01:19:10 +08:00
|
|
|
// Use a RPOT to make sure all registers are assigned before we choose
|
|
|
|
// the best mapping of the current instruction.
|
|
|
|
ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
|
2016-05-20 08:55:51 +08:00
|
|
|
for (MachineBasicBlock *MBB : RPOT) {
|
|
|
|
// Set a sensible insertion point so that subsequent calls to
|
|
|
|
// MIRBuilder.
|
|
|
|
MIRBuilder.setMBB(*MBB);
|
2016-06-09 00:45:04 +08:00
|
|
|
for (MachineBasicBlock::iterator MII = MBB->begin(), End = MBB->end();
|
|
|
|
MII != End;) {
|
|
|
|
// MI might be invalidated by the assignment, so move the
|
|
|
|
// iterator before hand.
|
2016-08-02 19:41:16 +08:00
|
|
|
MachineInstr &MI = *MII++;
|
|
|
|
|
2019-10-08 02:43:29 +08:00
|
|
|
// Ignore target-specific post-isel instructions: they should use proper
|
|
|
|
// regclasses.
|
|
|
|
if (isTargetSpecificOpcode(MI.getOpcode()) && !MI.isPreISelOpcode())
|
2016-08-02 19:41:16 +08:00
|
|
|
continue;
|
2020-04-09 02:04:13 +08:00
|
|
|
|
|
|
|
// Ignore inline asm instructions: they should use physical
|
|
|
|
// registers/regclasses
|
|
|
|
if (MI.isInlineAsm())
|
|
|
|
continue;
|
2016-08-02 19:41:16 +08:00
|
|
|
|
2020-04-15 02:35:25 +08:00
|
|
|
// Ignore debug info.
|
|
|
|
if (MI.isDebugInstr())
|
|
|
|
continue;
|
|
|
|
|
2021-03-25 02:28:40 +08:00
|
|
|
// Ignore IMPLICIT_DEF which must have a regclass.
|
|
|
|
if (MI.isImplicitDef())
|
|
|
|
continue;
|
|
|
|
|
2016-08-27 10:38:27 +08:00
|
|
|
if (!assignInstr(MI)) {
|
2017-02-24 05:05:42 +08:00
|
|
|
reportGISelFailure(MF, *TPC, *MORE, "gisel-regbankselect",
|
|
|
|
"unable to map instruction", MI);
|
2016-08-27 10:38:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-21 23:48:13 +08:00
|
|
|
|
|
|
|
// It's possible the mapping changed control flow, and moved the following
|
|
|
|
// instruction to a new block, so figure out the new parent.
|
|
|
|
if (MII != End) {
|
|
|
|
MachineBasicBlock *NextInstBB = MII->getParent();
|
|
|
|
if (NextInstBB != MBB) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Instruction mapping changed control flow\n");
|
|
|
|
MBB = NextInstBB;
|
|
|
|
MIRBuilder.setMBB(*MBB);
|
|
|
|
End = MBB->end();
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 00:45:04 +08:00
|
|
|
}
|
2016-05-20 08:55:51 +08:00
|
|
|
}
|
2019-02-21 23:48:13 +08:00
|
|
|
|
2016-05-21 01:36:54 +08:00
|
|
|
OptMode = SaveOptMode;
|
2016-04-06 03:06:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-20 08:35:26 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2016-05-20 08:49:10 +08:00
|
|
|
// Helper Classes Implementation
|
2016-05-20 08:35:26 +08:00
|
|
|
//------------------------------------------------------------------------------
|
2016-05-20 08:49:10 +08:00
|
|
|
RegBankSelect::RepairingPlacement::RepairingPlacement(
|
|
|
|
MachineInstr &MI, unsigned OpIdx, const TargetRegisterInfo &TRI, Pass &P,
|
|
|
|
RepairingPlacement::RepairingKind Kind)
|
|
|
|
// Default is, we are going to insert code to repair OpIdx.
|
2017-06-27 06:44:03 +08:00
|
|
|
: Kind(Kind), OpIdx(OpIdx),
|
|
|
|
CanMaterialize(Kind != RepairingKind::Impossible), P(P) {
|
2016-05-20 08:49:10 +08:00
|
|
|
const MachineOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isReg() && "Trying to repair a non-reg operand");
|
|
|
|
|
|
|
|
if (Kind != RepairingKind::Insert)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Repairings for definitions happen after MI, uses happen before.
|
|
|
|
bool Before = !MO.isDef();
|
|
|
|
|
|
|
|
// Check if we are done with MI.
|
|
|
|
if (!MI.isPHI() && !MI.isTerminator()) {
|
|
|
|
addInsertPoint(MI, Before);
|
|
|
|
// We are done with the initialization.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, look for the special cases.
|
|
|
|
if (MI.isPHI()) {
|
|
|
|
// - PHI must be the first instructions:
|
|
|
|
// * Before, we have to split the related incoming edge.
|
|
|
|
// * After, move the insertion point past the last phi.
|
|
|
|
if (!Before) {
|
|
|
|
MachineBasicBlock::iterator It = MI.getParent()->getFirstNonPHI();
|
|
|
|
if (It != MI.getParent()->end())
|
|
|
|
addInsertPoint(*It, /*Before*/ true);
|
|
|
|
else
|
|
|
|
addInsertPoint(*(--It), /*Before*/ false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We repair a use of a phi, we may need to split the related edge.
|
|
|
|
MachineBasicBlock &Pred = *MI.getOperand(OpIdx + 1).getMBB();
|
|
|
|
// Check if we can move the insertion point prior to the
|
|
|
|
// terminators of the predecessor.
|
2019-06-28 09:47:44 +08:00
|
|
|
Register Reg = MO.getReg();
|
2016-05-20 08:49:10 +08:00
|
|
|
MachineBasicBlock::iterator It = Pred.getLastNonDebugInstr();
|
|
|
|
for (auto Begin = Pred.begin(); It != Begin && It->isTerminator(); --It)
|
|
|
|
if (It->modifiesRegister(Reg, &TRI)) {
|
|
|
|
// We cannot hoist the repairing code in the predecessor.
|
|
|
|
// Split the edge.
|
|
|
|
addInsertPoint(Pred, *MI.getParent());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// At this point, we can insert in Pred.
|
|
|
|
|
|
|
|
// - If It is invalid, Pred is empty and we can insert in Pred
|
|
|
|
// wherever we want.
|
|
|
|
// - If It is valid, It is the first non-terminator, insert after It.
|
|
|
|
if (It == Pred.end())
|
|
|
|
addInsertPoint(Pred, /*Beginning*/ false);
|
|
|
|
else
|
|
|
|
addInsertPoint(*It, /*Before*/ false);
|
|
|
|
} else {
|
|
|
|
// - Terminators must be the last instructions:
|
|
|
|
// * Before, move the insert point before the first terminator.
|
|
|
|
// * After, we have to split the outcoming edges.
|
|
|
|
if (Before) {
|
|
|
|
// Check whether Reg is defined by any terminator.
|
2019-01-08 09:22:47 +08:00
|
|
|
MachineBasicBlock::reverse_iterator It = MI;
|
|
|
|
auto REnd = MI.getParent()->rend();
|
|
|
|
|
|
|
|
for (; It != REnd && It->isTerminator(); ++It) {
|
2019-01-08 20:54:26 +08:00
|
|
|
assert(!It->modifiesRegister(MO.getReg(), &TRI) &&
|
2019-01-08 09:22:47 +08:00
|
|
|
"copy insertion in middle of terminators not handled");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (It == REnd) {
|
|
|
|
addInsertPoint(*MI.getParent()->begin(), true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are sure to be right before the first terminator.
|
|
|
|
addInsertPoint(*It, /*Before*/ false);
|
2016-05-20 08:49:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Make sure Reg is not redefined by other terminators, otherwise
|
|
|
|
// we do not know how to split.
|
|
|
|
for (MachineBasicBlock::iterator It = MI, End = MI.getParent()->end();
|
|
|
|
++It != End;)
|
|
|
|
// The machine verifier should reject this kind of code.
|
2019-01-08 20:54:26 +08:00
|
|
|
assert(It->modifiesRegister(MO.getReg(), &TRI) &&
|
|
|
|
"Do not know where to split");
|
2016-05-20 08:49:10 +08:00
|
|
|
// Split each outcoming edges.
|
|
|
|
MachineBasicBlock &Src = *MI.getParent();
|
|
|
|
for (auto &Succ : Src.successors())
|
|
|
|
addInsertPoint(Src, Succ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegBankSelect::RepairingPlacement::addInsertPoint(MachineInstr &MI,
|
|
|
|
bool Before) {
|
|
|
|
addInsertPoint(*new InstrInsertPoint(MI, Before));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegBankSelect::RepairingPlacement::addInsertPoint(MachineBasicBlock &MBB,
|
|
|
|
bool Beginning) {
|
|
|
|
addInsertPoint(*new MBBInsertPoint(MBB, Beginning));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegBankSelect::RepairingPlacement::addInsertPoint(MachineBasicBlock &Src,
|
|
|
|
MachineBasicBlock &Dst) {
|
|
|
|
addInsertPoint(*new EdgeInsertPoint(Src, Dst, P));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegBankSelect::RepairingPlacement::addInsertPoint(
|
|
|
|
RegBankSelect::InsertPoint &Point) {
|
|
|
|
CanMaterialize &= Point.canMaterialize();
|
|
|
|
HasSplit |= Point.isSplit();
|
|
|
|
InsertPoints.emplace_back(&Point);
|
|
|
|
}
|
|
|
|
|
|
|
|
RegBankSelect::InstrInsertPoint::InstrInsertPoint(MachineInstr &Instr,
|
|
|
|
bool Before)
|
|
|
|
: InsertPoint(), Instr(Instr), Before(Before) {
|
|
|
|
// Since we do not support splitting, we do not need to update
|
|
|
|
// liveness and such, so do not do anything with P.
|
|
|
|
assert((!Before || !Instr.isPHI()) &&
|
|
|
|
"Splitting before phis requires more points");
|
|
|
|
assert((!Before || !Instr.getNextNode() || !Instr.getNextNode()->isPHI()) &&
|
|
|
|
"Splitting between phis does not make sense");
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegBankSelect::InstrInsertPoint::materialize() {
|
|
|
|
if (isSplit()) {
|
|
|
|
// Slice and return the beginning of the new block.
|
|
|
|
// If we need to split between the terminators, we theoritically
|
|
|
|
// need to know where the first and second set of terminators end
|
|
|
|
// to update the successors properly.
|
|
|
|
// Now, in pratice, we should have a maximum of 2 branch
|
|
|
|
// instructions; one conditional and one unconditional. Therefore
|
|
|
|
// we know how to update the successor by looking at the target of
|
|
|
|
// the unconditional branch.
|
|
|
|
// If we end up splitting at some point, then, we should update
|
|
|
|
// the liveness information and such. I.e., we would need to
|
|
|
|
// access P here.
|
|
|
|
// The machine verifier should actually make sure such cases
|
|
|
|
// cannot happen.
|
|
|
|
llvm_unreachable("Not yet implemented");
|
|
|
|
}
|
|
|
|
// Otherwise the insertion point is just the current or next
|
|
|
|
// instruction depending on Before. I.e., there is nothing to do
|
|
|
|
// here.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegBankSelect::InstrInsertPoint::isSplit() const {
|
|
|
|
// If the insertion point is after a terminator, we need to split.
|
|
|
|
if (!Before)
|
|
|
|
return Instr.isTerminator();
|
|
|
|
// If we insert before an instruction that is after a terminator,
|
|
|
|
// we are still after a terminator.
|
|
|
|
return Instr.getPrevNode() && Instr.getPrevNode()->isTerminator();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t RegBankSelect::InstrInsertPoint::frequency(const Pass &P) const {
|
|
|
|
// Even if we need to split, because we insert between terminators,
|
|
|
|
// this split has actually the same frequency as the instruction.
|
|
|
|
const MachineBlockFrequencyInfo *MBFI =
|
|
|
|
P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
|
|
|
|
if (!MBFI)
|
|
|
|
return 1;
|
|
|
|
return MBFI->getBlockFreq(Instr.getParent()).getFrequency();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t RegBankSelect::MBBInsertPoint::frequency(const Pass &P) const {
|
|
|
|
const MachineBlockFrequencyInfo *MBFI =
|
|
|
|
P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
|
|
|
|
if (!MBFI)
|
|
|
|
return 1;
|
|
|
|
return MBFI->getBlockFreq(&MBB).getFrequency();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegBankSelect::EdgeInsertPoint::materialize() {
|
|
|
|
// If we end up repairing twice at the same place before materializing the
|
|
|
|
// insertion point, we may think we have to split an edge twice.
|
|
|
|
// We should have a factory for the insert point such that identical points
|
|
|
|
// are the same instance.
|
|
|
|
assert(Src.isSuccessor(DstOrSplit) && DstOrSplit->isPredecessor(&Src) &&
|
|
|
|
"This point has already been split");
|
|
|
|
MachineBasicBlock *NewBB = Src.SplitCriticalEdge(DstOrSplit, P);
|
|
|
|
assert(NewBB && "Invalid call to materialize");
|
|
|
|
// We reuse the destination block to hold the information of the new block.
|
|
|
|
DstOrSplit = NewBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t RegBankSelect::EdgeInsertPoint::frequency(const Pass &P) const {
|
|
|
|
const MachineBlockFrequencyInfo *MBFI =
|
|
|
|
P.getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
|
|
|
|
if (!MBFI)
|
|
|
|
return 1;
|
|
|
|
if (WasMaterialized)
|
|
|
|
return MBFI->getBlockFreq(DstOrSplit).getFrequency();
|
|
|
|
|
|
|
|
const MachineBranchProbabilityInfo *MBPI =
|
|
|
|
P.getAnalysisIfAvailable<MachineBranchProbabilityInfo>();
|
|
|
|
if (!MBPI)
|
|
|
|
return 1;
|
|
|
|
// The basic block will be on the edge.
|
|
|
|
return (MBFI->getBlockFreq(&Src) * MBPI->getEdgeProbability(&Src, DstOrSplit))
|
|
|
|
.getFrequency();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegBankSelect::EdgeInsertPoint::canMaterialize() const {
|
|
|
|
// If this is not a critical edge, we should not have used this insert
|
|
|
|
// point. Indeed, either the successor or the predecessor should
|
|
|
|
// have do.
|
|
|
|
assert(Src.succ_size() > 1 && DstOrSplit->pred_size() > 1 &&
|
|
|
|
"Edge is not critical");
|
|
|
|
return Src.canSplitCriticalEdge(DstOrSplit);
|
|
|
|
}
|
|
|
|
|
2016-05-20 08:35:26 +08:00
|
|
|
RegBankSelect::MappingCost::MappingCost(const BlockFrequency &LocalFreq)
|
2017-06-27 06:44:03 +08:00
|
|
|
: LocalFreq(LocalFreq.getFrequency()) {}
|
2016-05-20 08:35:26 +08:00
|
|
|
|
|
|
|
bool RegBankSelect::MappingCost::addLocalCost(uint64_t Cost) {
|
|
|
|
// Check if this overflows.
|
|
|
|
if (LocalCost + Cost < LocalCost) {
|
|
|
|
saturate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
LocalCost += Cost;
|
|
|
|
return isSaturated();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegBankSelect::MappingCost::addNonLocalCost(uint64_t Cost) {
|
|
|
|
// Check if this overflows.
|
|
|
|
if (NonLocalCost + Cost < NonLocalCost) {
|
|
|
|
saturate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NonLocalCost += Cost;
|
|
|
|
return isSaturated();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegBankSelect::MappingCost::isSaturated() const {
|
|
|
|
return LocalCost == UINT64_MAX - 1 && NonLocalCost == UINT64_MAX &&
|
|
|
|
LocalFreq == UINT64_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegBankSelect::MappingCost::saturate() {
|
|
|
|
*this = ImpossibleCost();
|
|
|
|
--LocalCost;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegBankSelect::MappingCost RegBankSelect::MappingCost::ImpossibleCost() {
|
|
|
|
return MappingCost(UINT64_MAX, UINT64_MAX, UINT64_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegBankSelect::MappingCost::operator<(const MappingCost &Cost) const {
|
|
|
|
// Sort out the easy cases.
|
|
|
|
if (*this == Cost)
|
|
|
|
return false;
|
|
|
|
// If one is impossible to realize the other is cheaper unless it is
|
|
|
|
// impossible as well.
|
|
|
|
if ((*this == ImpossibleCost()) || (Cost == ImpossibleCost()))
|
|
|
|
return (*this == ImpossibleCost()) < (Cost == ImpossibleCost());
|
|
|
|
// If one is saturated the other is cheaper, unless it is saturated
|
|
|
|
// as well.
|
|
|
|
if (isSaturated() || Cost.isSaturated())
|
|
|
|
return isSaturated() < Cost.isSaturated();
|
|
|
|
// At this point we know both costs hold sensible values.
|
|
|
|
|
|
|
|
// If both values have a different base frequency, there is no much
|
|
|
|
// we can do but to scale everything.
|
|
|
|
// However, if they have the same base frequency we can avoid making
|
|
|
|
// complicated computation.
|
|
|
|
uint64_t ThisLocalAdjust;
|
|
|
|
uint64_t OtherLocalAdjust;
|
|
|
|
if (LLVM_LIKELY(LocalFreq == Cost.LocalFreq)) {
|
|
|
|
|
|
|
|
// At this point, we know the local costs are comparable.
|
|
|
|
// Do the case that do not involve potential overflow first.
|
|
|
|
if (NonLocalCost == Cost.NonLocalCost)
|
|
|
|
// Since the non-local costs do not discriminate on the result,
|
|
|
|
// just compare the local costs.
|
|
|
|
return LocalCost < Cost.LocalCost;
|
|
|
|
|
|
|
|
// The base costs are comparable so we may only keep the relative
|
|
|
|
// value to increase our chances of avoiding overflows.
|
|
|
|
ThisLocalAdjust = 0;
|
|
|
|
OtherLocalAdjust = 0;
|
|
|
|
if (LocalCost < Cost.LocalCost)
|
|
|
|
OtherLocalAdjust = Cost.LocalCost - LocalCost;
|
|
|
|
else
|
|
|
|
ThisLocalAdjust = LocalCost - Cost.LocalCost;
|
|
|
|
} else {
|
|
|
|
ThisLocalAdjust = LocalCost;
|
|
|
|
OtherLocalAdjust = Cost.LocalCost;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The non-local costs are comparable, just keep the relative value.
|
|
|
|
uint64_t ThisNonLocalAdjust = 0;
|
|
|
|
uint64_t OtherNonLocalAdjust = 0;
|
|
|
|
if (NonLocalCost < Cost.NonLocalCost)
|
|
|
|
OtherNonLocalAdjust = Cost.NonLocalCost - NonLocalCost;
|
|
|
|
else
|
|
|
|
ThisNonLocalAdjust = NonLocalCost - Cost.NonLocalCost;
|
|
|
|
// Scale everything to make them comparable.
|
|
|
|
uint64_t ThisScaledCost = ThisLocalAdjust * LocalFreq;
|
|
|
|
// Check for overflow on that operation.
|
|
|
|
bool ThisOverflows = ThisLocalAdjust && (ThisScaledCost < ThisLocalAdjust ||
|
|
|
|
ThisScaledCost < LocalFreq);
|
|
|
|
uint64_t OtherScaledCost = OtherLocalAdjust * Cost.LocalFreq;
|
|
|
|
// Check for overflow on the last operation.
|
|
|
|
bool OtherOverflows =
|
|
|
|
OtherLocalAdjust &&
|
|
|
|
(OtherScaledCost < OtherLocalAdjust || OtherScaledCost < Cost.LocalFreq);
|
|
|
|
// Add the non-local costs.
|
|
|
|
ThisOverflows |= ThisNonLocalAdjust &&
|
|
|
|
ThisScaledCost + ThisNonLocalAdjust < ThisNonLocalAdjust;
|
|
|
|
ThisScaledCost += ThisNonLocalAdjust;
|
|
|
|
OtherOverflows |= OtherNonLocalAdjust &&
|
|
|
|
OtherScaledCost + OtherNonLocalAdjust < OtherNonLocalAdjust;
|
|
|
|
OtherScaledCost += OtherNonLocalAdjust;
|
|
|
|
// If both overflows, we cannot compare without additional
|
|
|
|
// precision, e.g., APInt. Just give up on that case.
|
|
|
|
if (ThisOverflows && OtherOverflows)
|
|
|
|
return false;
|
|
|
|
// If one overflows but not the other, we can still compare.
|
|
|
|
if (ThisOverflows || OtherOverflows)
|
|
|
|
return ThisOverflows < OtherOverflows;
|
|
|
|
// Otherwise, just compare the values.
|
|
|
|
return ThisScaledCost < OtherScaledCost;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegBankSelect::MappingCost::operator==(const MappingCost &Cost) const {
|
|
|
|
return LocalCost == Cost.LocalCost && NonLocalCost == Cost.NonLocalCost &&
|
|
|
|
LocalFreq == Cost.LocalFreq;
|
|
|
|
}
|
2017-01-11 08:48:41 +08:00
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2017-01-28 10:02:38 +08:00
|
|
|
LLVM_DUMP_METHOD void RegBankSelect::MappingCost::dump() const {
|
2017-01-11 08:48:41 +08:00
|
|
|
print(dbgs());
|
|
|
|
dbgs() << '\n';
|
|
|
|
}
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|
2017-01-11 08:48:41 +08:00
|
|
|
|
|
|
|
void RegBankSelect::MappingCost::print(raw_ostream &OS) const {
|
|
|
|
if (*this == ImpossibleCost()) {
|
|
|
|
OS << "impossible";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isSaturated()) {
|
|
|
|
OS << "saturated";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
OS << LocalFreq << " * " << LocalCost << " + " << NonLocalCost;
|
|
|
|
}
|