2017-05-27 09:34:00 +08:00
|
|
|
//===- Localizer.cpp ---------------------- Localize some instrs -*- C++ -*-==//
|
|
|
|
//
|
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
|
2017-05-27 09:34:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// This file implements the Localizer class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/GlobalISel/Localizer.h"
|
|
|
|
#include "llvm/ADT/DenseMap.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/Analysis/TargetTransformInfo.h"
|
2017-05-27 09:34:00 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.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-05-27 09:34:00 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "localizer"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
char Localizer::ID = 0;
|
2019-06-18 07:20:29 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(Localizer, DEBUG_TYPE,
|
|
|
|
"Move/duplicate certain instructions close to their use",
|
|
|
|
false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(Localizer, DEBUG_TYPE,
|
|
|
|
"Move/duplicate certain instructions close to their use",
|
|
|
|
false, false)
|
2017-05-27 09:34:00 +08:00
|
|
|
|
2019-12-06 03:09:50 +08:00
|
|
|
Localizer::Localizer(std::function<bool(const MachineFunction &)> F)
|
|
|
|
: MachineFunctionPass(ID), DoNotRunPass(F) {}
|
|
|
|
|
|
|
|
Localizer::Localizer()
|
|
|
|
: Localizer([](const MachineFunction &) { return false; }) {}
|
2017-05-27 09:34:00 +08:00
|
|
|
|
2019-06-18 07:20:29 +08:00
|
|
|
void Localizer::init(MachineFunction &MF) {
|
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(MF.getFunction());
|
|
|
|
}
|
2017-05-27 09:34:00 +08:00
|
|
|
|
|
|
|
bool Localizer::shouldLocalize(const MachineInstr &MI) {
|
2019-06-18 07:20:29 +08:00
|
|
|
// Assuming a spill and reload of a value has a cost of 1 instruction each,
|
|
|
|
// this helper function computes the maximum number of uses we should consider
|
|
|
|
// for remat. E.g. on arm64 global addresses take 2 insts to materialize. We
|
|
|
|
// break even in terms of code size when the original MI has 2 users vs
|
|
|
|
// choosing to potentially spill. Any more than 2 users we we have a net code
|
|
|
|
// size increase. This doesn't take into account register pressure though.
|
|
|
|
auto maxUses = [](unsigned RematCost) {
|
|
|
|
// A cost of 1 means remats are basically free.
|
|
|
|
if (RematCost == 1)
|
|
|
|
return UINT_MAX;
|
|
|
|
if (RematCost == 2)
|
|
|
|
return 2U;
|
|
|
|
|
|
|
|
// Remat is too expensive, only sink if there's one user.
|
|
|
|
if (RematCost > 2)
|
|
|
|
return 1U;
|
|
|
|
llvm_unreachable("Unexpected remat cost");
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper to walk through uses and terminate if we've reached a limit. Saves
|
|
|
|
// us spending time traversing uses if all we want to know is if it's >= min.
|
|
|
|
auto isUsesAtMost = [&](unsigned Reg, unsigned MaxUses) {
|
|
|
|
unsigned NumUses = 0;
|
|
|
|
auto UI = MRI->use_instr_nodbg_begin(Reg), UE = MRI->use_instr_nodbg_end();
|
|
|
|
for (; UI != UE && NumUses < MaxUses; ++UI) {
|
|
|
|
NumUses++;
|
|
|
|
}
|
|
|
|
// If we haven't reached the end yet then there are more than MaxUses users.
|
|
|
|
return UI == UE;
|
|
|
|
};
|
|
|
|
|
2017-05-27 09:34:00 +08:00
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
// Constants-like instructions should be close to their users.
|
|
|
|
// We don't want long live-ranges for them.
|
|
|
|
case TargetOpcode::G_CONSTANT:
|
|
|
|
case TargetOpcode::G_FCONSTANT:
|
|
|
|
case TargetOpcode::G_FRAME_INDEX:
|
2019-06-21 08:36:19 +08:00
|
|
|
case TargetOpcode::G_INTTOPTR:
|
2017-05-27 09:34:00 +08:00
|
|
|
return true;
|
2019-06-18 07:20:29 +08:00
|
|
|
case TargetOpcode::G_GLOBAL_VALUE: {
|
|
|
|
unsigned RematCost = TTI->getGISelRematGlobalCost();
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MI.getOperand(0).getReg();
|
2019-06-18 07:20:29 +08:00
|
|
|
unsigned MaxUses = maxUses(RematCost);
|
|
|
|
if (MaxUses == UINT_MAX)
|
|
|
|
return true; // Remats are "free" so always localize.
|
|
|
|
bool B = isUsesAtMost(Reg, MaxUses);
|
|
|
|
return B;
|
|
|
|
}
|
2017-05-27 09:34:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 08:08:38 +08:00
|
|
|
void Localizer::getAnalysisUsage(AnalysisUsage &AU) const {
|
2019-06-18 07:20:29 +08:00
|
|
|
AU.addRequired<TargetTransformInfoWrapperPass>();
|
2018-07-13 08:08:38 +08:00
|
|
|
getSelectionDAGFallbackAnalysisUsage(AU);
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2017-05-27 09:34:00 +08:00
|
|
|
bool Localizer::isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
|
|
|
|
MachineBasicBlock *&InsertMBB) {
|
|
|
|
MachineInstr &MIUse = *MOUse.getParent();
|
|
|
|
InsertMBB = MIUse.getParent();
|
|
|
|
if (MIUse.isPHI())
|
|
|
|
InsertMBB = MIUse.getOperand(MIUse.getOperandNo(&MOUse) + 1).getMBB();
|
|
|
|
return InsertMBB == Def.getParent();
|
|
|
|
}
|
|
|
|
|
2019-06-21 08:36:19 +08:00
|
|
|
bool Localizer::localizeInterBlock(MachineFunction &MF,
|
|
|
|
LocalizedSetVecT &LocalizedInstrs) {
|
2019-06-18 07:20:29 +08:00
|
|
|
bool Changed = false;
|
|
|
|
DenseMap<std::pair<MachineBasicBlock *, unsigned>, unsigned> MBBWithLocalDef;
|
|
|
|
|
|
|
|
// Since the IRTranslator only emits constants into the entry block, and the
|
|
|
|
// rest of the GISel pipeline generally emits constants close to their users,
|
|
|
|
// we only localize instructions in the entry block here. This might change if
|
|
|
|
// we start doing CSE across blocks.
|
|
|
|
auto &MBB = MF.front();
|
2019-06-21 08:36:19 +08:00
|
|
|
for (auto RI = MBB.rbegin(), RE = MBB.rend(); RI != RE; ++RI) {
|
|
|
|
MachineInstr &MI = *RI;
|
2019-06-19 06:08:40 +08:00
|
|
|
if (!shouldLocalize(MI))
|
2019-06-18 07:20:29 +08:00
|
|
|
continue;
|
|
|
|
LLVM_DEBUG(dbgs() << "Should localize: " << MI);
|
|
|
|
assert(MI.getDesc().getNumDefs() == 1 &&
|
|
|
|
"More than one definition not supported yet");
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MI.getOperand(0).getReg();
|
2019-06-18 07:20:29 +08:00
|
|
|
// Check if all the users of MI are local.
|
|
|
|
// We are going to invalidation the list of use operands, so we
|
|
|
|
// can't use range iterator.
|
|
|
|
for (auto MOIt = MRI->use_begin(Reg), MOItEnd = MRI->use_end();
|
|
|
|
MOIt != MOItEnd;) {
|
|
|
|
MachineOperand &MOUse = *MOIt++;
|
|
|
|
// Check if the use is already local.
|
|
|
|
MachineBasicBlock *InsertMBB;
|
|
|
|
LLVM_DEBUG(MachineInstr &MIUse = *MOUse.getParent();
|
|
|
|
dbgs() << "Checking use: " << MIUse
|
|
|
|
<< " #Opd: " << MIUse.getOperandNo(&MOUse) << '\n');
|
|
|
|
if (isLocalUse(MOUse, MI, InsertMBB))
|
|
|
|
continue;
|
|
|
|
LLVM_DEBUG(dbgs() << "Fixing non-local use\n");
|
|
|
|
Changed = true;
|
|
|
|
auto MBBAndReg = std::make_pair(InsertMBB, Reg);
|
|
|
|
auto NewVRegIt = MBBWithLocalDef.find(MBBAndReg);
|
|
|
|
if (NewVRegIt == MBBWithLocalDef.end()) {
|
|
|
|
// Create the localized instruction.
|
|
|
|
MachineInstr *LocalizedMI = MF.CloneMachineInstr(&MI);
|
|
|
|
LocalizedInstrs.insert(LocalizedMI);
|
|
|
|
MachineInstr &UseMI = *MOUse.getParent();
|
|
|
|
if (MRI->hasOneUse(Reg) && !UseMI.isPHI())
|
|
|
|
InsertMBB->insert(InsertMBB->SkipPHIsAndLabels(UseMI), LocalizedMI);
|
|
|
|
else
|
|
|
|
InsertMBB->insert(InsertMBB->SkipPHIsAndLabels(InsertMBB->begin()),
|
|
|
|
LocalizedMI);
|
|
|
|
|
|
|
|
// Set a new register for the definition.
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register NewReg = MRI->createGenericVirtualRegister(MRI->getType(Reg));
|
2019-06-18 07:20:29 +08:00
|
|
|
MRI->setRegClassOrRegBank(NewReg, MRI->getRegClassOrRegBank(Reg));
|
|
|
|
LocalizedMI->getOperand(0).setReg(NewReg);
|
|
|
|
NewVRegIt =
|
|
|
|
MBBWithLocalDef.insert(std::make_pair(MBBAndReg, NewReg)).first;
|
|
|
|
LLVM_DEBUG(dbgs() << "Inserted: " << *LocalizedMI);
|
|
|
|
}
|
|
|
|
LLVM_DEBUG(dbgs() << "Update use with: " << printReg(NewVRegIt->second)
|
|
|
|
<< '\n');
|
|
|
|
// Update the user reg.
|
|
|
|
MOUse.setReg(NewVRegIt->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2019-06-21 08:36:19 +08:00
|
|
|
bool Localizer::localizeIntraBlock(LocalizedSetVecT &LocalizedInstrs) {
|
2019-06-18 07:20:29 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// For each already-localized instruction which has multiple users, then we
|
|
|
|
// scan the block top down from the current position until we hit one of them.
|
|
|
|
|
|
|
|
// FIXME: Consider doing inst duplication if live ranges are very long due to
|
|
|
|
// many users, but this case may be better served by regalloc improvements.
|
|
|
|
|
|
|
|
for (MachineInstr *MI : LocalizedInstrs) {
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MI->getOperand(0).getReg();
|
2019-06-18 07:20:29 +08:00
|
|
|
MachineBasicBlock &MBB = *MI->getParent();
|
|
|
|
// All of the user MIs of this reg.
|
|
|
|
SmallPtrSet<MachineInstr *, 32> Users;
|
2019-06-21 08:36:19 +08:00
|
|
|
for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
|
|
|
|
if (!UseMI.isPHI())
|
|
|
|
Users.insert(&UseMI);
|
|
|
|
}
|
|
|
|
// If all the users were PHIs then they're not going to be in our block,
|
|
|
|
// don't try to move this instruction.
|
|
|
|
if (Users.empty())
|
|
|
|
continue;
|
2019-06-18 07:20:29 +08:00
|
|
|
|
|
|
|
MachineBasicBlock::iterator II(MI);
|
|
|
|
++II;
|
|
|
|
while (II != MBB.end() && !Users.count(&*II))
|
|
|
|
++II;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Intra-block: moving " << *MI << " before " << *&*II
|
|
|
|
<< "\n");
|
|
|
|
assert(II != MBB.end() && "Didn't find the user in the MBB");
|
|
|
|
MI->removeFromParent();
|
|
|
|
MBB.insert(II, MI);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-05-27 09:34:00 +08:00
|
|
|
bool Localizer::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
// If the ISel pipeline failed, do not bother running that pass.
|
|
|
|
if (MF.getProperties().hasProperty(
|
|
|
|
MachineFunctionProperties::Property::FailedISel))
|
|
|
|
return false;
|
|
|
|
|
2019-12-06 03:09:50 +08:00
|
|
|
// Don't run the pass if the target asked so.
|
|
|
|
if (DoNotRunPass(MF))
|
|
|
|
return false;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Localize instructions for: " << MF.getName() << '\n');
|
2017-05-27 09:34:00 +08:00
|
|
|
|
|
|
|
init(MF);
|
|
|
|
|
2019-06-18 07:20:29 +08:00
|
|
|
// Keep track of the instructions we localized. We'll do a second pass of
|
|
|
|
// intra-block localization to further reduce live ranges.
|
2019-06-21 08:36:19 +08:00
|
|
|
LocalizedSetVecT LocalizedInstrs;
|
2017-05-27 09:34:00 +08:00
|
|
|
|
2019-06-18 07:20:29 +08:00
|
|
|
bool Changed = localizeInterBlock(MF, LocalizedInstrs);
|
2019-09-23 19:38:10 +08:00
|
|
|
Changed |= localizeIntraBlock(LocalizedInstrs);
|
|
|
|
return Changed;
|
2017-05-27 09:34:00 +08:00
|
|
|
}
|