2017-11-03 07:37:32 +08:00
|
|
|
//===-------------- MIRCanonicalizer.cpp - MIR Canonicalizer --------------===//
|
|
|
|
//
|
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-11-03 07:37:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The purpose of this pass is to employ a canonical code transformation so
|
|
|
|
// that code compiled with slightly different IR passes can be diffed more
|
|
|
|
// effectively than otherwise. This is done by renaming vregs in a given
|
|
|
|
// LiveRange in a canonical way. This pass also does a pseudo-scheduling to
|
|
|
|
// move defs closer to their use inorder to reduce diffs caused by slightly
|
|
|
|
// different schedules.
|
|
|
|
//
|
|
|
|
// Basic Usage:
|
|
|
|
//
|
|
|
|
// llc -o - -run-pass mir-canonicalizer example.mir
|
|
|
|
//
|
|
|
|
// Reorders instructions canonically.
|
|
|
|
// Renames virtual register operands canonically.
|
|
|
|
// Strips certain MIR artifacts (optionally).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-05 05:29:10 +08:00
|
|
|
#include "MIRVRegNamerUtils.h"
|
2017-11-03 07:37:32 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#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"
|
2022-03-15 17:54:19 +08:00
|
|
|
#include "llvm/Pass.h"
|
2019-10-19 08:22:07 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-11-03 07:37:32 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "mir-canonicalizer"
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
2018-04-16 16:12:15 +08:00
|
|
|
CanonicalizeFunctionNumber("canon-nth-function", cl::Hidden, cl::init(~0u),
|
|
|
|
cl::value_desc("N"),
|
|
|
|
cl::desc("Function number to canonicalize."));
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MIRCanonicalizer : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
MIRCanonicalizer() : MachineFunctionPass(ID) {}
|
|
|
|
|
|
|
|
StringRef getPassName() const override {
|
|
|
|
return "Rename register operands in a canonical ordering.";
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char MIRCanonicalizer::ID;
|
|
|
|
|
|
|
|
char &llvm::MIRCanonicalizerID = MIRCanonicalizer::ID;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(MIRCanonicalizer, "mir-canonicalizer",
|
2017-11-04 02:02:46 +08:00
|
|
|
"Rename Register Operands Canonically", false, false)
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
INITIALIZE_PASS_END(MIRCanonicalizer, "mir-canonicalizer",
|
2017-11-04 02:02:46 +08:00
|
|
|
"Rename Register Operands Canonically", false, false)
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
static std::vector<MachineBasicBlock *> GetRPOList(MachineFunction &MF) {
|
2019-05-31 05:37:25 +08:00
|
|
|
if (MF.empty())
|
|
|
|
return {};
|
2017-11-03 07:37:32 +08:00
|
|
|
ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
|
|
|
|
std::vector<MachineBasicBlock *> RPOList;
|
2021-01-22 11:59:46 +08:00
|
|
|
append_range(RPOList, RPOT);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
return RPOList;
|
|
|
|
}
|
|
|
|
|
2018-03-31 13:48:51 +08:00
|
|
|
static bool
|
|
|
|
rescheduleLexographically(std::vector<MachineInstr *> instructions,
|
|
|
|
MachineBasicBlock *MBB,
|
|
|
|
std::function<MachineBasicBlock::iterator()> getPos) {
|
|
|
|
|
|
|
|
bool Changed = false;
|
2018-05-13 14:07:20 +08:00
|
|
|
using StringInstrPair = std::pair<std::string, MachineInstr *>;
|
|
|
|
std::vector<StringInstrPair> StringInstrMap;
|
2018-03-31 13:48:51 +08:00
|
|
|
|
|
|
|
for (auto *II : instructions) {
|
|
|
|
std::string S;
|
|
|
|
raw_string_ostream OS(S);
|
|
|
|
II->print(OS);
|
|
|
|
OS.flush();
|
|
|
|
|
2019-10-29 20:54:20 +08:00
|
|
|
// Trim the assignment, or start from the beginning in the case of a store.
|
2020-12-17 15:28:32 +08:00
|
|
|
const size_t i = S.find('=');
|
2018-05-13 14:07:20 +08:00
|
|
|
StringInstrMap.push_back({(i == std::string::npos) ? S : S.substr(i), II});
|
2018-03-31 13:48:51 +08:00
|
|
|
}
|
|
|
|
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 10:13:45 +08:00
|
|
|
llvm::sort(StringInstrMap,
|
|
|
|
[](const StringInstrPair &a, const StringInstrPair &b) -> bool {
|
|
|
|
return (a.first < b.first);
|
|
|
|
});
|
2018-05-13 14:07:20 +08:00
|
|
|
|
2018-03-31 13:48:51 +08:00
|
|
|
for (auto &II : StringInstrMap) {
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2018-03-31 13:48:51 +08:00
|
|
|
dbgs() << "Splicing ";
|
|
|
|
II.second->dump();
|
|
|
|
dbgs() << " right before: ";
|
|
|
|
getPos()->dump();
|
|
|
|
});
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
MBB->splice(getPos(), MBB, II.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount,
|
|
|
|
MachineBasicBlock *MBB) {
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
2019-10-29 20:54:20 +08:00
|
|
|
// Calculates the distance of MI from the beginning of its parent BB.
|
2017-11-03 07:37:32 +08:00
|
|
|
auto getInstrIdx = [](const MachineInstr &MI) {
|
|
|
|
unsigned i = 0;
|
|
|
|
for (auto &CurMI : *MI.getParent()) {
|
|
|
|
if (&CurMI == &MI)
|
|
|
|
return i;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return ~0U;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Pre-Populate vector of instructions to reschedule so that we don't
|
|
|
|
// clobber the iterator.
|
|
|
|
std::vector<MachineInstr *> Instructions;
|
|
|
|
for (auto &MI : *MBB) {
|
|
|
|
Instructions.push_back(&MI);
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:08:15 +08:00
|
|
|
std::map<MachineInstr *, std::vector<MachineInstr *>> MultiUsers;
|
2019-06-11 08:00:25 +08:00
|
|
|
std::map<unsigned, MachineInstr *> MultiUserLookup;
|
|
|
|
unsigned UseToBringDefCloserToCount = 0;
|
2018-03-31 13:48:51 +08:00
|
|
|
std::vector<MachineInstr *> PseudoIdempotentInstructions;
|
|
|
|
std::vector<unsigned> PhysRegDefs;
|
|
|
|
for (auto *II : Instructions) {
|
|
|
|
for (unsigned i = 1; i < II->getNumOperands(); i++) {
|
|
|
|
MachineOperand &MO = II->getOperand(i);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isVirtualRegister(MO.getReg()))
|
2018-03-31 13:48:51 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!MO.isDef())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PhysRegDefs.push_back(MO.getReg());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 07:37:32 +08:00
|
|
|
for (auto *II : Instructions) {
|
|
|
|
if (II->getNumOperands() == 0)
|
|
|
|
continue;
|
2018-03-31 13:48:51 +08:00
|
|
|
if (II->mayLoadOrStore())
|
|
|
|
continue;
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
MachineOperand &MO = II->getOperand(0);
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg()))
|
2017-11-03 07:37:32 +08:00
|
|
|
continue;
|
2018-03-31 13:48:51 +08:00
|
|
|
if (!MO.isDef())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool IsPseudoIdempotent = true;
|
|
|
|
for (unsigned i = 1; i < II->getNumOperands(); i++) {
|
|
|
|
|
|
|
|
if (II->getOperand(i).isImm()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (II->getOperand(i).isReg()) {
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(II->getOperand(i).getReg()))
|
2020-11-20 14:07:55 +08:00
|
|
|
if (!llvm::is_contained(PhysRegDefs, II->getOperand(i).getReg())) {
|
2018-03-31 13:48:51 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IsPseudoIdempotent = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPseudoIdempotent) {
|
|
|
|
PseudoIdempotentInstructions.push_back(II);
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-03 07:37:32 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Operand " << 0 << " of "; II->dump(); MO.dump(););
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
MachineInstr *Def = II;
|
|
|
|
unsigned Distance = ~0U;
|
|
|
|
MachineInstr *UseToBringDefCloserTo = nullptr;
|
|
|
|
MachineRegisterInfo *MRI = &MBB->getParent()->getRegInfo();
|
|
|
|
for (auto &UO : MRI->use_nodbg_operands(MO.getReg())) {
|
|
|
|
MachineInstr *UseInst = UO.getParent();
|
|
|
|
|
|
|
|
const unsigned DefLoc = getInstrIdx(*Def);
|
|
|
|
const unsigned UseLoc = getInstrIdx(*UseInst);
|
|
|
|
const unsigned Delta = (UseLoc - DefLoc);
|
|
|
|
|
|
|
|
if (UseInst->getParent() != Def->getParent())
|
|
|
|
continue;
|
|
|
|
if (DefLoc >= UseLoc)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Delta < Distance) {
|
|
|
|
Distance = Delta;
|
|
|
|
UseToBringDefCloserTo = UseInst;
|
2019-06-11 08:00:25 +08:00
|
|
|
MultiUserLookup[UseToBringDefCloserToCount++] = UseToBringDefCloserTo;
|
2017-11-03 07:37:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto BBE = MBB->instr_end();
|
|
|
|
MachineBasicBlock::iterator DefI = BBE;
|
|
|
|
MachineBasicBlock::iterator UseI = BBE;
|
|
|
|
|
|
|
|
for (auto BBI = MBB->instr_begin(); BBI != BBE; ++BBI) {
|
|
|
|
|
|
|
|
if (DefI != BBE && UseI != BBE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (&*BBI == Def) {
|
|
|
|
DefI = BBI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (&*BBI == UseToBringDefCloserTo) {
|
|
|
|
UseI = BBI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DefI == BBE || UseI == BBE)
|
|
|
|
continue;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2017-11-03 07:37:32 +08:00
|
|
|
dbgs() << "Splicing ";
|
|
|
|
DefI->dump();
|
|
|
|
dbgs() << " right before: ";
|
|
|
|
UseI->dump();
|
|
|
|
});
|
|
|
|
|
2018-04-05 08:08:15 +08:00
|
|
|
MultiUsers[UseToBringDefCloserTo].push_back(Def);
|
2017-11-03 07:37:32 +08:00
|
|
|
Changed = true;
|
|
|
|
MBB->splice(UseI, MBB, DefI);
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:08:15 +08:00
|
|
|
// Sort the defs for users of multiple defs lexographically.
|
2019-06-11 08:00:25 +08:00
|
|
|
for (const auto &E : MultiUserLookup) {
|
2018-04-05 08:08:15 +08:00
|
|
|
|
2021-01-11 01:24:53 +08:00
|
|
|
auto UseI = llvm::find_if(MBB->instrs(), [&](MachineInstr &MI) -> bool {
|
|
|
|
return &MI == E.second;
|
|
|
|
});
|
2018-04-05 08:08:15 +08:00
|
|
|
|
|
|
|
if (UseI == MBB->instr_end())
|
|
|
|
continue;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Rescheduling Multi-Use Instructions Lexographically.";);
|
2018-04-05 08:08:15 +08:00
|
|
|
Changed |= rescheduleLexographically(
|
2019-06-11 08:00:25 +08:00
|
|
|
MultiUsers[E.second], MBB,
|
|
|
|
[&]() -> MachineBasicBlock::iterator { return UseI; });
|
2018-04-05 08:08:15 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 13:48:51 +08:00
|
|
|
PseudoIdempotentInstCount = PseudoIdempotentInstructions.size();
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Rescheduling Idempotent Instructions Lexographically.";);
|
2018-03-31 13:48:51 +08:00
|
|
|
Changed |= rescheduleLexographically(
|
|
|
|
PseudoIdempotentInstructions, MBB,
|
|
|
|
[&]() -> MachineBasicBlock::iterator { return MBB->begin(); });
|
|
|
|
|
2017-11-03 07:37:32 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2018-05-16 05:26:47 +08:00
|
|
|
static bool propagateLocalCopies(MachineBasicBlock *MBB) {
|
2018-04-16 17:03:03 +08:00
|
|
|
bool Changed = false;
|
|
|
|
MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
|
|
|
|
|
|
|
|
std::vector<MachineInstr *> Copies;
|
|
|
|
for (MachineInstr &MI : MBB->instrs()) {
|
|
|
|
if (MI.isCopy())
|
|
|
|
Copies.push_back(&MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (MachineInstr *MI : Copies) {
|
|
|
|
|
|
|
|
if (!MI->getOperand(0).isReg())
|
|
|
|
continue;
|
|
|
|
if (!MI->getOperand(1).isReg())
|
|
|
|
continue;
|
|
|
|
|
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
|
|
|
const Register Dst = MI->getOperand(0).getReg();
|
|
|
|
const Register Src = MI->getOperand(1).getReg();
|
2018-04-16 17:03:03 +08:00
|
|
|
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(Dst))
|
2018-04-16 17:03:03 +08:00
|
|
|
continue;
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(Src))
|
2018-04-16 17:03:03 +08:00
|
|
|
continue;
|
2019-05-31 12:49:58 +08:00
|
|
|
// Not folding COPY instructions if regbankselect has not set the RCs.
|
|
|
|
// Why are we only considering Register Classes? Because the verifier
|
|
|
|
// sometimes gets upset if the register classes don't match even if the
|
|
|
|
// types do. A future patch might add COPY folding for matching types in
|
|
|
|
// pre-registerbankselect code.
|
|
|
|
if (!MRI.getRegClassOrNull(Dst))
|
|
|
|
continue;
|
2018-04-16 17:03:03 +08:00
|
|
|
if (MRI.getRegClass(Dst) != MRI.getRegClass(Src))
|
|
|
|
continue;
|
|
|
|
|
2019-05-31 12:49:58 +08:00
|
|
|
std::vector<MachineOperand *> Uses;
|
2021-11-12 14:28:55 +08:00
|
|
|
for (MachineOperand &MO : MRI.use_operands(Dst))
|
|
|
|
Uses.push_back(&MO);
|
2019-05-31 12:49:58 +08:00
|
|
|
for (auto *MO : Uses)
|
2018-04-16 17:03:03 +08:00
|
|
|
MO->setReg(Src);
|
|
|
|
|
2019-05-31 12:49:58 +08:00
|
|
|
Changed = true;
|
2018-04-16 17:03:03 +08:00
|
|
|
MI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-11-03 07:37:32 +08:00
|
|
|
static bool doDefKillClear(MachineBasicBlock *MBB) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
for (auto &MI : *MBB) {
|
|
|
|
for (auto &MO : MI.operands()) {
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
if (!MO.isDef() && MO.isKill()) {
|
|
|
|
Changed = true;
|
|
|
|
MO.setIsKill(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MO.isDef() && MO.isDead()) {
|
|
|
|
Changed = true;
|
|
|
|
MO.setIsDead(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool runOnBasicBlock(MachineBasicBlock *MBB,
|
2019-12-01 08:00:10 +08:00
|
|
|
unsigned BasicBlockNum, VRegRenamer &Renamer) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2017-11-03 07:37:32 +08:00
|
|
|
dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << " \n\n";
|
|
|
|
dbgs() << "\n\n================================================\n\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n\n NEW BASIC BLOCK: " << MBB->getName() << "\n\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MBB Before Canonical Copy Propagation:\n";
|
|
|
|
MBB->dump(););
|
2018-04-16 17:03:03 +08:00
|
|
|
Changed |= propagateLocalCopies(MBB);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MBB After Canonical Copy Propagation:\n"; MBB->dump(););
|
2018-04-16 17:03:03 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MBB Before Scheduling:\n"; MBB->dump(););
|
2018-03-31 13:48:51 +08:00
|
|
|
unsigned IdempotentInstCount = 0;
|
|
|
|
Changed |= rescheduleCanonically(IdempotentInstCount, MBB);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MBB After Scheduling:\n"; MBB->dump(););
|
2017-11-03 07:37:32 +08:00
|
|
|
|
2019-12-01 08:00:10 +08:00
|
|
|
Changed |= Renamer.renameVRegs(MBB, BasicBlockNum);
|
2018-03-31 13:48:51 +08:00
|
|
|
|
2019-12-01 08:00:10 +08:00
|
|
|
// TODO: Consider dropping this. Dropping kill defs is probably not
|
|
|
|
// semantically sound.
|
2017-11-03 07:37:32 +08:00
|
|
|
Changed |= doDefKillClear(MBB);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Updated MachineBasicBlock:\n"; MBB->dump();
|
|
|
|
dbgs() << "\n";);
|
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "\n\n================================================\n\n");
|
2017-11-03 07:37:32 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MIRCanonicalizer::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
|
|
|
|
static unsigned functionNum = 0;
|
|
|
|
if (CanonicalizeFunctionNumber != ~0U) {
|
|
|
|
if (CanonicalizeFunctionNumber != functionNum++)
|
|
|
|
return false;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n Canonicalizing Function " << MF.getName()
|
|
|
|
<< "\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// we need a valid vreg to create a vreg type for skipping all those
|
|
|
|
// stray vreg numbers so reach alignment/canonical vreg values.
|
2018-04-16 16:12:15 +08:00
|
|
|
std::vector<MachineBasicBlock *> RPOList = GetRPOList(MF);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "\n\n NEW MACHINE FUNCTION: " << MF.getName() << " \n\n";
|
|
|
|
dbgs() << "\n\n================================================\n\n";
|
|
|
|
dbgs() << "Total Basic Blocks: " << RPOList.size() << "\n";
|
|
|
|
for (auto MBB
|
|
|
|
: RPOList) { dbgs() << MBB->getName() << "\n"; } dbgs()
|
|
|
|
<< "\n\n================================================\n\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
unsigned BBNum = 0;
|
|
|
|
bool Changed = false;
|
2018-04-05 08:27:15 +08:00
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
[MirNamer][Canonicalizer]: Perform instruction semantic based renaming
https://reviews.llvm.org/D70210
Previously:
Due to sensitivity of the algorithm with gaps, and extra instructions,
when diffing, often we see naming being off by a few. Makes the diff
unreadable even for tests with 7 and 8 instructions respectively.
Naming can change depending on candidates (and order of picking
candidates). Suddenly if there's one extra instruction somewhere, the
entire subtree would be named completely differently.
No consistent naming of similar instructions which occur in different
functions. If we try to do something like count the frequency
distribution of various differences across suite, then the above
sensitivity issues are going to result in poor results.
Instead:
Name instruction based on semantics of the instruction (hash of the
opcode and operands). Essentially for a given instruction that occurs in
any module/function it'll be named similarly (ie semantic). This has
some nice properties
Can easily look at many instructions and just check the hash and if
they're named similarly, then it's the same instruction. Makes it very
easy to spot the same instruction both multiple times, as well as across
many functions (useful for frequency distribution).
Independent of traversal/candidates/depth of graph. No need to keep
track of last index/gaps/skip count etc.
No off by few issues with diffs. I've tried the old vs new
implementation in files ranging from 30 to 700 instructions. In both
cases with the old algorithm, diffs are a sea of red, where as for the
semantic version, in both cases, the diffs line up beautifully.
Simplified implementation of the main loop (simple iteration) , no keep
track of what's visited and not.
Handle collision just by incrementing a counter. Roughly
bb[N]_hash_[CollisionCount].
Additionally with the new implementation, we can probably avoid doing
the hoisting of instructions to various places, as they'll likely be
named the same resulting in differences only based on collision (ie
regardless of whether the instruction is hoisted or not/close to use or
not, it'll be named the same hash which should result in use of the
instruction be identical with the only change being the collision count)
which is very easy to spot visually.
2019-11-16 00:23:32 +08:00
|
|
|
VRegRenamer Renamer(MRI);
|
2017-11-03 07:37:32 +08:00
|
|
|
for (auto MBB : RPOList)
|
2019-12-01 08:00:10 +08:00
|
|
|
Changed |= runOnBasicBlock(MBB, BBNum++, Renamer);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|