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).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2017-11-03 07:37:32 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
extern char &MIRCanonicalizerID;
|
|
|
|
} // 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
|
|
|
|
2018-04-16 16:12:15 +08:00
|
|
|
static cl::opt<unsigned> CanonicalizeBasicBlockNumber(
|
|
|
|
"canon-nth-basicblock", cl::Hidden, cl::init(~0u), cl::value_desc("N"),
|
|
|
|
cl::desc("BasicBlock 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
|
|
|
|
|
|
|
|
enum VRType { RSE_Reg = 0, RSE_FrameIndex, RSE_NewCandidate };
|
|
|
|
class TypedVReg {
|
|
|
|
VRType type;
|
|
|
|
unsigned reg;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TypedVReg(unsigned reg) : type(RSE_Reg), reg(reg) {}
|
|
|
|
TypedVReg(VRType type) : type(type), reg(~0U) {
|
|
|
|
assert(type != RSE_Reg && "Expected a non-register type.");
|
|
|
|
}
|
|
|
|
|
2018-04-16 16:12:15 +08:00
|
|
|
bool isReg() const { return type == RSE_Reg; }
|
|
|
|
bool isFrameIndex() const { return type == RSE_FrameIndex; }
|
|
|
|
bool isCandidate() const { return type == RSE_NewCandidate; }
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
VRType getType() const { return type; }
|
|
|
|
unsigned getReg() const {
|
|
|
|
assert(this->isReg() && "Expected a virtual or physical register.");
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
for (auto MBB : RPOT) {
|
|
|
|
RPOList.push_back(MBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Trim the assignment, or start from the begining in the case of a store.
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Calculates the distance of MI from the begining of its parent BB.
|
|
|
|
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()))
|
2018-03-31 13:48:51 +08:00
|
|
|
if (llvm::find(PhysRegDefs, II->getOperand(i).getReg()) ==
|
2018-04-16 16:12:15 +08:00
|
|
|
PhysRegDefs.end()) {
|
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
|
|
|
|
|
|
|
auto UseI =
|
|
|
|
std::find_if(MBB->instr_begin(), MBB->instr_end(),
|
2019-06-11 08:00:25 +08:00
|
|
|
[&](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;
|
|
|
|
|
|
|
|
const unsigned Dst = MI->getOperand(0).getReg();
|
|
|
|
const unsigned Src = MI->getOperand(1).getReg();
|
|
|
|
|
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;
|
|
|
|
for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI)
|
|
|
|
Uses.push_back(&*UI);
|
|
|
|
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
|
|
|
/// Here we find our candidates. What makes an interesting candidate?
|
|
|
|
/// An candidate for a canonicalization tree root is normally any kind of
|
|
|
|
/// instruction that causes side effects such as a store to memory or a copy to
|
|
|
|
/// a physical register or a return instruction. We use these as an expression
|
|
|
|
/// tree root that we walk inorder to build a canonical walk which should result
|
|
|
|
/// in canoncal vreg renaming.
|
|
|
|
static std::vector<MachineInstr *> populateCandidates(MachineBasicBlock *MBB) {
|
|
|
|
std::vector<MachineInstr *> Candidates;
|
|
|
|
MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
|
|
|
|
|
|
|
|
for (auto II = MBB->begin(), IE = MBB->end(); II != IE; ++II) {
|
|
|
|
MachineInstr *MI = &*II;
|
|
|
|
|
|
|
|
bool DoesMISideEffect = false;
|
|
|
|
|
|
|
|
if (MI->getNumOperands() > 0 && MI->getOperand(0).isReg()) {
|
|
|
|
const unsigned Dst = MI->getOperand(0).getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
DoesMISideEffect |= !Register::isVirtualRegister(Dst);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) {
|
2018-04-16 16:12:15 +08:00
|
|
|
if (DoesMISideEffect)
|
|
|
|
break;
|
2017-11-03 07:37:32 +08:00
|
|
|
DoesMISideEffect |= (UI->getParent()->getParent() != MI->getParent());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MI->mayStore() && !MI->isBranch() && !DoesMISideEffect)
|
|
|
|
continue;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Found Candidate: "; MI->dump(););
|
2017-11-03 07:37:32 +08:00
|
|
|
Candidates.push_back(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Candidates;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:55:41 +08:00
|
|
|
static void doCandidateWalk(std::vector<TypedVReg> &VRegs,
|
|
|
|
std::queue<TypedVReg> &RegQueue,
|
|
|
|
std::vector<MachineInstr *> &VisitedMIs,
|
|
|
|
const MachineBasicBlock *MBB) {
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
const MachineFunction &MF = *MBB->getParent();
|
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
|
|
|
|
while (!RegQueue.empty()) {
|
|
|
|
|
|
|
|
auto TReg = RegQueue.front();
|
|
|
|
RegQueue.pop();
|
|
|
|
|
|
|
|
if (TReg.isFrameIndex()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Popping frame index.\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
VRegs.push_back(TypedVReg(RSE_FrameIndex));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(TReg.isReg() && "Expected vreg or physreg.");
|
|
|
|
unsigned Reg = TReg.getReg();
|
|
|
|
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isVirtualRegister(Reg)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2017-11-03 07:37:32 +08:00
|
|
|
dbgs() << "Popping vreg ";
|
|
|
|
MRI.def_begin(Reg)->dump();
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!llvm::any_of(VRegs, [&](const TypedVReg &TR) {
|
|
|
|
return TR.isReg() && TR.getReg() == Reg;
|
|
|
|
})) {
|
|
|
|
VRegs.push_back(TypedVReg(Reg));
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Popping physreg.\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
VRegs.push_back(TypedVReg(Reg));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto RI = MRI.def_begin(Reg), RE = MRI.def_end(); RI != RE; ++RI) {
|
|
|
|
MachineInstr *Def = RI->getParent();
|
|
|
|
|
|
|
|
if (Def->getParent() != MBB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (llvm::any_of(VisitedMIs,
|
|
|
|
[&](const MachineInstr *VMI) { return Def == VMI; })) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2017-11-03 07:37:32 +08:00
|
|
|
dbgs() << "\n========================\n";
|
|
|
|
dbgs() << "Visited MI: ";
|
|
|
|
Def->dump();
|
|
|
|
dbgs() << "BB Name: " << Def->getParent()->getName() << "\n";
|
|
|
|
dbgs() << "\n========================\n";
|
|
|
|
});
|
|
|
|
VisitedMIs.push_back(Def);
|
|
|
|
for (unsigned I = 1, E = Def->getNumOperands(); I != E; ++I) {
|
|
|
|
|
|
|
|
MachineOperand &MO = Def->getOperand(I);
|
|
|
|
if (MO.isFI()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Pushing frame index.\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
RegQueue.push(TypedVReg(RSE_FrameIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
RegQueue.push(TypedVReg(MO.getReg()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 05:26:47 +08:00
|
|
|
namespace {
|
2018-04-05 08:27:15 +08:00
|
|
|
class NamedVRegCursor {
|
2018-04-16 16:12:15 +08:00
|
|
|
MachineRegisterInfo &MRI;
|
|
|
|
unsigned virtualVRegNumber;
|
2018-04-05 08:27:15 +08:00
|
|
|
|
2018-04-16 16:12:15 +08:00
|
|
|
public:
|
2019-05-31 14:02:38 +08:00
|
|
|
NamedVRegCursor(MachineRegisterInfo &MRI) : MRI(MRI), virtualVRegNumber(0) {}
|
2018-04-05 08:27:15 +08:00
|
|
|
|
2018-04-16 16:12:15 +08:00
|
|
|
void SkipVRegs() {
|
|
|
|
unsigned VRegGapIndex = 1;
|
2019-05-31 14:02:38 +08:00
|
|
|
if (!virtualVRegNumber) {
|
|
|
|
VRegGapIndex = 0;
|
|
|
|
virtualVRegNumber = MRI.createIncompleteVirtualRegister();
|
|
|
|
}
|
2018-04-16 16:12:15 +08:00
|
|
|
const unsigned VR_GAP = (++VRegGapIndex * 1000);
|
2018-04-05 08:27:15 +08:00
|
|
|
|
2018-04-16 16:12:15 +08:00
|
|
|
unsigned I = virtualVRegNumber;
|
|
|
|
const unsigned E = (((I + VR_GAP) / VR_GAP) + 1) * VR_GAP;
|
2018-04-05 08:27:15 +08:00
|
|
|
|
2018-04-16 16:12:15 +08:00
|
|
|
virtualVRegNumber = E;
|
|
|
|
}
|
2018-04-05 08:27:15 +08:00
|
|
|
|
2018-04-16 16:12:15 +08:00
|
|
|
unsigned getVirtualVReg() const { return virtualVRegNumber; }
|
2018-04-05 08:27:15 +08:00
|
|
|
|
2018-04-16 16:12:15 +08:00
|
|
|
unsigned incrementVirtualVReg(unsigned incr = 1) {
|
|
|
|
virtualVRegNumber += incr;
|
|
|
|
return virtualVRegNumber;
|
|
|
|
}
|
2018-04-05 08:27:15 +08:00
|
|
|
|
2019-05-31 02:06:28 +08:00
|
|
|
unsigned createVirtualRegister(unsigned VReg) {
|
2019-05-31 14:02:38 +08:00
|
|
|
if (!virtualVRegNumber)
|
|
|
|
SkipVRegs();
|
2018-04-16 16:12:15 +08:00
|
|
|
std::string S;
|
|
|
|
raw_string_ostream OS(S);
|
|
|
|
OS << "namedVReg" << (virtualVRegNumber & ~0x80000000);
|
|
|
|
OS.flush();
|
|
|
|
virtualVRegNumber++;
|
2019-05-31 02:06:28 +08:00
|
|
|
if (auto RC = MRI.getRegClassOrNull(VReg))
|
|
|
|
return MRI.createVirtualRegister(RC, OS.str());
|
|
|
|
return MRI.createGenericVirtualRegister(MRI.getType(VReg), OS.str());
|
2018-04-16 16:12:15 +08:00
|
|
|
}
|
2018-04-05 08:27:15 +08:00
|
|
|
};
|
2018-05-16 05:26:47 +08:00
|
|
|
} // namespace
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
static std::map<unsigned, unsigned>
|
|
|
|
GetVRegRenameMap(const std::vector<TypedVReg> &VRegs,
|
|
|
|
const std::vector<unsigned> &renamedInOtherBB,
|
2018-04-16 16:12:15 +08:00
|
|
|
MachineRegisterInfo &MRI, NamedVRegCursor &NVC) {
|
2017-11-03 07:37:32 +08:00
|
|
|
std::map<unsigned, unsigned> VRegRenameMap;
|
|
|
|
bool FirstCandidate = true;
|
|
|
|
|
|
|
|
for (auto &vreg : VRegs) {
|
|
|
|
if (vreg.isFrameIndex()) {
|
|
|
|
// We skip one vreg for any frame index because there is a good chance
|
|
|
|
// (especially when comparing SelectionDAG to GlobalISel generated MIR)
|
|
|
|
// that in the other file we are just getting an incoming vreg that comes
|
|
|
|
// from a copy from a frame index. So it's safe to skip by one.
|
2018-04-16 16:12:15 +08:00
|
|
|
unsigned LastRenameReg = NVC.incrementVirtualVReg();
|
|
|
|
(void)LastRenameReg;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Skipping rename for FI " << LastRenameReg << "\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
continue;
|
|
|
|
} else if (vreg.isCandidate()) {
|
|
|
|
|
|
|
|
// After the first candidate, for every subsequent candidate, we skip mod
|
|
|
|
// 10 registers so that the candidates are more likely to start at the
|
|
|
|
// same vreg number making it more likely that the canonical walk from the
|
|
|
|
// candidate insruction. We don't need to skip from the first candidate of
|
|
|
|
// the BasicBlock because we already skip ahead several vregs for each BB.
|
2018-04-05 08:27:15 +08:00
|
|
|
unsigned LastRenameReg = NVC.getVirtualVReg();
|
|
|
|
if (FirstCandidate)
|
|
|
|
NVC.incrementVirtualVReg(LastRenameReg % 10);
|
2017-11-03 07:37:32 +08:00
|
|
|
FirstCandidate = false;
|
|
|
|
continue;
|
2019-08-02 07:27:28 +08:00
|
|
|
} else if (!Register::isVirtualRegister(vreg.getReg())) {
|
2018-04-16 16:12:15 +08:00
|
|
|
unsigned LastRenameReg = NVC.incrementVirtualVReg();
|
|
|
|
(void)LastRenameReg;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2017-11-03 07:37:32 +08:00
|
|
|
dbgs() << "Skipping rename for Phys Reg " << LastRenameReg << "\n";
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Reg = vreg.getReg();
|
|
|
|
if (llvm::find(renamedInOtherBB, Reg) != renamedInOtherBB.end()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Vreg " << Reg
|
|
|
|
<< " already renamed in other BB.\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-31 02:06:28 +08:00
|
|
|
auto Rename = NVC.createVirtualRegister(Reg);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
if (VRegRenameMap.find(Reg) == VRegRenameMap.end()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Mapping vreg ";);
|
2017-11-03 07:37:32 +08:00
|
|
|
if (MRI.reg_begin(Reg) != MRI.reg_end()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(auto foo = &*MRI.reg_begin(Reg); foo->dump(););
|
2017-11-03 07:37:32 +08:00
|
|
|
} else {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << Reg;);
|
2017-11-03 07:37:32 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " to ";);
|
2017-11-03 07:37:32 +08:00
|
|
|
if (MRI.reg_begin(Rename) != MRI.reg_end()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(auto foo = &*MRI.reg_begin(Rename); foo->dump(););
|
2017-11-03 07:37:32 +08:00
|
|
|
} else {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << Rename;);
|
2017-11-03 07:37:32 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
VRegRenameMap.insert(std::pair<unsigned, unsigned>(Reg, Rename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VRegRenameMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool doVRegRenaming(std::vector<unsigned> &RenamedInOtherBB,
|
|
|
|
const std::map<unsigned, unsigned> &VRegRenameMap,
|
|
|
|
MachineRegisterInfo &MRI) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (auto I = VRegRenameMap.begin(), E = VRegRenameMap.end(); I != E; ++I) {
|
|
|
|
|
|
|
|
auto VReg = I->first;
|
|
|
|
auto Rename = I->second;
|
|
|
|
|
|
|
|
RenamedInOtherBB.push_back(Rename);
|
|
|
|
|
|
|
|
std::vector<MachineOperand *> RenameMOs;
|
|
|
|
for (auto &MO : MRI.reg_operands(VReg)) {
|
|
|
|
RenameMOs.push_back(&MO);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *MO : RenameMOs) {
|
|
|
|
Changed = true;
|
|
|
|
MO->setReg(Rename);
|
|
|
|
|
|
|
|
if (!MO->isDef())
|
|
|
|
MO->setIsKill(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
std::vector<StringRef> &bbNames,
|
|
|
|
std::vector<unsigned> &renamedInOtherBB,
|
2018-04-05 08:27:15 +08:00
|
|
|
unsigned &basicBlockNum, unsigned &VRegGapIndex,
|
|
|
|
NamedVRegCursor &NVC) {
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
if (CanonicalizeBasicBlockNumber != ~0U) {
|
|
|
|
if (CanonicalizeBasicBlockNumber != basicBlockNum++)
|
|
|
|
return false;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n Canonicalizing BasicBlock " << MBB->getName()
|
|
|
|
<< "\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (llvm::find(bbNames, MBB->getName()) != bbNames.end()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2017-11-03 07:37:32 +08:00
|
|
|
dbgs() << "Found potentially duplicate BasicBlocks: " << MBB->getName()
|
|
|
|
<< "\n";
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
MachineFunction &MF = *MBB->getParent();
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
|
|
|
|
bbNames.push_back(MBB->getName());
|
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
|
|
|
|
|
|
|
std::vector<MachineInstr *> Candidates = populateCandidates(MBB);
|
|
|
|
std::vector<MachineInstr *> VisitedMIs;
|
2018-11-17 09:44:25 +08:00
|
|
|
llvm::copy(Candidates, std::back_inserter(VisitedMIs));
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
std::vector<TypedVReg> VRegs;
|
|
|
|
for (auto candidate : Candidates) {
|
|
|
|
VRegs.push_back(TypedVReg(RSE_NewCandidate));
|
|
|
|
|
|
|
|
std::queue<TypedVReg> RegQueue;
|
|
|
|
|
|
|
|
// Here we walk the vreg operands of a non-root node along our walk.
|
|
|
|
// The root nodes are the original candidates (stores normally).
|
|
|
|
// These are normally not the root nodes (except for the case of copies to
|
|
|
|
// physical registers).
|
|
|
|
for (unsigned i = 1; i < candidate->getNumOperands(); i++) {
|
|
|
|
if (candidate->mayStore() || candidate->isBranch())
|
|
|
|
break;
|
|
|
|
|
|
|
|
MachineOperand &MO = candidate->getOperand(i);
|
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-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Enqueue register"; MO.dump(); dbgs() << "\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
RegQueue.push(TypedVReg(MO.getReg()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we walk the root candidates. We start from the 0th operand because
|
|
|
|
// the root is normally a store to a vreg.
|
|
|
|
for (unsigned i = 0; i < candidate->getNumOperands(); i++) {
|
|
|
|
|
|
|
|
if (!candidate->mayStore() && !candidate->isBranch())
|
|
|
|
break;
|
|
|
|
|
|
|
|
MachineOperand &MO = candidate->getOperand(i);
|
|
|
|
|
|
|
|
// TODO: Do we want to only add vregs here?
|
|
|
|
if (!MO.isReg() && !MO.isFI())
|
|
|
|
continue;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Enqueue Reg/FI"; MO.dump(); dbgs() << "\n";);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
2018-04-16 16:12:15 +08:00
|
|
|
RegQueue.push(MO.isReg() ? TypedVReg(MO.getReg())
|
|
|
|
: TypedVReg(RSE_FrameIndex));
|
2017-11-03 07:37:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
doCandidateWalk(VRegs, RegQueue, VisitedMIs, MBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have populated no vregs to rename then bail.
|
|
|
|
// The rest of this function does the vreg remaping.
|
|
|
|
if (VRegs.size() == 0)
|
|
|
|
return Changed;
|
|
|
|
|
2018-04-05 08:27:15 +08:00
|
|
|
auto VRegRenameMap = GetVRegRenameMap(VRegs, renamedInOtherBB, MRI, NVC);
|
2017-11-03 07:37:32 +08:00
|
|
|
Changed |= doVRegRenaming(renamedInOtherBB, VRegRenameMap, MRI);
|
2018-03-31 13:48:51 +08:00
|
|
|
|
|
|
|
// Here we renumber the def vregs for the idempotent instructions from the top
|
|
|
|
// of the MachineBasicBlock so that they are named in the order that we sorted
|
|
|
|
// them alphabetically. Eventually we wont need SkipVRegs because we will use
|
|
|
|
// named vregs instead.
|
2019-06-01 01:34:25 +08:00
|
|
|
if (IdempotentInstCount)
|
|
|
|
NVC.SkipVRegs();
|
2018-03-31 13:48:51 +08:00
|
|
|
|
|
|
|
auto MII = MBB->begin();
|
|
|
|
for (unsigned i = 0; i < IdempotentInstCount && MII != MBB->end(); ++i) {
|
|
|
|
MachineInstr &MI = *MII++;
|
|
|
|
Changed = true;
|
|
|
|
unsigned vRegToRename = MI.getOperand(0).getReg();
|
2019-05-31 02:06:28 +08:00
|
|
|
auto Rename = NVC.createVirtualRegister(vRegToRename);
|
2018-03-31 13:48:51 +08:00
|
|
|
|
|
|
|
std::vector<MachineOperand *> RenameMOs;
|
|
|
|
for (auto &MO : MRI.reg_operands(vRegToRename)) {
|
|
|
|
RenameMOs.push_back(&MO);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *MO : RenameMOs) {
|
|
|
|
MO->setReg(Rename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
std::vector<StringRef> BBNames;
|
|
|
|
std::vector<unsigned> RenamedInOtherBB;
|
|
|
|
|
|
|
|
unsigned GapIdx = 0;
|
|
|
|
unsigned BBNum = 0;
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
2018-04-05 08:27:15 +08:00
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
NamedVRegCursor NVC(MRI);
|
2017-11-03 07:37:32 +08:00
|
|
|
for (auto MBB : RPOList)
|
2018-04-16 16:12:15 +08:00
|
|
|
Changed |=
|
|
|
|
runOnBasicBlock(MBB, BBNames, RenamedInOtherBB, BBNum, GapIdx, NVC);
|
2017-11-03 07:37:32 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|